feat: import Sea vHost single-server testing release
This commit is contained in:
commit
248badd33e
441 files changed
+63221
No files matched your search
@@ -0,0 +1,175 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"net/mail"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"sea-vhost/internal/auth"
|
||||
"sea-vhost/internal/store"
|
||||
)
|
||||
|
||||
func oidcProviderKey(c secretServiceConfig) string {
|
||||
digest := sha256.Sum256([]byte(strings.TrimSuffix(strings.TrimSpace(c.Issuer), "/")))
|
||||
return "oidc:" + hex.EncodeToString(digest[:16])
|
||||
}
|
||||
func oidcPasswordAllowed(c secretServiceConfig, role string) bool {
|
||||
var v *bool
|
||||
switch role {
|
||||
case "admin":
|
||||
v = c.AdminPasswordLogin
|
||||
case "tenant":
|
||||
v = c.UserPasswordLogin
|
||||
default:
|
||||
return true
|
||||
}
|
||||
return v == nil || *v
|
||||
}
|
||||
func setOIDCCookie(w http.ResponseWriter, name, value string, age int) {
|
||||
http.SetCookie(w, &http.Cookie{Name: name, Value: value, Path: "/api/v1/auth/oidc", Secure: true, HttpOnly: true, SameSite: http.SameSiteLaxMode, MaxAge: age})
|
||||
}
|
||||
func truncateOIDCName(v string) string {
|
||||
chars := []rune(strings.TrimSpace(v))
|
||||
if len(chars) > 80 {
|
||||
chars = chars[:80]
|
||||
}
|
||||
return string(chars)
|
||||
}
|
||||
func validSSOEmail(p store.PendingOIDCLogin) bool {
|
||||
m, e := mail.ParseAddress(p.Email)
|
||||
return p.EmailVerified && e == nil && m.Address == p.Email && len(p.Email) <= 190
|
||||
}
|
||||
|
||||
func (s *Server) pendingOIDCLogin(w http.ResponseWriter, r *http.Request) {
|
||||
cookie, e := r.Cookie("sea_oidc_pending")
|
||||
if e != nil {
|
||||
writeJSON(w, 200, map[string]any{"pending": false})
|
||||
return
|
||||
}
|
||||
p, e := s.store.PendingOIDCLogin(r.Context(), cookie.Value)
|
||||
cfg := s.readServiceConfig(r, "oidc_config")
|
||||
if e != nil || !validOIDCConfig(cfg) || p.Provider != oidcProviderKey(cfg) {
|
||||
setOIDCCookie(w, "sea_oidc_pending", "", -1)
|
||||
writeJSON(w, 200, map[string]any{"pending": false})
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, map[string]any{"pending": true, "allowCreate": cfg.AutoCreate && validSSOEmail(p), "email": p.Email, "displayName": p.DisplayName})
|
||||
}
|
||||
|
||||
func (s *Server) completeOIDCLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if _, e := r.Cookie(impersonationCookie); e == nil {
|
||||
writeError(w, 403, "请先返回管理视图")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Action string `json:"action"`
|
||||
Identifier string `json:"identifier"`
|
||||
Password string `json:"password"`
|
||||
TOTPCode string `json:"totpCode"`
|
||||
}
|
||||
if decodeJSON(r, &input) != nil {
|
||||
writeError(w, 400, "请求格式错误")
|
||||
return
|
||||
}
|
||||
cookie, e := r.Cookie("sea_oidc_pending")
|
||||
if e != nil {
|
||||
writeError(w, 401, "SSO 登录请求已失效,请重新登录")
|
||||
return
|
||||
}
|
||||
p, e := s.store.PendingOIDCLogin(r.Context(), cookie.Value)
|
||||
cfg := s.readServiceConfig(r, "oidc_config")
|
||||
if e != nil || !validOIDCConfig(cfg) || p.Provider != oidcProviderKey(cfg) {
|
||||
writeError(w, 401, "SSO 登录请求已失效,请重新登录")
|
||||
return
|
||||
}
|
||||
if allowed, retry := s.loginLimiter.allow("oidc-complete:"+requestClientIP(r), time.Now(), loginIPBurst); !allowed {
|
||||
writeLoginRateLimit(w, retry)
|
||||
return
|
||||
}
|
||||
var target uint64
|
||||
var created *store.NewTenant
|
||||
switch input.Action {
|
||||
case "bind":
|
||||
started := time.Now()
|
||||
u, err := s.store.UserByLogin(r.Context(), strings.ToLower(strings.TrimSpace(input.Identifier)))
|
||||
hash := ""
|
||||
if err == nil {
|
||||
hash = u.PasswordHash
|
||||
}
|
||||
ok := auth.VerifyPasswordConstantWork(hash, input.Password)
|
||||
if err != nil || !ok || u.Role == "host" || len(input.Password) > 128 {
|
||||
minimumLoginDelay(started)
|
||||
writeError(w, 401, "账号或密码不正确,主机账号不可绑定 SSO")
|
||||
return
|
||||
}
|
||||
if !oidcPasswordAllowed(cfg, u.Role) {
|
||||
writeError(w, 403, "该账号已关闭密码登录,请通过已绑定的 SSO 身份登录")
|
||||
return
|
||||
}
|
||||
if u.MFAEnabled {
|
||||
security, e := s.store.UserSecurity(r.Context(), u.ID)
|
||||
if e != nil {
|
||||
writeError(w, 500, "无法验证双重认证")
|
||||
return
|
||||
}
|
||||
secret, e := s.secretBox.Open(mfaSecretLabel(u.ID), security.TOTPSecretCiphertext)
|
||||
valid := e == nil && auth.VerifyTOTP(secret, input.TOTPCode, time.Now())
|
||||
if !valid && strings.TrimSpace(input.TOTPCode) != "" {
|
||||
valid, e = s.store.ConsumeRecoveryCode(r.Context(), u.ID, auth.RecoveryCodeHash(input.TOTPCode))
|
||||
}
|
||||
if e != nil || !valid {
|
||||
writeCodedError(w, 401, "mfa_required", "请输入有效的动态验证码或恢复码")
|
||||
return
|
||||
}
|
||||
}
|
||||
target = u.ID
|
||||
case "create":
|
||||
if !cfg.AutoCreate || !validSSOEmail(p) {
|
||||
writeError(w, 403, "当前不允许创建 SSO 用户,或第三方邮箱尚未验证")
|
||||
return
|
||||
}
|
||||
random, e := randomURLToken(32)
|
||||
if e != nil {
|
||||
writeError(w, 500, "无法创建账号")
|
||||
return
|
||||
}
|
||||
password, e := auth.HashPassword(random)
|
||||
if e != nil {
|
||||
writeError(w, 500, "无法创建账号")
|
||||
return
|
||||
}
|
||||
name := p.DisplayName
|
||||
if name == "" {
|
||||
name = "SSO 用户"
|
||||
}
|
||||
digest := sha256.Sum256([]byte(random))
|
||||
created = &store.NewTenant{Slug: "sso-" + hex.EncodeToString(digest[:10]), Name: name, DisplayName: name, Email: p.Email, Password: password, PlanID: 1}
|
||||
default:
|
||||
writeError(w, 422, "请选择绑定账号或创建账号")
|
||||
return
|
||||
}
|
||||
target, e = s.store.CompletePendingOIDC(r.Context(), cookie.Value, oidcProviderKey(cfg), target, created)
|
||||
if e != nil {
|
||||
writeError(w, 409, "无法完成绑定或建号:请求可能已使用,或账号邮箱/SSO 身份已被绑定;相同邮箱不会自动合并")
|
||||
return
|
||||
}
|
||||
setOIDCCookie(w, "sea_oidc_pending", "", -1)
|
||||
u, e := s.store.UserByID(r.Context(), target)
|
||||
if e != nil {
|
||||
writeError(w, 401, "账号不可用")
|
||||
return
|
||||
}
|
||||
if e = s.issueSession(w, r, u); e != nil {
|
||||
writeError(w, 500, "绑定已完成,请重新使用 SSO 登录")
|
||||
return
|
||||
}
|
||||
detail := "SSO 登录时绑定已有用户"
|
||||
if created != nil {
|
||||
detail = "SSO 首次登录创建普通用户,未开通虚拟主机"
|
||||
}
|
||||
s.store.Log(r.Context(), u.TenantID, u.ID, "login", "oidc_session", "", requestClientIP(r), true, detail)
|
||||
writeJSON(w, 200, map[string]any{"completed": true})
|
||||
}
|
||||
Reference in new issue
Block a user