修复审查发现的认证配置邮件协议及前端问题
补齐安全运行时清理、配置候选恢复、泛域名证书事务提交、邮件副本与协议边界、插件隐私和安装回滚回归,并更新修复状态及待验收清单。
This commit is contained in:
1 parent
addfcea1cd
commit
6f79441585
71 files changed
+2853
-664
No files matched your search
@@ -120,16 +120,17 @@ func UpdateConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Requ
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{"generation": result.Generation, "pending": result.Pending}).FPrint(w)
|
||||
response.NewSuccessResponse(map[string]any{"generation": result.Generation, "pending": result.Pending, "candidateId": result.CandidateID}).FPrint(w)
|
||||
}
|
||||
|
||||
type configApplyRequest struct {
|
||||
Generation uint64 `json:"generation"`
|
||||
Generation uint64 `json:"generation"`
|
||||
CandidateID string `json:"candidateId"`
|
||||
}
|
||||
|
||||
// ApplyConfig 应用已保存的候选配置代次。
|
||||
func ApplyConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
manager, _, ok := configRequestManager(w, req)
|
||||
manager, section, ok := configRequestManager(w, req)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
@@ -142,7 +143,7 @@ func ApplyConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Reque
|
||||
writeConfigError(w, errors.New("invalid configuration generation"))
|
||||
return
|
||||
}
|
||||
result, err := manager.ApplyAuthorized(req.Context(), input.Generation, configActor(ctx, req))
|
||||
result, err := manager.ApplyPendingAuthorized(req.Context(), input.Generation, input.CandidateID, section, configActor(ctx, req))
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/dto/response"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
appcontext "git.pcux.cn/xuans/Smail/utils/context"
|
||||
)
|
||||
|
||||
// GetPendingConfig 返回候选的脱敏视图,用于刷新或重新登录后的恢复。
|
||||
func GetPendingConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
manager, ok := configManagerOrError(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigRead, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
pending := manager.Pending(configActor(ctx, req))
|
||||
if pending == nil {
|
||||
response.NewSuccessResponse(nil).FPrint(w)
|
||||
return
|
||||
}
|
||||
public, err := publicConfigSection(pending.Candidate, pending.Section)
|
||||
if err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{"generation": pending.Generation, "candidateId": pending.ID, "section": pending.Section, "sameSession": pending.SameSession, "committed": pending.Committed, "payload": public}).FPrint(w)
|
||||
}
|
||||
|
||||
func ManagePendingConfig(ctx *appcontext.Context, w http.ResponseWriter, req *http.Request) {
|
||||
manager, ok := configManagerOrError(w)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := authorizeCapability(ctx, access.CapabilityConfigWriteGlobal, "", false); err != nil {
|
||||
writeConfigAccessError(w, err)
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Generation uint64 `json:"generation"`
|
||||
CandidateID string `json:"candidateId"`
|
||||
Action string `json:"action"`
|
||||
Confirmation config.StepUpConfirmation `json:"confirmation"`
|
||||
}
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || input.Generation == 0 || (input.Action != "cancel" && input.Action != "resume") {
|
||||
writeConfigError(w, errors.New("invalid configuration request"))
|
||||
return
|
||||
}
|
||||
if err := manager.ManagePending(req.Context(), input.Generation, input.CandidateID, configActor(ctx, req), input.Confirmation, input.Action == "cancel"); err != nil {
|
||||
writeConfigError(w, err)
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(nil).FPrint(w)
|
||||
}
|
||||
@@ -82,21 +82,22 @@ func loginVerificationPolicyEnabled(enabled bool, policy string) bool {
|
||||
}
|
||||
|
||||
func beginLoginVerification(user *models.User, remoteIP string) (map[string]any, error) {
|
||||
runtime := currentSecurityRuntime()
|
||||
if !loginVerificationEnabled() {
|
||||
return nil, nil
|
||||
}
|
||||
if user == nil || user.ID <= 0 || securityRuntime.verification == nil || securityRuntime.external == nil {
|
||||
if user == nil || user.ID <= 0 || runtime.verification == nil || runtime.external == nil {
|
||||
return nil, errLoginVerificationUnavailable
|
||||
}
|
||||
binding, err := securityRuntime.external.FindVerifiedPrimaryByUser(user.ID)
|
||||
binding, err := runtime.external.FindVerifiedPrimaryByUser(user.ID)
|
||||
if err != nil {
|
||||
return nil, errLoginVerificationUnavailable
|
||||
}
|
||||
email, err := securityRuntime.external.Decrypt(*binding)
|
||||
email, err := runtime.external.Decrypt(*binding)
|
||||
if err != nil {
|
||||
return nil, errLoginVerificationUnavailable
|
||||
}
|
||||
challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{
|
||||
challenge, err := runtime.verification.Issue(verification.IssueRequest{
|
||||
Purpose: verification.PurposeLogin,
|
||||
Account: user.Account,
|
||||
Email: email,
|
||||
@@ -109,7 +110,7 @@ func beginLoginVerification(user *models.User, remoteIP string) (map[string]any,
|
||||
cleanupLoginVerificationLocked(time.Now())
|
||||
if len(loginVerificationState.items) >= loginVerificationMaxEntries {
|
||||
loginVerificationState.Unlock()
|
||||
securityRuntime.verification.Store.InvalidateChallenge(challenge.ID)
|
||||
runtime.verification.Store.InvalidateChallenge(challenge.ID)
|
||||
return nil, errLoginVerificationUnavailable
|
||||
}
|
||||
loginVerificationState.items[challenge.ID] = loginVerificationItem{
|
||||
@@ -128,6 +129,7 @@ func beginLoginVerification(user *models.User, remoteIP string) (map[string]any,
|
||||
|
||||
// ResendLoginVerification 使旧登录验证码失效后重新发送新验证码。
|
||||
func ResendLoginVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
var input resendLoginVerificationRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || strings.TrimSpace(input.ID) == "" {
|
||||
writeSecurityError(w, response.ParamsError, "验证码请求无效")
|
||||
@@ -145,12 +147,12 @@ func ResendLoginVerification(ctx *context.Context, w http.ResponseWriter, req *h
|
||||
writeSecurityError(w, response.ParamsError, "登录验证已失效,请重新登录")
|
||||
return
|
||||
}
|
||||
if securityRuntime.verification == nil || securityRuntime.verification.Store == nil {
|
||||
if runtime.verification == nil || runtime.verification.Store == nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证码服务暂不可用")
|
||||
return
|
||||
}
|
||||
securityRuntime.verification.Store.InvalidateChallenge(input.ID)
|
||||
challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{
|
||||
runtime.verification.Store.InvalidateChallenge(input.ID)
|
||||
challenge, err := runtime.verification.Issue(verification.IssueRequest{
|
||||
Purpose: verification.PurposeLogin,
|
||||
UserID: item.UserID,
|
||||
Account: item.Account,
|
||||
@@ -180,8 +182,9 @@ func ResendLoginVerification(ctx *context.Context, w http.ResponseWriter, req *h
|
||||
|
||||
// CompleteLoginVerification 只有验证码成功后才创建完整 Session。
|
||||
func CompleteLoginVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
var input loginVerificationRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.verification == nil {
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || runtime.verification == nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
@@ -196,7 +199,7 @@ func CompleteLoginVerification(ctx *context.Context, w http.ResponseWriter, req
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
if err := securityRuntime.verification.Verify(input.ID, item.Account, item.Email, input.Code, verification.PurposeLogin, remoteIP(req)); err != nil {
|
||||
if err := runtime.verification.Verify(input.ID, item.Account, item.Email, input.Code, verification.PurposeLogin, remoteIP(req)); err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/config"
|
||||
"git.pcux.cn/xuans/Smail/db"
|
||||
@@ -68,42 +69,67 @@ func rejectUnavailableRecovery(w http.ResponseWriter) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
var securityRuntime = struct {
|
||||
type securityRuntimeState struct {
|
||||
verification *verification.Service
|
||||
external *externalemail.Repository
|
||||
recovery *recovery.Service
|
||||
credentials *credentials.Service
|
||||
webauthn *webauthnservice.Service
|
||||
}{}
|
||||
}
|
||||
|
||||
var securityRuntime securityRuntimeState
|
||||
var securityRuntimeMu sync.RWMutex
|
||||
|
||||
// 每个请求持有同一份服务快照,发布锁不跨越数据库、投递和失效回调。
|
||||
func currentSecurityRuntime() securityRuntimeState {
|
||||
securityRuntimeMu.RLock()
|
||||
defer securityRuntimeMu.RUnlock()
|
||||
return securityRuntime
|
||||
}
|
||||
|
||||
// ConfigureSecurityRuntime 由主程序在数据库和内存安全服务就绪后调用。
|
||||
func ConfigureSecurityRuntime(verify *verification.Service, external *externalemail.Repository, recover *recovery.Service, credential *credentials.Service) {
|
||||
if recover != nil {
|
||||
recover.SetSecurityStateInvalidator(InvalidateSecurityRuntimeUser)
|
||||
}
|
||||
securityRuntimeMu.Lock()
|
||||
defer securityRuntimeMu.Unlock()
|
||||
securityRuntime.verification = verify
|
||||
securityRuntime.external = external
|
||||
securityRuntime.recovery = recover
|
||||
securityRuntime.credentials = credential
|
||||
if recover != nil {
|
||||
recover.SetSecurityStateInvalidator(InvalidateSecurityRuntimeUser)
|
||||
}
|
||||
}
|
||||
|
||||
func ConfigureWebAuthnRuntime(service *webauthnservice.Service) {
|
||||
securityRuntimeMu.Lock()
|
||||
defer securityRuntimeMu.Unlock()
|
||||
securityRuntime.webauthn = service
|
||||
}
|
||||
|
||||
// ClearSecurityRuntime 在停止入口后一次撤下全部安全服务,避免保留已停止的旧库依赖。
|
||||
func ClearSecurityRuntime() {
|
||||
securityRuntimeMu.Lock()
|
||||
securityRuntime = securityRuntimeState{}
|
||||
securityRuntimeMu.Unlock()
|
||||
loginVerificationState.Lock()
|
||||
clear(loginVerificationState.items)
|
||||
loginVerificationState.Unlock()
|
||||
}
|
||||
|
||||
// InvalidateSecurityRuntimeUser 清除密码重置后仍驻留在内存中的安全流程状态。
|
||||
func InvalidateSecurityRuntimeUser(userID int, account string) {
|
||||
if userID <= 0 {
|
||||
return
|
||||
}
|
||||
if securityRuntime.recovery != nil {
|
||||
securityRuntime.recovery.InvalidateUser(userID)
|
||||
runtime := currentSecurityRuntime()
|
||||
if runtime.recovery != nil {
|
||||
runtime.recovery.InvalidateUser(userID)
|
||||
}
|
||||
if securityRuntime.verification != nil && securityRuntime.verification.Store != nil {
|
||||
securityRuntime.verification.Store.InvalidateAccount(account)
|
||||
if runtime.verification != nil && runtime.verification.Store != nil {
|
||||
runtime.verification.Store.InvalidateAccount(account)
|
||||
}
|
||||
if securityRuntime.webauthn != nil {
|
||||
securityRuntime.webauthn.InvalidateUser(userID)
|
||||
if runtime.webauthn != nil {
|
||||
runtime.webauthn.InvalidateUser(userID)
|
||||
}
|
||||
loginVerificationState.Lock()
|
||||
for id, item := range loginVerificationState.items {
|
||||
@@ -122,7 +148,7 @@ func LoadSecurityContext(ctx *context.Context) {
|
||||
ctx.RecoveryCodeConfigured = false
|
||||
return
|
||||
}
|
||||
service := securityRuntime.credentials
|
||||
service := currentSecurityRuntime().credentials
|
||||
if service == nil && db.Instance != nil {
|
||||
service, _ = credentials.NewService(db.Instance)
|
||||
}
|
||||
@@ -136,8 +162,8 @@ func LoadSecurityContext(ctx *context.Context) {
|
||||
}
|
||||
|
||||
func credentialRuntime() *credentials.Service {
|
||||
if securityRuntime.credentials != nil {
|
||||
return securityRuntime.credentials
|
||||
if service := currentSecurityRuntime().credentials; service != nil {
|
||||
return service
|
||||
}
|
||||
if db.Instance == nil {
|
||||
return nil
|
||||
@@ -406,6 +432,7 @@ func RevokeProtocolCredential(ctx *context.Context, w http.ResponseWriter, req *
|
||||
response.NewSuccessResponse(map[string]any{"protocol": protocol, "revoked": true}).FPrint(w)
|
||||
}
|
||||
func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
@@ -422,11 +449,11 @@ func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http.
|
||||
writeSecurityError(w, response.ParamsError, "请求无效")
|
||||
return
|
||||
}
|
||||
if input.Email == "" || strings.TrimSpace(input.Account) == "" || !verification.IsSupportedPurpose(input.Purpose) || securityRuntime.verification == nil {
|
||||
if input.Email == "" || strings.TrimSpace(input.Account) == "" || !verification.IsSupportedPurpose(input.Purpose) || runtime.verification == nil {
|
||||
writeSecurityError(w, response.ParamsError, "请求已受理")
|
||||
return
|
||||
}
|
||||
challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{Purpose: input.Purpose, UserID: ctx.UserID, Account: input.Account, Email: input.Email}, remoteIP(req))
|
||||
challenge, err := runtime.verification.Issue(verification.IssueRequest{Purpose: input.Purpose, UserID: ctx.UserID, Account: input.Account, Email: input.Email}, remoteIP(req))
|
||||
if err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "请求已受理")
|
||||
return
|
||||
@@ -436,12 +463,13 @@ func RequestVerification(ctx *context.Context, w http.ResponseWriter, req *http.
|
||||
|
||||
// VerifyVerification 消费一次性验证码,只返回后续流程所需的短时票据。
|
||||
func VerifyVerification(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
}
|
||||
var input verificationCheckRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.verification == nil {
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || runtime.verification == nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
@@ -452,7 +480,7 @@ func VerifyVerification(ctx *context.Context, w http.ResponseWriter, req *http.R
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
if err := securityRuntime.verification.Verify(input.ID, input.Account, input.Email, input.Code, input.Purpose, remoteIP(req)); err != nil {
|
||||
if err := runtime.verification.Verify(input.ID, input.Account, input.Email, input.Code, input.Purpose, remoteIP(req)); err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
@@ -460,11 +488,12 @@ func VerifyVerification(ctx *context.Context, w http.ResponseWriter, req *http.R
|
||||
}
|
||||
|
||||
func BeginPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
if rejectUnavailableRecovery(w) {
|
||||
return
|
||||
}
|
||||
var input verificationRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.recovery == nil {
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || runtime.recovery == nil {
|
||||
writeSecurityError(w, response.ParamsError, "请求已受理")
|
||||
return
|
||||
}
|
||||
@@ -486,7 +515,7 @@ func BeginPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *htt
|
||||
writeSecurityError(w, response.ParamsError, "\u8bf7\u6c42\u5df2\u53d7\u7406")
|
||||
return
|
||||
}
|
||||
id, err := securityRuntime.recovery.Begin(recovery.BeginRequest{Identifier: input.Account, Email: input.Email, BrowserContext: browserContext}, remoteIP(req))
|
||||
id, err := runtime.recovery.Begin(recovery.BeginRequest{Identifier: input.Account, Email: input.Email, BrowserContext: browserContext}, remoteIP(req))
|
||||
if err != nil && id == "" {
|
||||
emailRecoveryFailureIPAttempts.recordFailure(emailRecoveryFailureIPAttempts.ipKey(req))
|
||||
writeSecurityError(w, response.ParamsError, "请求已受理")
|
||||
@@ -497,6 +526,7 @@ func BeginPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *htt
|
||||
}
|
||||
|
||||
func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
if rejectUnavailableRecovery(w) {
|
||||
return
|
||||
}
|
||||
@@ -506,7 +536,7 @@ func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *ht
|
||||
return
|
||||
}
|
||||
var input verificationCheckRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.recovery == nil {
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || runtime.recovery == nil {
|
||||
emailRecoveryFailureIPAttempts.recordFailure(failureKey)
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
@@ -524,9 +554,9 @@ func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *ht
|
||||
return
|
||||
}
|
||||
if methodOffline {
|
||||
ticket, err = securityRuntime.recovery.VerifyOfflineWithContext(input.Account, input.Question, input.Answer, input.RecoveryCode, remoteIP(req), browserContext)
|
||||
ticket, err = runtime.recovery.VerifyOfflineWithContext(input.Account, input.Question, input.Answer, input.RecoveryCode, remoteIP(req), browserContext)
|
||||
} else {
|
||||
ticket, err = securityRuntime.recovery.VerifyWithContext(input.ID, input.Account, input.Email, input.Code, input.RecoveryCode, remoteIP(req), browserContext)
|
||||
ticket, err = runtime.recovery.VerifyWithContext(input.ID, input.Account, input.Email, input.Code, input.RecoveryCode, remoteIP(req), browserContext)
|
||||
}
|
||||
if err != nil {
|
||||
emailRecoveryFailureIPAttempts.recordFailure(failureKey)
|
||||
@@ -537,6 +567,7 @@ func VerifyPasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *ht
|
||||
}
|
||||
|
||||
func CompletePasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
runtime := currentSecurityRuntime()
|
||||
if rejectUnavailableRecovery(w) {
|
||||
return
|
||||
}
|
||||
@@ -546,7 +577,7 @@ func CompletePasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *
|
||||
return
|
||||
}
|
||||
var input recoveryCompleteRequest
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || securityRuntime.recovery == nil || strings.TrimSpace(input.Ticket) == "" {
|
||||
if err := decodeJSONRequest(w, req, &input); err != nil || runtime.recovery == nil || strings.TrimSpace(input.Ticket) == "" {
|
||||
emailRecoveryFailureIPAttempts.recordFailure(failureKey)
|
||||
writeSecurityError(w, response.ParamsError, "重置失败")
|
||||
return
|
||||
@@ -557,7 +588,7 @@ func CompletePasswordRecovery(ctx *context.Context, w http.ResponseWriter, req *
|
||||
writeSecurityError(w, response.ParamsError, "\u9a8c\u8bc1\u5931\u8d25")
|
||||
return
|
||||
}
|
||||
if err := securityRuntime.recovery.CompleteWithContext(input.Ticket, input.NewPassword, browserContext); err != nil {
|
||||
if err := runtime.recovery.CompleteWithContext(input.Ticket, input.NewPassword, browserContext); err != nil {
|
||||
emailRecoveryFailureIPAttempts.recordFailure(failureKey)
|
||||
writeSecurityError(w, response.ParamsError, "重置失败")
|
||||
return
|
||||
@@ -609,11 +640,12 @@ func newOpaqueRecoveryChallengeID() (string, error) {
|
||||
}
|
||||
|
||||
func ListExternalEmails(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil {
|
||||
runtime := currentSecurityRuntime()
|
||||
if ctx == nil || ctx.UserID <= 0 || runtime.external == nil {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
}
|
||||
items, err := securityRuntime.external.List(ctx.UserID)
|
||||
items, err := runtime.external.List(ctx.UserID)
|
||||
if err != nil {
|
||||
log.WithError(err).Error("list external email failed")
|
||||
writeSecurityError(w, response.ServerError, "读取外部邮箱失败")
|
||||
@@ -621,7 +653,7 @@ func ListExternalEmails(ctx *context.Context, w http.ResponseWriter, req *http.R
|
||||
}
|
||||
result := make([]map[string]any, 0, len(items))
|
||||
for _, item := range items {
|
||||
email, err := securityRuntime.external.Decrypt(item)
|
||||
email, err := runtime.external.Decrypt(item)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -631,7 +663,8 @@ func ListExternalEmails(ctx *context.Context, w http.ResponseWriter, req *http.R
|
||||
}
|
||||
|
||||
func BindExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil || securityRuntime.verification == nil {
|
||||
runtime := currentSecurityRuntime()
|
||||
if ctx == nil || ctx.UserID <= 0 || runtime.external == nil || runtime.verification == nil {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
}
|
||||
@@ -660,14 +693,14 @@ func BindExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Re
|
||||
return
|
||||
}
|
||||
input.Email = normalizedEmail
|
||||
item, err := securityRuntime.external.Bind(externalemail.BindRequest{UserID: ctx.UserID, Email: input.Email, Type: input.Type})
|
||||
item, err := runtime.external.Bind(externalemail.BindRequest{UserID: ctx.UserID, Email: input.Email, Type: input.Type})
|
||||
if err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "外部邮箱绑定失败")
|
||||
return
|
||||
}
|
||||
challenge, err := securityRuntime.verification.Issue(verification.IssueRequest{Purpose: verification.PurposeExternalEmailBind, UserID: ctx.UserID, Account: ctx.UserAccount, Email: input.Email, ResourceID: item.ID}, remoteIP(req))
|
||||
challenge, err := runtime.verification.Issue(verification.IssueRequest{Purpose: verification.PurposeExternalEmailBind, UserID: ctx.UserID, Account: ctx.UserAccount, Email: input.Email, ResourceID: item.ID}, remoteIP(req))
|
||||
if err != nil {
|
||||
_ = securityRuntime.external.AbandonPending(ctx.UserID, item.ID)
|
||||
_ = runtime.external.AbandonPending(ctx.UserID, item.ID)
|
||||
writeSecurityError(w, response.ParamsError, "外部邮箱绑定失败")
|
||||
return
|
||||
}
|
||||
@@ -683,7 +716,8 @@ func currentUserPrimaryEmail(userID int, email string) (bool, error) {
|
||||
}
|
||||
|
||||
func VerifyExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil || securityRuntime.verification == nil {
|
||||
runtime := currentSecurityRuntime()
|
||||
if ctx == nil || ctx.UserID <= 0 || runtime.external == nil || runtime.verification == nil {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
}
|
||||
@@ -692,11 +726,11 @@ func VerifyExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
if err := securityRuntime.verification.VerifyBound(input.ID, ctx.UserAccount, input.Email, input.Code, verification.PurposeExternalEmailBind, input.BindingID, remoteIP(req)); err != nil {
|
||||
if err := runtime.verification.VerifyBound(input.ID, ctx.UserAccount, input.Email, input.Code, verification.PurposeExternalEmailBind, input.BindingID, remoteIP(req)); err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
if err := securityRuntime.external.Verify(ctx.UserID, input.BindingID); err != nil {
|
||||
if err := runtime.external.Verify(ctx.UserID, input.BindingID); err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "验证失败")
|
||||
return
|
||||
}
|
||||
@@ -704,7 +738,8 @@ func VerifyExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.
|
||||
}
|
||||
|
||||
func DisableExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 || securityRuntime.external == nil {
|
||||
runtime := currentSecurityRuntime()
|
||||
if ctx == nil || ctx.UserID <= 0 || runtime.external == nil {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
}
|
||||
@@ -713,16 +748,16 @@ func DisableExternalEmail(ctx *context.Context, w http.ResponseWriter, req *http
|
||||
writeSecurityError(w, response.ParamsError, "请求无效")
|
||||
return
|
||||
}
|
||||
if err := securityRuntime.external.Disable(ctx.UserID, input.ID); err != nil {
|
||||
if err := runtime.external.Disable(ctx.UserID, input.ID); err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "外部邮箱停用失败")
|
||||
return
|
||||
}
|
||||
if securityRuntime.verification != nil && securityRuntime.verification.Store != nil {
|
||||
securityRuntime.verification.Store.InvalidateResource(input.ID, verification.PurposeExternalEmailBind)
|
||||
securityRuntime.verification.Store.InvalidateAccount(ctx.UserAccount)
|
||||
if runtime.verification != nil && runtime.verification.Store != nil {
|
||||
runtime.verification.Store.InvalidateResource(input.ID, verification.PurposeExternalEmailBind)
|
||||
runtime.verification.Store.InvalidateAccount(ctx.UserAccount)
|
||||
}
|
||||
if securityRuntime.recovery != nil {
|
||||
securityRuntime.recovery.InvalidateUser(ctx.UserID)
|
||||
if runtime.recovery != nil {
|
||||
runtime.recovery.InvalidateUser(ctx.UserID)
|
||||
}
|
||||
loginVerificationState.Lock()
|
||||
for id, item := range loginVerificationState.items {
|
||||
|
||||
@@ -215,7 +215,10 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
}
|
||||
roleChanged := false
|
||||
originalDisabled := user.Disabled
|
||||
originalRole := user.EffectiveRole()
|
||||
originalSessionVersion := user.SessionVersion
|
||||
originalSecurityVersion := user.SecurityVersion
|
||||
if reqData.Role != "" && access.Role(reqData.Role) != targetRole {
|
||||
newRole := access.Role(reqData.Role)
|
||||
if !newRole.Valid() {
|
||||
@@ -237,7 +240,6 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
user.ApplyRole(newRole)
|
||||
roleChanged = true
|
||||
}
|
||||
if user.IsAdmin == 1 && reqData.Disabled != 0 {
|
||||
response.NewErrorResponse(response.ParamsError, "\u4e0d\u5141\u8bb8\u7981\u7528\u7ba1\u7406\u5458\u8d26\u6237", "").FPrint(w)
|
||||
@@ -247,10 +249,6 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
response.NewErrorResponse(response.ParamsError, "\u8bf7\u901a\u8fc7\u5b89\u5168\u8bbe\u7f6e\u4fee\u6539\u5f53\u524d\u8d26\u6237\u5bc6\u7801", "").FPrint(w)
|
||||
return
|
||||
}
|
||||
originalDisabled := user.Disabled
|
||||
originalRole := user.EffectiveRole()
|
||||
originalSessionVersion := user.SessionVersion
|
||||
originalSecurityVersion := user.SecurityVersion
|
||||
if reqData.Username != "" {
|
||||
username, validationErr := validation.NormalizeDisplayName(reqData.Username)
|
||||
if validationErr != nil {
|
||||
@@ -308,7 +306,7 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if reqData.Password != "" || user.Disabled != originalDisabled || user.EffectiveRole() != originalRole {
|
||||
updateColumns = append(updateColumns, "session_version", "security_version")
|
||||
}
|
||||
if num, err := tx.ID(user.ID).Where("session_version = ?", originalSessionVersion).Cols(updateColumns...).Update(&user); err != nil {
|
||||
if num, err := tx.ID(user.ID).Where("session_version = ? AND security_version = ?", originalSessionVersion, originalSecurityVersion).Cols(updateColumns...).Update(&user); err != nil {
|
||||
log.WithContext(ctx).WithError(err).Error("update user failed")
|
||||
response.NewErrorResponse(response.ServerError, "更新用户失败", "").FPrint(w)
|
||||
return
|
||||
@@ -329,9 +327,8 @@ func EditUser(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
return
|
||||
}
|
||||
committed = true
|
||||
if reqData.Password != "" {
|
||||
if user.SessionVersion != originalSessionVersion {
|
||||
InvalidateSecurityRuntimeUser(user.ID, user.Account)
|
||||
}
|
||||
_ = roleChanged
|
||||
response.NewSuccessResponse(user).FPrint(w)
|
||||
}
|
||||
@@ -60,7 +60,7 @@ var (
|
||||
)
|
||||
|
||||
func webAuthnRuntime() *webauthnservice.Service {
|
||||
return securityRuntime.webauthn
|
||||
return currentSecurityRuntime().webauthn
|
||||
}
|
||||
|
||||
// BeginWebAuthnRegistration 开始当前用户的安全密钥注册流程。
|
||||
@@ -218,7 +218,8 @@ func BeginWebAuthnLogin(ctx *context.Context, w http.ResponseWriter, req *http.R
|
||||
|
||||
// FinishWebAuthnLogin 完成安全密钥登录并创建普通业务会话。
|
||||
func FinishWebAuthnLogin(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
service := webAuthnRuntime()
|
||||
runtime := currentSecurityRuntime()
|
||||
service := runtime.webauthn
|
||||
if service == nil {
|
||||
writeSecurityError(w, response.ParamsError, "\u5b89\u5168\u5bc6\u94a5\u767b\u5f55\u4e0d\u53ef\u7528")
|
||||
return
|
||||
@@ -259,8 +260,8 @@ func FinishWebAuthnLogin(ctx *context.Context, w http.ResponseWriter, req *http.
|
||||
writeSecurityError(w, response.ParamsError, "登录验证已失效,请重新登录")
|
||||
return
|
||||
}
|
||||
if securityRuntime.verification != nil && securityRuntime.verification.Store != nil {
|
||||
securityRuntime.verification.Store.InvalidateChallenge(input.ChallengeID)
|
||||
if runtime.verification != nil && runtime.verification.Store != nil {
|
||||
runtime.verification.Store.InvalidateChallenge(input.ChallengeID)
|
||||
}
|
||||
consumePendingLoginVerification(input.ChallengeID, item)
|
||||
completeLoginSession(ctx, w, req, &user)
|
||||
@@ -270,7 +271,7 @@ func BeginWebAuthnRecovery(ctx *context.Context, w http.ResponseWriter, req *htt
|
||||
if rejectUnavailableRecovery(w) {
|
||||
return
|
||||
}
|
||||
service := securityRuntime.recovery
|
||||
service := currentSecurityRuntime().recovery
|
||||
if service == nil {
|
||||
writeSecurityError(w, response.ParamsError, "\u5b89\u5168\u5bc6\u94a5\u6062\u590d\u4e0d\u53ef\u7528")
|
||||
return
|
||||
@@ -304,7 +305,7 @@ func VerifyWebAuthnRecovery(ctx *context.Context, w http.ResponseWriter, req *ht
|
||||
if rejectUnavailableRecovery(w) {
|
||||
return
|
||||
}
|
||||
service := securityRuntime.recovery
|
||||
service := currentSecurityRuntime().recovery
|
||||
if service == nil {
|
||||
writeSecurityError(w, response.ParamsError, "\u5b89\u5168\u5bc6\u94a5\u6062\u590d\u4e0d\u53ef\u7528")
|
||||
return
|
||||
|
||||
Reference in new issue
Block a user