PHP搭建OAuth服务
<p>自己写OAuth后台太麻烦,直接拉取gitbub现成的。拉取活跃度比较高的<strong>bshaffer/oauth2-server-php</strong></p><p><strong>注:以下编码是Oauth四种认证中的第四种:凭证式。想了解其他几种方式,请移步阮一峰大大的博客http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html?utm_source=tuicool&utm_medium=referral</strong></p>
<p><img src="https://img2018.cnblogs.com/blog/1515577/201905/1515577-20190514161224394-2030889502.png"></p>
<p>1、首先拉取代码 https://github.com/bshaffer/oauth2-server-php.git</p>
<p>2、在编码之前先导入数据库</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">CREATE</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)"> `oauth_clients` (
`client_id` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`client_secret` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`redirect_uri` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">2000</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`grant_types` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`scope` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">4000</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`</span><span style="color: rgba(255, 0, 255, 1)">user_id</span>` <span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span> <span style="color: rgba(0, 0, 255, 1)">KEY</span><span style="color: rgba(0, 0, 0, 1)"> (`client_id`)
) ENGINE</span><span style="color: rgba(128, 128, 128, 1)">=</span>InnoDB <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> CHARSET<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">utf8;
</span><span style="color: rgba(0, 0, 255, 1)">CREATE</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)"> `oauth_jwt` (
`client_id` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`subject` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`public_key` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">2000</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">
) ENGINE</span><span style="color: rgba(128, 128, 128, 1)">=</span>InnoDB <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> CHARSET<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">utf8;
</span><span style="color: rgba(0, 0, 255, 1)">CREATE</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)"> `oauth_refresh_tokens` (
`refresh_token` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">40</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`client_id` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`</span><span style="color: rgba(255, 0, 255, 1)">user_id</span>` <span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`expires` </span><span style="color: rgba(0, 0, 255, 1)">timestamp</span> <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span> <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(255, 0, 255, 1)">CURRENT_TIMESTAMP</span> <span style="color: rgba(0, 0, 255, 1)">ON</span> <span style="color: rgba(0, 0, 255, 1)">UPDATE</span> <span style="color: rgba(255, 0, 255, 1)">CURRENT_TIMESTAMP</span><span style="color: rgba(0, 0, 0, 1)">,
`scope` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">4000</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span> <span style="color: rgba(0, 0, 255, 1)">KEY</span><span style="color: rgba(0, 0, 0, 1)"> (`refresh_token`)
) ENGINE</span><span style="color: rgba(128, 128, 128, 1)">=</span>InnoDB <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> CHARSET<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">utf8;
</span><span style="color: rgba(0, 0, 255, 1)">CREATE</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)"> `oauth_scopes` (
`scope` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`is_default` </span><span style="color: rgba(0, 0, 255, 1)">tinyint</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span> <span style="color: rgba(0, 0, 255, 1)">KEY</span><span style="color: rgba(0, 0, 0, 1)"> (`scope`)
) ENGINE</span><span style="color: rgba(128, 128, 128, 1)">=</span>InnoDB <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> CHARSET<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">utf8;
</span><span style="color: rgba(0, 0, 255, 1)">CREATE</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)"> `oauth_users` (
`username` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`password` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`first_name` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`last_name` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`email` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`email_verified` </span><span style="color: rgba(0, 0, 255, 1)">tinyint</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`scope` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">4000</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">
) ENGINE</span><span style="color: rgba(128, 128, 128, 1)">=</span>InnoDB <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> CHARSET<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">utf8;
</span><span style="color: rgba(0, 0, 255, 1)">CREATE</span> <span style="color: rgba(0, 0, 255, 1)">TABLE</span><span style="color: rgba(0, 0, 0, 1)"> `oauth_access_tokens` (
`access_token` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">40</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`client_id` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`</span><span style="color: rgba(255, 0, 255, 1)">user_id</span>` <span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">80</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
`expires` </span><span style="color: rgba(0, 0, 255, 1)">timestamp</span> <span style="color: rgba(128, 128, 128, 1)">NOT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span> <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(255, 0, 255, 1)">CURRENT_TIMESTAMP</span> <span style="color: rgba(0, 0, 255, 1)">ON</span> <span style="color: rgba(0, 0, 255, 1)">UPDATE</span> <span style="color: rgba(255, 0, 255, 1)">CURRENT_TIMESTAMP</span><span style="color: rgba(0, 0, 0, 1)">,
`scope` </span><span style="color: rgba(0, 0, 255, 1)">varchar</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">4000</span>) <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> <span style="color: rgba(0, 0, 255, 1)">NULL</span><span style="color: rgba(0, 0, 0, 1)">,
</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span> <span style="color: rgba(0, 0, 255, 1)">KEY</span><span style="color: rgba(0, 0, 0, 1)"> (`access_token`)
) ENGINE</span><span style="color: rgba(128, 128, 128, 1)">=</span>InnoDB <span style="color: rgba(0, 0, 255, 1)">DEFAULT</span> CHARSET<span style="color: rgba(128, 128, 128, 1)">=</span>utf8;</pre>
</div>
<p>3、在站点创建oauthConf.php(创建和配置OAuth的实例)</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(128, 0, 128, 1)">$conf</span> =<span style="color: rgba(0, 0, 0, 1)"> [
</span>'dsn' => 'mysql:dbname=open;host=127.0.0.1:3808',
'username' => 'root',
'password' => 'root'<span style="color: rgba(0, 0, 0, 1)">
];
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Autoloading (composer is preferred, but for this example let's just do this)</span>
<span style="color: rgba(0, 0, 255, 1)">require_once</span>('oauth2-server/src/OAuth2/Autoloader.php'<span style="color: rgba(0, 0, 0, 1)">);
OAuth2\Autoloader</span>::<span style="color: rgba(0, 0, 0, 1)">register();
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> $dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost"</span>
<span style="color: rgba(128, 0, 128, 1)">$storage</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> OAuth2\Storage\Pdo(<span style="color: rgba(128, 0, 128, 1)">$conf</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Pass a storage object or array of storage objects to the OAuth2 server class</span>
<span style="color: rgba(128, 0, 128, 1)">$server</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> OAuth2\Server(<span style="color: rgba(128, 0, 128, 1)">$storage</span><span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Add the "Client Credentials" grant type (it is the simplest of the grant types)</span>
<span style="color: rgba(128, 0, 128, 1)">$server</span>->addGrantType(<span style="color: rgba(0, 0, 255, 1)">new</span> OAuth2\GrantType\ClientCredentials(<span style="color: rgba(128, 0, 128, 1)">$storage</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Add the "Authorization Code" grant type (this is where the oauth magic happens)</span>
<span style="color: rgba(128, 0, 128, 1)">$server</span>->addGrantType(<span style="color: rgba(0, 0, 255, 1)">new</span> OAuth2\GrantType\AuthorizationCode(<span style="color: rgba(128, 0, 128, 1)">$storage</span>));</pre>
</div>
<p>4、获取令牌前,先向数据库插入一条测试数据</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">INSERT</span> <span style="color: rgba(0, 0, 255, 1)">INTO</span> oauth_clients (client_id, client_secret, redirect_uri) <span style="color: rgba(0, 0, 255, 1)">VALUES</span> ("arthurtest", "arthurpass", "http:<span style="color: rgba(128, 128, 128, 1)">//</span>arthur<span style="color: rgba(128, 128, 128, 1)">/</span>");</pre>
</div>
<p> </p>
<p>5、创建getToken.php(注:使用POST方法获取accessToken)</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> include our OAuth2 Server object</span>
<span style="color: rgba(0, 0, 255, 1)">require_once</span> __DIR__.'/server.php'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Handle a request for an OAuth2.0 Access Token and send the response to the client</span>
<span style="color: rgba(128, 0, 128, 1)">$server</span>->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();</pre>
</div>
<p style="text-align: left">6、执行getToken.php 获得access_token(注:需传入基本参数如下):</p>
<p style="text-align: left">参数:</p>
<table style="background-color: rgba(153, 204, 204, 1); height: 87px; width: 1010px" border="1" cellspacing="1" cellpadding="1" align="left">
<tbody>
<tr>
<td style="text-align: center">respose_type</td>
<td style="text-align: center"> authorization_code 标准的授权模式 password 基于用户密码的授权模式 client_credentials 基于密钥的授权模式 refresh_token 刷新token</td>
</tr>
<tr>
<td style="text-align: center">client_id</td>
<td style="text-align: center">应用id</td>
</tr>
<tr>
<td style="text-align: center">redirect_uri</td>
<td style="text-align: center">回调地址</td>
</tr>
</tbody>
</table>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left"> </p>
<p style="text-align: left">结果:</p>
<div class="cnblogs_code" style="text-align: left">
<pre><span style="color: rgba(0, 0, 0, 1)">{
</span>"access_token": "20dc6de50b4136430a4b391e49cb7f7d94e2fdf6",
"expires_in": 3600,
"token_type": "Bearer",
"scope": <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p style="text-align: left">7、现在已经拿到了令牌,就可以调用接口了,我们可以使用以下代码进行token合法验证</p>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> include our OAuth2 Server object</span>
<span style="color: rgba(0, 0, 255, 1)">require_once</span> __DIR__.'/server.php'<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Handle a request to a resource and authenticate the access token</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(128, 0, 128, 1)">$server</span>->verifyResourceRequest(OAuth2\Request::<span style="color: rgba(0, 0, 0, 1)">createFromGlobals())) {
</span><span style="color: rgba(128, 0, 128, 1)">$server</span>->getResponse()-><span style="color: rgba(0, 0, 0, 1)">send();
</span><span style="color: rgba(0, 0, 255, 1)">echo</span> json_encode(<span style="color: rgba(0, 0, 255, 1)">array</span>('success' => <span style="color: rgba(0, 0, 255, 1)">true</span>, 'message' => 'You accessed my APIs!'));</pre>
</div><br><br>
来源:https://www.cnblogs.com/arthurdou/p/10863220.html
頁:
[1]