Auth impl

This commit is contained in:
2024-01-07 15:52:23 +08:00
parent 632d614d1e
commit 69bae57473
6 changed files with 96 additions and 10 deletions

32
pkg/server/auth.go Normal file
View File

@ -0,0 +1,32 @@
package server
import (
"code.smartsheep.studio/hydrogen/bus/pkg/kit/adaptor"
"code.smartsheep.studio/hydrogen/bus/pkg/kit/publisher"
"code.smartsheep.studio/hydrogen/bus/pkg/wire"
"code.smartsheep.studio/hydrogen/passport/pkg/security"
"code.smartsheep.studio/hydrogen/passport/pkg/services"
)
func doAuth(c *publisher.RequestCtx) error {
token := adaptor.ParseAnyToStruct[string](c.Parameters)
claims, err := security.DecodeJwt(token)
if err != nil {
return c.SendError(wire.Unauthorized, err)
}
session, err := services.LookupSessionWithToken(claims.ID)
if err != nil {
return c.SendError(wire.Unauthorized, err)
} else if err := session.IsAvailable(); err != nil {
return c.SendError(wire.Unauthorized, err)
}
user, err := services.GetAccount(session.AccountID)
if err != nil {
return c.SendError(wire.Unauthorized, err)
}
return c.SendResponse(user.Permissions.Data())
}

View File

@ -42,4 +42,25 @@ var Commands = map[string]publisher.CommandManifest{
Requirements: wire.CommandRequirements{},
Handle: refreshToken,
},
"passport.auth": {
Name: "Auth with access token.",
Description: "Auth gateway request with access token.",
Requirements: wire.CommandRequirements{},
Providers: []wire.CommandProvider{
{
ID: "passport.auth",
Implementation: "gateway.auth",
Weight: 1000,
},
},
Handle: doAuth,
},
"passport.test": {
Name: "Test secured resource.",
Description: "Test secured resource connectivity.",
Requirements: wire.CommandRequirements{Authorized: true},
Handle: func(c *publisher.RequestCtx) error {
return c.SendResponse("You got me!")
},
},
}