@wix/sdk 1.13.1 → 1.14.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.
|
@@ -42,6 +42,7 @@ import { parsePublicKeyIfEncoded } from '../helpers.js';
|
|
|
42
42
|
export function AppStrategy(opts) {
|
|
43
43
|
const authServerBaseUrl = opts.authServerBaseUrl ?? 'https://www.wixapis.com';
|
|
44
44
|
let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
|
|
45
|
+
let cachedToken;
|
|
45
46
|
return {
|
|
46
47
|
getInstallUrl({ redirectUrl, token, state }) {
|
|
47
48
|
const params = new URLSearchParams();
|
|
@@ -94,6 +95,13 @@ export function AppStrategy(opts) {
|
|
|
94
95
|
};
|
|
95
96
|
},
|
|
96
97
|
async getAuthHeaders() {
|
|
98
|
+
if (cachedToken && cachedToken.expiresAt > Date.now()) {
|
|
99
|
+
return {
|
|
100
|
+
headers: {
|
|
101
|
+
Authorization: cachedToken.token,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}
|
|
97
105
|
if ('refreshToken' in opts || refreshToken) {
|
|
98
106
|
if (!opts.appSecret) {
|
|
99
107
|
throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
|
|
@@ -143,6 +151,10 @@ export function AppStrategy(opts) {
|
|
|
143
151
|
throw new Error(`Failed to exchange instance ID for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
144
152
|
}
|
|
145
153
|
const tokens = (await tokensRes.json());
|
|
154
|
+
cachedToken = {
|
|
155
|
+
token: tokens.access_token,
|
|
156
|
+
expiresAt: Date.now() + tokens.expires_in * 1000,
|
|
157
|
+
};
|
|
146
158
|
return {
|
|
147
159
|
headers: {
|
|
148
160
|
Authorization: tokens.access_token,
|
|
@@ -150,9 +162,28 @@ export function AppStrategy(opts) {
|
|
|
150
162
|
};
|
|
151
163
|
}
|
|
152
164
|
else if ('accessToken' in opts && opts.accessToken) {
|
|
165
|
+
const tokenRes = await fetch(new URL('/oauth2/token', authServerBaseUrl), {
|
|
166
|
+
method: 'POST',
|
|
167
|
+
headers: {
|
|
168
|
+
'Content-Type': 'application/json',
|
|
169
|
+
},
|
|
170
|
+
body: JSON.stringify({
|
|
171
|
+
grant_type: 'strip_bound_session',
|
|
172
|
+
client_secret: opts.appSecret,
|
|
173
|
+
access_token: opts.accessToken,
|
|
174
|
+
}),
|
|
175
|
+
});
|
|
176
|
+
if (tokenRes.status !== 200) {
|
|
177
|
+
throw new Error(`Failed to get unbound token. Unexpected status code from Wix OAuth API: ${tokenRes.status}`);
|
|
178
|
+
}
|
|
179
|
+
const { access_token, expires_in } = (await tokenRes.json());
|
|
180
|
+
cachedToken = {
|
|
181
|
+
token: access_token,
|
|
182
|
+
expiresAt: Date.now() + expires_in * 1000,
|
|
183
|
+
};
|
|
153
184
|
return {
|
|
154
185
|
headers: {
|
|
155
|
-
Authorization:
|
|
186
|
+
Authorization: access_token,
|
|
156
187
|
},
|
|
157
188
|
};
|
|
158
189
|
}
|
|
@@ -68,6 +68,7 @@ const helpers_js_1 = require("../helpers.js");
|
|
|
68
68
|
function AppStrategy(opts) {
|
|
69
69
|
const authServerBaseUrl = opts.authServerBaseUrl ?? 'https://www.wixapis.com';
|
|
70
70
|
let refreshToken = 'refreshToken' in opts ? opts.refreshToken : undefined;
|
|
71
|
+
let cachedToken;
|
|
71
72
|
return {
|
|
72
73
|
getInstallUrl({ redirectUrl, token, state }) {
|
|
73
74
|
const params = new URLSearchParams();
|
|
@@ -120,6 +121,13 @@ function AppStrategy(opts) {
|
|
|
120
121
|
};
|
|
121
122
|
},
|
|
122
123
|
async getAuthHeaders() {
|
|
124
|
+
if (cachedToken && cachedToken.expiresAt > Date.now()) {
|
|
125
|
+
return {
|
|
126
|
+
headers: {
|
|
127
|
+
Authorization: cachedToken.token,
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
}
|
|
123
131
|
if ('refreshToken' in opts || refreshToken) {
|
|
124
132
|
if (!opts.appSecret) {
|
|
125
133
|
throw new Error('App secret is required for retrieveing app-level access tokens. Make sure to pass it to the AppStrategy');
|
|
@@ -169,6 +177,10 @@ function AppStrategy(opts) {
|
|
|
169
177
|
throw new Error(`Failed to exchange instance ID for access token. Unexpected status code from Wix OAuth API: ${tokensRes.status}`);
|
|
170
178
|
}
|
|
171
179
|
const tokens = (await tokensRes.json());
|
|
180
|
+
cachedToken = {
|
|
181
|
+
token: tokens.access_token,
|
|
182
|
+
expiresAt: Date.now() + tokens.expires_in * 1000,
|
|
183
|
+
};
|
|
172
184
|
return {
|
|
173
185
|
headers: {
|
|
174
186
|
Authorization: tokens.access_token,
|
|
@@ -176,9 +188,28 @@ function AppStrategy(opts) {
|
|
|
176
188
|
};
|
|
177
189
|
}
|
|
178
190
|
else if ('accessToken' in opts && opts.accessToken) {
|
|
191
|
+
const tokenRes = await fetch(new URL('/oauth2/token', authServerBaseUrl), {
|
|
192
|
+
method: 'POST',
|
|
193
|
+
headers: {
|
|
194
|
+
'Content-Type': 'application/json',
|
|
195
|
+
},
|
|
196
|
+
body: JSON.stringify({
|
|
197
|
+
grant_type: 'strip_bound_session',
|
|
198
|
+
client_secret: opts.appSecret,
|
|
199
|
+
access_token: opts.accessToken,
|
|
200
|
+
}),
|
|
201
|
+
});
|
|
202
|
+
if (tokenRes.status !== 200) {
|
|
203
|
+
throw new Error(`Failed to get unbound token. Unexpected status code from Wix OAuth API: ${tokenRes.status}`);
|
|
204
|
+
}
|
|
205
|
+
const { access_token, expires_in } = (await tokenRes.json());
|
|
206
|
+
cachedToken = {
|
|
207
|
+
token: access_token,
|
|
208
|
+
expiresAt: Date.now() + expires_in * 1000,
|
|
209
|
+
};
|
|
179
210
|
return {
|
|
180
211
|
headers: {
|
|
181
|
-
Authorization:
|
|
212
|
+
Authorization: access_token,
|
|
182
213
|
},
|
|
183
214
|
};
|
|
184
215
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.1",
|
|
4
4
|
"license": "UNLICENSED",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Ronny Ringel",
|
|
@@ -119,5 +119,5 @@
|
|
|
119
119
|
"wallaby": {
|
|
120
120
|
"autoDetect": true
|
|
121
121
|
},
|
|
122
|
-
"falconPackageHash": "
|
|
122
|
+
"falconPackageHash": "ef0b5611a3bb59558e1a3fc6bdcfa01b80c1090dab21eaa4403bea26"
|
|
123
123
|
}
|