Closed dungphanxuan closed 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?
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;
}
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.
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
@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?!
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
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.
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
Hello, i try to call metho findIdentityByAccessToken but it return null, How can i fix it. thank.