riskio / oauth2-auth0
PHP League OAuth2-Client 的 Auth0 OAuth 2.0 客户端提供者
v2.4.1
2021-11-10 11:41 UTC
Requires
- php: ^7.3|^8.0
- league/oauth2-client: ^2.2.1
Requires (Dev)
- phpunit/phpunit: ^9.3
README
本软件包为 PHP League 的 OAuth 2.0 客户端 提供Auth0 OAuth 2.0 支持。
安装
使用 composer 进行安装
composer require riskio/oauth2-auth0
使用方法
使用方法与 The League 的 OAuth 客户端相同,使用 Riskio\OAuth2\Client\Provider\Auth0
作为提供者。
授权码流
您需要向提供者提供一些参数
- customDomain (可选)
- 描述:用于 Auth0 登录的自定义域名 - https://auth0.com/docs/custom-domains (例如:login.custom-domain.tld - 它将自动前缀为 https://。如果设置了此值,则忽略区域和帐户参数。)
- region (可选)
- 描述:Auth0 区域
- 值
- Riskio\OAuth2\Client\Provider\Auth0::REGION_US (默认值)
- Riskio\OAuth2\Client\Provider\Auth0::REGION_EU
- Riskio\OAuth2\Client\Provider\Auth0::REGION_AU
- Riskio\OAuth2\Client\Provider\Auth0::REGION_JP
- account (如果未设置 customDomain,则为必需)
- 描述:Auth0 帐户名称
- clientId
- 描述:由提供者分配给您的客户端 ID
- clientSecret
- 描述:由提供者分配给您的客户端密码
- redirectUri
session_start(); $provider = new Riskio\OAuth2\Client\Provider\Auth0([ 'region' => '{region}', 'account' => '{account}', 'clientId' => '{auth0-client-id}', 'clientSecret' => '{auth0-client-secret}', 'redirectUri' => 'https://example.com/callback-url' ]); if (!isset($_GET['code'])) { // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Optional: Now you have a token you can look up a users profile data try { // We got an access token, let's now get the user's details $user = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $user->getName()); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } // Use this to interact with an API on the users behalf echo $token->getToken(); }
刷新令牌
Auth0 的 OAuth 实现不使用刷新令牌。