refactor: separate backend domains and storage adapters
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/pkg/adminauth"
|
||||
)
|
||||
|
||||
type LoginResult struct {
|
||||
User map[string]any `json:"user"`
|
||||
Token string `json:"token"`
|
||||
ExpiresAt int64 `json:"expiresAt"`
|
||||
NeedChangePassword bool `json:"needChangePassword"`
|
||||
}
|
||||
|
||||
func (s *SystemService) CacheGet(ctx context.Context, key string) (string, bool, error) {
|
||||
return s.uc.CacheGet(ctx, key)
|
||||
}
|
||||
func (s *SystemService) CacheSet(ctx context.Context, key, value string, expiration time.Duration) error {
|
||||
return s.uc.CacheSet(ctx, key, value, expiration)
|
||||
}
|
||||
func (s *SystemService) CacheDelete(ctx context.Context, key string) error {
|
||||
return s.uc.CacheDelete(ctx, key)
|
||||
}
|
||||
func (s *SystemService) CacheIncrement(ctx context.Context, key string, expiration time.Duration) (int64, error) {
|
||||
return s.uc.CacheIncrement(ctx, key, expiration)
|
||||
}
|
||||
|
||||
func (s *SystemService) Login(ctx context.Context, username, password string) (*LoginResult, error) {
|
||||
u, err := s.uc.Login(ctx, username, password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if u.Enable != 1 {
|
||||
return nil, biz.ErrInvalidCredentials
|
||||
}
|
||||
if security, securityErr := s.settings.CurrentSecurity(ctx); securityErr == nil && security.PwdExpireEnable && u.PasswordUpdatedAt != nil && time.Since(*u.PasswordUpdatedAt) > time.Duration(security.PwdExpireDays)*24*time.Hour {
|
||||
u.MustChangePassword = true
|
||||
}
|
||||
return s.issueLogin(u, u.AuthorityID)
|
||||
}
|
||||
|
||||
func (s *SystemService) issueLogin(user *biz.User, authorityID uint) (*LoginResult, error) {
|
||||
expires, buffer := 7*24*time.Hour, 24*time.Hour
|
||||
secret, issuer := "", "kra"
|
||||
if s.config != nil && s.config.Jwt != nil {
|
||||
secret, issuer = s.config.Jwt.SigningKey, s.config.Jwt.Issuer
|
||||
if s.config.Jwt.ExpiresTime != nil {
|
||||
expires = s.config.Jwt.ExpiresTime.AsDuration()
|
||||
}
|
||||
if s.config.Jwt.BufferTime != nil {
|
||||
buffer = s.config.Jwt.BufferTime.AsDuration()
|
||||
}
|
||||
}
|
||||
token, claims, err := adminauth.Generate(secret, issuer, expires, buffer, user.ID, authorityID, user.UUID, user.Username, user.NickName, user.MustChangePassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &LoginResult{User: convertUser(user), Token: token, ExpiresAt: claims.ExpiresAt.UnixMilli(), NeedChangePassword: user.MustChangePassword}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user