Rework identity provider for the legacy OAuth2 tokens [skip ci]

This commit is contained in:
ErickSkrauch
2019-09-22 18:42:21 +03:00
parent c722c46ad5
commit cf62c686b1
10 changed files with 141 additions and 284 deletions

View File

@@ -8,19 +8,24 @@ use yii\web\UnauthorizedHttpException;
class IdentityFactory {
/**
* @throws UnauthorizedHttpException
* @param string $token
* @param string $type
*
* @return IdentityInterface
* @throws UnauthorizedHttpException
*/
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface {
if (empty($token)) {
throw new UnauthorizedHttpException('Incorrect token');
if (!empty($token)) {
if (mb_strlen($token) === 40) {
return LegacyOAuth2Identity::findIdentityByAccessToken($token, $type);
}
if (substr_count($token, '.') === 2) {
return JwtIdentity::findIdentityByAccessToken($token, $type);
}
}
if (substr_count($token, '.') === 2) {
return JwtIdentity::findIdentityByAccessToken($token, $type);
}
return OAuth2Identity::findIdentityByAccessToken($token, $type);
throw new UnauthorizedHttpException('Incorrect token');
}
}