-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathsession.ts
120 lines (113 loc) · 4.38 KB
/
session.ts
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
import {normalizeStoreFqdn} from './context/fqdn.js'
import {BugError} from './error.js'
import {getPartnersToken} from './environment.js'
import * as secureStore from '../../private/node/session/store.js'
import {exchangeCustomPartnerToken} from '../../private/node/session/exchange.js'
import {outputContent, outputToken, outputDebug} from '../../public/node/output.js'
import {ensureAuthenticated} from '../../private/node/session.js'
/**
* Session Object to access the Admin API, includes the token and the store FQDN.
*/
export interface AdminSession {
token: string
storeFqdn: string
}
/**
* Ensure that we have a valid session to access the Partners API.
* If SHOPIFY_CLI_PARTNERS_TOKEN exists, that token will be used to obtain a valid Partners Token
* If SHOPIFY_CLI_PARTNERS_TOKEN exists, scopes will be ignored.
*
* @param scopes - Optional array of extra scopes to authenticate with.
* @param _env - Optional environment variables to use.
* @returns The access token for the Partners API.
*/
export async function ensureAuthenticatedPartners(scopes: string[] = [], _env = process.env): Promise<string> {
outputDebug(outputContent`Ensuring that the user is authenticated with the Partners API with the following scopes:
${outputToken.json(scopes)}
`)
const envToken = getPartnersToken()
if (envToken) {
return (await exchangeCustomPartnerToken(envToken)).accessToken
}
const tokens = await ensureAuthenticated({partnersApi: {scopes}})
if (!tokens.partners) {
throw new BugError('No partners token found after ensuring authenticated')
}
return tokens.partners
}
/**
* Ensure that we have a valid session to access the Storefront API.
*
* @param scopes - Optional array of extra scopes to authenticate with.
* @param password - Optional password to use.
* @returns The access token for the Storefront API.
*/
export async function ensureAuthenticatedStorefront(
scopes: string[] = [],
password: string | undefined = undefined,
): Promise<string> {
if (password) return password
outputDebug(outputContent`Ensuring that the user is authenticated with the Storefront API with the following scopes:
${outputToken.json(scopes)}
`)
const tokens = await ensureAuthenticated({storefrontRendererApi: {scopes}})
if (!tokens.storefront) {
throw new BugError('No storefront token found after ensuring authenticated')
}
return tokens.storefront
}
/**
* Ensure that we have a valid Admin session for the given store.
*
* @param store - Store fqdn to request auth for.
* @param scopes - Optional array of extra scopes to authenticate with.
* @param forceRefresh - Optional flag to force a refresh of the token.
* @returns The access token for the Admin API.
*/
export async function ensureAuthenticatedAdmin(
store: string,
scopes: string[] = [],
forceRefresh = false,
): Promise<AdminSession> {
outputDebug(outputContent`Ensuring that the user is authenticated with the Admin API with the following scopes for the store ${outputToken.raw(
store,
)}:
${outputToken.json(scopes)}
`)
const tokens = await ensureAuthenticated({adminApi: {scopes, storeFqdn: store}}, process.env, forceRefresh)
if (!tokens.admin) {
throw new BugError('No admin token found after ensuring authenticated')
}
return tokens.admin
}
/**
* Ensure that we have a valid session to access the Theme API.
* If a password is provided, that token will be used against Theme Access API.
* Otherwise, it will ensure that the user is authenticated with the Admin API.
*
* @param store - Store fqdn to request auth for.
* @param password - Password generated from Theme Access app.
* @param scopes - Optional array of extra scopes to authenticate with.
* @param forceRefresh - Optional flag to force a refresh of the token.
* @returns The access token and store.
*/
export async function ensureAuthenticatedThemes(
store: string,
password: string | undefined,
scopes: string[] = [],
forceRefresh = false,
): Promise<AdminSession> {
outputDebug(outputContent`Ensuring that the user is authenticated with the Theme API with the following scopes:
${outputToken.json(scopes)}
`)
if (password) return {token: password, storeFqdn: await normalizeStoreFqdn(store)}
return ensureAuthenticatedAdmin(store, scopes, forceRefresh)
}
/**
* Logout from Shopify.
*
* @returns A promise that resolves when the logout is complete.
*/
export function logout(): Promise<void> {
return secureStore.remove()
}