按实际安全设置状态重复提醒并仅关闭当前登录提示
This commit is contained in:
1 parent
ad0cfaa4c0
commit
a021ef9d4b
11 files changed
+208
-71
No files matched your search
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/i18n"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
@@ -104,19 +105,28 @@ func loginSecurityFields(user *models.User) map[string]any {
|
||||
fields := map[string]any{
|
||||
"recoveryCodesConfigured": false,
|
||||
"showRecoverySetupPrompt": false,
|
||||
"showSecuritySetupPrompt": user != nil && user.SecuritySetupAcknowledged == 0,
|
||||
"showSecuritySetupPrompt": user != nil,
|
||||
"externalEmailConfigured": false,
|
||||
"webAuthnEnabled": webAuthnRuntime() != nil,
|
||||
}
|
||||
if user == nil || user.EffectiveRole() == access.RoleSuperAdmin {
|
||||
if user == nil {
|
||||
return fields
|
||||
}
|
||||
|
||||
configured := false
|
||||
if service := credentialRuntime(); service != nil {
|
||||
configured, _ = service.HasValidRecoveryCode(user.ID)
|
||||
if db.Instance != nil && user.ID > 0 {
|
||||
emailConfigured, err := db.Instance.Where("user_id = ? AND status = ?", user.ID, models.ExternalEmailStatusVerified).Exist(&models.ExternalEmail{})
|
||||
if err == nil {
|
||||
fields["externalEmailConfigured"] = emailConfigured
|
||||
}
|
||||
}
|
||||
fields["recoveryCodesConfigured"] = configured
|
||||
fields["showRecoverySetupPrompt"] = !configured
|
||||
configured := user.EffectiveRole() == access.RoleSuperAdmin
|
||||
if !configured {
|
||||
if service := credentialRuntime(); service != nil {
|
||||
configured, _ = service.HasValidRecoveryCode(user.ID)
|
||||
}
|
||||
fields["recoveryCodesConfigured"] = configured
|
||||
fields["showRecoverySetupPrompt"] = !configured
|
||||
}
|
||||
fields["showSecuritySetupPrompt"] = !configured || fields["externalEmailConfigured"] != true
|
||||
return fields
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/session"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
// AcknowledgeSecuritySetup 仅确认当前账号已经查看引导,不代表完成安全设置。
|
||||
// AcknowledgeSecuritySetup 仅关闭当前登录会话的提醒,下次登录重新检查设置。
|
||||
func AcknowledgeSecuritySetup(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
@@ -25,12 +26,10 @@ func AcknowledgeSecuritySetup(ctx *context.Context, w http.ResponseWriter, req *
|
||||
writeSecurityError(w, response.NeedLogin, "请重新登录")
|
||||
return
|
||||
}
|
||||
if user.SecuritySetupAcknowledged == 0 {
|
||||
user.SecuritySetupAcknowledged = 1
|
||||
if _, err := db.Instance.ID(ctx.UserID).Cols("security_setup_acknowledged").Update(&user); err != nil {
|
||||
writeSecurityError(w, response.ServerError, "保存安全引导状态失败,请重试")
|
||||
return
|
||||
}
|
||||
if session.Instance == nil || req == nil {
|
||||
writeSecurityError(w, response.ServerError, "保存提醒状态失败,请重试")
|
||||
return
|
||||
}
|
||||
session.Instance.Put(req.Context(), session.SecuritySetupDismissedKey, true)
|
||||
response.NewSuccessResponse(map[string]any{"showSecuritySetupPrompt": false}).FPrint(w)
|
||||
}
|
||||
@@ -1,12 +1,14 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
@@ -51,7 +53,8 @@ func TestLogoutDestroysSessionAndCSRFCookies(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecuritySetupAcknowledgementIsPersistentAndAccountScoped(t *testing.T) {
|
||||
func setupSecurityPromptTest(t *testing.T) *xorm.Engine {
|
||||
t.Helper()
|
||||
engine, err := xorm.NewEngine("sqlite", filepath.Join(t.TempDir(), "onboarding.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -59,9 +62,25 @@ func TestSecuritySetupAcknowledgementIsPersistentAndAccountScoped(t *testing.T)
|
||||
previous := db.Instance
|
||||
db.Instance = engine
|
||||
t.Cleanup(func() { db.Instance = previous; _ = engine.Close() })
|
||||
if err := engine.Sync2(&models.User{}); err != nil {
|
||||
if err := engine.Sync2(&models.User{}, &models.ExternalEmail{}, &models.RecoveryCode{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
previousRuntime := securityRuntime
|
||||
securityRuntime.credentials = nil
|
||||
t.Cleanup(func() { securityRuntime = previousRuntime })
|
||||
return engine
|
||||
}
|
||||
|
||||
func TestSecuritySetupAcknowledgementIsSessionScoped(t *testing.T) {
|
||||
engine := setupSecurityPromptTest(t)
|
||||
previousSession, previousConfig, previousManager := session.Instance, config.Instance, config.CurrentManager()
|
||||
session.Instance = scs.New()
|
||||
config.SetRuntimeManager(nil)
|
||||
config.Instance = &config.Config{Domain: "example.com"}
|
||||
t.Cleanup(func() {
|
||||
session.Instance, config.Instance = previousSession, previousConfig
|
||||
config.SetRuntimeManager(previousManager)
|
||||
})
|
||||
first := models.User{Account: "root", Name: "Root", Role: string(access.RoleSuperAdmin)}
|
||||
second := models.User{Account: "alice", Name: "Alice", Role: string(access.RoleUser)}
|
||||
if _, err := engine.Insert(&first, &second); err != nil {
|
||||
@@ -70,10 +89,18 @@ func TestSecuritySetupAcknowledgementIsPersistentAndAccountScoped(t *testing.T)
|
||||
if loginSecurityFields(&first)["showSecuritySetupPrompt"] != true {
|
||||
t.Fatal("first super administrator login must show security setup")
|
||||
}
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/security/setup/acknowledge", strings.NewReader(`{"user_id":2}`))
|
||||
loadedSession, err := session.Instance.Load(request.Context(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request = request.WithContext(loadedSession)
|
||||
if err := session.PutUser(loadedSession, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
recorder := httptest.NewRecorder()
|
||||
AcknowledgeSecuritySetup(&appcontext.Context{UserID: first.ID}, recorder,
|
||||
httptest.NewRequest(http.MethodPost, "/api/v1/security/setup/acknowledge", strings.NewReader(`{"user_id":2}`)))
|
||||
AcknowledgeSecuritySetup(&appcontext.Context{UserID: first.ID}, recorder, request)
|
||||
var payload struct {
|
||||
ErrorNo int `json:"errorNo"`
|
||||
}
|
||||
@@ -88,16 +115,87 @@ func TestSecuritySetupAcknowledgementIsPersistentAndAccountScoped(t *testing.T)
|
||||
if _, err := engine.ID(second.ID).Get(&untouched); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if loaded.SecuritySetupAcknowledged != 1 || untouched.SecuritySetupAcknowledged != 0 {
|
||||
t.Fatal("acknowledgement changed the wrong account")
|
||||
if loaded.SecuritySetupAcknowledged != 0 || untouched.SecuritySetupAcknowledged != 0 {
|
||||
t.Fatal("dismissal must not persist on either account")
|
||||
}
|
||||
fields := loginSecurityFields(&loaded)
|
||||
if fields["showSecuritySetupPrompt"] != false || fields["showRecoverySetupPrompt"] != false {
|
||||
t.Fatal("acknowledged super administrator must not receive either prompt")
|
||||
if fields["showSecuritySetupPrompt"] != true || fields["showRecoverySetupPrompt"] != false {
|
||||
t.Fatal("incomplete setup must be reported again on login")
|
||||
}
|
||||
if untouched.Password != second.Password || loaded.SecurityVersion != first.SecurityVersion {
|
||||
t.Fatal("acknowledgement changed security credentials")
|
||||
}
|
||||
otherSession, err := session.Instance.Load(context.Background(), "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if session.Instance.GetBool(otherSession, session.SecuritySetupDismissedKey) {
|
||||
t.Fatal("dismissal leaked to another session")
|
||||
}
|
||||
token, _, err := session.Instance.Commit(loadedSession)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
refreshed, err := session.Instance.Load(context.Background(), token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertInfoPrompt := func(want bool) {
|
||||
t.Helper()
|
||||
recorder := httptest.NewRecorder()
|
||||
Info(&appcontext.Context{UserID: first.ID, Role: access.RoleSuperAdmin}, recorder, request.WithContext(refreshed))
|
||||
var payload struct {
|
||||
ErrorNo int `json:"errorNo"`
|
||||
Data struct {
|
||||
Show bool `json:"showSecuritySetupPrompt"`
|
||||
} `json:"data"`
|
||||
}
|
||||
if err := json.Unmarshal(recorder.Body.Bytes(), &payload); err != nil || payload.ErrorNo != 0 || payload.Data.Show != want {
|
||||
t.Fatalf("unexpected refreshed prompt: %s", recorder.Body.String())
|
||||
}
|
||||
}
|
||||
assertInfoPrompt(false)
|
||||
if err := session.PutUser(refreshed, &first); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertInfoPrompt(true)
|
||||
}
|
||||
|
||||
func TestSecuritySetupRequiresUsableCredentials(t *testing.T) {
|
||||
for _, role := range []access.Role{access.RoleUser, access.RoleAdmin, access.RoleSuperAdmin} {
|
||||
for _, emailStatus := range []string{"", models.ExternalEmailStatusPending, models.ExternalEmailStatusVerified, models.ExternalEmailStatusDisabled} {
|
||||
for _, codeStatus := range []string{"missing", "valid", "used"} {
|
||||
t.Run(string(role)+"/"+emailStatus+"/"+codeStatus, func(t *testing.T) {
|
||||
engine := setupSecurityPromptTest(t)
|
||||
user := models.User{Account: "alice", Role: string(role), SecuritySetupAcknowledged: 1}
|
||||
if _, err := engine.Insert(&user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if emailStatus != "" {
|
||||
if _, err := engine.Insert(&models.ExternalEmail{UserID: user.ID, Status: emailStatus, EmailFingerprint: "fixture"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if codeStatus != "missing" {
|
||||
code := models.RecoveryCode{UserID: user.ID, CodeHash: "fixture"}
|
||||
if codeStatus == "used" {
|
||||
now := time.Now()
|
||||
code.UsedAt = &now
|
||||
}
|
||||
if _, err := engine.Insert(&code); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
fields := loginSecurityFields(&user)
|
||||
emailReady := emailStatus == models.ExternalEmailStatusVerified
|
||||
codeReady := codeStatus == "valid" || role == access.RoleSuperAdmin
|
||||
if fields["externalEmailConfigured"] != emailReady || fields["showSecuritySetupPrompt"] != (!emailReady || !codeReady) {
|
||||
t.Fatalf("unexpected security state: %#v", fields)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecuritySetupRejectsAnonymous(t *testing.T) {
|
||||
|
||||
+14
-15
@@ -9,6 +9,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/auth"
|
||||
"git.pcux.cn/xuans/Smail/session"
|
||||
"git.pcux.cn/xuans/Smail/utils/array"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
"git.pcux.cn/xuans/Smail/utils/csrf"
|
||||
@@ -226,22 +227,20 @@ func Info(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
domains := array.Difference(mailDomains.Accepted, []string{mailDomains.Primary})
|
||||
domains = append([]string{mailDomains.Primary}, domains...)
|
||||
result := map[string]any{
|
||||
"id": ctx.UserID,
|
||||
"account": ctx.UserAccount,
|
||||
"name": ctx.UserName,
|
||||
"role": ctx.EffectiveRole(),
|
||||
"capabilities": access.Capabilities(ctx.EffectiveRole()),
|
||||
"is_admin": ctx.IsAdmin,
|
||||
"must_change_password": ctx.MustChangePassword,
|
||||
"showSecuritySetupPrompt": ctx.SecuritySetupAcknowledged == 0,
|
||||
"webAuthnEnabled": webAuthnRuntime() != nil,
|
||||
"domains": domains,
|
||||
"id": ctx.UserID,
|
||||
"account": ctx.UserAccount,
|
||||
"name": ctx.UserName,
|
||||
"role": ctx.EffectiveRole(),
|
||||
"capabilities": access.Capabilities(ctx.EffectiveRole()),
|
||||
"is_admin": ctx.IsAdmin,
|
||||
"must_change_password": ctx.MustChangePassword,
|
||||
"domains": domains,
|
||||
}
|
||||
if ctx.EffectiveRole() != access.RoleSuperAdmin {
|
||||
result["recoveryCodesConfigured"] = ctx.RecoveryCodeConfigured
|
||||
result["showRecoverySetupPrompt"] = !ctx.RecoveryCodeConfigured
|
||||
} else {
|
||||
result["recoveryCodesConfigured"] = false
|
||||
for key, value := range loginSecurityFields(&models.User{ID: ctx.UserID, Role: string(ctx.EffectiveRole())}) {
|
||||
result[key] = value
|
||||
}
|
||||
if session.Instance != nil && session.Instance.GetBool(req.Context(), session.SecuritySetupDismissedKey) {
|
||||
result["showSecuritySetupPrompt"] = false
|
||||
result["showRecoverySetupPrompt"] = false
|
||||
}
|
||||
response.NewSuccessResponse(result).FPrint(w)
|
||||
|
||||
Reference in new issue
Block a user