授權碼
概觀
在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服務器的authorize
URL。
首先,將用戶重定向到以下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格式的標準訪問令牌:
{"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格式的標準訪問令牌:
{"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'
訪問令牌將包含一個刷新令牌:
{"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格式的標準訪問令牌:
{"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null}
如果服務器配置為始終發出一個新的刷新令牌,那么刷新令牌也會隨著此響應返回:
{"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格式的標準訪問令牌:
{"access_token":"03807cb390319329bdf6c777d4dfae9c0d3b3c35","expires_in":3600,"token_type":"bearer","scope":null}