2024-10-30 16:15:25 +00:00
|
|
|
package authkit
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-30 16:15:25 +00:00
|
|
|
"github.com/goccy/go-json"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetAccountFromUserInfo returns the account from the user info
|
|
|
|
// This method will not to query the database, it will parse the token and get the subject of the userinfo token
|
|
|
|
func GetAccountFromUserInfo(info *sec.UserInfo) models.Account {
|
|
|
|
raw, _ := json.Marshal(info.Metadata)
|
|
|
|
|
|
|
|
// We assume the token is signed by the same version of service
|
|
|
|
// So directly read the data out of the metadata
|
|
|
|
var out models.Account
|
|
|
|
_ = json.Unmarshal(raw, &out)
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseAccountMiddleware(c *fiber.Ctx) error {
|
|
|
|
if info, ok := c.Locals("nex_user").(*sec.UserInfo); ok {
|
|
|
|
c.Locals("user", GetAccountFromUserInfo(info))
|
|
|
|
}
|
|
|
|
return c.Next()
|
|
|
|
}
|