131 lines
3.3 KiB
Go
131 lines
3.3 KiB
Go
package router
|
|
|
|
import (
|
|
"janex/admin-dashboard-backend/modules/config"
|
|
"janex/admin-dashboard-backend/modules/database"
|
|
"janex/admin-dashboard-backend/modules/logger"
|
|
"janex/admin-dashboard-backend/modules/structs"
|
|
"janex/admin-dashboard-backend/modules/utils"
|
|
"janex/admin-dashboard-backend/routers/router/api/v1/grouptask"
|
|
"janex/admin-dashboard-backend/routers/router/api/v1/jxscanner"
|
|
log "janex/admin-dashboard-backend/routers/router/api/v1/logger"
|
|
"janex/admin-dashboard-backend/routers/router/api/v1/user"
|
|
"janex/admin-dashboard-backend/socketclients"
|
|
"time"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
func SetupRoutes(app *fiber.App) {
|
|
v1 := app.Group("/v1")
|
|
|
|
u := v1.Group("/user")
|
|
u.Post("/auth/login", user.UserLogin)
|
|
u.Delete("/auth/logout", userSessionValidation, user.UserLogout)
|
|
u.Delete("/session/:idForDeletion", userSessionValidation, user.SignOutSession)
|
|
u.Post("/avatar", userSessionValidation, user.UpdateAvatar)
|
|
|
|
s := v1.Group("/scanner")
|
|
s.Post("/", jxscanner.AddScanner)
|
|
s.Post("/scan", scannerSessionValidation, jxscanner.ScanResult)
|
|
s.Delete("/", scannerSessionValidation, jxscanner.DeleteScanner)
|
|
|
|
l := v1.Group("/log")
|
|
l.Get("/", userSessionValidation, log.GetSystemLog)
|
|
|
|
g := v1.Group("/grouptasks")
|
|
g.Post("/start", userApikeyTokenValidation, grouptask.StartGroupTask)
|
|
|
|
app.Static("/", config.Cfg.FolderPaths.PublicStatic)
|
|
}
|
|
|
|
func userApikeyTokenValidation(c *fiber.Ctx) error {
|
|
xApikey := utils.GetXApiKeyHeader(c)
|
|
|
|
if len(xApikey) != utils.LenHeaderXApiKey {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
|
|
var apiKey structs.UserApiKey
|
|
|
|
database.DB.Select("id, user_id, token, usage_count").First(&apiKey, "token = ?", xApikey)
|
|
|
|
if apiKey.Token != xApikey {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
|
|
lastUsed := time.Now()
|
|
|
|
database.DB.Model(&structs.UserApiKey{}).Where("id = ?", apiKey.Id).Updates(map[string]interface{}{
|
|
"usage_count": gorm.Expr("usage_count + ?", 1),
|
|
"last_used": lastUsed,
|
|
})
|
|
|
|
c.Locals("userId", apiKey.UserId)
|
|
|
|
socketclients.SendMessageToUser(apiKey.UserId, "", structs.SendSocketMessage{
|
|
Cmd: utils.SentCmdNewApiKeyUsageCount,
|
|
Body: struct {
|
|
Id string
|
|
UsageCount uint
|
|
LastUsed time.Time
|
|
}{
|
|
Id: apiKey.Id,
|
|
UsageCount: (apiKey.UsageCount + 1),
|
|
LastUsed: lastUsed,
|
|
},
|
|
})
|
|
|
|
logger.AddSystemLog(structs.LogMessage{
|
|
Id: 25,
|
|
Type: utils.LogTypeInfo,
|
|
Messages: []structs.LogData{
|
|
{Type: "userId", Value: apiKey.UserId},
|
|
},
|
|
})
|
|
|
|
return c.Next()
|
|
}
|
|
|
|
func userSessionValidation(c *fiber.Ctx) error {
|
|
xAuthorization := utils.GetXAuhorizationHeader(c)
|
|
|
|
if len(xAuthorization) != utils.LenHeaderXAuthorization {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
|
|
var userSession structs.UserSession
|
|
|
|
database.DB.First(&userSession, "id = ?", xAuthorization)
|
|
|
|
if userSession.Id != xAuthorization {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
|
|
c.Locals("userId", userSession.UserId)
|
|
|
|
return c.Next()
|
|
}
|
|
|
|
func scannerSessionValidation(c *fiber.Ctx) error {
|
|
xAuthorization := utils.GetXAuhorizationHeader(c)
|
|
|
|
if len(xAuthorization) != utils.LenHeaderXAuthorization {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
|
|
var scanner structs.Scanner
|
|
|
|
database.DB.First(&scanner, "session = ?", xAuthorization)
|
|
|
|
if scanner.Session != xAuthorization {
|
|
return fiber.ErrUnauthorized
|
|
}
|
|
|
|
c.Locals("scannerId", scanner.Id)
|
|
c.Locals("usedByUserId", scanner.UsedByUserId)
|
|
|
|
return c.Next()
|
|
}
|