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}-
?
客戶端憑證
概觀
在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 Code或User 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
總結
以上是生活随笔為你收集整理的oauth2-server-php-docs 授权类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 标准C程序设计七---77
- 下一篇: Linux下 安装Redis并配置服务