Web 登录(授权码 + PKCE)
推荐主路径 · 机密客户端
前置条件
- 已开通客户端:获得
client_id/client_secret - 回调地址已登记,且符合规则(生产使用 https;http 仅限 localhost)
- 新建客户端默认开启 PKCE(S256)与 Refresh Token 轮换
时序
- 应用生成
code_verifier,计算code_challenge = BASE64URL(SHA256(verifier)) - 浏览器跳转授权端点
- 用户登录并确认授权(若开启 consent)
- 回调携带
code与state - 服务端用 code + verifier + client 凭证换取 token
- 使用 access_token 调用 UserInfo;可选校验 id_token
1. 授权请求
GET https://login.heanbian.com/oauth2/authorize
response_type=code client_id=<client_id> redirect_uri=<registered_redirect_uri> scope=openid profile email state=<random> code_challenge=<challenge> code_challenge_method=S256
2. 换取令牌
POST https://login.heanbian.com/oauth2/token
curl -u '<client_id>:<client_secret>' \
-d 'grant_type=authorization_code' \
-d 'code=<code>' \
-d 'redirect_uri=<redirect_uri>' \
-d 'code_verifier=<verifier>' \
https://login.heanbian.com/oauth2/token
3. 获取用户信息
推荐标准端点:
curl -H 'Authorization: Bearer <access_token>' \
https://login.heanbian.com/userinfo
兼容端点(历史):https://login.heanbian.com/oauth2/userinfo。新接入请优先 /userinfo。
4. 刷新令牌
curl -u '<client_id>:<client_secret>' \
-d 'grant_type=refresh_token' \
-d 'refresh_token=<refresh_token>' \
https://login.heanbian.com/oauth2/token
默认不复用 refresh token(轮换)。每次刷新请保存新的 refresh_token。
回调 URI 规则
- 仅支持 http / https
- http 仅允许 localhost / 127.0.0.1 / ::1
- 禁止 userinfo、fragment