ikaras / yii2-oauth2-rest-template

Yii2 Rest APi Template with OAuth2 server
MIT License
95 stars 27 forks source link

method "findIdentityByAccessToken" return null #4

Closed dungphanxuan closed 9 years ago

dungphanxuan commented 9 years ago

Hello, i try to call metho findIdentityByAccessToken but it return null, How can i fix it. thank.

ikaras commented 9 years ago

Hi. First, thanks for interesting and issue! I need more information: sequence of operations what you had made to get null result. Have you get access token as described (by making request to ) and did you use it on your new request to controller?

dungphanxuan commented 9 years ago

Hi @ikaras . I overide findIdentityByAccessToken method with code and success.

 /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        /** @var \filsh\yii2\oauth2server\Module $module */

        $token = OauthAccessTokens::findOne(['access_token' => $token]);
        //check access token exprirest
        return !empty($token['user_id'])
            ? static::findIdentity($token['user_id'])
            : null;

    }
oscar-78 commented 9 years ago

Hi @dungphanxuan, your function will work. You can also use the below function as used in the template of @ikaras: https://github.com/ikaras/yii2-oauth2-rest-template/blob/master/application/api/models/User.php

    /**
     * @inheritdoc
     */
    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;
    }

Perhaps public static function findIdentityByOauth2() would be a better function declaration.

dungphanxuan commented 9 years ago

Thank @oscar-78 . My app is custom of @ikaras repository, so findIdentityByAccessToken of this repository not work.

Ah, i can't see method to check token is expires. I try to call api when token range to expires. But api also call. Thank

ikaras commented 9 years ago

@oscar-78, about:

Perhaps public static function findIdentityByOauth2() would be a better function declaration.

Good idea and yii2 gives you ability to do such, but findIdentityByAccessToken is described on IdentityInterface and using on \yii\web\User::loginByAccessToken (which called from the Authorization filter) - so it's system's name.

If you has several authorization systems based on tokens, probably, better to use second parameter $type and process it on your Identity class?!

zenus007 commented 8 years ago

findIdentityByAccessToken has error. Try to get var_dump($token['user_id']), it's return two value first return null, second value is user_id. So !empty($token['user_id']) always return false

mahendran-sakkarai commented 8 years ago

I got into a similar issue. I'm using "filsh/yii2-oauth2-server": "2.0.1".

I fixed the issue using this post posted by @fonemi.

I added the content below for reference:

Here is my User::findIdentityByAccessToken method code:

public static function findIdentityByAccessToken($token, $type = null) {
        $retval = null;

        $oauthServer = Yii::$app->getModule('oauth2')->getServer();
        $oauthRequest = Yii::$app->getModule('oauth2')->getRequest();

        $oauthServer->verifyResourceRequest($oauthRequest);

        $token = $oauthServer->getAccessTokenData($oauthRequest);
        $retval = self::findOne($token['user_id']);

        return $retval;
    }

Inspired by yii2-oauth2-server/filters/auth/CompositeAuth.php.

You should change use filsh\yii2\oauth2server\filters\auth\CompositeAuth; to use yii\filters\auth\CompositeAuth; (to default one) in your controllers' code in order to avoid double verification.

Maybe it helps.

froying commented 7 years ago

Because in the bearer there is the $ headers = $ request-> headers ('AUTHORIZATION'); ok apache or .htaccess already have this set CGIPassAuth on,sorry my bad english