• oauth2-server-php-docs 授权类型


    授权码

    概观

    Authorization Code交付式时使用的客户端想要请求访问受保护资源代表其他用户(即第三方)。这是最常与OAuth关联的授予类型。

    详细了解授权码

    用例

    • 代表第三方来电

    履行

    创建一个实例OAuth2GrantTypeAuthorizationCode并将其添加到您的服务器

    // create a storage object to hold new authorization codes
    $storage = new OAuth2StoragePdo(array('dsn' => 'sqlite:authcodes.sqlite'));
    
    // create the grant type
    $grantType = new OAuth2GrantTypeAuthorizationCode($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 OAuth2StoragePdo(array('dsn' => 'mysql:dbname=my_oauth2_db;host=localhost', 'username' => 'root', 'password' => ''));
    
    // create the server, and configure it to allow implicit
    $server = new OAuth2Server($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当用户具有与所述客户端的可信关系交付式(又名资源所有者密码凭证)被使用,并且因此可以直接供应的凭证。

    详细了解用户凭证

    用例

    • 当客户希望显示登录表单时
    • 对于由资源服务器拥有和运营的应用程序(例如移动或桌面应用程序)
    • 对于远离使用直接认证和存储凭证的应用程序

    履行

    创建一个实例OAuth2GrantTypeUserCredentials并将其添加到您的服务器

    // create some users in memory
    $users = array('bshaffer' => array('password' => 'brent123', 'first_name' => 'Brent', 'last_name' => 'Shaffer'));
    
    // create a storage object
    $storage = new OAuth2StorageMemory(array('user_credentials' => $users));
    
    // create the grant type
    $grantType = new OAuth2GrantTypeUserCredentials($storage);
    
    // add the grant type to your OAuth server
    $server->addGrantType($grantType);

    注意:用户存储对于每个应用程序都是高度自定义的,因此强烈建议您使用自己的存储 OAuth2StorageUserCredentialsInterface

    示例请求

    直接发送用户凭证来接收访问令牌:

    文本
    $ 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许可类型用于为了延长用户的资源的客户端的授权,以获得额外的访问令牌。

    阅读更多关于刷新令牌的信息

    用例

    • 允许客户长时间访问用户的资源
    • 为单独的资源调用检索相同或较小范围的附加标记

    履行

    创建一个实例OAuth2GrantTypeRefreshToken并将其添加到您的服务器

    // create a storage object to hold refresh tokens
    $storage = new OAuth2StoragePdo(array('dsn' => 'sqlite:refreshtokens.sqlite'));
    
    // create the grant type
    $grantType = new OAuth2GrantTypeRefreshToken($storage);
    
    // add the grant type to your OAuth server
    $server->addGrantType($grantType);

    注意:刷新令牌仅在使用Authorization CodeUser Credentials授予类型检索令牌时才提供 

    注意:刷新标记只有在存储实现OAuth2StorageRefreshTokenInterface提供给你的实例时才会被返回OAuth2Server

    组态

    刷新令牌授予类型具有以下配置:

    • 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 OAuth2GrantTypeRefreshToken($storage, array(
        'always_issue_new_refresh_token' => true
    ));

    访问令牌返回类型具有以下配置:

    • refresh_token_lifetime
      • 刷新令牌到期之前的时间
      • 默认:1209600(14天)

    例如:

    // the refresh tokens now last 28 days
    $accessToken = new OAuth2ResponseTypeAccessToken($accessStorage, $refreshStorage, array(
        'refresh_token_lifetime' => 2419200,
    ));
    
    $server = new OAuth2Server($storage, $config, $grantType, array($accessToken));

    但是,当使用服务器的配置数组创建服务器时,可以发送这两个配置选项:

    $server = new OAuth2Server($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载体

    用例

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

    履行

    创建一个实例OAuth2GrantTypeJwtBearer并将其添加到您的服务器:

    // 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 OAuth2StorageMemory(array('jwt' => $clientKeys));
    
    // specify your audience (typically, the URI of the oauth server)
    $audience = 'https://api.mysite.com';
    
    // create the grant type
    $grantType = new OAuth2GrantTypeJwtBearer($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 OAuth2EncryptionJwt();
    
        return $jwtUtil->encode($params, $privateKey, 'RS256');
    }

    注意:本示例使用OAuth2EncryptionJwt此库中提供类。这对于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}













     
  • 相关阅读:
    洛谷P4175 网络管理
    洛谷P2605 基站选址
    洛谷P3723 礼物
    bzoj3771 Triple
    洛谷P3321 序列统计
    bzoj2194 快速傅里叶之二
    1109课堂内容整理
    响应式网页
    表单隐藏域有什么作用?
    1106课堂笔记
  • 原文地址:https://www.cnblogs.com/endv/p/7842516.html
Copyright © 2020-2023  润新知