|
1 | 1 | import {IdentityClient} from './identity-client.js' |
2 | 2 | import {ApplicationToken, IdentityToken} from '../../session/schema.js' |
3 | | -import {ExchangeScopes} from '../../session/exchange.js' |
| 3 | +import {ExchangeScopes, TokenRequestResult} from '../../session/exchange.js' |
| 4 | +import {ok, Result} from '../../../../public/node/result.js' |
| 5 | +import {allDefaultScopes} from '../../session/scopes.js' |
| 6 | +import {applicationId} from '../../session/identity.js' |
4 | 7 |
|
5 | 8 | export class IdentityMockClient extends IdentityClient { |
| 9 | + private readonly mockUserId = '08978734-325e-44ce-bc65-34823a8d5180' |
| 10 | + private readonly authTokenPrefix = 'mtkn_' |
| 11 | + |
6 | 12 | async requestAccessToken(_scopes: string[]): Promise<IdentityToken> { |
7 | | - return {} as IdentityToken |
| 13 | + const tokens = this.generateTokens('identity') |
| 14 | + |
| 15 | + return Promise.resolve({ |
| 16 | + accessToken: tokens.accessToken, |
| 17 | + alias: '', |
| 18 | + expiresAt: this.getFutureDate(1), |
| 19 | + refreshToken: tokens.refreshToken, |
| 20 | + scopes: allDefaultScopes(), |
| 21 | + userId: this.mockUserId, |
| 22 | + }) |
8 | 23 | } |
9 | 24 |
|
10 | 25 | async exchangeAccessForApplicationTokens( |
11 | 26 | _identityToken: IdentityToken, |
12 | 27 | _scopes: ExchangeScopes, |
13 | 28 | _store?: string, |
14 | 29 | ): Promise<{[x: string]: ApplicationToken}> { |
15 | | - return {} |
| 30 | + return { |
| 31 | + [applicationId('app-management')]: this.generateTokens(applicationId('app-management')), |
| 32 | + [applicationId('business-platform')]: this.generateTokens(applicationId('business-platform')), |
| 33 | + [applicationId('admin')]: this.generateTokens(applicationId('admin')), |
| 34 | + [applicationId('partners')]: this.generateTokens(applicationId('partners')), |
| 35 | + [applicationId('storefront-renderer')]: this.generateTokens(applicationId('storefront-renderer')), |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + async tokenRequest(params: { |
| 40 | + [key: string]: string |
| 41 | + }): Promise<Result<TokenRequestResult, {error: string; store?: string}>> { |
| 42 | + const tokens = this.generateTokens(params?.audience ?? '') |
| 43 | + return ok({ |
| 44 | + access_token: tokens.accessToken, |
| 45 | + expires_in: this.getFutureDate(1).getTime(), |
| 46 | + refresh_token: tokens.refreshToken, |
| 47 | + scope: allDefaultScopes().join(' '), |
| 48 | + }) |
16 | 49 | } |
17 | 50 |
|
18 | 51 | async refreshAccessToken(_currentToken: IdentityToken): Promise<IdentityToken> { |
19 | | - return {} as IdentityToken |
| 52 | + const tokens = this.generateTokens('identity') |
| 53 | + |
| 54 | + return Promise.resolve({ |
| 55 | + accessToken: tokens.accessToken, |
| 56 | + alias: 'dev@shopify.com', |
| 57 | + expiresAt: this.getFutureDate(1), |
| 58 | + refreshToken: tokens.refreshToken, |
| 59 | + scopes: allDefaultScopes(), |
| 60 | + userId: this.mockUserId, |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + clientId(): string { |
| 65 | + return 'shopify-cli-development' |
| 66 | + } |
| 67 | + |
| 68 | + private readonly generateTokens = (appId: string) => { |
| 69 | + const now = this.getCurrentUnixTimestamp() |
| 70 | + const exp = now + 7200 |
| 71 | + |
| 72 | + const tokenPayload = { |
| 73 | + act: { |
| 74 | + iss: 'https://identity.shop.dev', |
| 75 | + sub: this.clientId(), |
| 76 | + }, |
| 77 | + aud: appId, |
| 78 | + client_id: this.clientId(), |
| 79 | + token_type: 'SLAT', |
| 80 | + exp, |
| 81 | + iat: now, |
| 82 | + iss: 'https://identity.shop.dev', |
| 83 | + scope: allDefaultScopes().join(' '), |
| 84 | + sub: this.mockUserId, |
| 85 | + sid: 'df63c65c-3731-48af-a28d-72ab16a6523a', |
| 86 | + auth_time: now, |
| 87 | + amr: ['pwd', 'device-auth'], |
| 88 | + device_uuid: '8ba644c8-7d2f-4260-9311-86df09195ee8', |
| 89 | + atl: 1.0, |
| 90 | + } |
| 91 | + |
| 92 | + const refreshTokenPayload = { |
| 93 | + ...tokenPayload, |
| 94 | + token_use: 'refresh', |
| 95 | + } |
| 96 | + |
| 97 | + return { |
| 98 | + accessToken: `${this.authTokenPrefix}${this.encodeTokenPayload(tokenPayload)}`, |
| 99 | + refreshToken: `${this.authTokenPrefix}${this.encodeTokenPayload(refreshTokenPayload)}`, |
| 100 | + expiresAt: new Date(exp * 1000), |
| 101 | + scopes: allDefaultScopes(), |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private getFutureDate(daysInFuture = 100): Date { |
| 106 | + const futureDate = new Date() |
| 107 | + futureDate.setDate(futureDate.getDate() + daysInFuture) |
| 108 | + return futureDate |
| 109 | + } |
| 110 | + |
| 111 | + private getCurrentUnixTimestamp(): number { |
| 112 | + return Math.floor(Date.now() / 1000) |
| 113 | + } |
| 114 | + |
| 115 | + private encodeTokenPayload(payload: object): string { |
| 116 | + return Buffer.from(JSON.stringify(payload)) |
| 117 | + .toString('base64') |
| 118 | + .replace(/[=]/g, '') |
| 119 | + .replace(/\+/g, '-') |
| 120 | + .replace(/\//g, '_') |
20 | 121 | } |
21 | 122 | } |
0 commit comments