package controllers import ( "encoding/json" "io" "strconv" "strings" "server/models" "server/pkg/identity" authsvc "server/services/auth" beego "github.com/beego/beego/v2/server/web" ) type PlatformAuthConfigController struct { beego.Controller } func (c *PlatformAuthConfigController) serveJSON(data map[string]interface{}) { c.Data["json"] = data _ = c.ServeJSON() } func (c *PlatformAuthConfigController) Prepare() { if _, err := identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndPlatform); err != nil { code := identity.Status(err) c.Ctx.Output.SetStatus(code) c.serveJSON(map[string]interface{}{"code": code, "msg": err.Error()}) c.StopRun() return } } func (c *PlatformAuthConfigController) Detail() { tid, _ := c.GetInt64("tid", 0) if tid == 0 { c.serveJSON(map[string]interface{}{"code": 400, "msg": "tid 不能为空"}) return } var cfg models.AuthTenantAuthConfig err := models.Orm.QueryTable(new(models.AuthTenantAuthConfig)). Filter("tid", tid).One(&cfg) if err != nil { policy := authsvc.GetTenantSessionPolicy(uint64(tid)) c.serveJSON(map[string]interface{}{ "code": 200, "msg": "success", "data": map[string]interface{}{ "tid": tid, "verify_type": "captcha", "open_verify": 1, "pwd_min_len": 8, "pwd_complexity": 0, "session_ttl": policy.SessionTTL, "max_session": policy.MaxSession, "kick_strategy": policy.KickStrategy, "mfa_required": 0, "ip_whitelist": "", "allow_third": "", "configured": false, }, }) return } ipList := "" if cfg.IPWhitelist != nil { ipList = *cfg.IPWhitelist } allowThird := "" if cfg.AllowThird != nil { allowThird = *cfg.AllowThird } c.serveJSON(map[string]interface{}{ "code": 200, "msg": "success", "data": map[string]interface{}{ "tid": cfg.Tid, "verify_type": cfg.VerifyType, "open_verify": cfg.OpenVerify, "pwd_min_len": cfg.PwdMinLen, "pwd_complexity": cfg.PwdComplexity, "session_ttl": cfg.SessionTTL, "max_session": cfg.MaxSession, "kick_strategy": cfg.KickStrategy, "mfa_required": cfg.MfaRequired, "ip_whitelist": ipList, "allow_third": allowThird, "configured": true, }, }) } func (c *PlatformAuthConfigController) Save() { var p struct { Tid uint64 `json:"tid"` VerifyType string `json:"verify_type"` OpenVerify *int8 `json:"open_verify"` PwdMinLen int `json:"pwd_min_len"` PwdComplexity int8 `json:"pwd_complexity"` SessionTTL int `json:"session_ttl"` MaxSession int `json:"max_session"` KickStrategy int8 `json:"kick_strategy"` MfaRequired int8 `json:"mfa_required"` IPWhitelist string `json:"ip_whitelist"` AllowThird string `json:"allow_third"` } raw, _ := io.ReadAll(c.Ctx.Request.Body) if len(raw) > 0 { if err := json.Unmarshal(raw, &p); err != nil { c.serveJSON(map[string]interface{}{"code": 400, "msg": "参数错误"}) return } } if p.Tid == 0 { c.serveJSON(map[string]interface{}{"code": 400, "msg": "tid 不能为空"}) return } ip := strings.TrimSpace(p.IPWhitelist) if ip != "" { var arr []string if err := json.Unmarshal([]byte(ip), &arr); err != nil { c.serveJSON(map[string]interface{}{"code": 400, "msg": "IP白名单必须是 JSON 数组,如 [\"1.2.3.4\"]"}) return } } cfg := models.AuthTenantAuthConfig{ Tid: p.Tid, VerifyType: defaultStrAuth2(p.VerifyType, "captcha"), OpenVerify: 1, PwdMinLen: p.PwdMinLen, PwdComplexity: p.PwdComplexity, SessionTTL: p.SessionTTL, MaxSession: p.MaxSession, KickStrategy: p.KickStrategy, MfaRequired: p.MfaRequired, } if p.OpenVerify != nil { cfg.OpenVerify = *p.OpenVerify } if cfg.PwdMinLen <= 0 { cfg.PwdMinLen = 8 } if cfg.SessionTTL <= 0 { cfg.SessionTTL = authsvc.DefaultSessionTTL } if cfg.MaxSession <= 0 { cfg.MaxSession = authsvc.DefaultMaxSession } if cfg.KickStrategy != models.KickStrategyReject { cfg.KickStrategy = models.KickStrategyKickOld } if ip != "" { cfg.IPWhitelist = &ip } if v := strings.TrimSpace(p.AllowThird); v != "" { cfg.AllowThird = &v } exist := models.Orm.QueryTable(new(models.AuthTenantAuthConfig)).Filter("tid", p.Tid).Exist() if exist { fields := []string{"verify_type", "open_verify", "pwd_min_len", "pwd_complexity", "session_ttl", "max_session", "kick_strategy", "mfa_required"} if cfg.IPWhitelist != nil { fields = append(fields, "ip_whitelist") } if cfg.AllowThird != nil { fields = append(fields, "allow_third") } if _, err := models.Orm.Update(&cfg, fields...); err != nil { c.serveJSON(map[string]interface{}{"code": 500, "msg": "保存失败: " + err.Error()}) return } } else { if _, err := models.Orm.Insert(&cfg); err != nil { c.serveJSON(map[string]interface{}{"code": 500, "msg": "保存失败: " + err.Error()}) return } } c.serveJSON(map[string]interface{}{"code": 200, "msg": "success"}) } func (c *PlatformAuthConfigController) Reset() { tid, err := strconv.ParseUint(c.Ctx.Input.Param(":tid"), 10, 64) if err != nil || tid == 0 { c.serveJSON(map[string]interface{}{"code": 400, "msg": "无效 tid"}) return } if _, err := models.Orm.QueryTable(new(models.AuthTenantAuthConfig)). Filter("tid", tid).Delete(); err != nil { c.serveJSON(map[string]interface{}{"code": 500, "msg": "重置失败: " + err.Error()}) return } c.serveJSON(map[string]interface{}{"code": 200, "msg": "已恢复默认"}) } func defaultStrAuth2(v, def string) string { if strings.TrimSpace(v) == "" { return def } return strings.TrimSpace(v) }