2024-01-07 07:52:23 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-02-18 03:20:22 +00:00
|
|
|
"code.smartsheep.studio/hydrogen/identity/pkg/security"
|
|
|
|
"code.smartsheep.studio/hydrogen/identity/pkg/services"
|
2024-01-26 17:11:32 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
"github.com/gofiber/fiber/v2/middleware/keyauth"
|
2024-01-07 07:52:23 +00:00
|
|
|
)
|
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
var auth = keyauth.New(keyauth.Config{
|
|
|
|
KeyLookup: "header:Authorization",
|
|
|
|
AuthScheme: "Bearer",
|
|
|
|
Validator: func(c *fiber.Ctx, token string) (bool, error) {
|
|
|
|
claims, err := security.DecodeJwt(token)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-01-07 07:52:23 +00:00
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
session, err := services.LookupSessionWithToken(claims.ID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
} else if err := session.IsAvailable(); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-01-07 07:52:23 +00:00
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
user, err := services.GetAccount(session.AccountID)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2024-01-07 07:52:23 +00:00
|
|
|
|
2024-01-27 16:42:02 +00:00
|
|
|
c.Locals("principal", user)
|
2024-01-07 07:52:23 +00:00
|
|
|
|
2024-01-26 17:11:32 +00:00
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
ContextKey: "token",
|
|
|
|
})
|