2019-08-01 14:47:12 +05:30
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace api\components\User;
|
|
|
|
|
|
|
|
use yii\web\UnauthorizedHttpException;
|
|
|
|
|
|
|
|
class IdentityFactory {
|
|
|
|
|
|
|
|
/**
|
2019-09-22 21:12:21 +05:30
|
|
|
* @param string $token
|
|
|
|
* @param string $type
|
|
|
|
*
|
2019-08-01 14:47:12 +05:30
|
|
|
* @return IdentityInterface
|
2019-09-22 21:12:21 +05:30
|
|
|
* @throws UnauthorizedHttpException
|
2019-08-01 14:47:12 +05:30
|
|
|
*/
|
|
|
|
public static function findIdentityByAccessToken($token, $type = null): IdentityInterface {
|
2019-09-22 21:12:21 +05:30
|
|
|
if (!empty($token)) {
|
|
|
|
if (mb_strlen($token) === 40) {
|
|
|
|
return LegacyOAuth2Identity::findIdentityByAccessToken($token, $type);
|
|
|
|
}
|
2019-08-01 14:47:12 +05:30
|
|
|
|
2019-09-22 21:12:21 +05:30
|
|
|
if (substr_count($token, '.') === 2) {
|
|
|
|
return JwtIdentity::findIdentityByAccessToken($token, $type);
|
|
|
|
}
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
2019-09-22 21:12:21 +05:30
|
|
|
throw new UnauthorizedHttpException('Incorrect token');
|
2019-08-01 14:47:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|