2024-02-10 16:23:50 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
|
|
|
"github.com/1f349/lavender/database"
|
|
|
|
"github.com/1f349/mjwt"
|
|
|
|
"github.com/1f349/mjwt/auth"
|
|
|
|
"github.com/1f349/mjwt/claims"
|
|
|
|
"github.com/go-oauth2/oauth2/v4"
|
|
|
|
"github.com/golang-jwt/jwt/v4"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type JWTAccessGenerate struct {
|
|
|
|
signer mjwt.Signer
|
|
|
|
db *database.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJWTAccessGenerate(signer mjwt.Signer, db *database.DB) *JWTAccessGenerate {
|
|
|
|
return &JWTAccessGenerate{signer, db}
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ oauth2.AccessGenerate = &JWTAccessGenerate{}
|
|
|
|
|
|
|
|
func (j *JWTAccessGenerate) Token(ctx context.Context, data *oauth2.GenerateBasic, isGenRefresh bool) (access, refresh string, err error) {
|
|
|
|
beginCtx, err := j.db.BeginCtx(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
roles, err := beginCtx.GetUserRoles(data.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
beginCtx.Rollback()
|
|
|
|
|
2024-02-14 21:04:52 +00:00
|
|
|
ps := claims.ParsePermStorage(roles)
|
2024-02-14 21:35:50 +00:00
|
|
|
out := claims.NewPermStorage()
|
2024-02-10 16:23:50 +00: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)
|
|
|
|
}
|
2024-02-10 16:23:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
access, err = j.signer.GenerateJwt(data.UserID, "", jwt.ClaimStrings{data.TokenInfo.GetClientID()}, data.TokenInfo.GetAccessExpiresIn(), auth.AccessTokenClaims{
|
2024-02-14 21:04:52 +00:00
|
|
|
Perms: out,
|
2024-02-10 16:23:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|