快速开始:用户 OAuth

快速开始:用户 OAuth

环境与入口

生产issuer为 https://accounts.appforges.com,staging为 https://accounts-staging.appforges.com。浏览器用户授权入口是对应issuer的 /oauth2/authorize,token交换是 /oauth2/token。用户业务API为对应环境的api域名;不要把REST /v1/oauth/authorize 当成浏览器登录入口。

在应用Credentials创建OAuth client,配置Public/Confidential模式、redirect URIs与allowed scopes。复制实际client_id,不把app_id当成client_id。Public client使用PKCE S256且不持有client_secret;Confidential secret只放安全后端。

用户授权步骤

  1. 生成随机state和code_verifier,保存到当前登录会话;code_challenge为verifier的SHA256再做base64url(去填充)。
  2. 浏览器跳转issuer的 /oauth2/authorize,参数包含response_type=code、client_id、完整登记的redirect_uri、scope、state、code_challenge、code_challenge_method=S256。用户profile至少申请 user:profile:read。
  3. 用户在accounts登录并授权。接收自己的redirect_uri回调;首先检查error和state,不要直接信任URL中的code。
  4. 对同issuer /oauth2/token 发form-encoded POST,提交grant_type=authorization_code、code、client_id、同一redirect_uri、原code_verifier。Confidential client另按登记的认证方式提供secret;Public client不提交secret。
  5. 安全保存token,按expires_in管理有效期,然后用快速开始读取用户档案。回调code一次使用;不记录code/token/secret。

应用创建本身不返回用户access token。授权需要真实用户会话与客户端许可,不用client_credentials绕过。