feat: 优化密保规则支持最低1位且无上限
This commit is contained in:
1 parent
187caebe0c
commit
169730436f
5 files changed
+52
-20
No files matched your search
@@ -55,10 +55,10 @@ func (s *Service) SetSecurityQuestion(userID int, question, answer string) error
|
||||
}
|
||||
question = strings.TrimSpace(norm.NFKC.String(question))
|
||||
answer = normalizeAnswer(answer)
|
||||
if len([]byte(question)) < 2 || len([]byte(question)) > 255 {
|
||||
if len([]rune(question)) < 1 {
|
||||
return ErrInvalidQuestion
|
||||
}
|
||||
if len([]byte(answer)) < 8 || len([]byte(answer)) > 256 {
|
||||
if len([]rune(answer)) < 1 {
|
||||
return ErrInvalidAnswer
|
||||
}
|
||||
hash, err := password.Hash(answer)
|
||||
@@ -71,12 +71,8 @@ func (s *Service) SetSecurityQuestion(userID int, question, answer string) error
|
||||
return fmt.Errorf("load security question: %w", err)
|
||||
}
|
||||
if has {
|
||||
version := item.Version + 1
|
||||
if version <= 0 {
|
||||
version = 1
|
||||
}
|
||||
item.Question, item.AnswerHash, item.Version = question, hash, version
|
||||
if _, err := s.engine.ID(item.ID).Cols("question", "answer_hash", "version", "updated_at").Update(item); err != nil {
|
||||
item.Question, item.AnswerHash = question, hash
|
||||
if _, err := s.engine.ID(item.ID).Cols("question", "answer_hash", "updated_at").Update(item); err != nil {
|
||||
return fmt.Errorf("update security question: %w", err)
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -32,6 +32,36 @@ func newFixture(t *testing.T) *Service {
|
||||
return service
|
||||
}
|
||||
|
||||
func TestSecurityQuestionSupportsSingleCharacterAndUnlimited(t *testing.T) {
|
||||
service := newFixture(t)
|
||||
if err := service.SetSecurityQuestion(1, "Q", "A"); err != nil {
|
||||
t.Fatalf("setting 1 char question and answer failed: %v", err)
|
||||
}
|
||||
question, ok, err := service.GetSecurityQuestion(1)
|
||||
if err != nil || !ok || question != "Q" {
|
||||
t.Fatalf("question = %q, ok=%v, err=%v", question, ok, err)
|
||||
}
|
||||
if verified, err := service.VerifySecurityQuestion(1, "A"); err != nil || !verified {
|
||||
t.Fatalf("verification failed: %v, %v", verified, err)
|
||||
}
|
||||
if err := service.SetSecurityQuestion(1, " ", "A"); !errors.Is(err, ErrInvalidQuestion) {
|
||||
t.Fatalf("want ErrInvalidQuestion, got: %v", err)
|
||||
}
|
||||
if err := service.SetSecurityQuestion(1, "Q", " "); !errors.Is(err, ErrInvalidAnswer) {
|
||||
t.Fatalf("want ErrInvalidAnswer, got: %v", err)
|
||||
}
|
||||
if err := service.SetSecurityQuestion(1, "UpdatedQ", "UpdatedA"); err != nil {
|
||||
t.Fatalf("update security question failed: %v", err)
|
||||
}
|
||||
updatedQuestion, ok, err := service.GetSecurityQuestion(1)
|
||||
if err != nil || !ok || updatedQuestion != "UpdatedQ" {
|
||||
t.Fatalf("updated question = %q, ok=%v, err=%v", updatedQuestion, ok, err)
|
||||
}
|
||||
if verified, err := service.VerifySecurityQuestion(1, "UpdatedA"); err != nil || !verified {
|
||||
t.Fatalf("verification of updated answer failed: %v, %v", verified, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecurityQuestionIsCaseInsensitiveAndNeverReturnsHash(t *testing.T) {
|
||||
service := newFixture(t)
|
||||
if err := service.SetSecurityQuestion(1, "第一只宠物叫什么?", "BlueBird123"); err != nil {
|
||||
|
||||
Reference in new issue
Block a user