跳到主要内容

授权端点

GET /oauth/authorize

引导用户进行 OAuth 授权。

请求参数

参数类型必需说明
response_typestring固定值 code
client_idstring应用的 Client ID
redirect_uristring回调地址,必须与注册时一致
scopestring权限范围。默认 openid,可选 openid profile email
statestring推荐随机字符串,防 CSRF

验证失败返回

条件响应
response_type 不是 code400 + 错误页
client_id 无效或已禁用400 + 错误页
redirect_uri 不在注册列表中400 + 错误页
scope 全部无效自动降级为 openid

行为

条件行为
用户未登录存储请求到 Session → 跳转 /login → 登录后返回授权页
用户已授权且 scope 足够直接发放授权码 → 302 重定向到 redirect_uri?code=...
用户未授权或 scope 不足显示授权确认页

已授权免确认机制

系统会检查 user_authorizations 表中是否已有该用户对该应用的授权记录。如果当前请求的 scope 是已授权 scope 的子集,则自动发放授权码,不再弹出授权页面。

已授权 scope: "openid"
当前请求 scope: "openid"
→ 免确认,直接发放 code

错误页

验证失败会显示 HTML 错误页面(非 JSON),包含错误描述。


POST /oauth/authorize

处理用户的授权确认/拒绝。

请求参数

参数类型说明
_csrfstringCSRF Token(必须)
actionstringallow 确认授权 / deny 拒绝授权
client_idstringClient ID
redirect_uristring回调地址
scopestring请求的权限范围
statestring状态参数(透传)

响应

授权成功(action=allow):

系统执行以下操作后重定向:

  1. 生成授权码(bin2hex(random_bytes(32)),64 字符 hex)
  2. 存入 oauth_authorization_codes 表,10 分钟有效
  3. 更新 user_authorizations 记录(如已存在则更新 scope)
  4. 确保 user_app_subjects 中有该用户在该应用下的 subject_id
  5. 302 重定向:redirect_uri?code=AUTH_CODE&state=STATE

拒绝授权(action=deny):

302 重定向:redirect_uri?error=access_denied&state=STATE


GET /oauth/deny

拒绝授权端点,用于 popup 模式下关闭窗口。

请求参数

参数说明
state状态参数(透传)
redirect_uri回调地址(可选)

行为

  • redirect_uri:重定向到 redirect_uri?error=access_denied&state=...
  • redirect_uri:执行 window.close() 关闭窗口