patrickbussmann/oauth2-apple
Composer 安装命令:
composer require patrickbussmann/oauth2-apple
包简介
Sign in with Apple OAuth 2.0 Client Provider for The PHP League OAuth2-Client
README 文档
README
This package provides Apple ID OAuth 2.0 support for the PHP League's OAuth 2.0 Client.
Before You Begin
Here you can find the official Apple documentation: https://developer.apple.com/documentation/signinwithapplerestapi
If you request email address or name please note that you'll get this only in your first login. When you log in a second time you will only get the user id - nothing more. Maybe Apple changes this sometime.
Installation
To install, use composer:
composer require patrickbussmann/oauth2-apple
Usage
Usage is the same as The League's OAuth client, using \League\OAuth2\Client\Provider\Apple as the provider.
Authorization Code Flow
// $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(); }
Revoke Code Flow
// $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(':-('); }
Managing Scopes
When creating your Apple authorization URL, you can specify the state and scopes your application may authorize.
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', // Scopes: https://developer.apple.com/documentation/authenticationservices/asauthorizationscope 'scope' => ['name', 'email'] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
If neither are defined, the provider will utilize internal defaults.
At the time of authoring this documentation, the following scopes are available.
- name (default)
- email (default)
Please note that you will get this informations only at the first log in of the user! In the following log ins you'll get only the user id!
If you only want to get the user id, you can set the scope as , then change all the $_POST to $_GET.
Refresh Tokens
If your access token expires you can refresh them with the refresh token.
$refreshToken = $token->getRefreshToken();
$refreshTokenExpiration = $token->getRefreshTokenExpires();
Testing
$ ./vendor/bin/phpunit
Contributing
Please see CONTRIBUTING for details.
Credits
Template for this repository was the LinkedIn.
License
The MIT License (MIT). Please see License File for more information.
patrickbussmann/oauth2-apple 适用场景与选型建议
patrickbussmann/oauth2-apple 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.77M 次下载、GitHub Stars 达 113, 最近一次更新时间为 2019 年 10 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「client」 「oauth」 「oauth2」 「apple」 「authorisation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 patrickbussmann/oauth2-apple 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 patrickbussmann/oauth2-apple 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 patrickbussmann/oauth2-apple 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Mock PSR-18 HTTP client
Asynchronous MQTT client built on React
统计信息
- 总下载量: 2.77M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 113
- 点击次数: 56
- 依赖项目数: 8
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-18