跳转到内容

授权码 + PKCE 登录流程

建议使用成熟的 OIDC 客户端库(如 Node.js 的 openid-client 或 Python 的 authlib),避免手写 JWT 验签与协议校验。

1. 发现配置并跳转到 /oidc/authorize

Section titled “1. 发现配置并跳转到 /oidc/authorize”

请求 https://jointtex.com/oidc/.well-known/openid-configuration,为每次浏览器登录生成全新的高熵 state、nonce 与 PKCE code_verifier(43–128 位非保留字符),并保存在你服务端绑定当前浏览器的短期会话中。

将用户重定向到:

https://jointtex.com/oidc/authorize
?response_type=code
&client_id=<your-client-id>
&redirect_uri=https://app.example.com/auth/jointtex/callback
&scope=openid%20profile%20email
&state=<random-state>
&nonce=<random-nonce>
&code_challenge=<BASE64URL(SHA256(code_verifier))>
&code_challenge_method=S256

支持的可选标准参数:

  • prompt=login 或 max_age=<秒数>:要求用户重新认证;随后校验 ID Token 中的 auth_time。
  • prompt=none:需要登录或同意时直接返回 login_required 或 consent_required。

JointTex 会先让用户登录(若未登录),再展示明确的授权同意页(包含应用名称、请求的 Scope 与完整回调 URI)。

  • 用户点击同意后,重定向回 redirect_uri,携带 code、state 与 iss=https://jointtex.com/oidc。
  • 用户取消则携带 error=access_denied 与原始 state 返回。

在回调接口中,首先核对 state 是否与浏览器会话一致,并校验 iss 严格等于 https://jointtex.com/oidc。

向 https://jointtex.com/oidc/token 发起表单 POST:

终端窗口
curl -X POST https://jointtex.com/oidc/token \
-u "<client_id>:<client_secret>" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "grant_type=authorization_code" \
--data-urlencode "code=<authorization-code>" \
--data-urlencode "redirect_uri=https://app.example.com/auth/jointtex/callback" \
--data-urlencode "code_verifier=<original-pkce-verifier>"

公开客户端(none)不使用 HTTP Basic,改为在表单中传递 client_id=<client_id>。

授权码有效期为 5 分钟,与客户端、回调 URI、PKCE 挑战、浏览器会话及应用版本绑定,并跨副本原子消费一次。

使用 https://jointtex.com/oidc/jwks 中的公钥验证 id_token:

  • 必须要求 RS256 算法和受信任的 kid(遇到未知 kid 时自动刷新 JWKS)。
  • 严格校验 iss(https://jointtex.com/oidc)、aud(你的 client_id)、exp/iat、会话里的 nonce 以及非空 sub。
  • 若请求了 max_age 或 prompt=login,同时校验 auth_time。
  • 绝不可仅解码 JWT 而不验证签名。

携带 Authorization: Bearer <access_token> 调用 GET https://jointtex.com/oidc/userinfo,并断言返回的 sub 与 ID Token 一致:

终端窗口
curl https://jointtex.com/oidc/userinfo \
-H "Authorization: Bearer <access_token>"
{
"sub": "9a7d0f4e-3c1b-4c4b-9e6a-1f2e3d4c5b6a",
"name": "Ada Lovelace",
"email": "[email protected]",
"email_verified": true
}

Node.js 完整示例(openid-client v6)

Section titled “Node.js 完整示例(openid-client v6)”
import * as client from "openid-client";
const config = await client.discovery(
new URL("https://jointtex.com/oidc"),
process.env.JOINTTEX_CLIENT_ID!,
process.env.JOINTTEX_CLIENT_SECRET!,
client.ClientSecretBasic(process.env.JOINTTEX_CLIENT_SECRET!),
);
// 1. 生成授权地址(将 verifier、state、nonce 存入浏览器会话)
const codeVerifier = client.randomPKCECodeVerifier();
const codeChallenge = await client.calculatePKCECodeChallenge(codeVerifier);
const state = client.randomState();
const nonce = client.randomNonce();
const authUrl = client.buildAuthorizationUrl(config, {
redirect_uri: "https://app.example.com/auth/jointtex/callback",
scope: "openid profile email",
code_challenge: codeChallenge,
code_challenge_method: "S256",
state,
nonce,
});
// 2. 在回调处理器中换取令牌并核验身份
const tokens = await client.authorizationCodeGrant(config, currentCallbackUrl, {
pkceCodeVerifier: session.codeVerifier,
expectedState: session.state,
expectedNonce: session.nonce,
});
const claims = tokens.claims()!;
const userInfo = await client.fetchUserInfo(config, tokens.access_token, claims.sub);
// 使用 (claims.iss, claims.sub) 作为本地用户主键