补充安全码加密存储及受权限约束的再次查看,增加前端验证和自动隐藏;构建已通过,云端业务验收待完成。
This commit is contained in:
1 parent
5e4ff07b1d
commit
2f24a72054
15 files changed
+411
-38
No files matched your search
@@ -352,6 +352,39 @@ func GetRecoveryCodeStatus(ctx *context.Context, w http.ResponseWriter, req *htt
|
||||
response.NewSuccessResponse(map[string]any{"configured": configured}).FPrint(w)
|
||||
}
|
||||
|
||||
// RevealRecoveryCodes 在服务端确认近期安全验证后返回当前用户有效安全码。
|
||||
func RevealRecoveryCodes(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
writeSecurityError(w, response.NeedLogin, "请先登录")
|
||||
return
|
||||
}
|
||||
if !ctx.RecentAuth {
|
||||
writeSecurityErrorWithCode(w, response.NoAccessPrivileges, "请先重新验证当前密码", string(access.CodeRecentAuthRequired))
|
||||
return
|
||||
}
|
||||
service := credentialRuntime()
|
||||
if service == nil {
|
||||
writeSecurityError(w, response.ServerError, "安全凭据服务不可用")
|
||||
return
|
||||
}
|
||||
codes, err := service.RevealRecoveryCodes(ctx.UserID)
|
||||
if err != nil {
|
||||
if errors.Is(err, credentials.ErrNoCiphertext) {
|
||||
writeSecurityError(w, response.ParamsError, "现有安全码无法重新查看,请重新生成安全码")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, credentials.ErrCodeNotFound) {
|
||||
writeSecurityError(w, response.ParamsError, "当前没有有效安全码")
|
||||
return
|
||||
}
|
||||
log.WithError(err).Error("reveal recovery codes failed")
|
||||
writeSecurityError(w, response.ServerError, "读取安全码失败")
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{"codes": codes, "revealed": true}).FPrint(w)
|
||||
}
|
||||
|
||||
// RevokeRecoveryCodes 撤销当前用户的全部有效离线恢复码。
|
||||
func RevokeRecoveryCodes(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
if ctx == nil || ctx.UserID <= 0 {
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/access"
|
||||
"git.pcux.cn/xuans/Smail/services/accountgroup"
|
||||
"git.pcux.cn/xuans/Smail/services/credentials"
|
||||
"git.pcux.cn/xuans/Smail/services/externalemail"
|
||||
"git.pcux.cn/xuans/Smail/services/verification"
|
||||
"git.pcux.cn/xuans/Smail/utils/context"
|
||||
@@ -105,6 +106,40 @@ func GetManagedRecoveryCodeStatus(ctx *context.Context, w http.ResponseWriter, r
|
||||
}).FPrint(w)
|
||||
}
|
||||
|
||||
// RevealManagedRecoveryCodes 在管理员完成近期安全验证后返回目标用户有效安全码。
|
||||
func RevealManagedRecoveryCodes(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
var input managedUserSecurityRequest
|
||||
if err := decodeUserRequest(w, req, &input); err != nil {
|
||||
writeSecurityError(w, response.ParamsError, "请求无效")
|
||||
return
|
||||
}
|
||||
target, ok := loadManagedSecurityUser(ctx, w, input.UserID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
service := credentialRuntime()
|
||||
if service == nil {
|
||||
writeSecurityError(w, response.ServerError, "安全凭据服务不可用")
|
||||
return
|
||||
}
|
||||
codes, err := service.RevealRecoveryCodes(target.ID)
|
||||
if err != nil {
|
||||
if errors.Is(err, credentials.ErrNoCiphertext) {
|
||||
writeSecurityError(w, response.ParamsError, "现有安全码无法重新查看,请重新生成安全码")
|
||||
return
|
||||
}
|
||||
if errors.Is(err, credentials.ErrCodeNotFound) {
|
||||
writeSecurityError(w, response.ParamsError, "当前没有有效安全码")
|
||||
return
|
||||
}
|
||||
log.WithContext(ctx).WithError(err).Error("reveal managed recovery codes failed")
|
||||
writeSecurityError(w, response.ServerError, "读取安全码失败")
|
||||
return
|
||||
}
|
||||
response.NewSuccessResponse(map[string]any{"user_id": target.ID, "codes": codes, "revealed": true}).FPrint(w)
|
||||
}
|
||||
|
||||
// GenerateManagedRecoveryCodes 为目标用户生成只显示一次的安全码。
|
||||
func GenerateManagedRecoveryCodes(ctx *context.Context, w http.ResponseWriter, req *http.Request) {
|
||||
var input managedUserSecurityRequest
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
package migration
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func applyRecoveryCodeCiphertextSchema(ctx context.Context, exec executor, dialect Dialect) error {
|
||||
definitions := map[Dialect]string{
|
||||
DialectSQLite: "TEXT NULL",
|
||||
DialectMySQL: "VARCHAR(512) NULL",
|
||||
DialectPostgres: "VARCHAR(512) NULL",
|
||||
}
|
||||
definition, ok := definitions[dialect]
|
||||
if !ok {
|
||||
return fmt.Errorf("unsupported migration dialect %q", dialect)
|
||||
}
|
||||
return ensureColumn(ctx, exec, dialect, "recovery_code", "code_ciphertext", definition)
|
||||
}
|
||||
@@ -42,6 +42,7 @@ func DefaultMigrations() []Migration {
|
||||
DefineForDialects(25, "create_notification_grants", notificationGrantStatements()),
|
||||
DefineForDialects(26, "create_account_groups", accountGroupStatements()),
|
||||
DefineCustom(27, "expand_notification_ciphertext", "expand_notification_ciphertext_v1", applyNotificationCiphertextSchema),
|
||||
DefineCustom(28, "expand_recovery_code_ciphertext", "expand_recovery_code_ciphertext_v1", applyRecoveryCodeCiphertextSchema),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ func registerV1Routes(mux *http.ServeMux) {
|
||||
registerV1CredentialsQuestionRoute(mux)
|
||||
registerV1TOTPRoute(mux)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes", "/api/security/credentials/recovery-codes", http.MethodGet, 0, controllers.GetRecoveryCodeStatus)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes/reveal", "/api/security/credentials/recovery-codes/reveal", http.MethodPost, 4<<10, controllers.RevealRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes/generate", "/api/security/credentials/recovery-codes/generate", http.MethodPost, 16<<10, controllers.GenerateRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/recovery-codes/revoke", "/api/security/credentials/recovery-codes/revoke", http.MethodPost, 4<<10, controllers.RevokeRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/security/credentials/protocols", "/api/security/credentials/protocols", http.MethodGet, 0, controllers.ListProtocolCredentials)
|
||||
@@ -89,6 +90,7 @@ func registerV1Routes(mux *http.ServeMux) {
|
||||
registerV1MethodRoute(mux, "/api/v1/user/groups/save", "/api/user/groups/save", http.MethodPost, 4<<10, controllers.SaveAccountGroup)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/groups/membership", "/api/user/groups/membership", http.MethodPost, 4<<10, controllers.SetAccountGroupMembership)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/security/recovery-codes", "/api/user/security/recovery-codes", http.MethodGet, 0, controllers.GetManagedRecoveryCodeStatus)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/security/recovery-codes/reveal", "/api/user/security/recovery-codes/reveal", http.MethodPost, 4<<10, controllers.RevealManagedRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/security/recovery-codes/generate", "/api/user/security/recovery-codes/generate", http.MethodPost, 16<<10, controllers.GenerateManagedRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/security/recovery-codes/revoke", "/api/user/security/recovery-codes/revoke", http.MethodPost, 4<<10, controllers.RevokeManagedRecoveryCodes)
|
||||
registerV1MethodRoute(mux, "/api/v1/user/security/external-email/list", "/api/user/security/external-email/list", http.MethodGet, 0, controllers.ListManagedExternalEmails)
|
||||
|
||||
@@ -78,6 +78,7 @@ func router(mux *http.ServeMux) error {
|
||||
mux.HandleFunc("/api/security/credentials/question", credentialsQuestionHandler())
|
||||
mux.HandleFunc("/api/security/credentials/recovery-codes", onlyMethod(http.MethodGet, contextIterceptor(controllers.GetRecoveryCodeStatus)))
|
||||
mux.HandleFunc("/api/security/credentials/recovery-codes/generate", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.GenerateRecoveryCodes))))
|
||||
mux.HandleFunc("/api/security/credentials/recovery-codes/reveal", onlyMethod(http.MethodPost, limitBody(4<<10, contextIterceptor(controllers.RevealRecoveryCodes))))
|
||||
mux.HandleFunc("/api/security/credentials/recovery-codes/revoke", onlyMethod(http.MethodPost, limitBody(4<<10, contextIterceptor(controllers.RevokeRecoveryCodes))))
|
||||
mux.HandleFunc("/api/security/credentials/protocols", onlyMethod(http.MethodGet, contextIterceptor(controllers.ListProtocolCredentials)))
|
||||
mux.HandleFunc("/api/security/credentials/protocols/enabled", onlyMethod(http.MethodPut, limitBody(16<<10, contextIterceptor(controllers.SetProtocolCredentialEnabled))))
|
||||
@@ -140,6 +141,7 @@ func router(mux *http.ServeMux) error {
|
||||
mux.HandleFunc("/api/user/list", onlyMethod(http.MethodPost, limitBody(64<<10, contextIterceptor(controllers.UserList))))
|
||||
mux.HandleFunc("/api/user/security/recovery-codes", onlyMethod(http.MethodGet, contextIterceptor(controllers.GetManagedRecoveryCodeStatus)))
|
||||
mux.HandleFunc("/api/user/security/recovery-codes/generate", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.GenerateManagedRecoveryCodes))))
|
||||
mux.HandleFunc("/api/user/security/recovery-codes/reveal", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.RevealManagedRecoveryCodes))))
|
||||
mux.HandleFunc("/api/user/security/recovery-codes/revoke", onlyMethod(http.MethodPost, limitBody(4<<10, contextIterceptor(controllers.RevokeManagedRecoveryCodes))))
|
||||
mux.HandleFunc("/api/user/security/external-email/list", onlyMethod(http.MethodGet, contextIterceptor(controllers.ListManagedExternalEmails)))
|
||||
mux.HandleFunc("/api/user/security/external-email/change", onlyMethod(http.MethodPost, limitBody(16<<10, contextIterceptor(controllers.ChangeManagedExternalEmail))))
|
||||
|
||||
@@ -246,6 +246,7 @@ func securitySetupAllowedPath(path, method string) bool {
|
||||
switch normalizedAPIPath(path) {
|
||||
case "/api/user/info", "/api/logout", "/api/security/reauth", "/api/security/setup/acknowledge",
|
||||
"/api/security/totp", "/api/security/credentials/recovery-codes/generate",
|
||||
"/api/security/credentials/recovery-codes/reveal",
|
||||
"/api/security/external-email/bind", "/api/security/external-email/verify":
|
||||
return true
|
||||
case "/api/security/credentials/recovery-codes", "/api/security/external-email/list", "/api/security/credentials/question":
|
||||
|
||||
@@ -40,6 +40,7 @@ var loggedOperations = map[string]string{
|
||||
"/api/security/setup/acknowledge": "确认安全初始化引导",
|
||||
"/api/security/credentials/question": "设置密保问题",
|
||||
"/api/security/credentials/recovery-codes/generate": "生成安全码",
|
||||
"/api/security/credentials/recovery-codes/reveal": "查看安全码",
|
||||
"/api/security/credentials/recovery-codes/revoke": "撤销安全码",
|
||||
"/api/security/credentials/protocols/enabled": "设置邮件客户端授权",
|
||||
"/api/security/credentials/protocols/generate": "生成邮件客户端安全码",
|
||||
@@ -61,6 +62,7 @@ var loggedOperations = map[string]string{
|
||||
"/api/user/edit": "编辑用户",
|
||||
"/api/user/profile": "修改个人资料",
|
||||
"/api/user/security/recovery-codes/generate": "管理用户安全码",
|
||||
"/api/user/security/recovery-codes/reveal": "查看用户安全码",
|
||||
"/api/user/security/recovery-codes/revoke": "撤销用户安全码",
|
||||
"/api/user/security/external-email/change": "申请修改用户外部邮箱",
|
||||
"/api/user/security/external-email/verify": "验证用户外部邮箱修改",
|
||||
|
||||
@@ -17,12 +17,13 @@ func (SecurityQuestion) TableName() string { return "security_question" }
|
||||
|
||||
// RecoveryCode 保存一次性离线恢复码的 Argon2id 哈希,UsedAt 非空表示已经消费或撤销。
|
||||
type RecoveryCode struct {
|
||||
ID int `xorm:"id bigint unsigned not null pk autoincr" json:"-"`
|
||||
UserID int `xorm:"user_id bigint not null index" json:"-"`
|
||||
CodeHash string `xorm:"code_hash varchar(255) notnull" json:"-"`
|
||||
CodeVersion int `xorm:"code_version int notnull default(1)" json:"-"`
|
||||
UsedAt *time.Time `xorm:"used_at datetime null index" json:"-"`
|
||||
CreatedAt time.Time `xorm:"created_at datetime created notnull index" json:"created_at"`
|
||||
ID int `xorm:"id bigint unsigned not null pk autoincr" json:"-"`
|
||||
UserID int `xorm:"user_id bigint not null index" json:"-"`
|
||||
CodeHash string `xorm:"code_hash varchar(255) notnull" json:"-"`
|
||||
CodeCiphertext string `xorm:"code_ciphertext varchar(512) null" json:"-"`
|
||||
CodeVersion int `xorm:"code_version int notnull default(1)" json:"-"`
|
||||
UsedAt *time.Time `xorm:"used_at datetime null index" json:"-"`
|
||||
CreatedAt time.Time `xorm:"created_at datetime created notnull index" json:"created_at"`
|
||||
}
|
||||
|
||||
func (RecoveryCode) TableName() string { return "recovery_code" }
|
||||
|
||||
@@ -6,11 +6,13 @@ import (
|
||||
"encoding/base32"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.pcux.cn/xuans/Smail/models"
|
||||
"git.pcux.cn/xuans/Smail/services/accountgroup"
|
||||
"git.pcux.cn/xuans/Smail/utils/credential"
|
||||
"git.pcux.cn/xuans/Smail/utils/password"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/text/unicode/norm"
|
||||
@@ -25,6 +27,7 @@ var (
|
||||
ErrInvalidCode = errors.New("recovery code is invalid")
|
||||
ErrCodeNotFound = errors.New("recovery code is not valid")
|
||||
ErrCodeConsumed = errors.New("recovery code is already consumed")
|
||||
ErrNoCiphertext = errors.New("recovery code ciphertext is unavailable")
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -177,6 +180,7 @@ func (s *Service) generateRecoveryCodes(ctx context.Context, userID int, count i
|
||||
}()
|
||||
codes = make([]string, 0, count)
|
||||
items := make([]models.RecoveryCode, 0, count)
|
||||
vaultRoot := s.VaultRoot()
|
||||
for i := 0; i < count; i++ {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
@@ -190,7 +194,14 @@ func (s *Service) generateRecoveryCodes(ctx context.Context, userID int, count i
|
||||
return nil, fmt.Errorf("hash recovery code: %w", err)
|
||||
}
|
||||
codes = append(codes, plain)
|
||||
items = append(items, models.RecoveryCode{UserID: userID, CodeHash: hash, CodeVersion: 1})
|
||||
ciphertext := ""
|
||||
if vaultRoot != "" {
|
||||
ciphertext, err = credential.Encrypt(vaultRoot, strconv.Itoa(userID)+":"+plain)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("protect recovery code: %w", err)
|
||||
}
|
||||
}
|
||||
items = append(items, models.RecoveryCode{UserID: userID, CodeHash: hash, CodeCiphertext: ciphertext, CodeVersion: 1})
|
||||
}
|
||||
phase = "lock"
|
||||
log.WithFields(log.Fields{"action": "生成离线恢复码", "phase": phase, "count": count,
|
||||
@@ -214,7 +225,7 @@ func (s *Service) generateRecoveryCodes(ctx context.Context, userID int, count i
|
||||
return nil, err
|
||||
}
|
||||
now := s.now()
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at").Update(&models.RecoveryCode{UsedAt: &now}); err != nil {
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at", "code_ciphertext").Update(&models.RecoveryCode{UsedAt: &now}); err != nil {
|
||||
_ = session.Rollback()
|
||||
return nil, fmt.Errorf("revoke old recovery codes: %w", err)
|
||||
}
|
||||
@@ -237,6 +248,50 @@ func (s *Service) generateRecoveryCodes(ctx context.Context, userID int, count i
|
||||
return codes, nil
|
||||
}
|
||||
|
||||
// RevealRecoveryCodes 解密当前用户有效的安全码,仅在调用方完成安全验证后使用。
|
||||
func (s *Service) RevealRecoveryCodes(userID int) ([]string, error) {
|
||||
if s == nil || s.engine == nil {
|
||||
return nil, ErrUnavailable
|
||||
}
|
||||
if userID <= 0 {
|
||||
return nil, ErrInvalidUser
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
vaultRoot := s.vaultRoot
|
||||
if vaultRoot == "" {
|
||||
return nil, ErrNoCiphertext
|
||||
}
|
||||
var items []models.RecoveryCode
|
||||
if err := s.engine.Where("user_id = ? AND used_at IS NULL", userID).Asc("id").Find(&items); err != nil {
|
||||
return nil, fmt.Errorf("load recovery codes for reveal: %w", err)
|
||||
}
|
||||
if len(items) == 0 {
|
||||
return nil, ErrCodeNotFound
|
||||
}
|
||||
result := make([]string, 0, len(items))
|
||||
for _, item := range items {
|
||||
if item.CodeCiphertext == "" || item.CodeHash == "" {
|
||||
return nil, ErrNoCiphertext
|
||||
}
|
||||
plain, err := credential.Decrypt(vaultRoot, item.CodeCiphertext)
|
||||
if err != nil {
|
||||
return nil, ErrNoCiphertext
|
||||
}
|
||||
account, code, ok := strings.Cut(plain, ":")
|
||||
if !ok || account != strconv.Itoa(userID) || !verifyRecoveryCodeHash(item.CodeHash, code) {
|
||||
return nil, ErrNoCiphertext
|
||||
}
|
||||
result = append(result, code)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func verifyRecoveryCodeHash(encoded, value string) bool {
|
||||
matched, err := password.Check(encoded, value)
|
||||
return err == nil && matched
|
||||
}
|
||||
|
||||
// ConsumeRecoveryCode 原子消费一个恢复码,成功后该码不可再次使用。
|
||||
func (s *Service) ConsumeRecoveryCode(userID int, code string) error {
|
||||
if s == nil || s.engine == nil {
|
||||
@@ -289,7 +344,7 @@ func (s *Service) consumeRecoveryCode(session recoveryCodeSession, userID int, c
|
||||
continue
|
||||
}
|
||||
now := s.now()
|
||||
affected, err := session.ID(item.ID).Where("used_at IS NULL").Cols("used_at").Update(&models.RecoveryCode{UsedAt: &now})
|
||||
affected, err := session.ID(item.ID).Where("used_at IS NULL").Cols("used_at", "code_ciphertext").Update(&models.RecoveryCode{UsedAt: &now})
|
||||
if err != nil {
|
||||
return fmt.Errorf("consume recovery code: %w", err)
|
||||
}
|
||||
@@ -336,7 +391,7 @@ func (s *Service) RevokeRecoveryCodes(userID int, guards ...accountgroup.WriteGu
|
||||
if err := accountgroup.CheckWrite(session, guards...); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at").Update(&models.RecoveryCode{UsedAt: &now}); err != nil {
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at", "code_ciphertext").Update(&models.RecoveryCode{UsedAt: &now}); err != nil {
|
||||
return fmt.Errorf("revoke recovery codes: %w", err)
|
||||
}
|
||||
return session.Commit()
|
||||
@@ -364,7 +419,7 @@ func (s *Service) RevokeAllWithSession(session *xorm.Session, userID int) error
|
||||
return ErrInvalidUser
|
||||
}
|
||||
now := s.now()
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at").Update(&models.RecoveryCode{UsedAt: &now}); err != nil {
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at", "code_ciphertext").Update(&models.RecoveryCode{UsedAt: &now}); err != nil {
|
||||
return fmt.Errorf("revoke recovery codes: %w", err)
|
||||
}
|
||||
if _, err := session.Where("user_id = ?", userID).Delete(&models.SecurityQuestion{}); err != nil {
|
||||
@@ -392,7 +447,7 @@ func revokeAllOnSession(session *xorm.Session, now func() time.Time, userID int)
|
||||
return fmt.Errorf("begin credential revocation: %w", err)
|
||||
}
|
||||
when := now()
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at").Update(&models.RecoveryCode{UsedAt: &when}); err != nil {
|
||||
if _, err := session.Where("user_id = ? AND used_at IS NULL", userID).Cols("used_at", "code_ciphertext").Update(&models.RecoveryCode{UsedAt: &when}); err != nil {
|
||||
_ = session.Rollback()
|
||||
return fmt.Errorf("revoke recovery codes: %w", err)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user