novak-collabim / oauth2-apple
使用 Apple OAuth 2.0 客户端提供者登录 The PHP League OAuth2-Client
1.0.0
2023-08-14 11:11 UTC
Requires
- ext-json: *
- firebase/php-jwt: ^5.2 || ^6.0
- lcobucci/jwt: ^3.4 || ^4.0
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ^1.3
- php-parallel-lint/php-parallel-lint: ^1.3
- phpunit/phpunit: ^5.7 || ^6.0 || ^9.3
- squizlabs/php_codesniffer: ^2.3 || ^3.0
README
此项目是从 [patrickbussmann/oauth2-apple] 分支出来的。
使用 Apple ID 提供者登录 OAuth 2.0 客户端
此包为 PHP League 的 OAuth 2.0 客户端 提供Apple ID OAuth 2.0 支持。
开始之前
您可以在以下位置找到官方的 Apple 文档:https://developer.apple.com/documentation/signinwithapplerestapi
如果您请求电子邮件地址或姓名,请注意,您只能在第一次登录时获取这些信息。当您第二次登录时,您将只能获取用户 ID - 没有更多。也许苹果公司会在某个时候改变这一点。
安装
要安装,请使用 composer
composer require patrickbussmann/oauth2-apple
用法
用法与 The League 的 OAuth 客户端相同,使用 \League\OAuth2\Client\Provider\Apple
作为提供者。
授权代码流
// $leeway is needed for clock skew Firebase\JWT\JWT::$leeway = 60; $provider = new League\OAuth2\Client\Provider\Apple([ 'clientId' => '{apple-client-id}', 'teamId' => '{apple-team-id}', // 1A234BFK46 https://developer.apple.com/account/#/membership/ (Team ID) 'keyFileId' => '{apple-key-file-id}', // 1ABC6523AA https://developer.apple.com/account/resources/authkeys/list (Key ID) 'keyFilePath' => '{apple-key-file-path}', // __DIR__ . '/AuthKey_1ABC6523AA.p8' -> Download key above 'redirectUri' => 'https://example.com/callback-url', ]); if (!isset($_POST['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($_POST['state']) || ($_POST['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { // Try to get an access token (using the authorization code grant) /** @var AppleAccessToken $token */ $token = $provider->getAccessToken('authorization_code', [ 'code' => $_POST['code'] ]); // Optional: Now you have a token you can look up a users profile data // Important: The most details are only visible in the very first login! // In the second and third and ... ones you'll only get the identifier of the user! 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->getFirstName()); } catch (Exception $e) { // Failed to get user details exit(':-('); } // Use this to interact with an API on the users behalf echo $token->getToken(); }
撤销代码流
// $leeway is needed for clock skew Firebase\JWT\JWT::$leeway = 60; $provider = new League\OAuth2\Client\Provider\Apple([ 'clientId' => '{apple-client-id}', 'teamId' => '{apple-team-id}', // 1A234BFK46 https://developer.apple.com/account/#/membership/ (Team ID) 'keyFileId' => '{apple-key-file-id}', // 1ABC6523AA https://developer.apple.com/account/resources/authkeys/list (Key ID) 'keyFilePath' => '{apple-key-file-path}', // __DIR__ . '/AuthKey_1ABC6523AA.p8' -> Download key above 'redirectUri' => 'https://example.com/callback-url', ]); $token = $token->getToken(); // Use the token of "Authorization Code Flow" which you saved somewhere for the user try { $provider->revokeAccessToken($token /*, 'access_token' or 'refresh_token' */); // Successfully revoked the token! } catch (Exception $e) { // Failed to revoke exit(':-('); }
管理作用域
在创建您的 Apple 授权 URL 时,您可以指定应用程序可能授权的状态和作用域。
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', // Scopes: https://developer.apple.com/documentation/authenticationservices/asauthorizationscope 'scope' => ['name', 'email'] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
如果没有定义,提供者将使用内部默认值。
在编写此文档时,以下作用域可用。
- name(默认)
- email(默认)
请注意,您只能在用户的第一次登录时获取这些信息!在随后的登录中,您将只能获取用户 ID!
如果您只想获取用户 ID,可以将 scope
设置为
,然后更改所有 $_POST
为 $_GET
。
刷新令牌
如果您的访问令牌已过期,您可以使用刷新令牌来刷新它们。
$refreshToken = $token->getRefreshToken();
$refreshTokenExpiration = $token->getRefreshTokenExpires();
测试
$ ./vendor/bin/phpunit
贡献
请参阅 CONTRIBUTING 获取详细信息。
致谢
此存储库的模板来自 LinkedIn。
许可证
MIT 许可证(MIT)。请参阅 许可证文件 获取更多信息。