dukt /
videos
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * @link https://dukt.net/videos/ |
||||
| 4 | * @copyright Copyright (c) Dukt |
||||
| 5 | * @license https://github.com/dukt/videos/blob/v2/LICENSE.md |
||||
| 6 | */ |
||||
| 7 | |||||
| 8 | namespace dukt\videos\services; |
||||
| 9 | |||||
| 10 | use dukt\videos\models\Token; |
||||
| 11 | use Exception; |
||||
| 12 | use League\OAuth2\Client\Token\AccessToken; |
||||
| 13 | use yii\base\Component; |
||||
| 14 | use dukt\videos\Plugin; |
||||
| 15 | use League\OAuth2\Client\Grant\RefreshToken; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * Class Oauth service. |
||||
| 19 | * |
||||
| 20 | * An instance of the Oauth service is globally accessible via [[Plugin::oauth `Plugin::$plugin->getOauth()`]]. |
||||
| 21 | * |
||||
| 22 | * @author Dukt <[email protected]> |
||||
| 23 | * @since 2.0 |
||||
| 24 | */ |
||||
| 25 | class Oauth extends Component |
||||
| 26 | { |
||||
| 27 | // Public Methods |
||||
| 28 | // ========================================================================= |
||||
| 29 | /** |
||||
| 30 | * Get a token by its gateway handle. |
||||
| 31 | * |
||||
| 32 | * @param $gatewayHandle |
||||
| 33 | * @return AccessToken|null |
||||
| 34 | * @throws \yii\base\InvalidConfigException |
||||
| 35 | */ |
||||
| 36 | public function getToken(string $gatewayHandle, bool $refresh = true): ?\League\OAuth2\Client\Token\AccessToken |
||||
| 37 | { |
||||
| 38 | $token = Plugin::getInstance()->getTokens()->getToken($gatewayHandle); |
||||
| 39 | |||||
| 40 | if (!$token instanceof \dukt\videos\models\Token) { |
||||
| 41 | return null; |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | return $this->createTokenFromData($gatewayHandle, $token->accessToken, $refresh); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 45 | } |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * Saves a token. |
||||
| 49 | * |
||||
| 50 | * @param $gatewayHandle |
||||
| 51 | * @param AccessToken $token |
||||
| 52 | * @return bool |
||||
| 53 | * @throws Exception |
||||
| 54 | */ |
||||
| 55 | public function saveToken($gatewayHandle, AccessToken $token): bool |
||||
| 56 | { |
||||
| 57 | $tokenModel = Plugin::getInstance()->getTokens()->getToken($gatewayHandle); |
||||
| 58 | |||||
| 59 | if ($tokenModel === null) { |
||||
| 60 | $tokenModel = new Token(); |
||||
| 61 | $tokenModel->gateway = $gatewayHandle; |
||||
| 62 | } |
||||
| 63 | |||||
| 64 | $tokenModel->accessToken = [ |
||||
|
0 ignored issues
–
show
It seems like
array('accessToken' => $...=> $token->getValues()) of type array<string,array|integer|string> is incompatible with the declared type null|string of property $accessToken.
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.. Loading history...
|
|||||
| 65 | 'accessToken' => $token->getToken(), |
||||
| 66 | 'expires' => $token->getExpires(), |
||||
| 67 | 'resourceOwnerId' => $token->getResourceOwnerId(), |
||||
| 68 | 'values' => $token->getValues(), |
||||
| 69 | ]; |
||||
| 70 | |||||
| 71 | if (!empty($token->getRefreshToken())) { |
||||
| 72 | $tokenModel->accessToken['refreshToken'] = $token->getRefreshToken(); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | Plugin::getInstance()->getTokens()->saveToken($tokenModel); |
||||
| 76 | |||||
| 77 | return true; |
||||
| 78 | } |
||||
| 79 | |||||
| 80 | /** |
||||
| 81 | * Deletes a token. |
||||
| 82 | * |
||||
| 83 | * @param $gatewayHandle |
||||
| 84 | * @return bool |
||||
| 85 | * @throws \Throwable |
||||
| 86 | * @throws \yii\base\InvalidConfigException |
||||
| 87 | * @throws \yii\db\StaleObjectException |
||||
| 88 | */ |
||||
| 89 | public function deleteToken($gatewayHandle): bool |
||||
| 90 | { |
||||
| 91 | $token = Plugin::getInstance()->getTokens()->getToken($gatewayHandle); |
||||
| 92 | |||||
| 93 | if (!$token instanceof \dukt\videos\models\Token) { |
||||
| 94 | return true; |
||||
| 95 | } |
||||
| 96 | |||||
| 97 | return Plugin::getInstance()->getTokens()->deleteTokenById($token->id); |
||||
|
0 ignored issues
–
show
It seems like
$token->id can also be of type null; however, parameter $id of dukt\videos\services\Tokens::deleteTokenById() does only seem to accept integer, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 98 | } |
||||
| 99 | |||||
| 100 | // Private Methods |
||||
| 101 | // ========================================================================= |
||||
| 102 | /** |
||||
| 103 | * Create token from data. |
||||
| 104 | * |
||||
| 105 | * |
||||
| 106 | * @return AccessToken|null |
||||
| 107 | * @throws \yii\base\InvalidConfigException |
||||
| 108 | */ |
||||
| 109 | private function createTokenFromData(string $gatewayHandle, array $data, bool $refreshToken = true): ?\League\OAuth2\Client\Token\AccessToken |
||||
| 110 | { |
||||
| 111 | if (!isset($data['accessToken'])) { |
||||
| 112 | return null; |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | $token = new AccessToken([ |
||||
| 116 | 'access_token' => $data['accessToken'] ?? null, |
||||
| 117 | 'expires' => $data['expires'] ?? null, |
||||
| 118 | 'refresh_token' => $data['refreshToken'] ?? null, |
||||
| 119 | 'resource_owner_id' => $data['resourceOwnerId'] ?? null, |
||||
| 120 | 'values' => $data['values'] ?? null, |
||||
| 121 | ]); |
||||
| 122 | |||||
| 123 | // Refresh OAuth token |
||||
| 124 | if ($refreshToken && !empty($token->getRefreshToken()) && $token->getExpires() && $token->hasExpired()) { |
||||
| 125 | $gateway = Plugin::$plugin->getGateways()->getGateway($gatewayHandle); |
||||
| 126 | $provider = $gateway->getOauthProvider(); |
||||
| 127 | $grant = new RefreshToken(); |
||||
| 128 | $newToken = $provider->getAccessToken($grant, ['refresh_token' => $token->getRefreshToken()]); |
||||
| 129 | |||||
| 130 | $token = new AccessToken([ |
||||
| 131 | 'access_token' => $newToken->getToken(), |
||||
| 132 | 'expires' => $newToken->getExpires(), |
||||
| 133 | 'refresh_token' => $token->getRefreshToken(), |
||||
| 134 | 'resource_owner_id' => $newToken->getResourceOwnerId(), |
||||
| 135 | 'values' => $newToken->getValues(), |
||||
| 136 | ]); |
||||
| 137 | |||||
| 138 | Plugin::$plugin->getOauth()->saveToken($gateway->getHandle(), $token); |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | return $token; |
||||
| 142 | } |
||||
| 143 | } |
||||
| 144 |