更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -17,7 +17,6 @@ import (
|
||||
"github.com/beego/beego/v2/server/web/context"
|
||||
)
|
||||
|
||||
// AuthLoginController 认证中心登录相关端点
|
||||
type AuthLoginController struct {
|
||||
beego.Controller
|
||||
}
|
||||
@@ -27,8 +26,6 @@ func (c *AuthLoginController) serveJSON(data map[string]interface{}) {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// LoginPage 统一登录页(beego 模板渲染)
|
||||
// GET /auth/login?redirect=<base64(回跳URL)>&client_id=xxx&step=tenant
|
||||
func (c *AuthLoginController) LoginPage() {
|
||||
clientID := strings.TrimSpace(c.GetString("client_id"))
|
||||
redirect := c.GetString("redirect")
|
||||
@@ -42,8 +39,6 @@ func (c *AuthLoginController) LoginPage() {
|
||||
if sid != "" && step != "tenant" {
|
||||
if session, err := authsvc.GetSession(sid); err == nil {
|
||||
if session.Tid == authsvc.PendingTenantID {
|
||||
// 已登录但还没选企业:直接带到企业选择步骤,
|
||||
// 不要再渲染一次登录表单让用户重新登录。
|
||||
target := "/auth/login?step=tenant&client_id=" + url.QueryEscape(clientID) +
|
||||
"&redirect=" + url.QueryEscape(redirect)
|
||||
c.Redirect(target, 302)
|
||||
@@ -54,19 +49,16 @@ func (c *AuthLoginController) LoginPage() {
|
||||
return
|
||||
}
|
||||
} else {
|
||||
// 会话已被吊销/过期:清掉失效 Cookie,避免后续请求继续带上它
|
||||
clearSessionCookieForCtx(c.Ctx)
|
||||
sid = ""
|
||||
}
|
||||
}
|
||||
|
||||
// 企业选择步骤:需要展示可进入的企业列表
|
||||
tenants := make([]authsvc.TenantOption, 0)
|
||||
if sid != "" {
|
||||
if session, err := authsvc.GetSession(sid); err == nil {
|
||||
list, _ := authsvc.ListTenantOptions(session.IdentityID)
|
||||
tenants = list
|
||||
// 只有一家企业直接进入,无需展示选择页
|
||||
if len(tenants) == 1 && step == "tenant" {
|
||||
if _, _, err := authsvc.ChooseTenant(sid, tenants[0].Tid, clientID); err == nil {
|
||||
if back := safeBackURL(clientID, redirect); back != "" {
|
||||
@@ -76,7 +68,6 @@ func (c *AuthLoginController) LoginPage() {
|
||||
}
|
||||
}
|
||||
} else if step == "tenant" {
|
||||
// 选企业时会话已失效:不要渲染一个空的企业列表,退回登录页重新登录
|
||||
clearSessionCookieForCtx(c.Ctx)
|
||||
c.Redirect("/auth/login?client_id="+url.QueryEscape(clientID), 302)
|
||||
return
|
||||
@@ -88,12 +79,9 @@ func (c *AuthLoginController) LoginPage() {
|
||||
c.Data["Step"] = step
|
||||
c.Data["Error"] = errMsg
|
||||
c.Data["Tenants"] = tenants
|
||||
// 自助账户流程提示:registered=注册成功 / registered_warn=注册成功但加入企业失败 / reset=密码已重置
|
||||
c.Data["Notice"] = c.GetString("notice")
|
||||
c.Data["WarnMsg"] = c.GetString("warn")
|
||||
|
||||
// 登录页品牌:platform「平台设置 → 登录页设置」里的配置,未配置时用默认文案。
|
||||
// 读取失败不阻断登录页,GetPlatformSiteTagMap 会返回空值。
|
||||
brand := models.GetPlatformSiteTagMap()
|
||||
loginTitle := strings.TrimSpace(brand["login_title"])
|
||||
if loginTitle == "" {
|
||||
@@ -108,8 +96,6 @@ func (c *AuthLoginController) LoginPage() {
|
||||
c.Data["LoginLogo"] = strings.TrimSpace(brand["login_logo"])
|
||||
c.Data["LoginBg"] = strings.TrimSpace(brand["login_bg"])
|
||||
|
||||
// 登录方式开关(平台设置 → 登录方式):值 "0" 关闭,未配置默认开启。
|
||||
// 三种方式全部关闭时禁止登录;只开一种时不显示 tab 直接展示对应面板。
|
||||
loginEnabled := func(tag string) bool {
|
||||
return strings.TrimSpace(brand[tag]) != "0"
|
||||
}
|
||||
@@ -133,14 +119,11 @@ func (c *AuthLoginController) LoginPage() {
|
||||
methodCount++
|
||||
}
|
||||
}
|
||||
// 只开启一种登录方式时不需要 tab 切换栏
|
||||
c.Data["LoginShowTabs"] = methodCount > 1
|
||||
|
||||
c.TplName = "auth/login.tpl"
|
||||
}
|
||||
|
||||
// LoginSubmit 提交登录
|
||||
// POST /auth/login { account, password, client_id, redirect }
|
||||
func (c *AuthLoginController) LoginSubmit() {
|
||||
var req struct {
|
||||
Account string `json:"account"`
|
||||
@@ -148,7 +131,6 @@ func (c *AuthLoginController) LoginSubmit() {
|
||||
ClientID string `json:"client_id"`
|
||||
Redirect string `json:"redirect"`
|
||||
DeviceID string `json:"device_id"`
|
||||
// 极验 4.0 验证结果:开启人机验证后由前端 SDK 产出,验证通过才允许继续登录
|
||||
CaptchaID string `json:"captcha_id"`
|
||||
LotNumber string `json:"lot_number"`
|
||||
PassToken string `json:"pass_token"`
|
||||
@@ -174,8 +156,6 @@ func (c *AuthLoginController) LoginSubmit() {
|
||||
return
|
||||
}
|
||||
|
||||
// 平台设置的登录方式开关兜底:账号密码被关或全部登录方式被关时直接拒绝
|
||||
// (前端已隐藏入口,这里防止绕过页面直接调接口)
|
||||
loginTags := models.GetPlatformSiteTagMap()
|
||||
pwdOff := strings.TrimSpace(loginTags["login_pwd_enabled"]) == "0"
|
||||
allOff := pwdOff &&
|
||||
@@ -190,8 +170,6 @@ func (c *AuthLoginController) LoginSubmit() {
|
||||
return
|
||||
}
|
||||
|
||||
// 开启人机验证时:先极验通过,再走账号密码校验
|
||||
// 小程序端(yz-miniapp)无法集成极验 SDK 且运行于微信沙箱内,跳过人机验证
|
||||
if strings.TrimSpace(req.ClientID) != "yz-miniapp" {
|
||||
if err := authsvc.VerifyLoginCaptcha(0, authsvc.CaptchaResult{
|
||||
CaptchaID: req.CaptchaID,
|
||||
@@ -214,12 +192,9 @@ func (c *AuthLoginController) LoginSubmit() {
|
||||
return
|
||||
}
|
||||
|
||||
// 写入认证中心会话 Cookie,供后续 authorize 识别登录态
|
||||
setSessionCookieForCtx(c.Ctx, result.Tokens.Sid)
|
||||
|
||||
if result.NeedChooseTenant {
|
||||
// 待选令牌必须一并下发:APP / 小程序等无 Cookie 环境的客户端
|
||||
// 只能靠 Bearer 令牌调 /auth/switch-tenant,浏览器端则继续用 yz_sid Cookie。
|
||||
c.serveJSON(map[string]interface{}{
|
||||
"code": 200,
|
||||
"msg": "请选择要进入的企业",
|
||||
@@ -241,8 +216,6 @@ func (c *AuthLoginController) LoginSubmit() {
|
||||
})
|
||||
}
|
||||
|
||||
// Tenants 当前登录身份可进入的企业列表(Bearer Token)
|
||||
// GET /auth/tenants
|
||||
func (c *AuthLoginController) Tenants() {
|
||||
claims := claimsFromHeaderLogin(c)
|
||||
if claims == nil {
|
||||
@@ -258,11 +231,6 @@ func (c *AuthLoginController) Tenants() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "data": list})
|
||||
}
|
||||
|
||||
// SwitchTenant 免密切换企业
|
||||
// POST /auth/switch-tenant { tid, client_id, redirect, sid }
|
||||
//
|
||||
// sid 来源优先级:yz_sid Cookie → Authorization Bearer 令牌中的 sid → 请求体 sid。
|
||||
// 请求体 sid 供无 Cookie 能力(且尚未拿到 Bearer 令牌)的客户端兜底。
|
||||
func (c *AuthLoginController) SwitchTenant() {
|
||||
var req struct {
|
||||
Tid uint64 `json:"tid"`
|
||||
@@ -304,18 +272,12 @@ func (c *AuthLoginController) SwitchTenant() {
|
||||
})
|
||||
}
|
||||
|
||||
// LogoutPage 单点登出入口(OIDC end_session_endpoint)
|
||||
// GET /auth/logout?post_logout_redirect_uri=&client_id=
|
||||
//
|
||||
// 清除认证中心 Cookie 并吊销会话与令牌;回跳地址必须在应用白名单内。
|
||||
// 登记了 backchannel_logout_uri 的应用会收到服务端登出通知(P2 阶段接入)。
|
||||
func (c *AuthLoginController) LogoutPage() {
|
||||
sid := strings.TrimSpace(c.Ctx.GetCookie(sessionCookieName))
|
||||
access := bearerTokenLogin(c)
|
||||
if sid != "" {
|
||||
_ = authsvc.RevokeSession(sid, models.RevokeReasonLogout)
|
||||
}
|
||||
// 走统一登出入口:吊销令牌并通知其他应用(单点登出)
|
||||
if access != "" {
|
||||
_ = authsvc.Logout(access, "")
|
||||
}
|
||||
@@ -328,7 +290,6 @@ func (c *AuthLoginController) LogoutPage() {
|
||||
c.Redirect(back, 302)
|
||||
return
|
||||
}
|
||||
// 回跳地址未登记时不要直接抛 JSON 给用户,退回登录页
|
||||
target := "/auth/login?error=" + url.QueryEscape("登出回跳地址未登记,已返回登录页")
|
||||
if cid != "" {
|
||||
target += "&client_id=" + url.QueryEscape(cid)
|
||||
@@ -336,7 +297,6 @@ func (c *AuthLoginController) LogoutPage() {
|
||||
c.Redirect(target, 302)
|
||||
return
|
||||
}
|
||||
// 浏览器直接访问(无回跳参数):跳登录页,避免页面显示裸 JSON
|
||||
target := "/auth/login"
|
||||
if cid != "" {
|
||||
target += "?client_id=" + url.QueryEscape(cid)
|
||||
@@ -344,8 +304,6 @@ func (c *AuthLoginController) LogoutPage() {
|
||||
c.Redirect(target, 302)
|
||||
}
|
||||
|
||||
// Sessions 在线设备列表
|
||||
// GET /auth/sessions
|
||||
func (c *AuthLoginController) Sessions() {
|
||||
claims := claimsFromHeaderLogin(c)
|
||||
if claims == nil {
|
||||
@@ -361,8 +319,6 @@ func (c *AuthLoginController) Sessions() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "data": list})
|
||||
}
|
||||
|
||||
// KickSession 踢下线指定设备
|
||||
// POST /auth/sessions/kick { sid }
|
||||
func (c *AuthLoginController) KickSession() {
|
||||
var req struct {
|
||||
Sid string `json:"sid"`
|
||||
@@ -383,7 +339,6 @@ func (c *AuthLoginController) KickSession() {
|
||||
c.serveJSON(map[string]interface{}{"code": 400, "msg": "参数错误"})
|
||||
return
|
||||
}
|
||||
// 只允许踢自己名下的会话
|
||||
owned := false
|
||||
if list, err := authsvc.ListActiveSessions(uint64(claims.UserID)); err == nil {
|
||||
for _, s := range list {
|
||||
@@ -404,8 +359,6 @@ func (c *AuthLoginController) KickSession() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "已下线"})
|
||||
}
|
||||
|
||||
// LogoutAction 退出登录(清 Cookie + 吊销会话与令牌)
|
||||
// POST /auth/logout
|
||||
func (c *AuthLoginController) LogoutAction() {
|
||||
sid := strings.TrimSpace(c.Ctx.GetCookie(sessionCookieName))
|
||||
access := ""
|
||||
@@ -423,20 +376,7 @@ func (c *AuthLoginController) LogoutAction() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "已登出"})
|
||||
}
|
||||
|
||||
// ClearSiteData 清除当前站点在浏览器中的全部缓存数据
|
||||
// POST /auth/clear-site-data
|
||||
//
|
||||
// 登录页「清除缓存」按钮调用。前端 JS 已清理同源的 Web Storage / Cache
|
||||
// Storage / IndexedDB / Service Worker,但 yz_sid 是 HttpOnly Cookie,前端
|
||||
// 读不到也删不掉,因此这里:
|
||||
// - 用 Set-Cookie 直接删除会话 Cookie(兼容所有浏览器);
|
||||
// - 返回 Clear-Site-Data 响应头,让浏览器在安全上下文(HTTPS / localhost)
|
||||
// 下把本 origin 的 cookies / storage / HTTP cache 一并清空。
|
||||
//
|
||||
// Clear-Site-Data 严格限定在响应所属 origin,不会影响其它站点。
|
||||
func (c *AuthLoginController) ClearSiteData() {
|
||||
// Cookie 被清后服务端会话将无人使用,同步吊销,避免留下仍有效的孤儿会话
|
||||
// 占用「1 号 1 机」名额。
|
||||
if sid := strings.TrimSpace(c.Ctx.GetCookie(sessionCookieName)); sid != "" {
|
||||
_ = authsvc.RevokeSession(sid, models.RevokeReasonLogout)
|
||||
}
|
||||
@@ -446,11 +386,6 @@ func (c *AuthLoginController) ClearSiteData() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "已清除"})
|
||||
}
|
||||
|
||||
// VerifyConfig 登录验证配置(替代 /backend/login/getOpenVerify)
|
||||
// GET /auth/verify-config?tid=
|
||||
//
|
||||
// 登录页在选企业之前调用,tid 可为空:此时以平台全局配置为准。
|
||||
// 返回的人机验证信息供登录页决定是否在点击登录后弹出极验。
|
||||
func (c *AuthLoginController) VerifyConfig() {
|
||||
tid, _ := c.GetInt64("tid", 0)
|
||||
sessionCfg := authsvc.GetTenantSessionPolicy(uint64(tid))
|
||||
@@ -475,14 +410,6 @@ func (c *AuthLoginController) VerifyConfig() {
|
||||
})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 工具
|
||||
|
||||
// safeBackURL 计算登录成功后的回跳地址。
|
||||
//
|
||||
// 优先使用业务端传来的 authorize 地址(base64url 编码);若链路中 redirect
|
||||
// 丢失(直接打开登录页、单点登出后再次登录、选企业时参数未透传等),则退回
|
||||
// 该应用已登记回跳地址所在的站点首页。业务站没有令牌时会自动重新发起 OIDC,
|
||||
// 认证中心此时已有会话可静默签码完成登录,不会卡在登录页形成循环。
|
||||
func safeBackURL(clientID, redirect string) string {
|
||||
if back := decodeRedirect(redirect); back != "" {
|
||||
return back
|
||||
@@ -514,7 +441,6 @@ func decodeRedirect(encoded string) string {
|
||||
return ""
|
||||
}
|
||||
back := string(raw)
|
||||
// 只允许站内地址,防开放重定向
|
||||
if !strings.HasPrefix(back, "/auth/") && !strings.HasPrefix(back, "http") {
|
||||
return ""
|
||||
}
|
||||
@@ -546,8 +472,6 @@ func bearerTokenLogin(c *AuthLoginController) string {
|
||||
}
|
||||
|
||||
func setSessionCookieForCtx(ctx *context.Context, sid string) {
|
||||
// Secure 仅在 https 下添加:本地 http 联调时浏览器会丢弃 Secure Cookie,
|
||||
// 导致 authorize 识别不到会话又被送回登录页
|
||||
flag := cookieSecureFlag(ctx)
|
||||
ctx.Output.Header("Set-Cookie",
|
||||
fmt.Sprintf("%s=%s; Path=/; Max-Age=%d; HttpOnly%s; SameSite=Lax",
|
||||
@@ -560,7 +484,6 @@ func clearSessionCookieForCtx(ctx *context.Context) {
|
||||
fmt.Sprintf("%s=; Path=/; Max-Age=0; HttpOnly%s; SameSite=Lax", sessionCookieName, flag))
|
||||
}
|
||||
|
||||
// cookieSecureFlag https 请求(或网关带 X-Forwarded-Proto: https)时返回 "; Secure"
|
||||
func cookieSecureFlag(ctx *context.Context) string {
|
||||
if ctx.Request.TLS != nil || strings.EqualFold(ctx.Request.Header.Get("X-Forwarded-Proto"), "https") {
|
||||
return "; Secure"
|
||||
|
||||
Reference in new issue
Block a user