|
1 | 1 | import {IdentityBaseClient} from './identity-base-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 IdentityBaseClient { |
| 9 | + private readonly mockUserId = '08978734-325e-44ce-bc65-34823a8d5180' |
| 10 | + private readonly mockSessionId = 'df63c65c-3731-48af-a28d-72ab16a6523a' |
| 11 | + private readonly mockDeviceUuid = '8ba644c8-7d2f-4260-9311-86df09195ee8' |
| 12 | + private readonly authTokenPrefix = 'mtkn_' |
| 13 | + |
6 | 14 | async requestAndPollForAccessToken(_scopes: string[]): Promise<IdentityToken> { |
7 | | - return {} as IdentityToken |
| 15 | + const now = this.getCurrentUnixTimestamp() |
| 16 | + const exp = now + 7200 |
| 17 | + const scopes = allDefaultScopes() |
| 18 | + |
| 19 | + const identityTokenPayload = { |
| 20 | + client_id: this.clientId(), |
| 21 | + token_type: 'SLAT', |
| 22 | + exp, |
| 23 | + iat: now, |
| 24 | + sub: this.mockUserId, |
| 25 | + iss: 'https://identity.shop.dev', |
| 26 | + sid: this.mockSessionId, |
| 27 | + auth_time: now, |
| 28 | + amr: ['pwd', 'device-auth'], |
| 29 | + device_uuid: this.mockDeviceUuid, |
| 30 | + scope: scopes.join(' '), |
| 31 | + atl: 1.0, |
| 32 | + } |
| 33 | + |
| 34 | + const refreshTokenPayload = { |
| 35 | + ...identityTokenPayload, |
| 36 | + token_use: 'refresh', |
| 37 | + } |
| 38 | + |
| 39 | + return Promise.resolve({ |
| 40 | + accessToken: `${this.authTokenPrefix}${this.encodeTokenPayload(identityTokenPayload)}`, |
| 41 | + alias: '', |
| 42 | + expiresAt: this.getFutureDate(1), |
| 43 | + refreshToken: `${this.authTokenPrefix}${this.encodeTokenPayload(refreshTokenPayload)}`, |
| 44 | + scopes, |
| 45 | + userId: this.mockUserId, |
| 46 | + }) |
8 | 47 | } |
9 | 48 |
|
10 | 49 | async exchangeAccessForApplicationTokens( |
11 | 50 | _identityToken: IdentityToken, |
12 | 51 | _scopes: ExchangeScopes, |
13 | 52 | _store?: string, |
14 | 53 | ): Promise<{[x: string]: ApplicationToken}> { |
15 | | - return {} |
| 54 | + const now = this.getCurrentUnixTimestamp() |
| 55 | + const exp = now + 7200 |
| 56 | + |
| 57 | + const generateAppToken = (appId: string): ApplicationToken => { |
| 58 | + const tokenPayload = { |
| 59 | + act: { |
| 60 | + iss: 'https://identity.shop.dev', |
| 61 | + sub: this.clientId(), |
| 62 | + }, |
| 63 | + aud: appId, |
| 64 | + client_id: this.clientId(), |
| 65 | + token_type: 'SLAT', |
| 66 | + exp, |
| 67 | + iat: now, |
| 68 | + iss: 'https://identity.shop.dev', |
| 69 | + scope: allDefaultScopes().join(' '), |
| 70 | + sub: this.mockUserId, |
| 71 | + sid: this.mockSessionId, |
| 72 | + auth_time: now, |
| 73 | + amr: ['pwd', 'device-auth'], |
| 74 | + device_uuid: this.mockDeviceUuid, |
| 75 | + atl: 1.0, |
| 76 | + } |
| 77 | + |
| 78 | + return { |
| 79 | + accessToken: `${this.authTokenPrefix}${this.encodeTokenPayload(tokenPayload)}`, |
| 80 | + expiresAt: new Date(exp * 1000), |
| 81 | + scopes: allDefaultScopes(), |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return { |
| 86 | + [applicationId('app-management')]: generateAppToken(applicationId('app-management')), |
| 87 | + [applicationId('business-platform')]: generateAppToken(applicationId('business-platform')), |
| 88 | + [applicationId('admin')]: generateAppToken(applicationId('admin')), |
| 89 | + [applicationId('partners')]: generateAppToken(applicationId('partners')), |
| 90 | + [applicationId('storefront-renderer')]: generateAppToken(applicationId('storefront-renderer')), |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + async tokenRequest(_params: { |
| 95 | + [key: string]: string |
| 96 | + }): Promise<Result<TokenRequestResult, {error: string; store?: string}>> { |
| 97 | + const token = await this.refreshAccessToken({} as IdentityToken) |
| 98 | + return ok({ |
| 99 | + access_token: token.accessToken, |
| 100 | + expires_in: this.getFutureDate(1).getTime(), |
| 101 | + refresh_token: token.refreshToken, |
| 102 | + scope: allDefaultScopes().join(' '), |
| 103 | + }) |
16 | 104 | } |
17 | 105 |
|
18 | 106 | async refreshAccessToken(_currentToken: IdentityToken): Promise<IdentityToken> { |
19 | | - return {} as IdentityToken |
| 107 | + const now = this.getCurrentUnixTimestamp() |
| 108 | + const exp = now + 7200 |
| 109 | + |
| 110 | + const identityTokenPayload = { |
| 111 | + client_id: this.clientId(), |
| 112 | + token_type: 'SLAT', |
| 113 | + exp, |
| 114 | + iat: now, |
| 115 | + sub: this.mockUserId, |
| 116 | + iss: 'https://identity.shop.dev', |
| 117 | + sid: this.mockSessionId, |
| 118 | + auth_time: now, |
| 119 | + amr: ['pwd', 'device-auth'], |
| 120 | + device_uuid: this.mockDeviceUuid, |
| 121 | + scope: allDefaultScopes().join(' '), |
| 122 | + atl: 1.0, |
| 123 | + } |
| 124 | + |
| 125 | + const refreshTokenPayload = { |
| 126 | + ...identityTokenPayload, |
| 127 | + token_use: 'refresh', |
| 128 | + } |
| 129 | + |
| 130 | + return Promise.resolve({ |
| 131 | + accessToken: `${this.authTokenPrefix}${this.encodeTokenPayload(identityTokenPayload)}`, |
| 132 | + |
| 133 | + expiresAt: this.getFutureDate(1), |
| 134 | + refreshToken: `${this.authTokenPrefix}${this.encodeTokenPayload(refreshTokenPayload)}`, |
| 135 | + scopes: allDefaultScopes(), |
| 136 | + userId: this.mockUserId, |
| 137 | + }) |
| 138 | + } |
| 139 | + |
| 140 | + clientId(): string { |
| 141 | + return 'shopify-cli-development' |
| 142 | + } |
| 143 | + |
| 144 | + private getFutureDate(daysInFuture = 100): Date { |
| 145 | + const futureDate = new Date() |
| 146 | + futureDate.setDate(futureDate.getDate() + daysInFuture) |
| 147 | + return futureDate |
| 148 | + } |
| 149 | + |
| 150 | + private getCurrentUnixTimestamp(): number { |
| 151 | + return Math.floor(Date.now() / 1000) |
| 152 | + } |
| 153 | + |
| 154 | + private encodeTokenPayload(payload: object): string { |
| 155 | + return Buffer.from(JSON.stringify(payload)) |
| 156 | + .toString('base64') |
| 157 | + .replace(/[=]/g, '') |
| 158 | + .replace(/\+/g, '-') |
| 159 | + .replace(/\//g, '_') |
20 | 160 | } |
21 | 161 | } |
0 commit comments