@wix/sdk 1.5.9 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/auth/api-key/package.json +3 -0
- package/auth/oauth2/package.json +3 -0
- package/auth/wix-app-oauth/package.json +3 -0
- package/build/ambassador-modules.d.ts +31 -0
- package/build/ambassador-modules.js +89 -0
- package/build/auth/ApiKeyAuthStrategy.d.ts +16 -0
- package/build/auth/ApiKeyAuthStrategy.js +22 -0
- package/build/auth/WixAppOAuthStrategy.d.ts +54 -0
- package/build/auth/WixAppOAuthStrategy.js +106 -0
- package/build/auth/oauth2/OAuthStrategy.d.ts +12 -0
- package/build/auth/oauth2/OAuthStrategy.js +357 -0
- package/build/auth/oauth2/constants.d.ts +5 -0
- package/build/auth/oauth2/constants.js +5 -0
- package/build/auth/oauth2/pkce-challenge.d.ts +5 -0
- package/build/auth/oauth2/pkce-challenge.js +33 -0
- package/build/auth/oauth2/types.d.ts +121 -0
- package/build/auth/oauth2/types.js +16 -0
- package/build/bi/biHeaderGenerator.d.ts +12 -0
- package/build/bi/biHeaderGenerator.js +17 -0
- package/build/common.d.ts +7 -0
- package/build/common.js +4 -0
- package/build/fetch-error.d.ts +9 -0
- package/build/fetch-error.js +31 -0
- package/build/helpers.d.ts +4 -0
- package/build/helpers.js +11 -0
- package/build/host-modules.d.ts +3 -0
- package/build/host-modules.js +5 -0
- package/build/iframeUtils.d.ts +4 -0
- package/build/iframeUtils.js +43 -0
- package/build/index.d.ts +8 -388
- package/build/index.js +9 -1115
- package/build/rest-modules.d.ts +7 -0
- package/build/rest-modules.js +82 -0
- package/build/tokenHelpers.d.ts +4 -0
- package/build/tokenHelpers.js +11 -0
- package/build/wixClient.d.ts +70 -0
- package/build/wixClient.js +86 -0
- package/build/wixMedia.d.ts +46 -0
- package/build/wixMedia.js +156 -0
- package/cjs/build/ambassador-modules.d.ts +31 -0
- package/cjs/build/ambassador-modules.js +95 -0
- package/cjs/build/auth/ApiKeyAuthStrategy.d.ts +16 -0
- package/cjs/build/auth/ApiKeyAuthStrategy.js +26 -0
- package/cjs/build/auth/WixAppOAuthStrategy.d.ts +54 -0
- package/cjs/build/auth/WixAppOAuthStrategy.js +110 -0
- package/cjs/build/auth/oauth2/OAuthStrategy.d.ts +12 -0
- package/cjs/build/auth/oauth2/OAuthStrategy.js +361 -0
- package/cjs/build/auth/oauth2/constants.d.ts +5 -0
- package/cjs/build/auth/oauth2/constants.js +8 -0
- package/cjs/build/auth/oauth2/pkce-challenge.d.ts +5 -0
- package/cjs/build/auth/oauth2/pkce-challenge.js +41 -0
- package/cjs/build/auth/oauth2/types.d.ts +121 -0
- package/cjs/build/auth/oauth2/types.js +19 -0
- package/cjs/build/bi/biHeaderGenerator.d.ts +12 -0
- package/cjs/build/bi/biHeaderGenerator.js +21 -0
- package/cjs/build/common.d.ts +7 -0
- package/cjs/build/common.js +7 -0
- package/cjs/build/fetch-error.d.ts +9 -0
- package/cjs/build/fetch-error.js +35 -0
- package/cjs/build/helpers.d.ts +4 -0
- package/cjs/build/helpers.js +16 -0
- package/cjs/build/host-modules.d.ts +3 -0
- package/cjs/build/host-modules.js +10 -0
- package/cjs/build/iframeUtils.d.ts +4 -0
- package/cjs/build/iframeUtils.js +50 -0
- package/cjs/build/index.d.ts +9 -0
- package/cjs/build/index.js +28 -0
- package/cjs/build/rest-modules.d.ts +7 -0
- package/cjs/build/rest-modules.js +87 -0
- package/cjs/build/tokenHelpers.d.ts +4 -0
- package/cjs/build/tokenHelpers.js +17 -0
- package/cjs/build/wixClient.d.ts +70 -0
- package/cjs/build/wixClient.js +90 -0
- package/cjs/build/wixMedia.d.ts +46 -0
- package/cjs/build/wixMedia.js +160 -0
- package/cjs/package.json +3 -0
- package/client/package.json +3 -0
- package/package.json +45 -22
- package/build/browser/index.mjs +0 -1075
- package/build/index.d.mts +0 -389
- package/build/index.mjs +0 -1066
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { AuthenticationStrategy } from '@wix/sdk-types';
|
|
2
|
+
export type WixAppOAuthStrategy = AuthenticationStrategy & {
|
|
3
|
+
getInstallUrl({ redirectUrl }: {
|
|
4
|
+
redirectUrl: string;
|
|
5
|
+
}): string;
|
|
6
|
+
handleOAuthCallback(url: string, opts?: {
|
|
7
|
+
state: string;
|
|
8
|
+
}): Promise<{
|
|
9
|
+
instanceId: string;
|
|
10
|
+
accessToken: string;
|
|
11
|
+
refreshToken: string;
|
|
12
|
+
}>;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Creates an authentication strategy for Wix Apps OAuth installation process.
|
|
16
|
+
* Use this authentication strategy when making requests to Wix APIs from your Wix App backend.
|
|
17
|
+
* @param opts Options for initializing the authentication strategy
|
|
18
|
+
* @param opts.appId The Wix App ID
|
|
19
|
+
* @param opts.appSecret The Wix App Secret
|
|
20
|
+
* @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
|
|
21
|
+
* @returns An authentication strategy that can be used with WixClient
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* import { WixAppOAuthStrategy, createClient } from '@wix/sdk';
|
|
25
|
+
* import { products } from '@wix/stores';
|
|
26
|
+
*
|
|
27
|
+
* const client = createClient({
|
|
28
|
+
* auth: WixAppOAuthStrategy({
|
|
29
|
+
* appId: 'appId',
|
|
30
|
+
* appSecret: 'appSecret',
|
|
31
|
+
* }),
|
|
32
|
+
* modules: { products },
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* const installUrl = client.auth.getInstallUrl({ redirectUrl: 'https://example.com' });
|
|
36
|
+
* // Redirect the user to the installUrl
|
|
37
|
+
*
|
|
38
|
+
* ...
|
|
39
|
+
*
|
|
40
|
+
* // in the callback handler of your http server
|
|
41
|
+
* // req.url is the url of the callback request
|
|
42
|
+
* const { instanceId, refreshToken } = await client.auth.handleOAuthCallback(req.url);
|
|
43
|
+
*
|
|
44
|
+
* // store the instanceId and refreshToken in your database
|
|
45
|
+
* // use the authorized client
|
|
46
|
+
* const products = await client.products.queryProducts().find();
|
|
47
|
+
*
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
export declare function WixAppOAuthStrategy(opts: {
|
|
51
|
+
appId: string;
|
|
52
|
+
appSecret: string;
|
|
53
|
+
refreshToken?: string;
|
|
54
|
+
}): WixAppOAuthStrategy;
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WixAppOAuthStrategy = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Creates an authentication strategy for Wix Apps OAuth installation process.
|
|
6
|
+
* Use this authentication strategy when making requests to Wix APIs from your Wix App backend.
|
|
7
|
+
* @param opts Options for initializing the authentication strategy
|
|
8
|
+
* @param opts.appId The Wix App ID
|
|
9
|
+
* @param opts.appSecret The Wix App Secret
|
|
10
|
+
* @param opts.refreshToken An optional refresh token previously retrieved from Wix OAuth API
|
|
11
|
+
* @returns An authentication strategy that can be used with WixClient
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { WixAppOAuthStrategy, createClient } from '@wix/sdk';
|
|
15
|
+
* import { products } from '@wix/stores';
|
|
16
|
+
*
|
|
17
|
+
* const client = createClient({
|
|
18
|
+
* auth: WixAppOAuthStrategy({
|
|
19
|
+
* appId: 'appId',
|
|
20
|
+
* appSecret: 'appSecret',
|
|
21
|
+
* }),
|
|
22
|
+
* modules: { products },
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* const installUrl = client.auth.getInstallUrl({ redirectUrl: 'https://example.com' });
|
|
26
|
+
* // Redirect the user to the installUrl
|
|
27
|
+
*
|
|
28
|
+
* ...
|
|
29
|
+
*
|
|
30
|
+
* // in the callback handler of your http server
|
|
31
|
+
* // req.url is the url of the callback request
|
|
32
|
+
* const { instanceId, refreshToken } = await client.auth.handleOAuthCallback(req.url);
|
|
33
|
+
*
|
|
34
|
+
* // store the instanceId and refreshToken in your database
|
|
35
|
+
* // use the authorized client
|
|
36
|
+
* const products = await client.products.queryProducts().find();
|
|
37
|
+
*
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-redeclare
|
|
41
|
+
function WixAppOAuthStrategy(opts) {
|
|
42
|
+
let refreshToken = opts.refreshToken;
|
|
43
|
+
return {
|
|
44
|
+
getInstallUrl({ redirectUrl }) {
|
|
45
|
+
return `https://www.wix.com/installer/install?appId=${opts.appId}&redirectUrl=${redirectUrl}`;
|
|
46
|
+
},
|
|
47
|
+
async handleOAuthCallback(url, oauthOpts) {
|
|
48
|
+
const params = new URLSearchParams(new URL(url).search);
|
|
49
|
+
const state = params.get('state');
|
|
50
|
+
if (state && oauthOpts?.state && state !== oauthOpts.state) {
|
|
51
|
+
throw new Error(`Invalid OAuth callback URL. Expected state to be "${oauthOpts.state}" but got "${state}"`);
|
|
52
|
+
}
|
|
53
|
+
const code = params.get('code');
|
|
54
|
+
const instanceId = params.get('instanceId');
|
|
55
|
+
if (!code || !instanceId) {
|
|
56
|
+
throw new Error('Invalid OAuth callback URL. Make sure you pass the url including the code and instanceId query params.');
|
|
57
|
+
}
|
|
58
|
+
const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
headers: {
|
|
61
|
+
'Content-Type': 'application/json',
|
|
62
|
+
},
|
|
63
|
+
body: JSON.stringify({
|
|
64
|
+
code,
|
|
65
|
+
client_id: opts.appId,
|
|
66
|
+
client_secret: opts.appSecret,
|
|
67
|
+
grant_type: 'authorization_code',
|
|
68
|
+
}),
|
|
69
|
+
});
|
|
70
|
+
if (tokensRes.status !== 200) {
|
|
71
|
+
throw new Error(`Failed to exchange authorization code for refresh token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
72
|
+
}
|
|
73
|
+
const tokens = await tokensRes.json();
|
|
74
|
+
refreshToken = tokens.refresh_token;
|
|
75
|
+
return {
|
|
76
|
+
instanceId,
|
|
77
|
+
accessToken: tokens.access_token,
|
|
78
|
+
refreshToken: tokens.refresh_token,
|
|
79
|
+
};
|
|
80
|
+
},
|
|
81
|
+
async getAuthHeaders() {
|
|
82
|
+
if (!refreshToken) {
|
|
83
|
+
throw new Error('Missing refresh token. Either pass it to the WixAppOAuthStrategy or use the handleOAuthCallback method to retrieve it.');
|
|
84
|
+
}
|
|
85
|
+
const tokensRes = await fetch('https://www.wixapis.com/oauth/access', {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: {
|
|
88
|
+
'Content-Type': 'application/json',
|
|
89
|
+
},
|
|
90
|
+
body: JSON.stringify({
|
|
91
|
+
refresh_token: refreshToken,
|
|
92
|
+
client_id: opts.appId,
|
|
93
|
+
client_secret: opts.appSecret,
|
|
94
|
+
grant_type: 'refresh_token',
|
|
95
|
+
}),
|
|
96
|
+
});
|
|
97
|
+
if (tokensRes.status !== 200) {
|
|
98
|
+
throw new Error(`Failed to exchange refresh token for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
99
|
+
}
|
|
100
|
+
const tokens = (await tokensRes.json());
|
|
101
|
+
refreshToken = tokens.refresh_token;
|
|
102
|
+
return {
|
|
103
|
+
headers: {
|
|
104
|
+
Authorization: tokens.access_token,
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
exports.WixAppOAuthStrategy = WixAppOAuthStrategy;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IOAuthStrategy, Tokens } from './types.js';
|
|
2
|
+
export declare function OAuthStrategy(config: {
|
|
3
|
+
clientId: string;
|
|
4
|
+
tokens?: Tokens;
|
|
5
|
+
}): IOAuthStrategy;
|
|
6
|
+
export interface TokenResponse {
|
|
7
|
+
access_token: string;
|
|
8
|
+
expires_in: number;
|
|
9
|
+
refresh_token: string | null;
|
|
10
|
+
token_type: string;
|
|
11
|
+
scope?: string | null;
|
|
12
|
+
}
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OAuthStrategy = void 0;
|
|
4
|
+
const wixClient_js_1 = require("../../wixClient.js");
|
|
5
|
+
const redirects_1 = require("@wix/redirects");
|
|
6
|
+
const tokenHelpers_js_1 = require("../../tokenHelpers.js");
|
|
7
|
+
const identity_1 = require("@wix/identity");
|
|
8
|
+
const common_js_1 = require("../../common.js");
|
|
9
|
+
const types_js_1 = require("./types.js");
|
|
10
|
+
const iframeUtils_js_1 = require("../../iframeUtils.js");
|
|
11
|
+
const constants_js_1 = require("./constants.js");
|
|
12
|
+
const biHeaderGenerator_js_1 = require("../../bi/biHeaderGenerator.js");
|
|
13
|
+
const pkce_challenge_js_1 = require("./pkce-challenge.js");
|
|
14
|
+
const moduleWithTokens = { redirects: redirects_1.redirects, authentication: identity_1.authentication, recovery: identity_1.recovery, verification: identity_1.verification };
|
|
15
|
+
function OAuthStrategy(config) {
|
|
16
|
+
const _tokens = config.tokens || {
|
|
17
|
+
accessToken: { value: '', expiresAt: 0 },
|
|
18
|
+
refreshToken: { value: '', role: types_js_1.TokenRole.NONE },
|
|
19
|
+
};
|
|
20
|
+
const setTokens = (tokens) => {
|
|
21
|
+
_tokens.accessToken = tokens.accessToken;
|
|
22
|
+
_tokens.refreshToken = tokens.refreshToken;
|
|
23
|
+
};
|
|
24
|
+
let _state = {
|
|
25
|
+
loginState: types_js_1.LoginState.INITIAL,
|
|
26
|
+
};
|
|
27
|
+
const getAuthHeaders = async () => {
|
|
28
|
+
if (!_tokens.accessToken?.value || (0, tokenHelpers_js_1.isTokenExpired)(_tokens.accessToken)) {
|
|
29
|
+
const tokens = await generateVisitorTokens({
|
|
30
|
+
refreshToken: _tokens.refreshToken,
|
|
31
|
+
});
|
|
32
|
+
setTokens(tokens);
|
|
33
|
+
}
|
|
34
|
+
return Promise.resolve({
|
|
35
|
+
headers: { Authorization: _tokens.accessToken.value },
|
|
36
|
+
});
|
|
37
|
+
};
|
|
38
|
+
const wixClientWithTokens = (0, wixClient_js_1.createClient)({
|
|
39
|
+
modules: moduleWithTokens,
|
|
40
|
+
auth: { getAuthHeaders },
|
|
41
|
+
});
|
|
42
|
+
const generateVisitorTokens = async (tokens) => {
|
|
43
|
+
if (tokens?.accessToken?.value &&
|
|
44
|
+
tokens?.refreshToken?.value &&
|
|
45
|
+
!(0, tokenHelpers_js_1.isTokenExpired)(tokens.accessToken)) {
|
|
46
|
+
return tokens;
|
|
47
|
+
}
|
|
48
|
+
if (tokens?.refreshToken?.value) {
|
|
49
|
+
try {
|
|
50
|
+
const newTokens = await renewToken(tokens.refreshToken);
|
|
51
|
+
return newTokens;
|
|
52
|
+
}
|
|
53
|
+
catch (e) {
|
|
54
|
+
// just continue and create a visitor one
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const tokensResponse = await fetchTokens({
|
|
58
|
+
clientId: config.clientId,
|
|
59
|
+
grantType: 'anonymous',
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
accessToken: (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in),
|
|
63
|
+
refreshToken: {
|
|
64
|
+
value: tokensResponse.refresh_token,
|
|
65
|
+
role: types_js_1.TokenRole.VISITOR,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
const renewToken = async (refreshToken) => {
|
|
70
|
+
const tokensResponse = await fetchTokens({
|
|
71
|
+
refreshToken: refreshToken.value,
|
|
72
|
+
grantType: 'refresh_token',
|
|
73
|
+
});
|
|
74
|
+
const accessToken = (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in);
|
|
75
|
+
return {
|
|
76
|
+
accessToken,
|
|
77
|
+
refreshToken,
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const generatePKCE = () => {
|
|
81
|
+
const pkceState = (0, pkce_challenge_js_1.pkceChallenge)();
|
|
82
|
+
return {
|
|
83
|
+
codeChallenge: pkceState.code_challenge,
|
|
84
|
+
codeVerifier: pkceState.code_verifier,
|
|
85
|
+
state: (0, pkce_challenge_js_1.pkceChallenge)().code_challenge,
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
const generateOAuthData = (redirectUri, originalUri) => {
|
|
89
|
+
const state = { redirectUri };
|
|
90
|
+
const pkceState = generatePKCE();
|
|
91
|
+
return {
|
|
92
|
+
...state,
|
|
93
|
+
originalUri: originalUri ?? '',
|
|
94
|
+
codeChallenge: pkceState.codeChallenge,
|
|
95
|
+
codeVerifier: pkceState.codeVerifier,
|
|
96
|
+
state: (0, pkce_challenge_js_1.pkceChallenge)().code_challenge,
|
|
97
|
+
};
|
|
98
|
+
};
|
|
99
|
+
const getAuthorizationUrlWithOptions = async (oauthData, responseMode, prompt, sessionToken) => {
|
|
100
|
+
const { redirectSession } = await wixClientWithTokens.redirects.createRedirectSession({
|
|
101
|
+
auth: {
|
|
102
|
+
authRequest: {
|
|
103
|
+
redirectUri: oauthData.redirectUri,
|
|
104
|
+
...(oauthData.redirectUri && {
|
|
105
|
+
redirectUri: oauthData.redirectUri,
|
|
106
|
+
}),
|
|
107
|
+
clientId: config.clientId,
|
|
108
|
+
codeChallenge: oauthData.codeChallenge,
|
|
109
|
+
codeChallengeMethod: 'S256',
|
|
110
|
+
responseMode,
|
|
111
|
+
responseType: 'code',
|
|
112
|
+
scope: 'offline_access',
|
|
113
|
+
state: oauthData.state,
|
|
114
|
+
...(sessionToken && { sessionToken }),
|
|
115
|
+
},
|
|
116
|
+
prompt: redirects_1.redirects.Prompt[prompt],
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
return { authUrl: redirectSession.fullUrl };
|
|
120
|
+
};
|
|
121
|
+
const getAuthUrl = async (oauthData, opts = {
|
|
122
|
+
prompt: 'login',
|
|
123
|
+
}) => {
|
|
124
|
+
return getAuthorizationUrlWithOptions(oauthData, opts.responseMode ?? 'fragment', opts.prompt ?? 'login');
|
|
125
|
+
};
|
|
126
|
+
const parseFromUrl = (url, responseMode = 'fragment') => {
|
|
127
|
+
const parsedUrl = new URL(url ?? window.location.href);
|
|
128
|
+
const params = responseMode === 'query'
|
|
129
|
+
? parsedUrl.searchParams
|
|
130
|
+
: new URLSearchParams(parsedUrl.hash.substring(1));
|
|
131
|
+
const code = params.get('code');
|
|
132
|
+
const state = params.get('state');
|
|
133
|
+
const error = params.get('error');
|
|
134
|
+
const errorDescription = params.get('error_description');
|
|
135
|
+
return { code, state, ...(error && { error, errorDescription }) };
|
|
136
|
+
};
|
|
137
|
+
const getMemberTokens = async (code, state, oauthData) => {
|
|
138
|
+
if (!code || !state) {
|
|
139
|
+
throw new Error('Missing code or _state');
|
|
140
|
+
}
|
|
141
|
+
else if (state !== oauthData.state) {
|
|
142
|
+
throw new Error('Invalid _state');
|
|
143
|
+
}
|
|
144
|
+
try {
|
|
145
|
+
const tokensResponse = await fetchTokens({
|
|
146
|
+
clientId: config.clientId,
|
|
147
|
+
grantType: 'authorization_code',
|
|
148
|
+
...(oauthData.redirectUri && { redirectUri: oauthData.redirectUri }),
|
|
149
|
+
code,
|
|
150
|
+
codeVerifier: oauthData.codeVerifier,
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
accessToken: (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in),
|
|
154
|
+
refreshToken: {
|
|
155
|
+
value: tokensResponse.refresh_token,
|
|
156
|
+
role: types_js_1.TokenRole.MEMBER,
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
catch (e) {
|
|
161
|
+
throw new Error('Failed to get member tokens');
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const logout = async (originalUrl) => {
|
|
165
|
+
const { redirectSession } = await wixClientWithTokens.redirects.createRedirectSession({
|
|
166
|
+
logout: { clientId: config.clientId },
|
|
167
|
+
callbacks: {
|
|
168
|
+
postFlowUrl: originalUrl,
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
_tokens.accessToken = { value: '', expiresAt: 0 };
|
|
172
|
+
_tokens.refreshToken = { value: '', role: types_js_1.TokenRole.NONE };
|
|
173
|
+
return { logoutUrl: redirectSession.fullUrl };
|
|
174
|
+
};
|
|
175
|
+
const handleState = (response) => {
|
|
176
|
+
if (response.state === identity_1.authentication.StateType.SUCCESS) {
|
|
177
|
+
return {
|
|
178
|
+
loginState: types_js_1.LoginState.SUCCESS,
|
|
179
|
+
data: { sessionToken: response.sessionToken },
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
else if (response.state === identity_1.authentication.StateType.REQUIRE_OWNER_APPROVAL) {
|
|
183
|
+
return {
|
|
184
|
+
loginState: types_js_1.LoginState.OWNER_APPROVAL_REQUIRED,
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
else if (response.state === identity_1.authentication.StateType.REQUIRE_EMAIL_VERIFICATION) {
|
|
188
|
+
_state = {
|
|
189
|
+
loginState: types_js_1.LoginState.EMAIL_VERIFICATION_REQUIRED,
|
|
190
|
+
data: { stateToken: response.stateToken },
|
|
191
|
+
};
|
|
192
|
+
return _state;
|
|
193
|
+
}
|
|
194
|
+
return {
|
|
195
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
196
|
+
error: 'Unknown _state',
|
|
197
|
+
};
|
|
198
|
+
};
|
|
199
|
+
const register = async (params) => {
|
|
200
|
+
try {
|
|
201
|
+
const res = await wixClientWithTokens.authentication.registerV2({
|
|
202
|
+
email: params.email,
|
|
203
|
+
}, {
|
|
204
|
+
password: params.password,
|
|
205
|
+
profile: params.profile,
|
|
206
|
+
...(params.captchaTokens && {
|
|
207
|
+
captchaTokens: [
|
|
208
|
+
{
|
|
209
|
+
Recaptcha: params.captchaTokens?.recaptchaToken,
|
|
210
|
+
InvisibleRecaptcha: params.captchaTokens?.invisibleRecaptchaToken,
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
}),
|
|
214
|
+
});
|
|
215
|
+
return handleState(res);
|
|
216
|
+
}
|
|
217
|
+
catch (e) {
|
|
218
|
+
const emailValidation = e.details.validationError?.fieldViolations?.find((v) => v.data.type === 'EMAIL');
|
|
219
|
+
if (emailValidation) {
|
|
220
|
+
return {
|
|
221
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
222
|
+
error: emailValidation.description,
|
|
223
|
+
errorCode: 'invalidEmail',
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (e.details.applicationError?.code === constants_js_1.MISSING_CAPTCHA) {
|
|
227
|
+
return {
|
|
228
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
229
|
+
error: e.message,
|
|
230
|
+
errorCode: 'missingCaptchaToken',
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
if (e.details.applicationError?.code === constants_js_1.EMAIL_EXISTS) {
|
|
234
|
+
return {
|
|
235
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
236
|
+
error: e.message,
|
|
237
|
+
errorCode: 'emailAlreadyExists',
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
if (e.details.applicationError?.code === constants_js_1.INVALID_CAPTCHA) {
|
|
241
|
+
return {
|
|
242
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
243
|
+
error: e.message,
|
|
244
|
+
errorCode: 'invalidCaptchaToken',
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
249
|
+
error: e.message,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const login = async (params) => {
|
|
254
|
+
try {
|
|
255
|
+
const res = await wixClientWithTokens.authentication.loginV2({
|
|
256
|
+
email: params.email,
|
|
257
|
+
}, {
|
|
258
|
+
password: params.password,
|
|
259
|
+
...(params.captchaTokens && {
|
|
260
|
+
captchaTokens: [
|
|
261
|
+
{
|
|
262
|
+
Recaptcha: params.captchaTokens?.recaptchaToken,
|
|
263
|
+
InvisibleRecaptcha: params.captchaTokens?.invisibleRecaptchaToken,
|
|
264
|
+
},
|
|
265
|
+
],
|
|
266
|
+
}),
|
|
267
|
+
});
|
|
268
|
+
return handleState(res);
|
|
269
|
+
}
|
|
270
|
+
catch (e) {
|
|
271
|
+
return {
|
|
272
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
273
|
+
error: e.message,
|
|
274
|
+
errorCode: e.details.applicationError?.code === constants_js_1.MISSING_CAPTCHA
|
|
275
|
+
? 'missingCaptchaToken'
|
|
276
|
+
: e.details.applicationError?.code === constants_js_1.INVALID_CAPTCHA
|
|
277
|
+
? 'invalidCaptchaToken'
|
|
278
|
+
: e.details.applicationError.code === constants_js_1.INVALID_PASSWORD
|
|
279
|
+
? 'invalidPassword'
|
|
280
|
+
: e.details.applicationError.code === constants_js_1.RESET_PASSWORD
|
|
281
|
+
? 'resetPassword'
|
|
282
|
+
: 'invalidEmail',
|
|
283
|
+
};
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
const processVerification = async (nextInputs, state) => {
|
|
287
|
+
const stateToUse = state ?? _state;
|
|
288
|
+
if (stateToUse.loginState === types_js_1.LoginState.EMAIL_VERIFICATION_REQUIRED) {
|
|
289
|
+
const code = nextInputs.verificationCode ?? nextInputs.code;
|
|
290
|
+
const res = await wixClientWithTokens.verification.verifyDuringAuthentication(code, { stateToken: stateToUse.data.stateToken });
|
|
291
|
+
return handleState(res);
|
|
292
|
+
}
|
|
293
|
+
return {
|
|
294
|
+
loginState: types_js_1.LoginState.FAILURE,
|
|
295
|
+
error: 'Unknown _state',
|
|
296
|
+
};
|
|
297
|
+
};
|
|
298
|
+
const getMemberTokensForDirectLogin = async (sessionToken) => {
|
|
299
|
+
const oauthPKCE = generatePKCE();
|
|
300
|
+
const { authUrl } = await getAuthorizationUrlWithOptions(oauthPKCE, 'web_message', 'none', sessionToken);
|
|
301
|
+
const iframePromise = (0, iframeUtils_js_1.addPostMessageListener)(oauthPKCE.state);
|
|
302
|
+
const iframeEl = (0, iframeUtils_js_1.loadFrame)(authUrl);
|
|
303
|
+
return iframePromise
|
|
304
|
+
.then((res) => {
|
|
305
|
+
return getMemberTokens(res.code, res.state, oauthPKCE);
|
|
306
|
+
})
|
|
307
|
+
.finally(() => {
|
|
308
|
+
if (document.body.contains(iframeEl)) {
|
|
309
|
+
iframeEl.parentElement?.removeChild(iframeEl);
|
|
310
|
+
}
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
const sendPasswordResetEmail = async (email, redirectUri) => {
|
|
314
|
+
await wixClientWithTokens.recovery.sendRecoveryEmail(email, {
|
|
315
|
+
redirect: { url: redirectUri, clientId: config.clientId },
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
const loggedIn = () => {
|
|
319
|
+
return _tokens.refreshToken.role === types_js_1.TokenRole.MEMBER;
|
|
320
|
+
};
|
|
321
|
+
return {
|
|
322
|
+
generateVisitorTokens,
|
|
323
|
+
renewToken,
|
|
324
|
+
parseFromUrl,
|
|
325
|
+
getAuthUrl,
|
|
326
|
+
getMemberTokens,
|
|
327
|
+
generateOAuthData,
|
|
328
|
+
getAuthHeaders,
|
|
329
|
+
setTokens,
|
|
330
|
+
getTokens: () => _tokens,
|
|
331
|
+
loggedIn,
|
|
332
|
+
logout,
|
|
333
|
+
register,
|
|
334
|
+
processVerification,
|
|
335
|
+
login,
|
|
336
|
+
getMemberTokensForDirectLogin,
|
|
337
|
+
sendPasswordResetEmail,
|
|
338
|
+
captchaInvisibleSiteKey: '6LdoPaUfAAAAAJphvHoUoOob7mx0KDlXyXlgrx5v',
|
|
339
|
+
captchaVisibleSiteKey: '6Ld0J8IcAAAAANyrnxzrRlX1xrrdXsOmsepUYosy',
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
exports.OAuthStrategy = OAuthStrategy;
|
|
343
|
+
const fetchTokens = async (payload) => {
|
|
344
|
+
const res = await fetch(`https://${common_js_1.API_URL}/oauth2/token`, {
|
|
345
|
+
method: 'POST',
|
|
346
|
+
body: JSON.stringify(payload),
|
|
347
|
+
headers: {
|
|
348
|
+
...(0, biHeaderGenerator_js_1.biHeaderGenerator)({
|
|
349
|
+
entityFqdn: 'wix.identity.oauth.v1.refresh_token',
|
|
350
|
+
methodFqn: 'wix.identity.oauth2.v1.Oauth2Ng.Token',
|
|
351
|
+
packageName: '@wix/sdk',
|
|
352
|
+
}),
|
|
353
|
+
'Content-Type': 'application/json',
|
|
354
|
+
},
|
|
355
|
+
});
|
|
356
|
+
if (res.status !== 200) {
|
|
357
|
+
throw new Error('something went wrong');
|
|
358
|
+
}
|
|
359
|
+
const json = await res.json();
|
|
360
|
+
return json;
|
|
361
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RESET_PASSWORD = exports.INVALID_PASSWORD = exports.EMAIL_EXISTS = exports.INVALID_CAPTCHA = exports.MISSING_CAPTCHA = void 0;
|
|
4
|
+
exports.MISSING_CAPTCHA = '-19971';
|
|
5
|
+
exports.INVALID_CAPTCHA = '-19970';
|
|
6
|
+
exports.EMAIL_EXISTS = '-19995';
|
|
7
|
+
exports.INVALID_PASSWORD = '-19976';
|
|
8
|
+
exports.RESET_PASSWORD = '-19973';
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.generateChallenge = exports.pkceChallenge = void 0;
|
|
7
|
+
const sha256_js_1 = __importDefault(require("crypto-js/sha256.js"));
|
|
8
|
+
const enc_base64url_js_1 = __importDefault(require("crypto-js/enc-base64url.js"));
|
|
9
|
+
function pkceChallenge(length) {
|
|
10
|
+
if (!length) {
|
|
11
|
+
length = 43;
|
|
12
|
+
}
|
|
13
|
+
if (length < 43 || length > 128) {
|
|
14
|
+
throw new Error(`Expected a length between 43 and 128. Received ${length}.`);
|
|
15
|
+
}
|
|
16
|
+
const verifier = generateVerifier(length);
|
|
17
|
+
const challenge = generateChallenge(verifier);
|
|
18
|
+
return {
|
|
19
|
+
code_verifier: verifier,
|
|
20
|
+
code_challenge: challenge,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
exports.pkceChallenge = pkceChallenge;
|
|
24
|
+
function generateVerifier(length) {
|
|
25
|
+
return random(length);
|
|
26
|
+
}
|
|
27
|
+
function generateChallenge(code_verifier) {
|
|
28
|
+
return (0, sha256_js_1.default)(code_verifier).toString(enc_base64url_js_1.default);
|
|
29
|
+
}
|
|
30
|
+
exports.generateChallenge = generateChallenge;
|
|
31
|
+
function random(size) {
|
|
32
|
+
const mask = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~';
|
|
33
|
+
let result = '';
|
|
34
|
+
const randomUints = crypto.getRandomValues(new Uint8Array(size));
|
|
35
|
+
for (let i = 0; i < size; i++) {
|
|
36
|
+
// cap the value of the randomIndex to mask.length - 1
|
|
37
|
+
const randomIndex = randomUints[i] % mask.length;
|
|
38
|
+
result += mask[randomIndex];
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|