bgi-online/yii2-oauth2-server

PHP的OAuth2服务器

安装: 23

依赖项: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 167

类型:yii2-extension

2.0.1 2015-06-19 12:30 UTC

This package is not auto-updated.

Last update: 2024-09-29 01:48:12 UTC


README

OAuth2服务器的包装器(https://github.com/bshaffer/oauth2-server-php

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist filsh/yii2-oauth2-server "*"

"filsh/yii2-oauth2-server": "~2.0"

将以下内容添加到你的 composer.json 文件的 require 部分。

要使用此扩展,只需将以下代码添加到你的应用程序配置中,作为新的模块

'modules'=>[
        //other modules .....
        'oauth2' => [
            'class' => 'filsh\yii2\oauth2server\Module',            
            'tokenParamName' => 'accessToken',
            'tokenAccessLifetime' => 3600 * 24,
            'storageMap' => [
                'user_credentials' => 'app\models\User',
            ],
            'grantTypes' => [
                'user_credentials' => [
                    'class' => 'OAuth2\GrantType\UserCredentials',
                ],
                'refresh_token' => [
                    'class' => 'OAuth2\GrantType\RefreshToken',
                    'always_issue_new_refresh_token' => true
                ]
            ]
        ]
    ],

如果你想要获取Json Web Token (JWT) 而不是传统令牌,你需要在模块中设置 'useJwtToken' => true,然后定义两个更多的配置:'public_key' => 'app\storage\PublicKeyStorage',这是实现 PublickKeyInterface 的类,以及 'access_token' => 'app\storage\JwtAccessToken',它实现了 JwtAccessTokenInterface.php

OAuth2基本库提供了默认的 access_token,它工作得很好,除了它会尝试将令牌保存到数据库中。因此,我决定从它继承并覆盖尝试保存的部分(令牌大小太大,在数据库中的 VARCHAR(40) 上崩溃)。

TL;DR,以下是一些示例类 access_token

<?php

namespace app\storage;

/**
 *
 * @author Stefano Mtangoo <mwinjilisti at gmail dot com>
 */
class JwtAccessToken extends \OAuth2\Storage\JwtAccessToken
{  
    public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
    {
         
    }

    public function unsetAccessToken($access_token)
    {
        
    } 
}

public_key

<?php
namespace app\storage;

class PublicKeyStorage implements \OAuth2\Storage\PublicKeyInterface{


    private $pbk =  null;
    private $pvk =  null; 
    
    public function __construct()
    {
        //files should be in same directory as this file
        //keys can be generated using OpenSSL tool with command: 
        /*
          private key:
          openssl genrsa -out privkey.pem 2048

          public key:
          openssl rsa -in privkey.pem -pubout -out pubkey.pem
        */
        $this->pbk =  file_get_contents('privkey.pem', true); 
        $this->pvk =  file_get_contents('pubkey.pem', true); 
    }

    public function getPublicKey($client_id = null){ 
        return  $this->pbk;
    }

    public function getPrivateKey($client_id = null){ 
        return  $this->pvk;
    }

    public function getEncryptionAlgorithm($client_id = null){
        return 'HS256';
    }

}

注意:你需要应用 这个 PR,或者你可以通过检查 这个diff 来自己修复它。PR的其余部分只是如果你想要使用firebase JWT库(这并不是强制的)。

此外,扩展 common\models\User - 用户模型 - 实现 \OAuth2\Storage\UserCredentialsInterface 接口,以便将oauth2凭证数据存储在用户表中。你应该实现

  • findIdentityByAccessToken()
  • checkUserCredentials()
  • getUserDetails()

如果你更喜欢扩展模型,请这样做(请记住更新配置文件)

use Yii;

class User extends common\models\User implements \OAuth2\Storage\UserCredentialsInterface
{

    /**
     * Implemented for Oauth2 Interface
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        /** @var \filsh\yii2\oauth2server\Module $module */
        $module = Yii::$app->getModule('oauth2');
        $token = $module->getServer()->getResourceController()->getToken();
        return !empty($token['user_id'])
                    ? static::findIdentity($token['user_id'])
                    : null;
    }

    /**
     * Implemented for Oauth2 Interface
     */
    public function checkUserCredentials($username, $password)
    {
        $user = static::findByUsername($username);
        if (empty($user)) {
            return false;
        }
        return $user->validatePassword($password);
    }

    /**
     * Implemented for Oauth2 Interface
     */
    public function getUserDetails($username)
    {
        $user = static::findByUsername($username);
        return ['user_id' => $user->getId()];
    }
}

下一步你应该运行迁移

yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/migrations

这个迁移创建oauth2数据库架构并插入测试用户凭证 testclient:testpass 用于 http://fake/

向urlManager添加URL规则

'urlManager' => [
    'enablePrettyUrl' => true, //only if you want to use petty URLs
    'rules' => [
        'POST oauth2/<action:\w+>' => 'oauth2/rest/<action>',
        ...
    ]
]

使用方法

要使用此扩展,只需添加基础控制器的行为

use yii\helpers\ArrayHelper;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
use filsh\yii2\oauth2server\filters\ErrorToExceptionFilter;
use filsh\yii2\oauth2server\filters\auth\CompositeAuth;

class Controller extends \yii\rest\Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' => CompositeAuth::className(),
                'authMethods' => [
                    ['class' => HttpBearerAuth::className()],
                    ['class' => QueryParamAuth::className(), 'tokenParam' => 'accessToken'],
                ]
            ],
            'exceptionFilter' => [
                'class' => ErrorToExceptionFilter::className()
            ],
        ]);
    }
}

获取访问令牌(JavaScript示例)

var url = window.location.host + "/oauth2/token";
var data = {
    'grant_type':'password',
    'username':'<some login from your user table>',
    'password':'<real pass>',
    'client_id':'testclient',
    'client_secret':'testpass'
};
//ajax POST `data` to `url` here
//

更多信息,请参阅 https://github.com/bshaffer/oauth2-server-php