提交的内容
This commit is contained in:
115
vendor/overtrue/socialite/README.md
vendored
Executable file → Normal file
115
vendor/overtrue/socialite/README.md
vendored
Executable file → Normal file
@ -12,19 +12,17 @@ Socialite 是一个 [OAuth2](https://oauth.net/2/) 认证工具。 它的灵感
|
||||
|
||||
如果你喜欢我的项目并想支持我,[点击这里 :heart:](https://github.com/sponsors/overtrue)
|
||||
|
||||
# 版本要求
|
||||
## 版本要求
|
||||
|
||||
```
|
||||
PHP >= 8.0.2
|
||||
```
|
||||
|
||||
# 安装
|
||||
## 安装
|
||||
|
||||
```shell
|
||||
$ composer require "overtrue/socialite" -vvv
|
||||
composer require "overtrue/socialite" -vvv
|
||||
```
|
||||
|
||||
# 使用指南
|
||||
## 使用指南
|
||||
|
||||
用户只需要创建相应配置变量,然后通过工具为各个平台创建认证应用,并轻松获取该平台的 access_token 和用户相关信息。工具实现逻辑详见参照各大平台 OAuth2 文档。
|
||||
|
||||
@ -56,7 +54,7 @@ $socialite = new SocialiteManager($config);
|
||||
|
||||
$url = $socialite->create('github')->redirect();
|
||||
|
||||
return redirect($url);
|
||||
return redirect($url);
|
||||
```
|
||||
|
||||
`callback.php`:
|
||||
@ -117,20 +115,20 @@ $config = [
|
||||
$config = [
|
||||
// 为 github 应用起别名为 foo
|
||||
'foo' => [
|
||||
'provider' => 'github', // <-- provider name
|
||||
'client_id' => 'your-app-id',
|
||||
'provider' => 'github', // <-- provider name
|
||||
'client_id' => 'your-app-id',
|
||||
'client_secret' => 'your-app-secret',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
],
|
||||
|
||||
|
||||
// 另外一个名字叫做 bar 的 github 应用
|
||||
'bar' => [
|
||||
'provider' => 'github', // <-- provider name
|
||||
'client_id' => 'your-app-id',
|
||||
'provider' => 'github', // <-- provider name
|
||||
'client_id' => 'your-app-id',
|
||||
'client_secret' => 'your-app-secret',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
],
|
||||
|
||||
|
||||
//...
|
||||
];
|
||||
|
||||
@ -159,7 +157,7 @@ $config = [
|
||||
];
|
||||
|
||||
$socialite = new SocialiteManager($config);
|
||||
|
||||
|
||||
$socialite->extend('myprovider', function(array $config) {
|
||||
return new MyCustomProvider($config);
|
||||
});
|
||||
@ -167,12 +165,14 @@ $socialite->extend('myprovider', function(array $config) {
|
||||
$app = $socialite->create('foo');
|
||||
```
|
||||
|
||||
2. 使用服务提供类
|
||||
1. 使用服务提供类
|
||||
|
||||
>👋🏻 你的自定义服务提供类必须实现`Overtrue\Socialite\Contracts\ProviderInterface` 接口
|
||||
> [!IMPORTANT]
|
||||
>
|
||||
> 👋🏻 你的自定义服务提供类必须实现 `Overtrue\Socialite\Contracts\ProviderInterface` 接口
|
||||
|
||||
```php
|
||||
class MyCustomProvider implements \Overtrue\Socialite\Contracts\ProviderInterface
|
||||
class MyCustomProvider implements \Overtrue\Socialite\Contracts\ProviderInterface
|
||||
{
|
||||
//...
|
||||
}
|
||||
@ -183,10 +183,10 @@ class MyCustomProvider implements \Overtrue\Socialite\Contracts\ProviderInterfac
|
||||
```php
|
||||
$config = [
|
||||
'foo' => [
|
||||
'provider' => MyCustomProvider::class, // <-- 类名
|
||||
'client_id' => 'your-app-id',
|
||||
'provider' => MyCustomProvider::class, // <-- 类名
|
||||
'client_id' => 'your-app-id',
|
||||
'client_secret' => 'your-app-secret',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
],
|
||||
];
|
||||
|
||||
@ -194,8 +194,6 @@ $socialite = new SocialiteManager($config);
|
||||
$app = $socialite->create('foo');
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 平台
|
||||
|
||||
不同的平台有不同的配置方法,为了确保工具的正常运行,所以请确保你所使用的平台的配置都是如期设置的。
|
||||
@ -208,8 +206,8 @@ $app = $socialite->create('foo');
|
||||
$config = [
|
||||
'alipay' => [
|
||||
// 这个键名还能像官方文档那样叫做 'app_id'
|
||||
'client_id' => 'your-app-id',
|
||||
|
||||
'client_id' => 'your-app-id',
|
||||
|
||||
// 请根据官方文档,在官方管理后台配置 RSA2
|
||||
// 注意: 这是你自己的私钥
|
||||
// 注意: 不允许私钥内容有其他字符
|
||||
@ -219,7 +217,7 @@ $config = [
|
||||
// 确保这里的值与你在服务后台绑定的地址值一致
|
||||
// 这个键名还能像官方文档那样叫做 'redirect_url'
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
|
||||
|
||||
// 沙箱模式接入地址见 https://opendocs.alipay.com/open/220/105337#%E5%85%B3%E4%BA%8E%E6%B2%99%E7%AE%B1
|
||||
'sandbox' => false,
|
||||
]
|
||||
@ -252,7 +250,7 @@ $config = [
|
||||
// or 'app_id'
|
||||
'client_id' => 'your app id',
|
||||
|
||||
// or 'app_secret'
|
||||
// or 'app_secret'
|
||||
'client_secret' => 'your app secret',
|
||||
|
||||
// or 'redirect_url'
|
||||
@ -332,7 +330,6 @@ $user = $socialite->create('xigua')->userFromCode('here is auth code');
|
||||
$user = $socialite->create('xigua')->withOpenId('openId')->userFromToken('here is the access token');
|
||||
```
|
||||
|
||||
|
||||
### [百度](https://developer.baidu.com/wiki/index.php?title=docs/oauth)
|
||||
|
||||
其他配置没啥区别,在用法上,可以很轻易的选择重定向登录页面的模式,通过 `withDisplay()`
|
||||
@ -361,7 +358,7 @@ $config = [
|
||||
// or 'app_id'
|
||||
'client_id' => 'your app id',
|
||||
|
||||
// or 'app_secret'
|
||||
// or 'app_secret'
|
||||
'client_secret' => 'your app secret',
|
||||
|
||||
// or 'redirect_url'
|
||||
@ -391,7 +388,7 @@ $config = [
|
||||
// or 'app_id'
|
||||
'client_id' => 'your app id',
|
||||
|
||||
// or 'app_secret'
|
||||
// or 'app_secret'
|
||||
'client_secret' => 'your app secret',
|
||||
|
||||
// or 'redirect_url'
|
||||
@ -413,7 +410,7 @@ $larkDriver->withDefaultMode()->withAppTicket('app_ticket')->userFromCode('here
|
||||
|
||||
### [淘宝](https://open.taobao.com/doc.htm?docId=102635&docType=1&source=search)
|
||||
|
||||
其他配置与其他平台的一样,你能选择你想要展示的重定向页面类型通过使用 `withView()`
|
||||
其他配置与其他平台的一样,你能选择你想要展示的重定向页面类型通过使用 `withView()`
|
||||
|
||||
```php
|
||||
$authUrl = $socialite->create('taobao')->withView('wap')->redirect();
|
||||
@ -432,9 +429,9 @@ $authUrl = $socialite->create('taobao')->withView('wap')->redirect();
|
||||
[
|
||||
'wechat' =>
|
||||
[
|
||||
'client_id' => 'client_id',
|
||||
'client_id' => 'client_id',
|
||||
'client_secret' => 'client_secret',
|
||||
'redirect' => 'redirect-url',
|
||||
'redirect' => 'redirect-url',
|
||||
|
||||
// 开放平台 - 第三方平台所需
|
||||
'component' => [
|
||||
@ -448,7 +445,6 @@ $authUrl = $socialite->create('taobao')->withView('wap')->redirect();
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
### [Coding](https://coding.net/help/openapi#oauth)
|
||||
|
||||
您需要额外配置 `team_url` 为您的团队域名,例如:
|
||||
@ -456,7 +452,7 @@ $authUrl = $socialite->create('taobao')->withView('wap')->redirect();
|
||||
```php
|
||||
$config = [
|
||||
'coding' => [
|
||||
'team_url' => 'https://{your-team}.coding.net',
|
||||
'team_url' => 'https://{your-team}.coding.net',
|
||||
'client_id' => 'your app id',
|
||||
'client_secret' => 'your app secret',
|
||||
'redirect' => 'redirect URL',
|
||||
@ -464,6 +460,23 @@ $config = [
|
||||
];
|
||||
```
|
||||
|
||||
### [PayPal](https://developer.paypal.com/docs/log-in-with-paypal/)
|
||||
|
||||
您可能需要设置responseType,可以使用`withResponseType`函数进行设置,默认是`code` 还可以设置为`id_token` 或`code` & `id_token`
|
||||
|
||||
> <https://developer.paypal.com/docs/log-in-with-paypal/integrate/generate-button/>
|
||||
|
||||
```php
|
||||
$config = [
|
||||
'paypal' => [
|
||||
'client_id' => 'AT******************',
|
||||
'client_secret' => 'EK**************',
|
||||
'sandbox' => false,
|
||||
'redirect_url'=>"nativexo://paypalpay",
|
||||
],
|
||||
];
|
||||
```
|
||||
|
||||
## 其他一些技巧
|
||||
|
||||
### Scopes
|
||||
@ -498,7 +511,7 @@ $socialite->withRedirectUrl($url)->redirect();
|
||||
```php
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
|
||||
$config = [
|
||||
//...
|
||||
];
|
||||
@ -510,7 +523,7 @@ $socialite = new SocialiteManager($config);
|
||||
|
||||
$url = $socialite->create('github')->withState($state)->redirect();
|
||||
|
||||
return redirect($url);
|
||||
return redirect($url);
|
||||
```
|
||||
|
||||
### 检验回调的 `state`
|
||||
@ -520,10 +533,10 @@ return redirect($url);
|
||||
```php
|
||||
<?php
|
||||
session_start();
|
||||
|
||||
|
||||
$state = request()->query('state');
|
||||
$code = request()->query('code');
|
||||
|
||||
|
||||
// Check the state received with current session id
|
||||
if ($state != hash('sha256', session_id())) {
|
||||
exit('State does not match!');
|
||||
@ -544,10 +557,9 @@ $response = $socialite->create('google')
|
||||
->with(['hd' => 'example.com'])->redirect();
|
||||
```
|
||||
|
||||
|
||||
## User interface
|
||||
|
||||
### 标准的 user api:
|
||||
### 标准的 user api
|
||||
|
||||
```php
|
||||
$user = $socialite->create('github')->userFromCode($code);
|
||||
@ -596,7 +608,7 @@ mixed $user->getId();
|
||||
?string $user->getEmail();
|
||||
?string $user->getAvatar();
|
||||
?string $user->getRaw();
|
||||
?string $user->getAccessToken();
|
||||
?string $user->getAccessToken();
|
||||
?string $user->getRefreshToken();
|
||||
?int $user->getExpiresIn();
|
||||
?array $user->getTokenResponse();
|
||||
@ -604,7 +616,7 @@ mixed $user->getId();
|
||||
|
||||
```
|
||||
|
||||
### 从 OAuth API 响应中取得原始数据
|
||||
### 从 OAuth API 响应中取得原始数据
|
||||
|
||||
`$user->getRaw()` 方法会返回一个 **array**。
|
||||
|
||||
@ -612,7 +624,7 @@ mixed $user->getId();
|
||||
|
||||
`$user->getTokenResponse()` 方法会返回一个 **array** 里面是响应从获取 token 时候 API 返回的响应。
|
||||
|
||||
> 注意:当你使用 `userFromCode()` 时,这个方法只返回一个 **有效的数组**,否则将返回 **null**,因为 `userFromToken() ` 没有 token 的 HTTP 响应。
|
||||
> 注意:当你使用 `userFromCode()` 时,这个方法只返回一个 **有效的数组**,否则将返回 **null**,因为 `userFromToken()` 没有 token 的 HTTP 响应。
|
||||
|
||||
### 通过 access token 获取用户信息
|
||||
|
||||
@ -621,11 +633,9 @@ $accessToken = 'xxxxxxxxxxx';
|
||||
$user = $socialite->userFromToken($accessToken);
|
||||
```
|
||||
|
||||
## Enjoy it! :heart:
|
||||
|
||||
|
||||
# Enjoy it! :heart:
|
||||
|
||||
# 参照
|
||||
## 参照
|
||||
|
||||
- [Alipay - 用户信息授权](https://opendocs.alipay.com/open/289/105656)
|
||||
- [DingTalk - 扫码登录第三方网站](https://ding-doc.dingtalk.com/doc#/serverapi3/mrugr3)
|
||||
@ -648,15 +658,14 @@ $user = $socialite->userFromToken($accessToken);
|
||||
- [Tapd - 用户授权说明](https://www.tapd.cn/help/show#1120003271001000093)
|
||||
- [Line - OAuth 2.0](https://developers.line.biz/en/docs/line-login/integrate-line-login/)
|
||||
- [Gitee - OAuth文档](https://gitee.com/api/v5/oauth_doc#/)
|
||||
- [PayPal - OAuth文档](https://developer.paypal.com/docs/log-in-with-paypal/)
|
||||
|
||||
|
||||
|
||||
# PHP 扩展包开发
|
||||
## PHP 扩展包开发
|
||||
|
||||
> 想知道如何从零开始构建 PHP 扩展包?
|
||||
>
|
||||
> 请关注我的实战课程,我会在此课程中分享一些扩展开发经验 —— [《PHP 扩展包实战教程 - 从入门到发布》](https://learnku.com/courses/creating-package)
|
||||
|
||||
# License
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
Reference in New Issue
Block a user