Yii2 oauth2 client_credential令牌认证

时间:2016-05-23 07:56:01

标签: authentication oauth-2.0 yii2 yii2-validation

我使用Filsh / yii2-oauth2-server,

我可以使用$ _pos来访问我的API。

在控制器中:

if (!isset($_post['token'])){
//exeption
}
else{ 
token = $_post('token');
}
if ((Accesstokens::isAccessTokenValid($token))) {
// do some thing.
}
in my Accesstokens model :

public static function isAccessTokenValid($token)
    {
        if (empty($token)) {
            return false;
        }
        $query = (new \yii\db\Query())
            ->select(['access_token','expires'])
            ->from('oauth_access_tokens')
            ->where(['access_token' => $token])
//            ->limit(10)
            ->one();
        if (empty($query)) {
            return false;
        }
        $expire = $query['expires'];
        return $expire > date('Y-m-d h:i:s');
    } 

如果我使用密码(user_credential)令牌,我可以使用:

实现bearer auth
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()
            ],
        ]);
    }

但是这种方法在验证用户时使用用户表,而不是oauth_client表来验证没有用户的客户端。

如何在没有用户表的情况下仅在客户端表上验证令牌库?

Request:
GET http://myapi.com/api/www/index.php/oauth2/token
grand_type = client_credentials
client_id = id1
client_secret = secret1

Response:
"access_token": "thisistheccesstoken",
"expires_in": 31104000,
"token_type": "Bearer",
"scope": "default"

Request:
GET http://myapi.com/api/www/index.php/oauth2/v1/get/frogs
HEADER Authorization: Bearer thisistheaccesstoken

Response:
all the frogs

使用方法:HEADER授权:持票人?有人可以帮忙吗?

0 个答案:

没有答案