博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oauth2-server-php-docs 授权类型
阅读量:6904 次
发布时间:2019-06-27

本文共 9034 字,大约阅读时间需要 30 分钟。

授权码

概观

Authorization Code交付式时使用的客户端想要请求访问受保护资源代表其他用户(即第三方)。这是最常与OAuth关联的授予类型。

用例

  • 代表第三方来电

履行

创建一个实例OAuth2\GrantType\AuthorizationCode并将其添加到您的服务器

// create a storage object to hold new authorization codes$storage = new OAuth2\Storage\Pdo(array('dsn' => 'sqlite:authcodes.sqlite')); // create the grant type $grantType = new OAuth2\GrantType\AuthorizationCode($storage); // add the grant type to your OAuth server $server->addGrantType($grantType);

示例请求

授权码使用Authorize Controller。客户端必须将用户发送到OAuth服务器的authorizeURL。

首先,将用户重定向到以下URL:

文本
https://api.mysite.com/authorize?response_type=code&client_id=TestClient&redirect_uri=https://myredirecturi.com/cb

成功的授权将通过提供的redirect_uri将URL中的授权代码传递给客户端:

文本
https://myredirecturi.com/cb?code=SplxlOBeZQQYbYS6WxSbIA&state=xyz

完成此操作后,可以使用授权码请求令牌。

文本
$ curl -u TestClient:TestSecret https://api.mysite.com/token -d 'grant_type=authorization_code&code=xyz'

成功的令牌请求将返回JSON格式的标准访问令牌:

json
{
"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null}

含蓄

概观

Implicit补助类型类似于授权码交付式,它用于请求代表其他用户的访问受保护的资源(即第三方)。它针对公共客户端进行了优化,例如在JavaScript或移动设备上实现的客户端凭证无法存储的公共客户端。

用例

  • 代表第三方来电
  • 对于基于浏览器的应用程序(javscript)
  • 对于本地应用程序(桌面和移动设备)
  • 对于不能安全存储客户端证书的任何应用程序

履行

在创建服务器时,只需配置服务器以允许隐式授权类型

// create a storage object for your server$storage = new OAuth2\Storage\Pdo(array('dsn' => 'mysql:dbname=my_oauth2_db;host=localhost', 'username' => 'root', 'password' => '')); // create the server, and configure it to allow implicit $server = new OAuth2\Server($storage, array( 'allow_implicit' => true, ));

这允许Authorize Controller直接从请求返回访问令牌到服务器authorize端点。

示例请求

当使用隐式授权类型时,令牌使用 Authorize Controller。客户端通过response_type=token在OAuth服务器的“授权”端点中设置querystring参数来指定授权类型。

首先,将用户重定向到以下URL:

文本
https://api.mysite.com/authorize?response_type=token&client_id=TestClient&redirect_uri=https://myredirecturi.com/cb

一个成功的令牌请求将被返回到URL的片段中:

文本
https://myredirecturi.com/cb#access_token=2YotnFZFEjr1zCsicMWpAA&state=xyz&token_type=bearer&expires_in=3600

演示

 

用户凭证

概观

User Credentials当用户具有与所述客户端的可信关系交付式(又名资源所有者密码凭证)被使用,并且因此可以直接供应的凭证。

用例

  • 当客户希望显示登录表单时
  • 对于由资源服务器拥有和运营的应用程序(例如移动或桌面应用程序)
  • 对于远离使用直接认证和存储凭证的应用程序

履行

创建一个实例OAuth2\GrantType\UserCredentials并将其添加到您的服务器

// create some users in memory$users = array('bshaffer' => array('password' => 'brent123', 'first_name' => 'Brent', 'last_name' => 'Shaffer')); // create a storage object $storage = new OAuth2\Storage\Memory(array('user_credentials' => $users)); // create the grant type $grantType = new OAuth2\GrantType\UserCredentials($storage); // add the grant type to your OAuth server $server->addGrantType($grantType);

注意:用户存储对于每个应用程序都是高度自定义的,因此强烈建议您使用自己的存储 OAuth2\Storage\UserCredentialsInterface

示例请求

直接发送用户凭证来接收访问令牌:

文本
$ curl -u TestClient:TestSecret https://api.mysite.com/token -d 'grant_type=password&username=bshaffer&password=brent123'

如果您的客户端是public(默认情况下,没有秘密与存储中的客户端相关联),则可以省略client_secret请求中的值:

文本
$ curl https://api.mysite.com/token -d 'grant_type=password&client_id=TestClient&username=bshaffer&password=brent123'

成功的令牌请求将返回JSON格式的标准访问令牌:

json
{
"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null}

刷新令牌

概观

所述Refresh Token许可类型用于为了延长用户的资源的客户端的授权,以获得额外的访问令牌。

用例

  • 允许客户长时间访问用户的资源
  • 为单独的资源调用检索相同或较小范围的附加标记

履行

创建一个实例OAuth2\GrantType\RefreshToken并将其添加到您的服务器

// create a storage object to hold refresh tokens$storage = new OAuth2\Storage\Pdo(array('dsn' => 'sqlite:refreshtokens.sqlite')); // create the grant type $grantType = new OAuth2\GrantType\RefreshToken($storage); // add the grant type to your OAuth server $server->addGrantType($grantType);

注意:刷新令牌仅在使用Authorization CodeUser Credentials授予类型检索令牌时才提供 。

注意:刷新标记只有在存储实现OAuth2\Storage\RefreshTokenInterface提供给你的实例时才会被返回OAuth2\Server

组态

刷新令牌授予类型具有以下配置:

  • always_issue_new_refresh_token
    • 是否在成功的令牌请求时发出新的刷新令牌
    • 默认:false

例如:

// the refresh token grant request will have a "refresh_token" field// with a new refresh token on each request$grantType = new OAuth2\GrantType\RefreshToken($storage, array( 'always_issue_new_refresh_token' => true ));

访问令牌返回类型具有以下配置:

  • refresh_token_lifetime
    • 刷新令牌到期之前的时间
    • 默认:1209600(14天)

例如:

// the refresh tokens now last 28 days$accessToken = new OAuth2\ResponseType\AccessToken($accessStorage, $refreshStorage, array( 'refresh_token_lifetime' => 2419200, )); $server = new OAuth2\Server($storage, $config, $grantType, array($accessToken));

但是,当使用服务器的配置数组创建服务器时,可以发送这两个配置选项:

$server = new OAuth2\Server($storage, array( 'always_issue_new_refresh_token' => true, 'refresh_token_lifetime' => 2419200, ));

示例请求

首先,必须使用Authorizaton Code或User Credentials授权类型来检索刷新令牌:

文本
$ curl -u TestClient:TestSecret https://api.mysite.com/token -d 'grant_type=password&username=bshaffer&password=brent123'

访问令牌将包含一个刷新令牌:

json
{    "access_token":"2YotnFZFEjr1zCsicMWpAA", "expires_in":3600, "token_type": "bearer", "scope":null, "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA", }

这个刷新令牌可以用来生成一个等于或小于范围的新访问令牌:

文本
$ curl -u TestClient:TestSecret https://api.mysite.com/token -d 'grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA'

成功的令牌请求将返回JSON格式的标准访问令牌:

json
{
"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null}

如果服务器配置为始终发出一个新的刷新令牌,那么刷新令牌也会随着此响应返回:

json
{
"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null,"refresh_token":"s6BhdRkqt303807bdf6c78"}

智威汤逊旗手

概观

所述JWT Bearer许可类型用于当客户端想要而不发送敏感信息,如客户端秘密来接收访问令牌。这也可以与受信任的客户端一起使用,以在没有用户授权的情况下访问用户资源。

用例

  • 与客户端证书授权类型相同的好处
  • 允许在不传输证书的情况下进行安全呼叫
  • 对于可信的客户端,允许访问用户资源而不授权

履行

创建一个实例OAuth2\GrantType\JwtBearer并将其添加到您的服务器:

// load public key from keystore$public_key = file_get_contents('id_rsa.pub'); // assign the public key to a client and user $clientKeys = array('TestClient' => array('subject' => 'User1', 'key' => $public_key)); // create a storage object $storage = new OAuth2\Storage\Memory(array('jwt' => $clientKeys)); // specify your audience (typically, the URI of the oauth server) $audience = 'https://api.mysite.com'; // create the grant type $grantType = new OAuth2\GrantType\JwtBearer($storage, $audience); // add the grant type to your OAuth server $server->addGrantType($grantType);

示例请求

JWT请求需要使用来签署JWT断言 。下面的代码片段提供了一个如何完成的例子。

/** * Generate a JWT * * @param $privateKey The private key to use to sign the token * @param $iss The issuer, usually the client_id * @param $sub The subject, usually a user_id * @param $aud The audience, usually the URI for the oauth server * @param $exp The expiration date. If the current time is greater than the exp, the JWT is invalid * @param $nbf The "not before" time. If the current time is less than the nbf, the JWT is invalid * @param $jti The "jwt token identifier", or nonce for this JWT * * @return string */function generateJWT($privateKey, $iss, $sub, $aud, $exp = null, $nbf = null, $jti = null) { if (!$exp) { $exp = time() + 1000; } $params = array( 'iss' => $iss, 'sub' => $sub, 'aud' => $aud, 'exp' => $exp, 'iat' => time(), ); if ($nbf) { $params['nbf'] = $nbf; } if ($jti) { $params['jti'] = $jti; } $jwtUtil = new OAuth2\Encryption\Jwt(); return $jwtUtil->encode($params, $privateKey, 'RS256'); }

注意:本示例使用OAuth2\Encryption\Jwt此库中提供的类。这对于JWT身份验证不是必需的,但是方便。

然后可以调用该函数来为请求生成负载。编写一个脚本来生成jwt并请求一个令牌:

$private_key = file_get_contents('id_rsa'); $client_id = 'TestClient'; $user_id = 'User1'; $grant_type = 'urn:ietf:params:oauth:grant-type:jwt-bearer'; $jwt = generateJWT($private_key, $client_id, $user_id, 'https://api.mysite.com'); passthru("curl https://api.mysite.com/token -d 'grant_type=$grant_type&assertion=$jwt'");

成功的令牌请求将返回JSON格式的标准访问令牌:

json
{
"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null}
 
 

转载地址:http://xildl.baihongyu.com/

你可能感兴趣的文章
Linux自动执行sh脚本
查看>>
普通rgb转换为16进制
查看>>
MCU多串口收发
查看>>
Linux用户及用户组管理
查看>>
py 的 第 25 天
查看>>
Python随笔11
查看>>
Python随笔3
查看>>
Spring 定时执行任务
查看>>
ARTS打卡计划第三周-Review
查看>>
jQuery validation
查看>>
JavaScript知识架构学习路径(一)- 变量篇
查看>>
组合·莫比乌斯函数
查看>>
MySQL左右连接查询中的NULL的数据筛选问题
查看>>
在Linux下运行引入了外部jar包的java程序
查看>>
段错误 核心已转储尝试解决
查看>>
正则表达式
查看>>
第四次软工作业-代码规范
查看>>
USACO Section 1.5 Number Triangles
查看>>
酷派大神 F1 连接调试,日志获取的方法
查看>>
20050425:公测啊,晚点再说
查看>>