-
Notifications
You must be signed in to change notification settings - Fork 188
Expand file tree
/
Copy pathOAuth.php
More file actions
445 lines (392 loc) · 16.7 KB
/
OAuth.php
File metadata and controls
445 lines (392 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
<?php
declare(strict_types=1);
namespace Shopify\Auth;
use Shopify\Clients\Http;
use Shopify\Clients\HttpHeaders;
use Shopify\Clients\HttpResponse;
use Shopify\Context;
use Shopify\Exception\CookieNotFoundException;
use Shopify\Exception\CookieSetException;
use Shopify\Exception\HttpRequestException;
use Shopify\Exception\InvalidArgumentException;
use Shopify\Exception\InvalidJwtPayloadException;
use Shopify\Exception\InvalidOAuthException;
use Shopify\Exception\MissingArgumentException;
use Shopify\Exception\OAuthSessionNotFoundException;
use Shopify\Exception\SessionStorageException;
use Shopify\Utils;
use Ramsey\Uuid\Uuid;
/**
* Provides methods to perform OAuth with Shopify.
*/
class OAuth
{
public const SESSION_ID_COOKIE_NAME = 'shopify_session_id';
public const SESSION_ID_SIG_COOKIE_NAME = 'shopify_session_id_sig';
public const ACCESS_TOKEN_POST_PATH = '/admin/oauth/access_token';
/**
* Initializes a session and cookie for the OAuth process, and returns the authorization url
*
* @param string $shop A Shopify domain name or hostname
* @param string $redirectPath Redirect path for callback
* @param bool $isOnline Whether or not the session is online
* @param null|callable $setCookieFunction An optional override for setting cookie in response
*
* @return string The URL for OAuth redirection
* @throws \Shopify\Exception\CookieSetException
* @throws \Shopify\Exception\PrivateAppException
* @throws \Shopify\Exception\SessionStorageException
* @throws \Shopify\Exception\UninitializedContextException
*/
public static function begin(
string $shop,
string $redirectPath,
bool $isOnline,
?callable $setCookieFunction = null
): string {
Context::throwIfUninitialized();
Context::throwIfPrivateApp("OAuth is not allowed for private apps");
$sanitizedShop = Utils::sanitizeShopDomain($shop);
if (!isset($sanitizedShop)) {
throw new InvalidArgumentException("Invalid shop domain: $shop");
}
$redirectPath = trim(strtolower($redirectPath));
$redirectPath = ($redirectPath[0] == '/') ? $redirectPath : '/' . $redirectPath;
$mySessionId = $isOnline ? Uuid::uuid4()->toString() : self::getOfflineSessionId($sanitizedShop);
$cookieSet = self::setCookieSessionId($setCookieFunction, $mySessionId, strtotime('+1 minute'));
if (!$cookieSet) {
throw new CookieSetException(
'OAuth Cookie could not be saved.'
);
}
$session = new Session($mySessionId, $sanitizedShop, $isOnline, Uuid::uuid4()->toString());
if ($isOnline) {
$session->setExpires(strtotime('+1 minute'));
$grantOptions = 'per-user';
} else {
$grantOptions = '';
}
$sessionStored = Context::$SESSION_STORAGE->storeSession($session);
if (!$sessionStored) {
throw new SessionStorageException(
'OAuth Session could not be saved. Please check your session storage functionality.'
);
}
$query = [
'client_id' => Context::$API_KEY,
'scope' => Context::$SCOPES->toString(),
'redirect_uri' => Context::$HOST_SCHEME . '://' . Context::$HOST_NAME . $redirectPath,
'state' => $session->getState(),
'grant_options[]' => $grantOptions,
];
return "https://{$sanitizedShop}/admin/oauth/authorize?" . http_build_query($query);
}
/**
* Performs the OAuth callback steps, checking the returned parameters and fetching the access token, preparing the
* session for further usage. If successful, the updated session is returned.
*
* @param array $cookies HTTP request cookies, from which the OAuth session will be loaded. This
* must be a hash of cookie name => value pairs. Value will be forcibly cast
* to string so objects that implement toString will also work.
* @param array $query The HTTP request URL query values.
* @param null|callable $setCookieFunction An optional override for setting cookie in response.
*
* @return Session
* @throws \Shopify\Exception\HttpRequestException
* @throws \Shopify\Exception\InvalidOAuthException
* @throws \Shopify\Exception\OAuthCookieNotFoundException
* @throws \Shopify\Exception\OAuthSessionNotFoundException
* @throws \Shopify\Exception\PrivateAppException
* @throws \Shopify\Exception\SessionStorageException
* @throws \Shopify\Exception\UninitializedContextException
*/
public static function callback(array $cookies, array $query, ?callable $setCookieFunction = null): Session
{
Context::throwIfUninitialized();
Context::throwIfPrivateApp('OAuth is not allowed for private apps');
$cookieSessionId = self::getCookieSessionId($cookies);
$session = Context::$SESSION_STORAGE->loadSession($cookieSessionId);
if (!$session) {
throw new OAuthSessionNotFoundException(
'You may have taken more than 60 seconds to complete the OAuth process and the session cannot be found'
);
}
if (!self::isCallbackQueryValid($query, $session)) {
throw new InvalidOAuthException('Invalid OAuth callback.');
}
$response = self::fetchAccessToken($query, $session);
$session->setAccessToken($response->getAccessToken());
$session->setScope($response->getScope());
if ($session->isOnline()) {
/** @var AccessTokenOnlineResponse $response */
$session->setExpires(time() + $response->getExpiresIn());
$session->setOnlineAccessInfo($response->getAssociatedUser());
// If this is an online session in an embedded app, we replace it with a session that can be loaded from a
// JWT.
if (Context::$IS_EMBEDDED_APP) {
$jwtSessionId = self::getJwtSessionId($session->getShop(), $session->getOnlineAccessInfo()->getId());
$jwtSession = $session->clone($jwtSessionId);
$sessionDeleted = Context::$SESSION_STORAGE->deleteSession($session->getId());
if (!$sessionDeleted) {
throw new SessionStorageException(
'OAuth Session could not be deleted. Please check your session storage functionality.',
);
}
$session = $jwtSession;
}
}
$sessionStored = Context::$SESSION_STORAGE->storeSession($session);
if (!$sessionStored) {
throw new SessionStorageException(
'OAuth Session could not be saved. Please check your session storage functionality.',
);
}
$sessionExpiration = ($session->getExpires() ? (int)$session->getExpires()->format('U') : null);
$cookieSet = self::setCookieSessionId(
$setCookieFunction,
$cookieSessionId,
Context::$IS_EMBEDDED_APP ? time() : $sessionExpiration
);
if (!$cookieSet) {
throw new CookieSetException(
'OAuth Cookie could not be saved.'
);
}
return $session;
}
/**
* Builds a session id that can be loaded from JWTs from App Bridge
*
* @param string $shop The session's shop
* @param string|int $userId |int The session's user
*
* @return string
*/
public static function getJwtSessionId(string $shop, $userId): string
{
return "{$shop}_{$userId}";
}
/**
* Retrieves the offline session ID tied to a single shop
*
* @param string $shop The session's shop
* @return string the offline session ID
*/
public static function getOfflineSessionId(string $shop): string
{
return "offline_{$shop}";
}
/**
* Extracts the current session ID from the headers
*
* @param array $rawHeaders HTTP headers returned from the request context
* @param array $cookies HTTP request cookies
* @param bool $isOnline Whether to load online or offline sessions
*
* @return string The ID of the current session
* @throws \Shopify\Exception\MissingArgumentException
* @throws \Shopify\Exception\CookieNotFoundException
*/
public static function getCurrentSessionId(array $rawHeaders, array $cookies, bool $isOnline): string
{
if (Context::$IS_EMBEDDED_APP) {
if ($rawHeaders) {
$headers = new HttpHeaders($rawHeaders);
if (!$headers->has('authorization')) {
throw new MissingArgumentException('Missing Authorization key in headers array');
}
$auth = $headers->get('authorization');
preg_match('/^Bearer (.+)$/', $auth, $matches);
if (!$matches) {
throw new MissingArgumentException('Missing Bearer token in authorization header');
}
$jwtPayload = Utils::decodeSessionToken($matches[1]);
if (!empty($jwtPayload['dest'])) {
$shop = preg_replace('/^https:\/\//', '', $jwtPayload['dest']);
} elseif (!empty($jwtPayload['input_data']->shop->domain)) {
$shop = preg_replace('/^https:\/\//', '', $jwtPayload['input_data']->shop->domain);
} else {
throw new InvalidJwtPayloadException('Missing shop value in JWT payload');
}
if ($isOnline) {
$currentSessionId = self::getJwtSessionId($shop, $jwtPayload['sub']);
} else {
$currentSessionId = self::getOfflineSessionId($shop);
}
} else {
throw new MissingArgumentException('Missing headers argument for embedded app');
}
} else {
if (!$cookies) {
throw new CookieNotFoundException('Could not find the current session id in the cookies');
}
$currentSessionId = self::getCookieSessionId($cookies);
}
return $currentSessionId;
}
/**
* Fetches the current session ID from the given cookies.
*
* @param array $cookies The $cookies param from `callback`
*
* @return string The ID of the current session
* @throws \Shopify\Exception\CookieNotFoundException
*/
private static function getCookieSessionId(array $cookies): string
{
$signature = $cookies[self::SESSION_ID_SIG_COOKIE_NAME] ?? null;
$cookieId = $cookies[self::SESSION_ID_COOKIE_NAME] ?? null;
$sessionId = null;
if ($signature && $cookieId) {
$expectedSignature = hash_hmac('sha256', $cookieId, Context::$API_SECRET_KEY);
if ($signature === $expectedSignature) {
$sessionId = $cookieId;
}
}
if (!$sessionId) {
throw new CookieNotFoundException("Could not find the current session id in the cookies");
}
return (string)$sessionId;
}
/**
* Sets the session ID in the right cookie.
*
* @param null|callable $setCookieFunction An optional override for setting cookie in response
* @param string $sessionId The ID of the session to save
* @param int $expiration Epoch timestamp (in s) when the cookie expires
*
* @return bool Whether the cookie was successfully set
*/
private static function setCookieSessionId(?callable $setCookieFunction, $sessionId, $expiration): bool
{
$signature = hash_hmac('sha256', $sessionId, Context::$API_SECRET_KEY);
$signatureCookie = new OAuthCookie($signature, self::SESSION_ID_SIG_COOKIE_NAME, $expiration, true, true);
$cookie = new OAuthCookie($sessionId, self::SESSION_ID_COOKIE_NAME, $expiration, true, true);
if ($setCookieFunction) {
$cookieSet = $setCookieFunction($signatureCookie);
$cookieSet = $cookieSet && $setCookieFunction($cookie);
} else {
// @codeCoverageIgnoreStart
// cannot mock setcookie() function
$cookieSet = setcookie(
$signatureCookie->getName(),
$signatureCookie->getValue(),
$signatureCookie->getExpire(),
"",
"",
$signatureCookie->isSecure(),
$signatureCookie->isHttpOnly(),
);
$cookieSet = $cookieSet && setcookie(
$cookie->getName(),
$cookie->getValue(),
$cookie->getExpire(),
"",
"",
$cookie->isSecure(),
$cookie->isHttpOnly(),
);
// @codeCoverageIgnoreEnd
}
return $cookieSet;
}
/**
* Checks whether the given query parameters are from a valid callback request.
*
* @param array $query The URL query parameters
* @param Session $session The current session
*
* @return bool
* @throws \Shopify\Exception\UninitializedContextException
*/
private static function isCallbackQueryValid(array $query, Session $session): bool
{
$sanitizedShop = Utils::sanitizeShopDomain($query['shop'] ?? '');
$state = $query['state'] ?? '';
$code = $query['code'] ?? '';
return (
($code) &&
($sanitizedShop && strcmp($session->getShop(), $sanitizedShop) === 0) &&
($state && strcmp($session->getState(), $state) === 0) &&
Utils::validateHmac($query, Context::$API_SECRET_KEY)
);
}
/**
* Fetches the access token for the given OAuth session, using the query parameters returned by Shopify
*
* @param array $query The URL query params from the OAuth callback
* @param Session $session The OAuth session
*
* @return AccessTokenResponse|AccessTokenOnlineResponse The access token exchanged for the OAuth code
* @throws \Shopify\Exception\HttpRequestException
*/
private static function fetchAccessToken(
array $query,
Session $session
) {
$post = [
'client_id' => Context::$API_KEY,
'client_secret' => Context::$API_SECRET_KEY,
'code' => $query['code'],
];
$client = new Http($session->getShop());
$response = self::requestAccessToken($client, $post);
if ($response->getStatusCode() !== 200) {
throw new HttpRequestException("Failed to get access token: {$response->getDecodedBody()}");
}
if ($session->isOnline()) {
return self::buildAccessTokenOnlineResponse($response->getDecodedBody());
} else {
return self::buildAccessTokenResponse($response->getDecodedBody());
}
}
/**
* Builds an online access token response object
*
* @param array $body The HTTP response body
*/
private static function buildAccessTokenOnlineResponse(array $body): AccessTokenOnlineResponse
{
$associatedUser = new AccessTokenOnlineUserInfo(
$body['associated_user']['id'],
$body['associated_user']['first_name'],
$body['associated_user']['last_name'],
$body['associated_user']['email'],
$body['associated_user']['email_verified'],
$body['associated_user']['account_owner'],
$body['associated_user']['locale'],
$body['associated_user']['collaborator'],
);
return new AccessTokenOnlineResponse(
$body['access_token'],
$body['scope'],
$body['expires_in'],
$body['associated_user_scope'],
$associatedUser,
);
}
/**
* Builds an offline access token response object
*
* @param array $body The HTTP response body
*/
private static function buildAccessTokenResponse(array $body): AccessTokenResponse
{
return new AccessTokenResponse($body['access_token'], $body['scope']);
}
/**
* Fires the actual request for the access token. This was isolated so it can be stubbed in unit tests.
*
* @param Http $client
* @param array $post The POST payload
*
* @return \Shopify\Clients\HttpResponse
* @throws \Psr\Http\Client\ClientExceptionInterface
* @throws \Shopify\Exception\UninitializedContextException
* @codeCoverageIgnore
*/
public static function requestAccessToken(Http $client, array $post): HttpResponse
{
return $client->post(self::ACCESS_TOKEN_POST_PATH, $post);
}
}