165 lines
5.9 KiB
Go
165 lines
5.9 KiB
Go
package httpapi
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"sea-vhost/internal/auth"
|
|
)
|
|
|
|
func mfaSecretLabel(userID uint64) string {
|
|
return fmt.Sprintf("mfa-user:%d", userID)
|
|
}
|
|
|
|
func (s *Server) accountSecurity(w http.ResponseWriter, r *http.Request) {
|
|
user := currentUser(r)
|
|
security, err := s.store.UserSecurity(r.Context(), user.ID)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法读取账号安全设置")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"mfaEnabled": security.TOTPEnabled})
|
|
}
|
|
|
|
func (s *Server) changePassword(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
CurrentPassword string `json:"currentPassword"`
|
|
NewPassword string `json:"newPassword"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
if len(input.NewPassword) < 12 || len(input.NewPassword) > 128 || input.NewPassword == input.CurrentPassword {
|
|
writeError(w, http.StatusUnprocessableEntity, "新密码应为 12–128 个字符且不能与原密码相同")
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
security, err := s.store.UserSecurity(r.Context(), user.ID)
|
|
if err != nil || !auth.VerifyPasswordConstantWork(security.PasswordHash, input.CurrentPassword) {
|
|
writeError(w, http.StatusUnauthorized, "当前密码不正确")
|
|
return
|
|
}
|
|
hash, err := auth.HashPassword(input.NewPassword)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法更新密码")
|
|
return
|
|
}
|
|
cookie, _ := r.Cookie("sea_session")
|
|
if cookie == nil || s.store.ChangePassword(r.Context(), user.ID, hash, cookie.Value) != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法更新密码")
|
|
return
|
|
}
|
|
s.store.Log(r.Context(), user.TenantID, user.ID, "update", "account_password", "", requestClientIP(r), true, "修改账号密码并注销其他会话")
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) setupMFA(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
CurrentPassword string `json:"currentPassword"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
security, err := s.store.UserSecurity(r.Context(), user.ID)
|
|
if err != nil || !auth.VerifyPasswordConstantWork(security.PasswordHash, input.CurrentPassword) {
|
|
writeError(w, http.StatusUnauthorized, "当前密码不正确")
|
|
return
|
|
}
|
|
if security.TOTPEnabled {
|
|
writeError(w, http.StatusConflict, "双重认证已经启用")
|
|
return
|
|
}
|
|
secret, err := auth.NewTOTPSecret()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法生成双重认证密钥")
|
|
return
|
|
}
|
|
ciphertext, err := s.secretBox.Seal(mfaSecretLabel(user.ID), secret)
|
|
if err != nil || s.store.SetPendingTOTP(r.Context(), user.ID, ciphertext) != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法保存双重认证设置")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"secret": secret, "otpauthURI": auth.TOTPURI(secret, user.LoginName)})
|
|
}
|
|
|
|
func (s *Server) enableMFA(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
Code string `json:"code"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
security, err := s.store.UserSecurity(r.Context(), user.ID)
|
|
secret := ""
|
|
if err == nil && len(security.TOTPSecretCiphertext) > 0 {
|
|
secret, err = s.secretBox.Open(mfaSecretLabel(user.ID), security.TOTPSecretCiphertext)
|
|
}
|
|
if err != nil || secret == "" {
|
|
writeError(w, http.StatusConflict, "请先生成双重认证密钥")
|
|
return
|
|
}
|
|
if !auth.VerifyTOTP(secret, input.Code, time.Now()) {
|
|
writeError(w, http.StatusUnprocessableEntity, "动态验证码无效")
|
|
return
|
|
}
|
|
codes, err := auth.NewRecoveryCodes(10)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法生成恢复码")
|
|
return
|
|
}
|
|
hashes := make([][]byte, 0, len(codes))
|
|
for _, code := range codes {
|
|
hashes = append(hashes, auth.RecoveryCodeHash(code))
|
|
}
|
|
if err = s.store.EnableTOTP(r.Context(), user.ID, hashes); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法启用双重认证")
|
|
return
|
|
}
|
|
s.store.Log(r.Context(), user.TenantID, user.ID, "enable", "account_mfa", "", requestClientIP(r), true, "启用 TOTP 双重认证")
|
|
writeJSON(w, http.StatusOK, map[string]any{"recoveryCodes": codes})
|
|
}
|
|
|
|
func (s *Server) disableMFA(w http.ResponseWriter, r *http.Request) {
|
|
var input struct {
|
|
CurrentPassword string `json:"currentPassword"`
|
|
Code string `json:"code"`
|
|
}
|
|
if err := decodeJSON(r, &input); err != nil {
|
|
writeError(w, http.StatusBadRequest, "请求格式错误")
|
|
return
|
|
}
|
|
user := currentUser(r)
|
|
security, err := s.store.UserSecurity(r.Context(), user.ID)
|
|
if err != nil || !auth.VerifyPasswordConstantWork(security.PasswordHash, input.CurrentPassword) {
|
|
writeError(w, http.StatusUnauthorized, "当前密码不正确")
|
|
return
|
|
}
|
|
if !security.TOTPEnabled {
|
|
writeError(w, http.StatusConflict, "双重认证尚未启用")
|
|
return
|
|
}
|
|
secret, err := s.secretBox.Open(mfaSecretLabel(user.ID), security.TOTPSecretCiphertext)
|
|
validCode := err == nil && auth.VerifyTOTP(secret, input.Code, time.Now())
|
|
if !validCode && strings.TrimSpace(input.Code) != "" {
|
|
validCode, err = s.store.ConsumeRecoveryCode(r.Context(), user.ID, auth.RecoveryCodeHash(input.Code))
|
|
}
|
|
if err != nil || !validCode {
|
|
writeError(w, http.StatusUnprocessableEntity, "动态验证码或恢复码无效")
|
|
return
|
|
}
|
|
if err = s.store.DisableTOTP(r.Context(), user.ID); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "无法关闭双重认证")
|
|
return
|
|
}
|
|
s.store.Log(r.Context(), user.TenantID, user.ID, "disable", "account_mfa", "", requestClientIP(r), true, "关闭 TOTP 双重认证并注销全部会话")
|
|
http.SetCookie(w, &http.Cookie{Name: "sea_session", Value: "", Path: "/", HttpOnly: true, Secure: s.cfg.CookieSecure, SameSite: http.SameSiteStrictMode, MaxAge: -1})
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|