11
11
12
12
namespace chillerlan \OAuth \Providers ;
13
13
14
- use chillerlan \HTTP \Utils \QueryUtil ;
14
+ use chillerlan \HTTP \Utils \{ MessageUtil , QueryUtil , UriUtil } ;
15
15
use chillerlan \OAuth \Core \{AccessToken , AuthenticatedUser , OAuthProvider , UserInfo };
16
- use chillerlan \HTTP \Utils \UriUtil ;
17
16
use Psr \Http \Message \{RequestInterface , ResponseInterface , UriInterface };
18
- use function explode , intval , preg_replace ;
17
+ use function explode , intval , str_replace ;
19
18
20
19
/**
21
20
* Steam OpenID
22
21
*
23
22
* @see https://steamcommunity.com/dev
24
23
* @see https://partner.steamgames.com/doc/webapi_overview
24
+ * @see https://partner.steamgames.com/doc/features/auth
25
25
* @see https://steamwebapi.azurewebsites.net/
26
26
*/
27
27
class Steam extends OAuthProvider implements UserInfo{
@@ -54,9 +54,22 @@ public function getAuthorizationURL(array|null $params = null, array|null $scope
54
54
}
55
55
56
56
/**
57
- *
57
+ * Obtains an "authentication token" (the steamID64)
58
+ */
59
+ public function getAccessToken (array $ urlQuery ):AccessToken {
60
+ $ body = $ this ->getAccessTokenRequestBodyParams ($ urlQuery );
61
+ $ response = $ this ->sendAccessTokenRequest ($ this ->accessTokenURL , $ body );
62
+ $ token = $ this ->parseTokenResponse ($ response , $ urlQuery ['openid_claimed_id ' ]);
63
+
64
+ $ this ->storage ->storeAccessToken ($ token , $ this ->name );
65
+
66
+ return $ token ;
67
+ }
68
+
69
+ /**
70
+ * prepares the request body parameters for the access token request
58
71
*/
59
- public function getAccessToken (array $ received ):AccessToken {
72
+ protected function getAccessTokenRequestBodyParams (array $ received ):array {
60
73
61
74
$ body = [
62
75
'openid.mode ' => 'check_authentication ' ,
@@ -68,33 +81,27 @@ public function getAccessToken(array $received):AccessToken{
68
81
$ body ['openid. ' .$ item ] = $ received ['openid_ ' .$ item ];
69
82
}
70
83
84
+ return $ body ;
85
+ }
86
+
87
+ /**
88
+ * sends a request to the access token endpoint $url with the given $params as URL query
89
+ */
90
+ protected function sendAccessTokenRequest (string $ url , array $ body ):ResponseInterface {
91
+
71
92
$ request = $ this ->requestFactory
72
- ->createRequest ('POST ' , $ this -> accessTokenURL )
93
+ ->createRequest ('POST ' , $ url )
73
94
->withHeader ('Content-Type ' , 'application/x-www-form-urlencoded ' )
74
95
->withBody ($ this ->streamFactory ->createStream (QueryUtil::build ($ body )));
75
96
76
- $ token = $ this ->parseTokenResponse ($ this ->http ->sendRequest ($ request ));
77
- $ id = preg_replace ('/[^\d]/ ' , '' , $ received ['openid_claimed_id ' ]);
78
-
79
- // as this method is intended for one-time authentication only we'll not receive a token.
80
- // instead we're gonna save the verified steam user id as token as it is required
81
- // for several "authenticated" endpoints.
82
- $ token ->accessToken = $ id ;
83
- $ token ->extraParams = [
84
- 'claimed_id ' => $ received ['openid_claimed_id ' ],
85
- 'id_int ' => intval ($ id ),
86
- ];
87
-
88
- $ this ->storage ->storeAccessToken ($ token , $ this ->name );
89
-
90
- return $ token ;
97
+ return $ this ->http ->sendRequest ($ request );
91
98
}
92
99
93
100
/**
94
101
* @throws \chillerlan\OAuth\Providers\ProviderException
95
102
*/
96
- protected function parseTokenResponse (ResponseInterface $ response ):AccessToken {
97
- $ data = explode ("\x0a" , ( string ) $ response-> getBody ( ));
103
+ protected function parseTokenResponse (ResponseInterface $ response, string $ claimed_id ):AccessToken {
104
+ $ data = explode ("\x0a" , MessageUtil:: getContents ( $ response ));
98
105
99
106
if (!isset ($ data [1 ]) || !str_starts_with ($ data [1 ], 'is_valid ' )){
100
107
throw new ProviderException ('unable to parse token response ' );
@@ -104,17 +111,24 @@ protected function parseTokenResponse(ResponseInterface $response):AccessToken{
104
111
throw new ProviderException ('invalid id ' );
105
112
}
106
113
107
- // the response is only validation, so we'll just return an empty token and add the id in the next step
108
114
$ token = $ this ->createAccessToken ();
115
+ $ id = str_replace ('https://steamcommunity.com/openid/id/ ' , '' , $ claimed_id );
109
116
110
- $ token ->accessToken = 'SteamID ' ;
117
+ // as this method is intended for one-time authentication only we'll not receive a token.
118
+ // instead we're gonna save the verified steam user id as token as it is required
119
+ // for several "authenticated" endpoints.
120
+ $ token ->accessToken = $ id ;
111
121
$ token ->expires = AccessToken::NEVER_EXPIRES ;
122
+ $ token ->extraParams = [
123
+ 'claimed_id ' => $ claimed_id ,
124
+ 'id_int ' => intval ($ id ),
125
+ ];
112
126
113
127
return $ token ;
114
128
}
115
129
116
130
/**
117
- *
131
+ * @inheritDoc
118
132
*/
119
133
public function getRequestAuthorization (RequestInterface $ request , AccessToken |null $ token = null ):RequestInterface {
120
134
$ uri = UriUtil::withQueryValue ($ request ->getUri (), 'key ' , $ this ->options ->secret );
0 commit comments