西安知名网站建设公司,百度网页版微信,广东企业网站建设,重庆招聘网官网授权码 概观 在Authorization Code交付式时使用的客户端想要请求访问受保护资源代表其他用户#xff08;即第三方#xff09;。这是最常与OAuth关联的授予类型。 详细了解授权码 用例 代表第三方来电履行 创建一个实例OAuth2\GrantType\AuthorizationCode并将其添加到您的服务…授权码 概观 在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_typecodeclient_idTestClientredirect_urihttps://myredirecturi.com/cb 成功的授权将通过提供的redirect_uri将URL中的授权代码传递给客户端 文本 https://myredirecturi.com/cb?codeSplxlOBeZQQYbYS6WxSbIAstatexyz 完成此操作后可以使用授权码请求令牌。 文本 $ curl -u TestClient:TestSecret https://api.mysite.com/token -d grant_typeauthorization_codecodexyz 成功的令牌请求将返回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:dbnamemy_oauth2_db;hostlocalhost, 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_typetoken在OAuth服务器的“授权”端点中设置querystring参数来指定授权类型。 首先将用户重定向到以下URL 文本 https://api.mysite.com/authorize?response_typetokenclient_idTestClientredirect_urihttps://myredirecturi.com/cb 一个成功的令牌请求将被返回到URL的片段中 文本 https://myredirecturi.com/cb#access_token2YotnFZFEjr1zCsicMWpAAstatexyztoken_typebearerexpires_in3600 演示 请参阅隐式授予类型演示 用户凭证 概观 在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_typepasswordusernamebshafferpasswordbrent123 如果您的客户端是public默认情况下没有秘密与存储中的客户端相关联则可以省略client_secret请求中的值 文本 $ curl https://api.mysite.com/token -d grant_typepasswordclient_idTestClientusernamebshafferpasswordbrent123 成功的令牌请求将返回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_typeclient_credentials# using POST Body
$ curl https://api.mysite.com/token -d grant_typeclient_credentialsclient_idTestClientclient_secretTestSecret 成功的令牌请求将返回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 刷新令牌到期之前的时间默认120960014天例如 腓 // 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_typepasswordusernamebshafferpasswordbrent123 访问令牌将包含一个刷新令牌 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_typerefresh_tokenrefresh_tokentGzv3JOkF0XG5Qx2TlKWIA 成功的令牌请求将返回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_typeassertion$jwt); 成功的令牌请求将返回JSON格式的标准访问令牌 json {access_token:03807cb390319329bdf6c777d4dfae9c0d3b3c35,expires_in:3600,token_type:bearer,scope:null} 转载于:https://www.cnblogs.com/endv/p/7842516.html