@wix/sdk 1.15.12 → 1.15.14
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/build/auth/SiteSessionAuth.d.ts +23 -0
- package/build/auth/SiteSessionAuth.js +95 -0
- package/build/auth/oauth2/OAuthStrategy.js +20 -20
- package/cjs/build/auth/SiteSessionAuth.d.ts +23 -0
- package/cjs/build/auth/SiteSessionAuth.js +98 -0
- package/cjs/build/auth/oauth2/OAuthStrategy.js +20 -20
- package/package.json +13 -9
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RefreshToken, Tokens } from './oauth2/types.js';
|
|
2
|
+
export declare function SiteSessionAuth(config: {
|
|
3
|
+
clientId: string;
|
|
4
|
+
publicKey?: string;
|
|
5
|
+
tokens?: Tokens;
|
|
6
|
+
}): {
|
|
7
|
+
generateVisitorTokens: (tokens?: Partial<Tokens>) => Promise<Tokens>;
|
|
8
|
+
renewToken: (refreshToken: RefreshToken) => Promise<Tokens>;
|
|
9
|
+
getAuthHeaders: () => Promise<{
|
|
10
|
+
headers: {
|
|
11
|
+
Authorization: string;
|
|
12
|
+
};
|
|
13
|
+
}>;
|
|
14
|
+
setTokens: (tokens: Tokens) => void;
|
|
15
|
+
getTokens: () => Tokens;
|
|
16
|
+
};
|
|
17
|
+
export interface TokenResponse {
|
|
18
|
+
access_token: string;
|
|
19
|
+
expires_in: number;
|
|
20
|
+
refresh_token: string | null;
|
|
21
|
+
token_type: string;
|
|
22
|
+
scope?: string | null;
|
|
23
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { biHeaderGenerator } from '../bi/biHeaderGenerator.js';
|
|
2
|
+
import { DEFAULT_API_URL } from '../common.js';
|
|
3
|
+
import { createAccessToken, isTokenExpired } from '../tokenHelpers.js';
|
|
4
|
+
import { TokenRole } from './oauth2/types.js';
|
|
5
|
+
export function SiteSessionAuth(config) {
|
|
6
|
+
const _tokens = config.tokens || {
|
|
7
|
+
accessToken: { value: '', expiresAt: 0 },
|
|
8
|
+
refreshToken: { value: '', role: TokenRole.NONE },
|
|
9
|
+
};
|
|
10
|
+
const setTokens = (tokens) => {
|
|
11
|
+
_tokens.accessToken = tokens.accessToken;
|
|
12
|
+
_tokens.refreshToken = tokens.refreshToken;
|
|
13
|
+
};
|
|
14
|
+
const getAuthHeaders = async () => {
|
|
15
|
+
if (!_tokens.accessToken?.value || isTokenExpired(_tokens.accessToken)) {
|
|
16
|
+
const tokens = await generateVisitorTokens({
|
|
17
|
+
refreshToken: _tokens.refreshToken,
|
|
18
|
+
});
|
|
19
|
+
setTokens(tokens);
|
|
20
|
+
}
|
|
21
|
+
return Promise.resolve({
|
|
22
|
+
headers: { Authorization: _tokens.accessToken.value },
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
const generateVisitorTokens = async (tokens) => {
|
|
26
|
+
if (tokens?.accessToken?.value &&
|
|
27
|
+
tokens?.refreshToken?.value &&
|
|
28
|
+
!isTokenExpired(tokens.accessToken)) {
|
|
29
|
+
return tokens;
|
|
30
|
+
}
|
|
31
|
+
if (tokens?.refreshToken?.value) {
|
|
32
|
+
try {
|
|
33
|
+
const newTokens = await renewToken(tokens.refreshToken);
|
|
34
|
+
return newTokens;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
// just continue and create a visitor one
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const tokensResponse = await fetchTokens({
|
|
41
|
+
clientId: config.clientId,
|
|
42
|
+
grantType: 'anonymous',
|
|
43
|
+
});
|
|
44
|
+
return {
|
|
45
|
+
accessToken: createAccessToken(tokensResponse.access_token, tokensResponse.expires_in),
|
|
46
|
+
refreshToken: {
|
|
47
|
+
value: tokensResponse.refresh_token,
|
|
48
|
+
role: TokenRole.VISITOR,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const renewToken = async (refreshToken) => {
|
|
53
|
+
const tokensResponse = await fetchTokens({
|
|
54
|
+
refreshToken: refreshToken.value,
|
|
55
|
+
grantType: 'refresh_token',
|
|
56
|
+
});
|
|
57
|
+
const accessToken = createAccessToken(tokensResponse.access_token, tokensResponse.expires_in);
|
|
58
|
+
return {
|
|
59
|
+
accessToken,
|
|
60
|
+
refreshToken,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
return {
|
|
64
|
+
generateVisitorTokens,
|
|
65
|
+
renewToken,
|
|
66
|
+
getAuthHeaders,
|
|
67
|
+
setTokens,
|
|
68
|
+
getTokens: () => _tokens,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
const fetchTokens = async (payload, headers = {}) => {
|
|
72
|
+
const res = await fetch(`https://${DEFAULT_API_URL}/oauth2/token`, {
|
|
73
|
+
method: 'POST',
|
|
74
|
+
body: JSON.stringify(payload),
|
|
75
|
+
headers: {
|
|
76
|
+
...biHeaderGenerator({
|
|
77
|
+
entityFqdn: 'wix.identity.oauth.v1.refresh_token',
|
|
78
|
+
methodFqn: 'wix.identity.oauth2.v1.Oauth2Ng.Token',
|
|
79
|
+
packageName: '@wix/sdk',
|
|
80
|
+
}),
|
|
81
|
+
'Content-Type': 'application/json',
|
|
82
|
+
...headers,
|
|
83
|
+
},
|
|
84
|
+
});
|
|
85
|
+
if (res.status !== 200) {
|
|
86
|
+
let responseJson;
|
|
87
|
+
try {
|
|
88
|
+
responseJson = await res.json();
|
|
89
|
+
}
|
|
90
|
+
catch { }
|
|
91
|
+
throw new Error(`Failed to fetch tokens from OAuth API: ${res.statusText}. request id: ${res.headers.get('x-request-id')}. ${responseJson ? `Response: ${JSON.stringify(responseJson)}` : ''}`);
|
|
92
|
+
}
|
|
93
|
+
const json = await res.json();
|
|
94
|
+
return json;
|
|
95
|
+
};
|
|
@@ -142,25 +142,20 @@ export function OAuthStrategy(config) {
|
|
|
142
142
|
else if (state !== oauthData.state) {
|
|
143
143
|
throw new Error('Invalid _state');
|
|
144
144
|
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
catch (e) {
|
|
162
|
-
throw new Error('Failed to get member tokens');
|
|
163
|
-
}
|
|
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: createAccessToken(tokensResponse.access_token, tokensResponse.expires_in),
|
|
154
|
+
refreshToken: {
|
|
155
|
+
value: tokensResponse.refresh_token,
|
|
156
|
+
role: TokenRole.MEMBER,
|
|
157
|
+
},
|
|
158
|
+
};
|
|
164
159
|
};
|
|
165
160
|
const logout = async (originalUrl) => {
|
|
166
161
|
const { redirectSession } = await wixClientWithTokens.redirects.createRedirectSession({
|
|
@@ -372,7 +367,12 @@ const fetchTokens = async (payload, headers = {}) => {
|
|
|
372
367
|
},
|
|
373
368
|
});
|
|
374
369
|
if (res.status !== 200) {
|
|
375
|
-
|
|
370
|
+
let responseJson;
|
|
371
|
+
try {
|
|
372
|
+
responseJson = await res.json();
|
|
373
|
+
}
|
|
374
|
+
catch { }
|
|
375
|
+
throw new Error(`Failed to fetch tokens from OAuth API: ${res.statusText}. request id: ${res.headers.get('x-request-id')}. ${responseJson ? `Response: ${JSON.stringify(responseJson)}` : ''}`);
|
|
376
376
|
}
|
|
377
377
|
const json = await res.json();
|
|
378
378
|
return json;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { RefreshToken, Tokens } from './oauth2/types.js';
|
|
2
|
+
export declare function SiteSessionAuth(config: {
|
|
3
|
+
clientId: string;
|
|
4
|
+
publicKey?: string;
|
|
5
|
+
tokens?: Tokens;
|
|
6
|
+
}): {
|
|
7
|
+
generateVisitorTokens: (tokens?: Partial<Tokens>) => Promise<Tokens>;
|
|
8
|
+
renewToken: (refreshToken: RefreshToken) => Promise<Tokens>;
|
|
9
|
+
getAuthHeaders: () => Promise<{
|
|
10
|
+
headers: {
|
|
11
|
+
Authorization: string;
|
|
12
|
+
};
|
|
13
|
+
}>;
|
|
14
|
+
setTokens: (tokens: Tokens) => void;
|
|
15
|
+
getTokens: () => Tokens;
|
|
16
|
+
};
|
|
17
|
+
export interface TokenResponse {
|
|
18
|
+
access_token: string;
|
|
19
|
+
expires_in: number;
|
|
20
|
+
refresh_token: string | null;
|
|
21
|
+
token_type: string;
|
|
22
|
+
scope?: string | null;
|
|
23
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SiteSessionAuth = SiteSessionAuth;
|
|
4
|
+
const biHeaderGenerator_js_1 = require("../bi/biHeaderGenerator.js");
|
|
5
|
+
const common_js_1 = require("../common.js");
|
|
6
|
+
const tokenHelpers_js_1 = require("../tokenHelpers.js");
|
|
7
|
+
const types_js_1 = require("./oauth2/types.js");
|
|
8
|
+
function SiteSessionAuth(config) {
|
|
9
|
+
const _tokens = config.tokens || {
|
|
10
|
+
accessToken: { value: '', expiresAt: 0 },
|
|
11
|
+
refreshToken: { value: '', role: types_js_1.TokenRole.NONE },
|
|
12
|
+
};
|
|
13
|
+
const setTokens = (tokens) => {
|
|
14
|
+
_tokens.accessToken = tokens.accessToken;
|
|
15
|
+
_tokens.refreshToken = tokens.refreshToken;
|
|
16
|
+
};
|
|
17
|
+
const getAuthHeaders = async () => {
|
|
18
|
+
if (!_tokens.accessToken?.value || (0, tokenHelpers_js_1.isTokenExpired)(_tokens.accessToken)) {
|
|
19
|
+
const tokens = await generateVisitorTokens({
|
|
20
|
+
refreshToken: _tokens.refreshToken,
|
|
21
|
+
});
|
|
22
|
+
setTokens(tokens);
|
|
23
|
+
}
|
|
24
|
+
return Promise.resolve({
|
|
25
|
+
headers: { Authorization: _tokens.accessToken.value },
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
const generateVisitorTokens = async (tokens) => {
|
|
29
|
+
if (tokens?.accessToken?.value &&
|
|
30
|
+
tokens?.refreshToken?.value &&
|
|
31
|
+
!(0, tokenHelpers_js_1.isTokenExpired)(tokens.accessToken)) {
|
|
32
|
+
return tokens;
|
|
33
|
+
}
|
|
34
|
+
if (tokens?.refreshToken?.value) {
|
|
35
|
+
try {
|
|
36
|
+
const newTokens = await renewToken(tokens.refreshToken);
|
|
37
|
+
return newTokens;
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
// just continue and create a visitor one
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const tokensResponse = await fetchTokens({
|
|
44
|
+
clientId: config.clientId,
|
|
45
|
+
grantType: 'anonymous',
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
accessToken: (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in),
|
|
49
|
+
refreshToken: {
|
|
50
|
+
value: tokensResponse.refresh_token,
|
|
51
|
+
role: types_js_1.TokenRole.VISITOR,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
const renewToken = async (refreshToken) => {
|
|
56
|
+
const tokensResponse = await fetchTokens({
|
|
57
|
+
refreshToken: refreshToken.value,
|
|
58
|
+
grantType: 'refresh_token',
|
|
59
|
+
});
|
|
60
|
+
const accessToken = (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in);
|
|
61
|
+
return {
|
|
62
|
+
accessToken,
|
|
63
|
+
refreshToken,
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
return {
|
|
67
|
+
generateVisitorTokens,
|
|
68
|
+
renewToken,
|
|
69
|
+
getAuthHeaders,
|
|
70
|
+
setTokens,
|
|
71
|
+
getTokens: () => _tokens,
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
const fetchTokens = async (payload, headers = {}) => {
|
|
75
|
+
const res = await fetch(`https://${common_js_1.DEFAULT_API_URL}/oauth2/token`, {
|
|
76
|
+
method: 'POST',
|
|
77
|
+
body: JSON.stringify(payload),
|
|
78
|
+
headers: {
|
|
79
|
+
...(0, biHeaderGenerator_js_1.biHeaderGenerator)({
|
|
80
|
+
entityFqdn: 'wix.identity.oauth.v1.refresh_token',
|
|
81
|
+
methodFqn: 'wix.identity.oauth2.v1.Oauth2Ng.Token',
|
|
82
|
+
packageName: '@wix/sdk',
|
|
83
|
+
}),
|
|
84
|
+
'Content-Type': 'application/json',
|
|
85
|
+
...headers,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
if (res.status !== 200) {
|
|
89
|
+
let responseJson;
|
|
90
|
+
try {
|
|
91
|
+
responseJson = await res.json();
|
|
92
|
+
}
|
|
93
|
+
catch { }
|
|
94
|
+
throw new Error(`Failed to fetch tokens from OAuth API: ${res.statusText}. request id: ${res.headers.get('x-request-id')}. ${responseJson ? `Response: ${JSON.stringify(responseJson)}` : ''}`);
|
|
95
|
+
}
|
|
96
|
+
const json = await res.json();
|
|
97
|
+
return json;
|
|
98
|
+
};
|
|
@@ -145,25 +145,20 @@ function OAuthStrategy(config) {
|
|
|
145
145
|
else if (state !== oauthData.state) {
|
|
146
146
|
throw new Error('Invalid _state');
|
|
147
147
|
}
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
catch (e) {
|
|
165
|
-
throw new Error('Failed to get member tokens');
|
|
166
|
-
}
|
|
148
|
+
const tokensResponse = await fetchTokens({
|
|
149
|
+
clientId: config.clientId,
|
|
150
|
+
grantType: 'authorization_code',
|
|
151
|
+
...(oauthData.redirectUri && { redirectUri: oauthData.redirectUri }),
|
|
152
|
+
code,
|
|
153
|
+
codeVerifier: oauthData.codeVerifier,
|
|
154
|
+
});
|
|
155
|
+
return {
|
|
156
|
+
accessToken: (0, tokenHelpers_js_1.createAccessToken)(tokensResponse.access_token, tokensResponse.expires_in),
|
|
157
|
+
refreshToken: {
|
|
158
|
+
value: tokensResponse.refresh_token,
|
|
159
|
+
role: types_js_1.TokenRole.MEMBER,
|
|
160
|
+
},
|
|
161
|
+
};
|
|
167
162
|
};
|
|
168
163
|
const logout = async (originalUrl) => {
|
|
169
164
|
const { redirectSession } = await wixClientWithTokens.redirects.createRedirectSession({
|
|
@@ -375,7 +370,12 @@ const fetchTokens = async (payload, headers = {}) => {
|
|
|
375
370
|
},
|
|
376
371
|
});
|
|
377
372
|
if (res.status !== 200) {
|
|
378
|
-
|
|
373
|
+
let responseJson;
|
|
374
|
+
try {
|
|
375
|
+
responseJson = await res.json();
|
|
376
|
+
}
|
|
377
|
+
catch { }
|
|
378
|
+
throw new Error(`Failed to fetch tokens from OAuth API: ${res.statusText}. request id: ${res.headers.get('x-request-id')}. ${responseJson ? `Response: ${JSON.stringify(responseJson)}` : ''}`);
|
|
379
379
|
}
|
|
380
380
|
const json = await res.json();
|
|
381
381
|
return json;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/sdk",
|
|
3
|
-
"version": "1.15.
|
|
3
|
+
"version": "1.15.14",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Ronny Ringel",
|
|
@@ -27,6 +27,10 @@
|
|
|
27
27
|
"import": "./build/auth/oauth2/OAuthStrategy.js",
|
|
28
28
|
"require": "./cjs/build/auth/oauth2/OAuthStrategy.js"
|
|
29
29
|
},
|
|
30
|
+
"./auth/site-session": {
|
|
31
|
+
"import": "./build/auth/SiteSessionAuth.js",
|
|
32
|
+
"require": "./cjs/build/auth/SiteSessionAuth.js"
|
|
33
|
+
},
|
|
30
34
|
"./auth/api-key": {
|
|
31
35
|
"import": "./build/auth/ApiKeyAuthStrategy.js",
|
|
32
36
|
"require": "./cjs/build/auth/ApiKeyAuthStrategy.js"
|
|
@@ -72,30 +76,30 @@
|
|
|
72
76
|
"@wix/image-kit": "^1.102.0",
|
|
73
77
|
"@wix/redirects": "^1.0.70",
|
|
74
78
|
"@wix/sdk-context": "0.0.1",
|
|
75
|
-
"@wix/sdk-runtime": "0.3.
|
|
76
|
-
"@wix/sdk-types": "^1.13.
|
|
79
|
+
"@wix/sdk-runtime": "0.3.38",
|
|
80
|
+
"@wix/sdk-types": "^1.13.6",
|
|
77
81
|
"jose": "^5.10.0",
|
|
78
|
-
"type-fest": "^4.
|
|
82
|
+
"type-fest": "^4.37.0"
|
|
79
83
|
},
|
|
80
84
|
"optionalDependencies": {
|
|
81
85
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0 || ^17.0.0"
|
|
82
86
|
},
|
|
83
87
|
"devDependencies": {
|
|
84
88
|
"@types/is-ci": "^3.0.4",
|
|
85
|
-
"@types/node": "^20.17.
|
|
89
|
+
"@types/node": "^20.17.24",
|
|
86
90
|
"@vitest/ui": "^1.6.1",
|
|
87
91
|
"@wix/ecom": "^1.0.886",
|
|
88
92
|
"@wix/events": "^1.0.382",
|
|
89
93
|
"@wix/metro": "^1.0.93",
|
|
90
94
|
"@wix/metro-runtime": "^1.1891.0",
|
|
91
|
-
"@wix/sdk-runtime": "0.3.
|
|
95
|
+
"@wix/sdk-runtime": "0.3.38",
|
|
92
96
|
"eslint": "^8.57.1",
|
|
93
97
|
"eslint-config-sdk": "0.0.0",
|
|
94
98
|
"graphql": "^16.8.0",
|
|
95
99
|
"is-ci": "^3.0.1",
|
|
96
100
|
"jsdom": "^22.1.0",
|
|
97
|
-
"msw": "^2.7.
|
|
98
|
-
"typescript": "^5.
|
|
101
|
+
"msw": "^2.7.3",
|
|
102
|
+
"typescript": "^5.8.2",
|
|
99
103
|
"vitest": "^1.6.1",
|
|
100
104
|
"vitest-teamcity-reporter": "^0.3.1"
|
|
101
105
|
},
|
|
@@ -122,5 +126,5 @@
|
|
|
122
126
|
"wallaby": {
|
|
123
127
|
"autoDetect": true
|
|
124
128
|
},
|
|
125
|
-
"falconPackageHash": "
|
|
129
|
+
"falconPackageHash": "d16eec14806eb8569de493f1244e12fae4f2b2cdffdbd7528b9849c4"
|
|
126
130
|
}
|