快速開始:用戶 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只放安全後端。
用戶授權步驟
- 生成隨機state和code_verifier,保存到當前登錄會話;code_challenge為verifier的SHA256再做base64url(去填充)。
- 瀏覽器跳轉issuer的
/oauth2/authorize,參數包含response_type=code、client_id、完整登記的redirect_uri、scope、state、code_challenge、code_challenge_method=S256。用戶profile至少申請user:profile:read。 - 用戶在accounts登錄並授權。接收自己的redirect_uri回調;首先檢查error和state,不要直接信任URL中的code。
- 對同issuer
/oauth2/token發form-encoded POST,提交grant_type=authorization_code、code、client_id、同一redirect_uri、原code_verifier。Confidential client另按登記的認證方式提供secret;Public client不提交secret。 - 安全保存token,按expires_in管理有效期,然後用快速開始讀取用戶檔案。回調code一次使用;不記錄code/token/secret。
應用創建本身不返回用戶access token。授權需要真實用戶會話與客戶端許可,不用client_credentials繞過。