|
12 | 12 | namespace chillerlan\OAuth\Providers; |
13 | 13 |
|
14 | 14 | use chillerlan\HTTP\Utils\QueryUtil; |
15 | | -use chillerlan\OAuth\Core\{AccessToken, OAuthProvider}; |
| 15 | +use chillerlan\OAuth\Core\{AccessToken, AuthenticatedUser, OAuthProvider, UserInfo}; |
| 16 | +use chillerlan\HTTP\Utils\UriUtil; |
16 | 17 | use Psr\Http\Message\{RequestInterface, ResponseInterface, UriInterface}; |
17 | 18 | use function explode, intval, preg_replace; |
18 | 19 |
|
|
23 | 24 | * @see https://partner.steamgames.com/doc/webapi_overview |
24 | 25 | * @see https://steamwebapi.azurewebsites.net/ |
25 | 26 | */ |
26 | | -class SteamOpenID extends OAuthProvider{ |
| 27 | +class SteamOpenID extends OAuthProvider implements UserInfo{ |
27 | 28 |
|
28 | 29 | protected string $authorizationURL = 'https://steamcommunity.com/openid/login'; |
29 | 30 | protected string $accessTokenURL = 'https://steamcommunity.com/openid/login'; |
@@ -114,17 +115,34 @@ protected function parseTokenResponse(ResponseInterface $response):AccessToken{ |
114 | 115 | * |
115 | 116 | */ |
116 | 117 | public function getRequestAuthorization(RequestInterface $request, AccessToken|null $token = null):RequestInterface{ |
117 | | - $token ??= $this->storage->getAccessToken($this->name); |
| 118 | + $uri = UriUtil::withQueryValue($request->getUri(), 'key', $this->options->secret); |
118 | 119 |
|
119 | | - $uri = (string)$request->getUri(); |
120 | | - $params = ['key' => $this->options->secret]; |
| 120 | + return $request->withUri($uri); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @inheritDoc |
| 125 | + * @codeCoverageIgnore |
| 126 | + */ |
| 127 | + public function me():AuthenticatedUser{ |
| 128 | + $token = $this->storage->getAccessToken($this->name); |
| 129 | + $json = $this->getMeResponseData('/ISteamUser/GetPlayerSummaries/v0002/', ['steamids' => $token->accessToken]); |
121 | 130 |
|
122 | | - // the steamid parameter does not necessarily specify the current user, so add it only when it's not already set |
123 | | - if(!str_contains($uri, 'steamid=')){ |
124 | | - $params['steamid']= $token->accessToken; |
| 131 | + if(!isset($json['response']['players'][0])){ |
| 132 | + throw new ProviderException('invalid response'); |
125 | 133 | } |
126 | 134 |
|
127 | | - return $request->withUri($this->uriFactory->createUri(QueryUtil::merge($uri, $params))); |
| 135 | + $data = $json['response']['players'][0]; |
| 136 | + |
| 137 | + $userdata = [ |
| 138 | + 'data' => $data, |
| 139 | + 'avatar' => $data['avatarfull'], |
| 140 | + 'displayName' => $data['personaname'], |
| 141 | + 'id' => $data['steamid'], |
| 142 | + 'url' => $data['profileurl'], |
| 143 | + ]; |
| 144 | + |
| 145 | + return new AuthenticatedUser($userdata); |
128 | 146 | } |
129 | 147 |
|
130 | 148 | } |
0 commit comments