TestsLing /
easy-im
| 1 | <?php |
||
| 2 | |||
| 3 | namespace EasyIM\TencentIM\Auth; |
||
| 4 | |||
| 5 | use EasyIM\Kernel\AccessToken as BaseAccessToken; |
||
| 6 | use EasyIM\Kernel\Events\AccessTokenRefreshed; |
||
| 7 | use Psr\Http\Message\RequestInterface; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Class AccessToken |
||
| 11 | * |
||
| 12 | * @package EasyIM\TencentIM\Auth |
||
| 13 | * @author longing <[email protected]> |
||
| 14 | */ |
||
| 15 | class AccessToken extends BaseAccessToken |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $tokenKey = 'usersig'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | protected $cachePrefix = 'EasyIM.kernel.usersig.'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param bool $refresh |
||
| 29 | * |
||
| 30 | * @return array |
||
| 31 | * |
||
| 32 | * @throws \EasyIM\Kernel\Exceptions\HttpException |
||
| 33 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 34 | * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException |
||
| 35 | * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
||
| 36 | * @throws \EasyIM\Kernel\Exceptions\RuntimeException |
||
| 37 | */ |
||
| 38 | public function getToken(bool $refresh = false): array |
||
| 39 | { |
||
| 40 | $cacheKey = $this->getCacheKey(); |
||
| 41 | $cache = $this->getCache(); |
||
| 42 | |||
| 43 | if (!$refresh && $cache->has($cacheKey)) { |
||
| 44 | /** @var array $sign */ |
||
| 45 | $sign = $cache->get($cacheKey); |
||
| 46 | if ($this->verifySig($sign)) { |
||
| 47 | return $sign; |
||
| 48 | } |
||
| 49 | } |
||
| 50 | |||
| 51 | /** @var array $token */ |
||
| 52 | $token = $this->requestToken(); |
||
| 53 | |||
| 54 | $this->setToken($token[$this->tokenKey], $token['expires_in'] ?? 7200); |
||
| 55 | |||
| 56 | $this->app->events->dispatch(new AccessTokenRefreshed($this)); |
||
| 57 | |||
| 58 | return $token; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * verifySig |
||
| 63 | * |
||
| 64 | * @param array $sign |
||
| 65 | * |
||
| 66 | * @return mixed |
||
| 67 | */ |
||
| 68 | public function verifySig(array $sign) |
||
| 69 | { |
||
| 70 | $tLSSigAPIv2 = $this->app['sign']; |
||
| 71 | $identifier = $this->app['config']['identifier']; |
||
| 72 | |||
| 73 | return $tLSSigAPIv2->verifySig($sign[$this->tokenKey], $identifier, $initTime, $expireTime, $errorMsg); |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
Comprehensibility
Best Practice
introduced
by
Comprehensibility
Best Practice
introduced
by
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * @param RequestInterface $request |
||
| 79 | * @param array $requestOptions |
||
| 80 | * |
||
| 81 | * @return RequestInterface |
||
| 82 | * @throws \EasyIM\Kernel\Exceptions\HttpException |
||
| 83 | * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException |
||
| 84 | * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException |
||
| 85 | * @throws \EasyIM\Kernel\Exceptions\RuntimeException |
||
| 86 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 87 | */ |
||
| 88 | public function applyToRequest(RequestInterface $request, array $requestOptions = []): RequestInterface |
||
| 89 | { |
||
| 90 | parse_str($request->getUri()->getQuery(), $query); |
||
| 91 | |||
| 92 | $query = http_build_query(array_merge( |
||
| 93 | $this->getQuery(), |
||
| 94 | $query, |
||
| 95 | $this->getCredentials(), |
||
| 96 | ['random' => mt_rand(1, 99999999)] |
||
| 97 | )); |
||
| 98 | |||
| 99 | return $request->withUri($request->getUri()->withQuery($query)); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * requestToken |
||
| 104 | * |
||
| 105 | * @return array |
||
| 106 | */ |
||
| 107 | public function requestToken(): array |
||
| 108 | { |
||
| 109 | $tLSSigAPIv2 = $this->app['sign']; |
||
| 110 | $identifier = $this->app['config']['identifier']; |
||
| 111 | $expire = $this->app['config']['expire'] ?? 86400 * 2; |
||
| 112 | |||
| 113 | return [ |
||
| 114 | $this->queryName ?? $this->tokenKey => $tLSSigAPIv2->genSig($identifier, $expire), |
||
| 115 | 'expires_in' => $expire |
||
| 116 | ]; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | protected function getCredentials(): array |
||
| 123 | { |
||
| 124 | return [ |
||
| 125 | 'sdkappid' => $this->app['config']['sdk_app_id'], |
||
| 126 | 'identifier' => $this->app['config']['identifier'], |
||
| 127 | 'contenttype' => 'json' |
||
| 128 | ]; |
||
| 129 | } |
||
| 130 | } |
||
| 131 |