更新密码找回uniapp
This commit is contained in:
1 parent
7f9a66da0f
commit
3fdda89833
1304 files changed
+3503
-26132
No files matched your search
@@ -1,7 +1,3 @@
|
||||
// Package auth 统一认证中心(UAC)控制器:api.yunzer.cn/auth
|
||||
//
|
||||
// 实现 OIDC 1.0(基于 OAuth 2.1 + PKCE)标准端点,
|
||||
// 以后每开发一个新软件,只需在 yz_auth_client 注册一条即可接入。
|
||||
package auth
|
||||
|
||||
import (
|
||||
@@ -22,20 +18,17 @@ import (
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// 认证中心会话 Cookie(仅作用于认证中心域名,用于 authorize 阶段识别登录态)
|
||||
const (
|
||||
sessionCookieName = "yz_sid"
|
||||
sessionCookieTTL = 7200
|
||||
)
|
||||
|
||||
// grant_type 常量
|
||||
const (
|
||||
GrantAuthCode = "authorization_code"
|
||||
GrantRefresh = "refresh_token"
|
||||
ResponseTypeCode = "code"
|
||||
)
|
||||
|
||||
// AuthOidcController OIDC 标准端点
|
||||
type AuthOidcController struct {
|
||||
beego.Controller
|
||||
}
|
||||
@@ -50,8 +43,6 @@ func (c *AuthOidcController) fail(status int, msg string) {
|
||||
c.serveJSON(map[string]interface{}{"error": msg})
|
||||
}
|
||||
|
||||
// Discovery OIDC 发现文档
|
||||
// GET /auth/.well-known/openid-configuration
|
||||
func (c *AuthOidcController) Discovery() {
|
||||
issuer := jwtutil.Issuer()
|
||||
if issuer == "" {
|
||||
@@ -76,8 +67,6 @@ func (c *AuthOidcController) Discovery() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// JWKS 公钥集合(各应用本地验签用)
|
||||
// GET /auth/jwks.json
|
||||
func (c *AuthOidcController) JWKS() {
|
||||
keys := jwtutil.JWKS()
|
||||
if keys == nil {
|
||||
@@ -87,10 +76,6 @@ func (c *AuthOidcController) JWKS() {
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
// Authorize 授权端点
|
||||
// GET /auth/authorize?client_id=&redirect_uri=&response_type=code&scope=&state=&code_challenge=&code_challenge_method=S256
|
||||
//
|
||||
// 未登录时重定向到统一登录页,登录后再回到本端点完成授权。
|
||||
func (c *AuthOidcController) Authorize() {
|
||||
clientID := strings.TrimSpace(c.GetString("client_id"))
|
||||
redirectURI := strings.TrimSpace(c.GetString("redirect_uri"))
|
||||
@@ -130,7 +115,6 @@ func (c *AuthOidcController) Authorize() {
|
||||
return
|
||||
}
|
||||
|
||||
// 登录态:Cookie 优先(浏览器跳转),其次 Authorization(服务端调用)
|
||||
sid := strings.TrimSpace(c.Ctx.GetCookie(sessionCookieName))
|
||||
if sid == "" {
|
||||
if claims := claimsFromHeader(c); claims != nil {
|
||||
@@ -138,7 +122,6 @@ func (c *AuthOidcController) Authorize() {
|
||||
}
|
||||
}
|
||||
if sid == "" {
|
||||
// 未登录 → 去登录页,登录后带着参数回来
|
||||
back := fmt.Sprintf("%s?%s", authorizePath(c), c.Ctx.Request.URL.RawQuery)
|
||||
target := fmt.Sprintf("/auth/login?redirect=%s&client_id=%s",
|
||||
base64.RawURLEncoding.EncodeToString([]byte(back)), clientID)
|
||||
@@ -156,7 +139,6 @@ func (c *AuthOidcController) Authorize() {
|
||||
return
|
||||
}
|
||||
|
||||
// 已登录但未选择企业:跳登录页的企业选择步骤
|
||||
if session.Tid == authsvc.PendingTenantID {
|
||||
back := fmt.Sprintf("%s?%s", authorizePath(c), c.Ctx.Request.URL.RawQuery)
|
||||
target := fmt.Sprintf("/auth/login?step=tenant&redirect=%s&client_id=%s",
|
||||
@@ -179,15 +161,10 @@ func (c *AuthOidcController) Authorize() {
|
||||
c.Redirect(fmt.Sprintf("%s%scode=%s&state=%s", redirectURI, sep, code, state), 302)
|
||||
}
|
||||
|
||||
// Token 令牌端点
|
||||
// POST /auth/token
|
||||
// - grant_type=authorization_code:code + code_verifier(PKCE)+ client_id
|
||||
// - grant_type=refresh_token:refresh_token + client_id
|
||||
func (c *AuthOidcController) Token() {
|
||||
grantType := strings.TrimSpace(c.GetString("grant_type"))
|
||||
clientID := strings.TrimSpace(c.GetString("client_id"))
|
||||
if clientID == "" {
|
||||
// 兼容表单/JSON 以外的取参方式
|
||||
clientID = strings.TrimSpace(c.Ctx.Request.FormValue("client_id"))
|
||||
}
|
||||
if grantType == "" {
|
||||
@@ -239,8 +216,6 @@ func (c *AuthOidcController) Token() {
|
||||
}
|
||||
}
|
||||
|
||||
// UserInfo 用户信息端点,需 Bearer Token
|
||||
// GET /auth/userinfo
|
||||
func (c *AuthOidcController) UserInfo() {
|
||||
claims := claimsFromHeader(c)
|
||||
if claims == nil {
|
||||
@@ -264,7 +239,6 @@ func (c *AuthOidcController) UserInfo() {
|
||||
return
|
||||
}
|
||||
|
||||
// 业务接口与前端统一使用 identity_id 作为用户标识
|
||||
account, name, groupID := "", "", uint64(0)
|
||||
if bind, err := authsvc.GetTenantUser(identity.ID, uint64(claims.TenantId)); err == nil {
|
||||
groupID = bind.GroupID
|
||||
@@ -282,8 +256,6 @@ func (c *AuthOidcController) UserInfo() {
|
||||
name = profile.Nickname
|
||||
}
|
||||
|
||||
// 当前会话所在企业名称(一人多企业时必须是"已选中的那一家",
|
||||
// 而非全部可进入企业;前端直接展示该字段,避免自行拼接 tenants)
|
||||
tenantName := ""
|
||||
for _, t := range profile.Tenants {
|
||||
if t.Tid == uint64(claims.TenantId) {
|
||||
@@ -298,8 +270,6 @@ func (c *AuthOidcController) UserInfo() {
|
||||
"union_id": identity.UnionID,
|
||||
"tid": claims.TenantId,
|
||||
"tenant_name": tenantName,
|
||||
// 诊断用:业务接口按 user_type 判定权限(backend / app),
|
||||
// 出现「无权访问」时可先看这里的值是否正确
|
||||
"user_type": claims.UserType,
|
||||
"group_id": groupID,
|
||||
"account": account,
|
||||
@@ -313,8 +283,6 @@ func (c *AuthOidcController) UserInfo() {
|
||||
})
|
||||
}
|
||||
|
||||
// Introspect 令牌校验,供资源服务(各业务后端)调用
|
||||
// POST /auth/token 之外的独立端点;比本地 JWKS 验签更实时(可查黑名单)
|
||||
func (c *AuthOidcController) Introspect() {
|
||||
token := strings.TrimSpace(c.GetString("token"))
|
||||
if token == "" {
|
||||
@@ -346,8 +314,6 @@ func (c *AuthOidcController) Introspect() {
|
||||
})
|
||||
}
|
||||
|
||||
// Revoke 吊销令牌(登出/踢下线)
|
||||
// POST /auth/revoke
|
||||
func (c *AuthOidcController) Revoke() {
|
||||
token := strings.TrimSpace(c.GetString("token"))
|
||||
refresh := strings.TrimSpace(c.GetString("refresh_token"))
|
||||
@@ -362,21 +328,14 @@ func (c *AuthOidcController) Revoke() {
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "已吊销"})
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- 内部工具
|
||||
|
||||
func authorizePath(c *AuthOidcController) string {
|
||||
return "/auth/authorize"
|
||||
}
|
||||
|
||||
// clearSessionCookie 清除认证中心会话 Cookie。
|
||||
// 与写入复用同一实现:http 环境写入的 Cookie 不带 Secure,若清除时硬编码
|
||||
// Secure,浏览器会拒绝删除,失效的 yz_sid 一直残留,authorize 每次都判定
|
||||
// 未登录并把用户送回登录页。
|
||||
func clearSessionCookie(c *AuthOidcController) {
|
||||
clearSessionCookieForCtx(c.Ctx)
|
||||
}
|
||||
|
||||
// setSessionCookie 写入认证中心会话 Cookie
|
||||
func setSessionCookie(c *AuthOidcController, sid string) {
|
||||
setSessionCookieForCtx(c.Ctx, sid)
|
||||
}
|
||||
@@ -405,7 +364,6 @@ func claimsFromHeader(c *AuthOidcController) *jwtutil.Claims {
|
||||
return claims
|
||||
}
|
||||
|
||||
// findClient 查询启用状态的应用
|
||||
func findClient(clientID string) (*models.AuthClient, error) {
|
||||
var client models.AuthClient
|
||||
err := models.Orm.QueryTable(new(models.AuthClient)).
|
||||
@@ -418,7 +376,6 @@ func findClient(clientID string) (*models.AuthClient, error) {
|
||||
return &client, nil
|
||||
}
|
||||
|
||||
// allowRedirect 校验回跳地址是否在白名单内(精确匹配,防钓鱼)
|
||||
func allowRedirect(client *models.AuthClient, uri string) bool {
|
||||
if client.RedirectURIs == nil || *client.RedirectURIs == "" {
|
||||
return false
|
||||
@@ -435,11 +392,6 @@ func allowRedirect(client *models.AuthClient, uri string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// allowLogoutRedirect 登出回跳地址校验。
|
||||
//
|
||||
// 先精确匹配白名单,再按 origin(协议+主机+端口)放宽匹配:
|
||||
// 实际使用中「末尾斜杠」「带 #/login 片段」等差异很常见,
|
||||
// 只做精确匹配会导致明明同域却跳不回去,因此同域即放行。
|
||||
func allowLogoutRedirect(client *models.AuthClient, uri string) bool {
|
||||
raw := ""
|
||||
if client.PostLogoutURIs != nil {
|
||||
@@ -475,7 +427,6 @@ func allowLogoutRedirect(client *models.AuthClient, uri string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// issueAuthCode 生成一次性授权码(明文返回,库中只存哈希)
|
||||
func issueAuthCode(clientID string, identityID, tid uint64, redirectURI, challenge, method, scope, nonce string) (string, error) {
|
||||
plain, err := randomString(32)
|
||||
if err != nil {
|
||||
@@ -504,7 +455,6 @@ func issueAuthCode(clientID string, identityID, tid uint64, redirectURI, challen
|
||||
return plain, nil
|
||||
}
|
||||
|
||||
// exchangeCode 用授权码换令牌(校验 PKCE、一次性、有效期)
|
||||
func exchangeCode(code, verifier, clientID string) (*authsvc.TokenPair, *jwtutil.Claims, error) {
|
||||
sum := sha256.Sum256([]byte(code))
|
||||
var stored models.AuthCode
|
||||
@@ -521,14 +471,10 @@ func exchangeCode(code, verifier, clientID string) (*authsvc.TokenPair, *jwtutil
|
||||
if !verifyPKCE(verifier, stored.CodeChallenge, stored.CodeChallengeMethod) {
|
||||
return nil, nil, fmt.Errorf("invalid_grant")
|
||||
}
|
||||
// 一次性:立即标记已用
|
||||
_, _ = models.Orm.QueryTable(new(models.AuthCode)).
|
||||
Filter("code_hash", stored.CodeHash).
|
||||
Update(map[string]interface{}{"used": 1})
|
||||
|
||||
// 复用登录阶段建立的会话,切勿在此新建:新建会话会触发「1 号 1 机」
|
||||
// 互踢,把浏览器 yz_sid Cookie 对应的会话吊销,导致下一次回认证中心
|
||||
// 又看到登录页(登录成功后反复跳回登录界面)。
|
||||
session, err := authsvc.ReuseSession(authsvc.SessionInfo{
|
||||
IdentityID: stored.IdentityID,
|
||||
Tid: stored.Tid,
|
||||
@@ -553,9 +499,6 @@ func exchangeCode(code, verifier, clientID string) (*authsvc.TokenPair, *jwtutil
|
||||
Tid: stored.Tid,
|
||||
ClientID: clientID,
|
||||
Sid: session.Sid,
|
||||
// 不指定 UserType:由 IssueTokens 按应用编码给出 backend / app。
|
||||
// 这里曾硬编码 "tenant",而业务接口只认 backend/app,导致换码签出的首张令牌
|
||||
// 被 /backend/modules/getTenantList 等判为「无权访问」(续期路径本来就不传,故会自愈)。
|
||||
Amr: authsvc.AmrPwd,
|
||||
AccessTTL: accessTTL,
|
||||
RefreshTTL: refreshTTL,
|
||||
@@ -571,7 +514,6 @@ func exchangeCode(code, verifier, clientID string) (*authsvc.TokenPair, *jwtutil
|
||||
return pair, claims, nil
|
||||
}
|
||||
|
||||
// verifyPKCE 校验 PKCE(S256 或 plain)
|
||||
func verifyPKCE(verifier, challenge, method string) bool {
|
||||
if challenge == "" {
|
||||
return false
|
||||
@@ -583,7 +525,6 @@ func verifyPKCE(verifier, challenge, method string) bool {
|
||||
return verifier == challenge
|
||||
}
|
||||
|
||||
// buildIDToken 生成 OIDC ID Token
|
||||
func buildIDToken(claims *jwtutil.Claims, clientID string) (string, error) {
|
||||
return jwtutil.SignToken(jwtutil.TokenOptions{
|
||||
Alg: jwtutil.AlgRS256,
|
||||
@@ -599,7 +540,6 @@ func buildIDToken(claims *jwtutil.Claims, clientID string) (string, error) {
|
||||
})
|
||||
}
|
||||
|
||||
// randomString 生成随机串
|
||||
func randomString(n int) (string, error) {
|
||||
buf := make([]byte, n)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
|
||||
Reference in new issue
Block a user