优化
This commit is contained in:
@@ -5,17 +5,15 @@ import (
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/conf"
|
||||
"kra/pkg/adminauth"
|
||||
)
|
||||
|
||||
type TokenService struct {
|
||||
uc *biz.TokenUsecase
|
||||
runtime *conf.Runtime
|
||||
uc *biz.TokenUsecase
|
||||
issuer biz.TokenIssuer
|
||||
}
|
||||
|
||||
func NewTokenService(uc *biz.TokenUsecase, runtime *conf.Runtime) *TokenService {
|
||||
return &TokenService{uc: uc, runtime: runtime}
|
||||
func NewTokenService(uc *biz.TokenUsecase, issuer biz.TokenIssuer) *TokenService {
|
||||
return &TokenService{uc: uc, issuer: issuer}
|
||||
}
|
||||
|
||||
func tokenDTO(v *biz.APIToken) map[string]any {
|
||||
@@ -31,27 +29,15 @@ func (s *TokenService) CreateAPIToken(ctx context.Context, userID, authorityID u
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
secret, issuer := "", "kra"
|
||||
buffer := 24 * time.Hour
|
||||
config := s.runtime.Admin()
|
||||
if config != nil && config.Jwt != nil {
|
||||
secret = config.Jwt.SigningKey
|
||||
if config.Jwt.Issuer != "" {
|
||||
issuer = config.Jwt.Issuer
|
||||
}
|
||||
if config.Jwt.BufferTime != nil {
|
||||
buffer = config.Jwt.BufferTime.AsDuration()
|
||||
}
|
||||
}
|
||||
token, _, err := adminauth.Generate(secret, issuer, duration, buffer, user.ID, authorityID, user.UUID, user.Username, user.NickName, false)
|
||||
issued, err := s.issuer.IssueToken(user, authorityID, false, duration)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
record := &biz.APIToken{UserID: userID, AuthorityID: authorityID, Token: token, Status: true, ExpiresAt: time.Now().Add(duration), Remark: remark}
|
||||
record := &biz.APIToken{UserID: userID, AuthorityID: authorityID, Token: issued.Value, Status: true, ExpiresAt: time.Now().Add(duration), Remark: remark}
|
||||
if err = s.uc.CreateAPIToken(ctx, record); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
return issued.Value, nil
|
||||
}
|
||||
func (s *TokenService) APITokens(ctx context.Context, page, size int, userID uint, status *bool) ([]map[string]any, int64, error) {
|
||||
items, total, err := s.uc.ListAPITokens(ctx, page, size, userID, status)
|
||||
|
||||
Reference in New Issue
Block a user