oauth2-server-php-docs 授權類型

授權碼

概觀

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許可類型用于當客戶端想要而不發送敏感信息,如客戶端秘密來接收訪問令牌。這也可以與受信任的客戶端一起使用,以在沒有用戶授權的情況下訪問用戶資源。

關于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}













轉載于:https://www.cnblogs.com/endv/p/7842516.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390813.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390813.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390813.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

flask框架視圖和路由_角度視圖,路由和NgModule的解釋

flask框架視圖和路由Angular vs AngularJS (Angular vs AngularJS) AngularJS (versions 1.x) is a JavaScript-based open source framework. It is cross platform and is used to develop Single Page Web Application (SPWA). AngularJS(版本1.x)是一個基于JavaScript的開源…

NGUI EventDelagate事件委托

using System.Collections; using System.Collections.Generic; using UnityEngine;public class BUttonClick : MonoBehaviour {public UIButton button_01;void Start(){if (button_01 null){Debug.Log("button組件丟失了");}else{//首先將腳本中的ClicktheButton…

leetcode 461. 漢明距離(位運算)

兩個整數之間的漢明距離指的是這兩個數字對應二進制位不同的位置的數目。 給出兩個整數 x 和 y&#xff0c;計算它們之間的漢明距離。 注意&#xff1a; 0 ≤ x, y < 231. 示例:輸入: x 1, y 4輸出: 2解釋: 1 (0 0 0 1) 4 (0 1 0 0)↑ ↑上面的箭頭指出了對應二進…

圖深度學習-第2部分

有關深層學習的FAU講義 (FAU LECTURE NOTES ON DEEP LEARNING) These are the lecture notes for FAU’s YouTube Lecture “Deep Learning”. This is a full transcript of the lecture video & matching slides. We hope, you enjoy this as much as the videos. Of cou…

Linux下 安裝Redis并配置服務

一、簡介 1、 Redis為單進程單線程模式&#xff0c;采用隊列模式將并發訪問變成串行訪問。 2、 Redis不僅僅支持簡單的k/v類型的數據&#xff0c;同時還提供list&#xff0c;set&#xff0c;zset&#xff0c;hash等數據結構的存儲。 3、 Redis支持數據的備份&#xff0c;即mas…

大omega記號_什么是大歐米茄符號?

大omega記號Similar to big O notation, big Omega(Ω) function is used in computer science to describe the performance or complexity of an algorithm.與大O表示法相似&#xff0c;大Omega(Ω)函數在計算機科學中用于描述算法的性能或復雜性。 If a running time is Ω…

leetcode 477. 漢明距離總和(位運算)

theme: healer-readable 題目 兩個整數的 漢明距離 指的是這兩個數字的二進制數對應位不同的數量。 計算一個數組中&#xff0c;任意兩個數之間漢明距離的總和。 示例: 輸入: 4, 14, 2 輸出: 6 解釋: 在二進制表示中&#xff0c;4表示為0100&#xff0c;14表示為1110&…

什么是跨域及跨域請求資源的方法?

1、由于瀏覽器同源策略&#xff0c;凡是發送請求url的協議、域名、端口三者之間任意一與當前頁面地址不同即為跨域。 2、跨域請求資源的方法&#xff1a; (1)、porxy代理(反向服務器代理) 首先將用戶發送的請求發送給中間的服務器&#xff0c;然后通過中間服務器再發送給后臺服…

量子信息與量子計算_量子計算為23美分。

量子信息與量子計算On Aug 13, 2020, AWS announced the General Availability of Amazon Braket. Braket is their fully managed quantum computing service. It is available on an on-demand basis, much like SageMaker. That means the everyday developer and data scie…

全面理解Java內存模型

Java內存模型即Java Memory Model&#xff0c;簡稱JMM。JMM定義了Java 虛擬機(JVM)在計算機內存(RAM)中的工作方式。JVM是整個計算機虛擬模型&#xff0c;所以JMM是隸屬于JVM的。 如果我們要想深入了解Java并發編程&#xff0c;就要先理解好Java內存模型。Java內存模型定義了多…

React Native指南

React本機 (React Native) React Native is a cross-platform framework for building mobile applications that can run outside of the browser?—?most commonly iOS and Android applicationsReact Native是一個跨平臺框架&#xff0c;用于構建可在瀏覽器外部運行的移動…

leetcode 1074. 元素和為目標值的子矩陣數量(map+前綴和)

給出矩陣 matrix 和目標值 target&#xff0c;返回元素總和等于目標值的非空子矩陣的數量。 子矩陣 x1, y1, x2, y2 是滿足 x1 < x < x2 且 y1 < y < y2 的所有單元 matrix[x][y] 的集合。 如果 (x1, y1, x2, y2) 和 (x1’, y1’, x2’, y2’) 兩個子矩陣中部分坐…

失物招領php_新奧爾良圣徒隊是否增加了失物招領?

失物招領phpOver the past couple of years, the New Orleans Saints’ offense has been criticized for its lack of wide receiver options. Luckily for Saints’ fans like me, this area has been addressed by the signing of Emmanuel Sanders back in March — or has…

教你分分鐘使用Retrofit+Rxjava實現網絡請求

擼代碼之前&#xff0c;先簡單了解一下為什么Retrofit這么受大家青睞吧。 Retrofit是Square公司出品的基于OkHttp封裝的一套RESTful&#xff08;目前流行的一套api設計的風格&#xff09;網絡請求框架。它內部使用了大量的設計模式&#xff0c;以達到高度解耦的目的&#xff1b…

線程與進程區別

一.定義&#xff1a; 進程&#xff08;process&#xff09;是一塊包含了某些資源的內存區域。操作系統利用進程把它的工作劃分為一些功能單元。 進程中所包含的一個或多個執行單元稱為線程&#xff08;thread&#xff09;。進程還擁有一個私有的虛擬地址空間&#xff0c;該空間…

基本SQL命令-您應該知道的數據庫查詢和語句列表

SQL stands for Structured Query Language. SQL commands are the instructions used to communicate with a database to perform tasks, functions, and queries with data.SQL代表結構化查詢語言。 SQL命令是用于與數據庫通信以執行任務&#xff0c;功能和數據查詢的指令。…

leetcode 5756. 兩個數組最小的異或值之和(狀態壓縮dp)

題目 給你兩個整數數組 nums1 和 nums2 &#xff0c;它們長度都為 n 。 兩個數組的 異或值之和 為 (nums1[0] XOR nums2[0]) (nums1[1] XOR nums2[1]) … (nums1[n - 1] XOR nums2[n - 1]) &#xff08;下標從 0 開始&#xff09;。 比方說&#xff0c;[1,2,3] 和 [3,2,1…

客戶細分模型_Avarto金融解決方案的客戶細分和監督學習模型

客戶細分模型Lets assume that you are a CEO of a company which have some X amount of customers in a city with 1000 *X population. Analyzing the trends/features of your customer and segmenting the population of the city to land new potential customers would …

用 Go 編寫一個簡單的 WebSocket 推送服務

用 Go 編寫一個簡單的 WebSocket 推送服務 本文中代碼可以在 github.com/alfred-zhon… 獲取。 背景 最近拿到需求要在網頁上展示報警信息。以往報警信息都是通過短信&#xff0c;微信和 App 推送給用戶的&#xff0c;現在要讓登錄用戶在網頁端也能實時接收到報警推送。 依稀記…

leetcode 231. 2 的冪

給你一個整數 n&#xff0c;請你判斷該整數是否是 2 的冪次方。如果是&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 如果存在一個整數 x 使得 n 2x &#xff0c;則認為 n 是 2 的冪次方。 示例 1&#xff1a; 輸入&#xff1a;n 1 輸出&#xff1a;tr…