lavender/server/jwt.go

59 lines
1.5 KiB
Go
Raw Normal View History

package server
import (
"context"
"crypto/sha256"
"encoding/base64"
"github.com/1f349/mjwt"
"github.com/1f349/mjwt/auth"
"github.com/go-oauth2/oauth2/v4"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"strings"
)
type JWTAccessGenerate struct {
signer *mjwt.Issuer
2024-09-13 15:31:40 +01:00
db mjwtGetUserRoles
}
2024-09-13 15:31:40 +01:00
func NewMJWTAccessGenerate(signer *mjwt.Issuer, db mjwtGetUserRoles) *JWTAccessGenerate {
return &JWTAccessGenerate{signer, db}
}
var _ oauth2.AccessGenerate = &JWTAccessGenerate{}
2024-09-13 15:31:40 +01:00
type mjwtGetUserRoles interface {
GetUserRoles(ctx context.Context, subject string) ([]string, error)
}
func (j *JWTAccessGenerate) Token(ctx context.Context, data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) {
roles, err := j.db.GetUserRoles(ctx, data.UserID)
if err != nil {
return "", "", err
}
2024-09-02 22:54:03 +01:00
ps := auth.NewPermStorage()
for _, role := range roles {
ps.Set(role)
}
out := auth.NewPermStorage()
2024-09-02 22:54:03 +01:00
ForEachRole(data.Client.(interface{ UsePerms() []string }).UsePerms(), func(role string) {
2024-02-14 21:35:50 +00:00
for _, i := range ps.Filter(strings.Split(role, " ")).Dump() {
out.Set(i)
}
})
access, err = j.signer.GenerateJwt(data.UserID, "", jwt.ClaimStrings{data.TokenInfo.GetClientID()}, data.TokenInfo.GetAccessExpiresIn(), auth.AccessTokenClaims{
Perms: out,
})
if isGenRefresh {
t := uuid.NewHash(sha256.New(), uuid.New(), []byte(access), 5).String()
refresh = base64.URLEncoding.EncodeToString([]byte(t))
refresh = strings.ToUpper(strings.TrimRight(refresh, "="))
}
return
}