You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 8.0 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <h1 align="center"> Socialite</h1>
  2. <p align="center">
  3. <a href="https://travis-ci.org/overtrue/socialite"><img src="https://travis-ci.org/overtrue/socialite.svg?branch=master" alt="Build Status"></a>
  4. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/v/stable.svg" alt="Latest Stable Version"></a>
  5. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/v/unstable.svg" alt="Latest Unstable Version"></a>
  6. <a href="https://scrutinizer-ci.com/g/overtrue/socialite/build-status/master"><img src="https://scrutinizer-ci.com/g/overtrue/socialite/badges/build.png?b=master" alt="Build Status"></a>
  7. <a href="https://scrutinizer-ci.com/g/overtrue/socialite/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/socialite/badges/quality-score.png?b=master" alt="Scrutinizer Code Quality"></a>
  8. <a href="https://scrutinizer-ci.com/g/overtrue/socialite/?branch=master"><img src="https://scrutinizer-ci.com/g/overtrue/socialite/badges/coverage.png?b=master" alt="Code Coverage"></a>
  9. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/downloads" alt="Total Downloads"></a>
  10. <a href="https://packagist.org/packages/overtrue/socialite"><img src="https://poser.pugx.org/overtrue/socialite/license" alt="License"></a>
  11. </p>
  12. <p align="center">Socialite is an OAuth2 Authentication tool. It is inspired by <a href="https://github.com/laravel/socialite">laravel/socialite</a>, You can easily use it in any PHP project.</p>
  13. <p align="center">
  14. <br>
  15.  <b>创造不息,交付不止</b>
  16. <br>
  17. <a href="https://www.yousails.com">
  18. <img src="https://yousails.com/banners/brand.png" width=350>
  19. </a>
  20. </p>
  21. # Requirement
  22. ```
  23. PHP >= 5.4
  24. ```
  25. # Installation
  26. ```shell
  27. $ composer require "overtrue/socialite:~1.1"
  28. ```
  29. # Usage
  30. For Laravel 5: [overtrue/laravel-socialite](https://github.com/overtrue/laravel-socialite)
  31. `authorize.php`:
  32. ```php
  33. <?php
  34. use Overtrue\Socialite\SocialiteManager;
  35. $config = [
  36. 'github' => [
  37. 'client_id' => 'your-app-id',
  38. 'client_secret' => 'your-app-secret',
  39. 'redirect' => 'http://localhost/socialite/callback.php',
  40. ],
  41. ];
  42. $socialite = new SocialiteManager($config);
  43. $response = $socialite->driver('github')->redirect();
  44. echo $response;// or $response->send();
  45. ```
  46. `callback.php`:
  47. ```php
  48. <?php
  49. // ...
  50. $user = $socialite->driver('github')->user();
  51. $user->getId(); // 1472352
  52. $user->getNickname(); // "overtrue"
  53. $user->getName(); // "安正超"
  54. $user->getEmail(); // "anzhengchao@gmail.com"
  55. $user->getProviderName(); // GitHub
  56. ...
  57. ```
  58. ### Configuration
  59. Now we support the following sites:
  60. `facebook`, `github`, `google`, `linkedin`, `weibo`, `qq`, `wechat`, `wechat_open`, and `douban`.
  61. Each drive uses the same configuration keys: `client_id`, `client_secret`, `redirect`.
  62. Example:
  63. ```
  64. ...
  65. 'weibo' => [
  66. 'client_id' => 'your-app-id',
  67. 'client_secret' => 'your-app-secret',
  68. 'redirect' => 'http://localhost/socialite/callback.php',
  69. ],
  70. ...
  71. ```
  72. Special configuration options for [WeChat Open Platform](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN)
  73. ```
  74. 'wechat_open' => [
  75. 'client_id' => 'your-app-id',
  76. 'client_secret' => ['your-component-appid', 'your-component-access-token'],
  77. 'redirect' => 'http://localhost/socialite/callback.php',
  78. ]
  79. ```
  80. ### Scope
  81. Before redirecting the user, you may also set "scopes" on the request using the scope method. This method will overwrite all existing scopes:
  82. ```php
  83. $response = $socialite->driver('github')
  84. ->scopes(['scope1', 'scope2'])->redirect();
  85. ```
  86. ### Redirect URL
  87. You may also want to dynamic set `redirect`,you can use the following methods to change the `redirect` URL:
  88. ```php
  89. $socialite->redirect($url);
  90. // or
  91. $socialite->withRedirectUrl($url)->redirect();
  92. // or
  93. $socialite->setRedirectUrl($url)->redirect();
  94. ```
  95. > WeChat scopes:
  96. - `snsapi_base`, `snsapi_userinfo` - Used to Media Platform Authentication.
  97. - `snsapi_login` - Used to web Authentication.
  98. ### Additional parameters
  99. To include any optional parameters in the request, call the with method with an associative array:
  100. ```php
  101. $response = $socialite->driver('google')
  102. ->with(['hd' => 'example.com'])->redirect();
  103. ```
  104. ### User interface
  105. #### Standard user api:
  106. ```php
  107. $user = $socialite->driver('weibo')->user();
  108. ```
  109. ```json
  110. {
  111. "id": 1472352,
  112. "nickname": "overtrue",
  113. "name": "安正超",
  114. "email": "anzhengchao@gmail.com",
  115. "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
  116. "original": {
  117. "login": "overtrue",
  118. "id": 1472352,
  119. "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
  120. "gravatar_id": "",
  121. "url": "https://api.github.com/users/overtrue",
  122. "html_url": "https://github.com/overtrue",
  123. ...
  124. },
  125. "token": {
  126. "access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
  127. "token_type": "bearer",
  128. "scope": "user:email"
  129. }
  130. }
  131. ```
  132. You can fetch the user attribute as a array key like this:
  133. ```php
  134. $user['id']; // 1472352
  135. $user['nickname']; // "overtrue"
  136. $user['name']; // "安正超"
  137. $user['email']; // "anzhengchao@gmail.com"
  138. ...
  139. ```
  140. Or using method:
  141. ```php
  142. $user->getId();
  143. $user->getNickname();
  144. $user->getName();
  145. $user->getEmail();
  146. $user->getAvatar();
  147. $user->getOriginal();
  148. $user->getToken();// or $user->getAccessToken()
  149. $user->getProviderName(); // GitHub/Google/Facebook...
  150. ```
  151. #### Get original response from OAuth API
  152. The `$user->getOriginal()` method will return an array of the API raw response.
  153. #### Get access token Object
  154. You can get the access token instance of current session by call `$user->getToken()` or `$user->getAccessToken()` or `$user['token']` .
  155. ### Get user with access token
  156. ```php
  157. $accessToken = new AccessToken(['access_token' => $accessToken]);
  158. $user = $socialite->user($accessToken);
  159. ```
  160. ### Custom Session or Request instance.
  161. You can set the request with your custom `Request` instance which instanceof `Symfony\Component\HttpFoundation\Request` before you call `driver` method.
  162. ```php
  163. $request = new Request(); // or use AnotherCustomRequest.
  164. $socialite = new SocialiteManager($config, $request);
  165. ```
  166. Or set request to `SocialiteManager` instance:
  167. ```php
  168. $socialite->setRequest($request);
  169. ```
  170. You can get the request from `SocialiteManager` instance by `getRequest()`:
  171. ```php
  172. $request = $socialite->getRequest();
  173. ```
  174. #### Set custom session manager.
  175. By default, the `SocialiteManager` use `Symfony\Component\HttpFoundation\Session\Session` instance as session manager, you can change it as following lines:
  176. ```php
  177. $session = new YourCustomSessionManager();
  178. $socialite->getRequest()->setSession($session);
  179. ```
  180. > Your custom session manager must be implement the [`Symfony\Component\HttpFoundation\Session\SessionInterface`](http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/Session/SessionInterface.html).
  181. Enjoy it! :heart:
  182. # Reference
  183. - [Google - OpenID Connect](https://developers.google.com/identity/protocols/OpenIDConnect)
  184. - [Facebook - Graph API](https://developers.facebook.com/docs/graph-api)
  185. - [Linkedin - Authenticating with OAuth 2.0](https://developer.linkedin.com/docs/oauth2)
  186. - [微博 - OAuth 2.0 授权机制说明](http://open.weibo.com/wiki/%E6%8E%88%E6%9D%83%E6%9C%BA%E5%88%B6%E8%AF%B4%E6%98%8E)
  187. - [QQ - OAuth 2.0 登录QQ](http://wiki.connect.qq.com/oauth2-0%E7%AE%80%E4%BB%8B)
  188. - [微信公众平台 - OAuth文档](http://mp.weixin.qq.com/wiki/9/01f711493b5a02f24b04365ac5d8fd95.html)
  189. - [微信开放平台 - 网站应用微信登录开发指南](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419316505&token=&lang=zh_CN)
  190. - [微信开放平台 - 代公众号发起网页授权](https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419318590&token=&lang=zh_CN)
  191. - [豆瓣 - OAuth 2.0 授权机制说明](http://developers.douban.com/wiki/?title=oauth2)
  192. # License
  193. MIT