授权码

概观

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}

客户端凭证

概观

Client Credentials当客户端请求其控制下访问受保护的资源授权类型被使用(即不存在第三方)。

详细了解客户端凭据

用例

  • 服务电话
  • 代表创建客户端的用户的呼叫。

履行

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

// create test clients in memory
$clients = array('TestClient' => array('client_secret' => 'TestSecret')); // create a storage object
$storage = new OAuth2\Storage\Memory(array('client_credentials' => $clients)); // create the grant type
$grantType = new OAuth2\GrantType\ClientCredentials($storage); // add the grant type to your OAuth server
$server->addGrantType($grantType);

组态

Client Credentials授权类型具有以下配置:

  • allow_credentials_in_request_body

    • 除了授权HTTP头之外,是否在POST主体中查找凭证
    • 默认值:true

例如:

// this request will only allow authorization via the Authorize HTTP Header (Http Basic)
$grantType = new OAuth2\GrantType\ClientCredentials($storage, array(
'allow_credentials_in_request_body' => false
));

示例请求

文本
# using HTTP Basic Authentication
$ curl -u TestClient:TestSecret https://api.mysite.com/token -d 'grant_type=client_credentials' # using POST Body
$ curl https://api.mysite.com/token -d 'grant_type=client_credentials&client_id=TestClient&client_secret=TestSecret'

成功的令牌请求将返回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许可类型用于当客户端想要而不发送敏感信息,如客户端秘密来接收访问令牌。这也可以与受信任的客户端一起使用,以在没有用户授权的情况下访问用户资源。

阅读更多关于jwt载体

用例

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

履行

创建一个实例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}



 

oauth2-server-php-docs 授权类型的更多相关文章

  1. 使用 OAuth2-Server-php 在 Yii 框架上搭建 OAuth2 Server

    原文转自 http://www.cnblogs.com/ldms/p/4565547.html Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后 ...

  2. 使用 OAuth2-Server-php 搭建 OAuth2 Server

    Yii 有很多 extension 可以使用,在查看了 Yii 官网上提供的与 OAuth 相关的扩展后,发现了几个 OAuth2 的客户端扩展,但是并没有找到可以作为 OAuth2 Server 的 ...

  3. OAuth2:隐式授权(Implicit Grant)类型的开放授权

    适用范围 仅需临时访问的场景 用户会定期在API提供者那里进行登录 OAuth客户端运行在浏览器中(Javascript.Flash等) 浏览器绝对可信,因为该类型可能会将访问令牌泄露给恶意用户或应用 ...

  4. OAuth2:客户端证书授权(Client Credentials)类型的开放授权

    适应范围 认证服务器不提供像用户数据这样的重要资源,仅仅是有限的只读资源或者一些开放的API.例如使用了第三方的静态文件服务,如Google Storage或Amazon S3.这样,你的应用需要通过 ...

  5. OAuth2.0 用户验证授权标准 理解

    OAuth2.0是一套标准. 一.问题 这个标准解决了这样的一个问题. 允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用.  ...

  6. 理解OAuth2.0协议和授权机制

    无论是自然资源还是互联网上的资源,需要控制使用权与被使用权,以保护资源的安全.合理的使用和有效的管控. 项目中,我们需要控制的是用户资源,既要保证有效用户的合理使用,又要防范非法用户的攻击.如此,如何 ...

  7. IdentityServer4【Topic】之授权类型

    Grant Types 授权类型 授权类型指出了一个客户端如何与IdentityServer进行交互.OpenID Conect和OAuth2.0定义了如下的授权类型: Implicit Author ...

  8. OAuth2.0认证和授权以及单点登录

    https://www.cnblogs.com/shizhiyi/p/7754721.html OAuth2.0认证和授权机制讲解 2017-10-30 15:33 by shizhiyi, 2273 ...

  9. CAS3.5.x(x>1)支持OAuth2 server

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

随机推荐

  1. 解决svn中文乱码的问题

    需要的工具:sqlitexiaz 工具下载: 链接:https://pan.baidu.com/s/1cz1Pvw 密码:yp64 1 首先在项目的根目录下,找到.svn(如果找不到,需要设置将隐藏文 ...

  2. [Go] sync.Once 的用法

    sync.Once.Do(f func()) 是一个非常有意思的东西,能保证 once 只执行一次,无论你是否更换 once.Do(xx) 这里的方法,这个 sync.Once块 只会执行一次. pa ...

  3. 网站前端优化技术 BigPipe分块处理技术

    前端优化已经到极致了么?业务还在为看到不停的而揪心么?还在为2秒率不达标苦恼么? 好吧我知道答案,大家一如既往的烦恼中... 那么接下来我们看看,facebook,淘宝,人人网,一淘都是怎么做前端优化 ...

  4. 计算机意外地重新启动或遇到错误。windows安装无法继续。若要安装windows 请单击 确定 重新启动计算机

    快安装完系统时遇到提示:计算机意外地重新启动或遇到错误.Windows 安装无法继续.若要安装Windows,请单击“确定”重新启动计算机,然后重新启动安装”.如下图所示: 解决办法: 当出现如上提示 ...

  5. Struts2标签的<s:set>标签与JSTL的<c:set>标签

    <s:set>标签 set标签 用于将某个值放入指定范围内.例如application.session范围等. 当某个值所在的对象图深度非常深时,例如如下:person.worker.wi ...

  6. Ubuntu64位下使用eclipse闪退的解决

    解决办法: 删除文件 [workspace]/.metadata/.plugins/org.eclipse.e4.workbench/workbench.xmi

  7. 《ASP.NET Web API 2框架揭秘》

    <ASP.NET Web API 2框架揭秘> 基本信息 作者: 蒋金楠 出版社:电子工业出版社 ISBN:9787121235368 上架时间:2014-7-5 出版日期:2014 年7 ...

  8. SVG.js 元素操作整理(一)

    一.属性操作Attributes var draw = SVG('svg1').size(300, 300); //attr() 属性操作 //设置属性的值 var rect = draw.rect( ...

  9. 零基础写python爬虫之使用Scrapy框架编写爬虫

    网络爬虫,是在网上进行数据抓取的程序,使用它能够抓取特定网页的HTML数据.虽然我们利用一些库开发一个爬虫程序,但是使用框架可以大大提高效率,缩短开发时间.Scrapy是一个使用Python编写的,轻 ...

  10. protobuf标准消息方法

    protobuf标准消息方法 1.标准消息方法 每个消息类包含一些其他方法允许你检查和控制整个消息,包括: · IsInitialized() :检查是否所有必须(required)字段都已经被赋值了 ...