优化
This commit is contained in:
@@ -111,6 +111,10 @@ func (uc *AuthenticationUsecase) Login(ctx context.Context, attempt *LoginAttemp
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %v", ErrTokenIssue, err)
|
||||
}
|
||||
// Record a successful credential login immediately after issuing the JWT.
|
||||
// Multipoint-session persistence happens afterwards, so a Redis/cache
|
||||
// failure must not erase the successful-login audit event.
|
||||
uc.recordLogin(ctx, attempt, true, "登录成功", user.ID)
|
||||
if uc.security.UseMultipoint() {
|
||||
oldToken, cacheErr := uc.security.ActiveToken(ctx, user.Username)
|
||||
if cacheErr != nil {
|
||||
@@ -123,7 +127,6 @@ func (uc *AuthenticationUsecase) Login(ctx context.Context, attempt *LoginAttemp
|
||||
return nil, fmt.Errorf("%w: %v", ErrLoginState, cacheErr)
|
||||
}
|
||||
}
|
||||
uc.recordLogin(ctx, attempt, true, "登录成功", user.ID)
|
||||
return &AuthenticationResult{User: user, Token: issued.Value, ExpiresAt: issued.ExpiresAt, NeedChangePassword: user.MustChangePassword}, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package biz
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type authenticationUserRepo struct {
|
||||
UserRepo
|
||||
user *User
|
||||
}
|
||||
|
||||
func (r *authenticationUserRepo) FindUserByUsername(context.Context, string) (*User, error) {
|
||||
return r.user, nil
|
||||
}
|
||||
|
||||
type authenticationSecurityRepo struct{ SecurityRepo }
|
||||
|
||||
func (*authenticationSecurityRepo) SecurityConfig(context.Context) (*SecurityConfig, error) {
|
||||
return &SecurityConfig{CaptchaOpen: 2, CaptchaTimeout: 60}, nil
|
||||
}
|
||||
|
||||
type authenticationCache struct{ activeErr error }
|
||||
|
||||
func (c *authenticationCache) Get(_ context.Context, key string) (string, bool, error) {
|
||||
if key == "admin" {
|
||||
return "", false, c.activeErr
|
||||
}
|
||||
return "", false, nil
|
||||
}
|
||||
func (*authenticationCache) Set(context.Context, string, string, time.Duration) error { return nil }
|
||||
func (*authenticationCache) Delete(context.Context, string) error { return nil }
|
||||
func (*authenticationCache) Increment(context.Context, string, time.Duration) (int64, error) {
|
||||
return 1, nil
|
||||
}
|
||||
|
||||
type authenticationSettings struct{ RuntimeSettings }
|
||||
|
||||
func (*authenticationSettings) UseMultipoint() bool { return true }
|
||||
|
||||
type authenticationIssuer struct{ TokenIssuer }
|
||||
|
||||
func (*authenticationIssuer) IssueToken(*User, uint, bool, time.Duration) (*IssuedToken, error) {
|
||||
return &IssuedToken{Value: "token", ExpiresAt: time.Now().Add(time.Hour), TTL: time.Hour}, nil
|
||||
}
|
||||
|
||||
type authenticationAudit struct {
|
||||
AuditRecordRepo
|
||||
logins []*LoginLog
|
||||
}
|
||||
|
||||
func (a *authenticationAudit) RecordLogin(_ context.Context, value *LoginLog) error {
|
||||
a.logins = append(a.logins, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestLoginRecordsSuccessBeforeMultipointCacheFailure(t *testing.T) {
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte("secret"), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := NewUserUsecase(&authenticationUserRepo{user: &User{ID: 1, Username: "admin", Password: string(hash), AuthorityID: 888, Enable: 1}})
|
||||
cacheErr := errors.New("cache unavailable")
|
||||
security := NewSecurityUsecase(&authenticationSecurityRepo{}, &authenticationCache{activeErr: cacheErr}, &authenticationSettings{}, nil)
|
||||
audit := &authenticationAudit{}
|
||||
uc := NewAuthenticationUsecase(users, security, &authenticationIssuer{}, audit)
|
||||
|
||||
_, err = uc.Login(context.Background(), &LoginAttempt{Username: "admin", Password: "secret", IP: "127.0.0.1", Agent: "test"})
|
||||
if !errors.Is(err, ErrLoginState) {
|
||||
t.Fatalf("expected login-state failure, got %v", err)
|
||||
}
|
||||
if len(audit.logins) != 1 || !audit.logins[0].Status || audit.logins[0].ErrorMessage != "登录成功" || audit.logins[0].UserID != 1 {
|
||||
t.Fatalf("expected successful login audit before cache failure, got %+v", audit.logins)
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ type Department struct {
|
||||
Sort int
|
||||
LeaderID uint
|
||||
Leader *User
|
||||
Status bool
|
||||
Status *bool
|
||||
Children []*Department
|
||||
NamePath string
|
||||
}
|
||||
|
||||
@@ -76,9 +76,14 @@ func (uc *MediaUsecase) Upload(ctx context.Context, userID uint, name, suppliedM
|
||||
}
|
||||
media := &MediaFile{Name: name, CategoryID: categoryID, URL: stored.URL, Tag: strings.TrimPrefix(filepath.Ext(name), "."), Key: key, Size: stored.Size, Mime: suppliedMIME, MD5: hex.EncodeToString(hash.Sum(nil)), UserID: userID}
|
||||
if save {
|
||||
if err = uc.CreateMedia(ctx, media); err != nil {
|
||||
_ = uc.files.Delete(ctx, key)
|
||||
return nil, err
|
||||
count, countErr := uc.MediaKeyReferences(ctx, key)
|
||||
if countErr != nil {
|
||||
return nil, countErr
|
||||
}
|
||||
if count == 0 {
|
||||
if err = uc.CreateMedia(ctx, media); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return media, nil
|
||||
|
||||
@@ -17,6 +17,8 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var ErrUploadSessionNotFound = errors.New("upload session not found")
|
||||
|
||||
func (uc *MediaUsecase) chunkPrefix(uploadID uint) string {
|
||||
directory := "uploads/chunks"
|
||||
if uc.settings != nil {
|
||||
@@ -44,11 +46,13 @@ func (uc *MediaUsecase) InitUpload(ctx context.Context, userID uint, name, hash
|
||||
}
|
||||
}
|
||||
session, err := uc.FindUploadingSession(ctx, userID, hash)
|
||||
if err != nil {
|
||||
if errors.Is(err, ErrUploadSessionNotFound) {
|
||||
session = &UploadSession{UserID: userID, FileName: name, FileHash: hash, FileSize: size, ChunkSize: chunkSize, ChunkTotal: total, Status: "uploading"}
|
||||
if err = uc.CreateUploadSession(ctx, session); err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
} else if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
chunks, err := uc.ListChunks(ctx, session.ID)
|
||||
if err != nil {
|
||||
@@ -146,7 +150,6 @@ func (uc *MediaUsecase) CompleteUpload(ctx context.Context, userID, uploadID uin
|
||||
// byte count.
|
||||
media := &MediaFile{Name: session.FileName, URL: stored.URL, Tag: strings.TrimPrefix(filepath.Ext(session.FileName), "."), Key: key, Size: session.FileSize, Mime: mime, MD5: hash, UserID: userID}
|
||||
if err = uc.CreateMedia(ctx, media); err != nil {
|
||||
_ = uc.files.Delete(ctx, key)
|
||||
return fail(err)
|
||||
}
|
||||
_ = uc.CompleteUploadSession(ctx, uploadID, key, media.ID)
|
||||
|
||||
@@ -12,7 +12,7 @@ type Position struct {
|
||||
Name string
|
||||
Code string
|
||||
Sort int
|
||||
Status bool
|
||||
Status *bool
|
||||
Remark string
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user