@bytescale/sdk 3.11.0 → 3.12.0
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/dist/browser/cjs/main.js +89 -16
- package/dist/browser/esm/main.mjs +89 -16
- package/dist/types/private/ConsoleUtils.d.ts +1 -0
- package/dist/types/private/dtos/AuthSwConfigDto.d.ts +2 -0
- package/dist/types/private/dtos/AuthSwConfigEntryDto.d.ts +6 -0
- package/dist/types/private/dtos/AuthSwHeaderDto.d.ts +4 -0
- package/dist/types/private/dtos/AuthSwSetConfigDto.d.ts +5 -0
- package/dist/types/private/model/AuthManagerInterface.d.ts +42 -0
- package/dist/types/private/model/AuthSession.d.ts +1 -0
- package/dist/types/public/browser/AuthManagerBrowser.d.ts +10 -0
- package/dist/types/public/shared/generated/runtime.d.ts +2 -2
- package/package.json +4 -2
package/dist/browser/cjs/main.js
CHANGED
|
@@ -2372,6 +2372,11 @@ var ConsoleUtils = /*#__PURE__*/function () {
|
|
|
2372
2372
|
ConsoleUtils_classCallCheck(this, ConsoleUtils);
|
|
2373
2373
|
}
|
|
2374
2374
|
ConsoleUtils_createClass(ConsoleUtils, null, [{
|
|
2375
|
+
key: "debug",
|
|
2376
|
+
value: function debug(message) {
|
|
2377
|
+
console.log(ConsoleUtils.prefix(message));
|
|
2378
|
+
}
|
|
2379
|
+
}, {
|
|
2375
2380
|
key: "warn",
|
|
2376
2381
|
value: function warn(message) {
|
|
2377
2382
|
console.warn(ConsoleUtils.prefix(message));
|
|
@@ -2480,22 +2485,25 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2480
2485
|
value: function beginAuthSession(params) {
|
|
2481
2486
|
try {
|
|
2482
2487
|
var _this2 = this;
|
|
2483
|
-
var newSession = {
|
|
2484
|
-
accessToken: undefined,
|
|
2485
|
-
accessTokenRefreshHandle: undefined,
|
|
2486
|
-
params: params,
|
|
2487
|
-
isActive: true
|
|
2488
|
-
};
|
|
2489
2488
|
return AuthManagerBrowser_await(_this2.authSessionMutex.safe(AuthManagerBrowser_async(function () {
|
|
2490
2489
|
// We check both 'session' and 'sessionDisposing' here, as we don't want to call 'beginAuthSession' until the session is fully disposed.
|
|
2491
2490
|
if (_this2.isAuthSessionActive()) {
|
|
2492
2491
|
throw new Error("Auth session already active. Please call 'await endAuthSession()' and then call 'await beginAuthSession(...)' to start a new auth session.");
|
|
2493
2492
|
}
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
|
|
2493
|
+
return AuthManagerBrowser_await(_this2.registerAuthServiceWorker(params), function (_this2$registerAuthSe) {
|
|
2494
|
+
var newSession = {
|
|
2495
|
+
accessToken: undefined,
|
|
2496
|
+
accessTokenRefreshHandle: undefined,
|
|
2497
|
+
params: params,
|
|
2498
|
+
isActive: true,
|
|
2499
|
+
authServiceWorker: _this2$registerAuthSe
|
|
2500
|
+
};
|
|
2501
|
+
AuthSessionState.setSession(newSession);
|
|
2502
|
+
return newSession;
|
|
2503
|
+
});
|
|
2504
|
+
})), function (session) {
|
|
2497
2505
|
// IMPORTANT: must be called outside the above, else re-entrant deadlock will occur.
|
|
2498
|
-
return AuthManagerBrowser_awaitIgnored(_this2.refreshAccessToken(
|
|
2506
|
+
return AuthManagerBrowser_awaitIgnored(_this2.refreshAccessToken(session));
|
|
2499
2507
|
});
|
|
2500
2508
|
} catch (e) {
|
|
2501
2509
|
return Promise.reject(e);
|
|
@@ -2516,12 +2524,55 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2516
2524
|
if (session.accessTokenRefreshHandle !== undefined) {
|
|
2517
2525
|
clearTimeout(session.accessTokenRefreshHandle);
|
|
2518
2526
|
}
|
|
2519
|
-
return
|
|
2527
|
+
return AuthManagerBrowser_await(_this4.deleteAccessToken(session.params), function () {
|
|
2528
|
+
_this4.setServiceWorkerConfig(session, []); // Prevent service worker from authorizing subsequent requests.
|
|
2529
|
+
});
|
|
2520
2530
|
})));
|
|
2521
2531
|
} catch (e) {
|
|
2522
2532
|
return Promise.reject(e);
|
|
2523
2533
|
}
|
|
2524
2534
|
}
|
|
2535
|
+
/**
|
|
2536
|
+
* Idempotent.
|
|
2537
|
+
*
|
|
2538
|
+
* Only returns once the service worker has been activated.
|
|
2539
|
+
*
|
|
2540
|
+
* We don't need to unregister it: we just need to clear the config when auth ends.
|
|
2541
|
+
*/
|
|
2542
|
+
}, {
|
|
2543
|
+
key: "registerAuthServiceWorker",
|
|
2544
|
+
value: function registerAuthServiceWorker(_ref) {
|
|
2545
|
+
var serviceWorkerScript = _ref.serviceWorkerScript;
|
|
2546
|
+
try {
|
|
2547
|
+
if (serviceWorkerScript === undefined) {
|
|
2548
|
+
return undefined;
|
|
2549
|
+
}
|
|
2550
|
+
return AuthManagerBrowser_catch(function () {
|
|
2551
|
+
if ("serviceWorker" in navigator) {
|
|
2552
|
+
return AuthManagerBrowser_await(navigator.serviceWorker.register(serviceWorkerScript), function (registration) {
|
|
2553
|
+
return registration.active !== null ? registration.active : AuthManagerBrowser_awaitIgnored(new Promise(function (resolve) {
|
|
2554
|
+
registration.addEventListener("updatefound", function () {
|
|
2555
|
+
var newWorker = registration.installing;
|
|
2556
|
+
if (newWorker !== null) {
|
|
2557
|
+
newWorker.addEventListener("statechange", function () {
|
|
2558
|
+
if (newWorker.state === "activated") {
|
|
2559
|
+
resolve(newWorker);
|
|
2560
|
+
}
|
|
2561
|
+
});
|
|
2562
|
+
}
|
|
2563
|
+
});
|
|
2564
|
+
}));
|
|
2565
|
+
}); // Occurs when: (service worker is already registered AND there's been no code changes) OR (service worker was not previously registered).
|
|
2566
|
+
} else {
|
|
2567
|
+
ConsoleUtils.warn("Service workers not supported by browser. Falling back to Bytescale CDN cookies for auth. These are third-party cookies, which some modern browsers block.");
|
|
2568
|
+
}
|
|
2569
|
+
}, function (e) {
|
|
2570
|
+
throw new Error("Failed to install Bytescale Auth Service Worker (SW). ".concat(e.name, ": ").concat(e.message));
|
|
2571
|
+
});
|
|
2572
|
+
} catch (e) {
|
|
2573
|
+
return Promise.reject(e);
|
|
2574
|
+
}
|
|
2575
|
+
}
|
|
2525
2576
|
}, {
|
|
2526
2577
|
key: "refreshAccessToken",
|
|
2527
2578
|
value: function refreshAccessToken(session) {
|
|
@@ -2539,12 +2590,20 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2539
2590
|
return AuthManagerBrowser_await(session.params.authHeaders(), function (_session$params$authH) {
|
|
2540
2591
|
return AuthManagerBrowser_await(_getAccessToken.call(_this6, _session$params, _session$params$authH), function (jwt) {
|
|
2541
2592
|
return AuthManagerBrowser_await(_this6.setAccessToken(session.params, jwt), function (setTokenResult) {
|
|
2593
|
+
_this6.setServiceWorkerConfig(session, [{
|
|
2594
|
+
headers: [{
|
|
2595
|
+
key: "Authorization-Token",
|
|
2596
|
+
value: jwt
|
|
2597
|
+
}],
|
|
2598
|
+
expires: Date.now() + setTokenResult.ttlSeconds * 1000,
|
|
2599
|
+
urlPrefix: "".concat(_this6.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
|
|
2600
|
+
}]);
|
|
2542
2601
|
var desiredTtl = setTokenResult.ttlSeconds - _this6.refreshBeforeExpirySeconds;
|
|
2543
2602
|
timeout = Math.max(desiredTtl, _this6.minJwtTtlSeconds);
|
|
2544
2603
|
if (desiredTtl !== timeout) {
|
|
2545
2604
|
ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(timeout, " seconds before refreshing."));
|
|
2546
2605
|
}
|
|
2547
|
-
// There's need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
|
|
2606
|
+
// There's no need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
|
|
2548
2607
|
timeout = Math.min(timeout, _this6.maxJwtTtlSeconds);
|
|
2549
2608
|
session.accessToken = setTokenResult.accessToken;
|
|
2550
2609
|
});
|
|
@@ -2554,7 +2613,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2554
2613
|
// Use 'warn' instead of 'error' since this happens frequently, i.e. user goes through a tunnel, and some customers report these errors to systems like Sentry, so we don't want to spam.
|
|
2555
2614
|
ConsoleUtils.warn("Unable to refresh JWT access token: ".concat(e));
|
|
2556
2615
|
});
|
|
2557
|
-
}, function (_wasThrown,
|
|
2616
|
+
}, function (_wasThrown, _result3) {
|
|
2558
2617
|
session.accessTokenRefreshHandle = window.setTimeout(function () {
|
|
2559
2618
|
_this6.refreshAccessToken(session).then(function () {},
|
|
2560
2619
|
// Should not occur, as this method shouldn't throw errors.
|
|
@@ -2562,19 +2621,33 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2562
2621
|
return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
|
|
2563
2622
|
});
|
|
2564
2623
|
}, timeout * 1000);
|
|
2565
|
-
return AuthManagerBrowser_rethrow(_wasThrown,
|
|
2624
|
+
return AuthManagerBrowser_rethrow(_wasThrown, _result3);
|
|
2566
2625
|
}));
|
|
2567
2626
|
})));
|
|
2568
2627
|
} catch (e) {
|
|
2569
2628
|
return Promise.reject(e);
|
|
2570
2629
|
}
|
|
2571
2630
|
}
|
|
2631
|
+
}, {
|
|
2632
|
+
key: "setServiceWorkerConfig",
|
|
2633
|
+
value: function setServiceWorkerConfig(session, config) {
|
|
2634
|
+
var _a;
|
|
2635
|
+
var message = {
|
|
2636
|
+
type: "SET_CONFIG",
|
|
2637
|
+
config: config
|
|
2638
|
+
};
|
|
2639
|
+
(_a = session.authServiceWorker) === null || _a === void 0 ? void 0 : _a.postMessage(message);
|
|
2640
|
+
}
|
|
2572
2641
|
}, {
|
|
2573
2642
|
key: "getAccessTokenUrl",
|
|
2574
2643
|
value: function getAccessTokenUrl(params) {
|
|
2644
|
+
return "".concat(this.getCdnUrl(params), "/api/v1/access_tokens/").concat(params.accountId);
|
|
2645
|
+
}
|
|
2646
|
+
}, {
|
|
2647
|
+
key: "getCdnUrl",
|
|
2648
|
+
value: function getCdnUrl(params) {
|
|
2575
2649
|
var _a;
|
|
2576
|
-
|
|
2577
|
-
return "".concat(cdnUrl, "/api/v1/access_tokens/").concat(params.accountId);
|
|
2650
|
+
return BytescaleApiClientConfigUtils.getCdnUrl((_a = params.options) !== null && _a !== void 0 ? _a : {});
|
|
2578
2651
|
}
|
|
2579
2652
|
}, {
|
|
2580
2653
|
key: "deleteAccessToken",
|
|
@@ -2357,6 +2357,11 @@ var ConsoleUtils = /*#__PURE__*/function () {
|
|
|
2357
2357
|
ConsoleUtils_classCallCheck(this, ConsoleUtils);
|
|
2358
2358
|
}
|
|
2359
2359
|
ConsoleUtils_createClass(ConsoleUtils, null, [{
|
|
2360
|
+
key: "debug",
|
|
2361
|
+
value: function debug(message) {
|
|
2362
|
+
console.log(ConsoleUtils.prefix(message));
|
|
2363
|
+
}
|
|
2364
|
+
}, {
|
|
2360
2365
|
key: "warn",
|
|
2361
2366
|
value: function warn(message) {
|
|
2362
2367
|
console.warn(ConsoleUtils.prefix(message));
|
|
@@ -2465,22 +2470,25 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2465
2470
|
value: function beginAuthSession(params) {
|
|
2466
2471
|
try {
|
|
2467
2472
|
var _this2 = this;
|
|
2468
|
-
var newSession = {
|
|
2469
|
-
accessToken: undefined,
|
|
2470
|
-
accessTokenRefreshHandle: undefined,
|
|
2471
|
-
params: params,
|
|
2472
|
-
isActive: true
|
|
2473
|
-
};
|
|
2474
2473
|
return AuthManagerBrowser_await(_this2.authSessionMutex.safe(AuthManagerBrowser_async(function () {
|
|
2475
2474
|
// We check both 'session' and 'sessionDisposing' here, as we don't want to call 'beginAuthSession' until the session is fully disposed.
|
|
2476
2475
|
if (_this2.isAuthSessionActive()) {
|
|
2477
2476
|
throw new Error("Auth session already active. Please call 'await endAuthSession()' and then call 'await beginAuthSession(...)' to start a new auth session.");
|
|
2478
2477
|
}
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2478
|
+
return AuthManagerBrowser_await(_this2.registerAuthServiceWorker(params), function (_this2$registerAuthSe) {
|
|
2479
|
+
var newSession = {
|
|
2480
|
+
accessToken: undefined,
|
|
2481
|
+
accessTokenRefreshHandle: undefined,
|
|
2482
|
+
params: params,
|
|
2483
|
+
isActive: true,
|
|
2484
|
+
authServiceWorker: _this2$registerAuthSe
|
|
2485
|
+
};
|
|
2486
|
+
AuthSessionState.setSession(newSession);
|
|
2487
|
+
return newSession;
|
|
2488
|
+
});
|
|
2489
|
+
})), function (session) {
|
|
2482
2490
|
// IMPORTANT: must be called outside the above, else re-entrant deadlock will occur.
|
|
2483
|
-
return AuthManagerBrowser_awaitIgnored(_this2.refreshAccessToken(
|
|
2491
|
+
return AuthManagerBrowser_awaitIgnored(_this2.refreshAccessToken(session));
|
|
2484
2492
|
});
|
|
2485
2493
|
} catch (e) {
|
|
2486
2494
|
return Promise.reject(e);
|
|
@@ -2501,12 +2509,55 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2501
2509
|
if (session.accessTokenRefreshHandle !== undefined) {
|
|
2502
2510
|
clearTimeout(session.accessTokenRefreshHandle);
|
|
2503
2511
|
}
|
|
2504
|
-
return
|
|
2512
|
+
return AuthManagerBrowser_await(_this4.deleteAccessToken(session.params), function () {
|
|
2513
|
+
_this4.setServiceWorkerConfig(session, []); // Prevent service worker from authorizing subsequent requests.
|
|
2514
|
+
});
|
|
2505
2515
|
})));
|
|
2506
2516
|
} catch (e) {
|
|
2507
2517
|
return Promise.reject(e);
|
|
2508
2518
|
}
|
|
2509
2519
|
}
|
|
2520
|
+
/**
|
|
2521
|
+
* Idempotent.
|
|
2522
|
+
*
|
|
2523
|
+
* Only returns once the service worker has been activated.
|
|
2524
|
+
*
|
|
2525
|
+
* We don't need to unregister it: we just need to clear the config when auth ends.
|
|
2526
|
+
*/
|
|
2527
|
+
}, {
|
|
2528
|
+
key: "registerAuthServiceWorker",
|
|
2529
|
+
value: function registerAuthServiceWorker(_ref) {
|
|
2530
|
+
var serviceWorkerScript = _ref.serviceWorkerScript;
|
|
2531
|
+
try {
|
|
2532
|
+
if (serviceWorkerScript === undefined) {
|
|
2533
|
+
return undefined;
|
|
2534
|
+
}
|
|
2535
|
+
return AuthManagerBrowser_catch(function () {
|
|
2536
|
+
if ("serviceWorker" in navigator) {
|
|
2537
|
+
return AuthManagerBrowser_await(navigator.serviceWorker.register(serviceWorkerScript), function (registration) {
|
|
2538
|
+
return registration.active !== null ? registration.active : AuthManagerBrowser_awaitIgnored(new Promise(function (resolve) {
|
|
2539
|
+
registration.addEventListener("updatefound", function () {
|
|
2540
|
+
var newWorker = registration.installing;
|
|
2541
|
+
if (newWorker !== null) {
|
|
2542
|
+
newWorker.addEventListener("statechange", function () {
|
|
2543
|
+
if (newWorker.state === "activated") {
|
|
2544
|
+
resolve(newWorker);
|
|
2545
|
+
}
|
|
2546
|
+
});
|
|
2547
|
+
}
|
|
2548
|
+
});
|
|
2549
|
+
}));
|
|
2550
|
+
}); // Occurs when: (service worker is already registered AND there's been no code changes) OR (service worker was not previously registered).
|
|
2551
|
+
} else {
|
|
2552
|
+
ConsoleUtils.warn("Service workers not supported by browser. Falling back to Bytescale CDN cookies for auth. These are third-party cookies, which some modern browsers block.");
|
|
2553
|
+
}
|
|
2554
|
+
}, function (e) {
|
|
2555
|
+
throw new Error("Failed to install Bytescale Auth Service Worker (SW). ".concat(e.name, ": ").concat(e.message));
|
|
2556
|
+
});
|
|
2557
|
+
} catch (e) {
|
|
2558
|
+
return Promise.reject(e);
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2510
2561
|
}, {
|
|
2511
2562
|
key: "refreshAccessToken",
|
|
2512
2563
|
value: function refreshAccessToken(session) {
|
|
@@ -2524,12 +2575,20 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2524
2575
|
return AuthManagerBrowser_await(session.params.authHeaders(), function (_session$params$authH) {
|
|
2525
2576
|
return AuthManagerBrowser_await(_getAccessToken.call(_this6, _session$params, _session$params$authH), function (jwt) {
|
|
2526
2577
|
return AuthManagerBrowser_await(_this6.setAccessToken(session.params, jwt), function (setTokenResult) {
|
|
2578
|
+
_this6.setServiceWorkerConfig(session, [{
|
|
2579
|
+
headers: [{
|
|
2580
|
+
key: "Authorization-Token",
|
|
2581
|
+
value: jwt
|
|
2582
|
+
}],
|
|
2583
|
+
expires: Date.now() + setTokenResult.ttlSeconds * 1000,
|
|
2584
|
+
urlPrefix: "".concat(_this6.getCdnUrl(session.params), "/").concat(session.params.accountId, "/")
|
|
2585
|
+
}]);
|
|
2527
2586
|
var desiredTtl = setTokenResult.ttlSeconds - _this6.refreshBeforeExpirySeconds;
|
|
2528
2587
|
timeout = Math.max(desiredTtl, _this6.minJwtTtlSeconds);
|
|
2529
2588
|
if (desiredTtl !== timeout) {
|
|
2530
2589
|
ConsoleUtils.warn("JWT expiration is too short: waiting for ".concat(timeout, " seconds before refreshing."));
|
|
2531
2590
|
}
|
|
2532
|
-
// There's need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
|
|
2591
|
+
// There's no need to print a warning for this: it's OK to silently request the JWT before it expires. Also, this is 24 days in this case!
|
|
2533
2592
|
timeout = Math.min(timeout, _this6.maxJwtTtlSeconds);
|
|
2534
2593
|
session.accessToken = setTokenResult.accessToken;
|
|
2535
2594
|
});
|
|
@@ -2539,7 +2598,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2539
2598
|
// Use 'warn' instead of 'error' since this happens frequently, i.e. user goes through a tunnel, and some customers report these errors to systems like Sentry, so we don't want to spam.
|
|
2540
2599
|
ConsoleUtils.warn("Unable to refresh JWT access token: ".concat(e));
|
|
2541
2600
|
});
|
|
2542
|
-
}, function (_wasThrown,
|
|
2601
|
+
}, function (_wasThrown, _result3) {
|
|
2543
2602
|
session.accessTokenRefreshHandle = window.setTimeout(function () {
|
|
2544
2603
|
_this6.refreshAccessToken(session).then(function () {},
|
|
2545
2604
|
// Should not occur, as this method shouldn't throw errors.
|
|
@@ -2547,19 +2606,33 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
2547
2606
|
return ConsoleUtils.error("Unexpected error when refreshing JWT access token: ".concat(e));
|
|
2548
2607
|
});
|
|
2549
2608
|
}, timeout * 1000);
|
|
2550
|
-
return AuthManagerBrowser_rethrow(_wasThrown,
|
|
2609
|
+
return AuthManagerBrowser_rethrow(_wasThrown, _result3);
|
|
2551
2610
|
}));
|
|
2552
2611
|
})));
|
|
2553
2612
|
} catch (e) {
|
|
2554
2613
|
return Promise.reject(e);
|
|
2555
2614
|
}
|
|
2556
2615
|
}
|
|
2616
|
+
}, {
|
|
2617
|
+
key: "setServiceWorkerConfig",
|
|
2618
|
+
value: function setServiceWorkerConfig(session, config) {
|
|
2619
|
+
var _a;
|
|
2620
|
+
var message = {
|
|
2621
|
+
type: "SET_CONFIG",
|
|
2622
|
+
config: config
|
|
2623
|
+
};
|
|
2624
|
+
(_a = session.authServiceWorker) === null || _a === void 0 ? void 0 : _a.postMessage(message);
|
|
2625
|
+
}
|
|
2557
2626
|
}, {
|
|
2558
2627
|
key: "getAccessTokenUrl",
|
|
2559
2628
|
value: function getAccessTokenUrl(params) {
|
|
2629
|
+
return "".concat(this.getCdnUrl(params), "/api/v1/access_tokens/").concat(params.accountId);
|
|
2630
|
+
}
|
|
2631
|
+
}, {
|
|
2632
|
+
key: "getCdnUrl",
|
|
2633
|
+
value: function getCdnUrl(params) {
|
|
2560
2634
|
var _a;
|
|
2561
|
-
|
|
2562
|
-
return "".concat(cdnUrl, "/api/v1/access_tokens/").concat(params.accountId);
|
|
2635
|
+
return BytescaleApiClientConfigUtils.getCdnUrl((_a = params.options) !== null && _a !== void 0 ? _a : {});
|
|
2563
2636
|
}
|
|
2564
2637
|
}, {
|
|
2565
2638
|
key: "deleteAccessToken",
|
|
@@ -18,6 +18,48 @@ export interface BeginAuthSessionParams {
|
|
|
18
18
|
* Optional configuration.
|
|
19
19
|
*/
|
|
20
20
|
options?: Pick<BytescaleApiClientConfig, "fetchApi" | "cdnUrl">;
|
|
21
|
+
/**
|
|
22
|
+
* The Bytescale Auth Service Worker enables JWT-based auth for browsers that block third-party cookies.
|
|
23
|
+
*
|
|
24
|
+
* You must set this field if you need to support browsers that block third-party cookies (like Safari).
|
|
25
|
+
*
|
|
26
|
+
* This feature works by running a "service worker" in the background that adds "Authorization" and "Authorization-Token"
|
|
27
|
+
* request headers to HTTP requests made to the Bytescale CDN. This allows the Bytescale CDN to authorize requests
|
|
28
|
+
* to private files using JWTs issued by your application. Historically, these requests have been authorized using
|
|
29
|
+
* JWT cookies (i.e. JWTs sent to the Bytescale CDN via the "Cookies" header). However, modern browsers are starting
|
|
30
|
+
* to block these cookies, meaning "Authorization" request headers must be used instead. Authorization headers can
|
|
31
|
+
* only be added to requests originating from page elements like "<img>" elements through the use of service workers.
|
|
32
|
+
*
|
|
33
|
+
* Usage:
|
|
34
|
+
*
|
|
35
|
+
* 1. Create a JavaScript file that contains the following line:
|
|
36
|
+
*
|
|
37
|
+
* importScripts("https://js.bytescale.com/auth-sw/v1");
|
|
38
|
+
*
|
|
39
|
+
* 2. Host this JavaScript file from your website:
|
|
40
|
+
*
|
|
41
|
+
* 2a. It MUST be under the ROOT directory of your website.
|
|
42
|
+
* (e.g. "/bytescale-auth-sw.js")
|
|
43
|
+
*
|
|
44
|
+
* 2b. It MUST be on the SAME DOMAIN as your website.
|
|
45
|
+
* (e.g. "www.example.com" and not "assets.example.com")
|
|
46
|
+
*
|
|
47
|
+
* 3. Specify the absolute path to your JavaScript file in the 'beginAuthSession' call.
|
|
48
|
+
* (e.g. { ..., serviceWorkerScript: "/bytescale-auth-sw.js" })
|
|
49
|
+
*
|
|
50
|
+
* Examples:
|
|
51
|
+
*
|
|
52
|
+
* - CORRECT: "/bytescale-auth-sw.js"
|
|
53
|
+
* - INCORRECT: "bytescale-auth-sw.js"
|
|
54
|
+
* - INCORRECT: "/scripts/bytescale-auth-sw.js"
|
|
55
|
+
* - INCORRECT: "https://example.com/bytescale-auth-sw.js"
|
|
56
|
+
*
|
|
57
|
+
* Why does the script need to be hosted on the website's domain, under the root directory?:
|
|
58
|
+
*
|
|
59
|
+
* Service workers can only interact with events raised by pages at the same level or below them, hence why your
|
|
60
|
+
* script must be hosted on your website's domain in the root directory.
|
|
61
|
+
*/
|
|
62
|
+
serviceWorkerScript: string | undefined;
|
|
21
63
|
}
|
|
22
64
|
export interface AuthManagerInterface {
|
|
23
65
|
/**
|
|
@@ -2,6 +2,7 @@ import { BeginAuthSessionParams } from "./AuthManagerInterface";
|
|
|
2
2
|
export interface AuthSession {
|
|
3
3
|
accessToken: string | undefined;
|
|
4
4
|
accessTokenRefreshHandle: number | undefined;
|
|
5
|
+
authServiceWorker: ServiceWorker | undefined;
|
|
5
6
|
isActive: boolean;
|
|
6
7
|
params: BeginAuthSessionParams;
|
|
7
8
|
}
|
|
@@ -12,8 +12,18 @@ declare class AuthManagerImpl implements AuthManagerInterface {
|
|
|
12
12
|
isAuthSessionActive(): boolean;
|
|
13
13
|
beginAuthSession(params: BeginAuthSessionParams): Promise<void>;
|
|
14
14
|
endAuthSession(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Idempotent.
|
|
17
|
+
*
|
|
18
|
+
* Only returns once the service worker has been activated.
|
|
19
|
+
*
|
|
20
|
+
* We don't need to unregister it: we just need to clear the config when auth ends.
|
|
21
|
+
*/
|
|
22
|
+
private registerAuthServiceWorker;
|
|
15
23
|
private refreshAccessToken;
|
|
24
|
+
private setServiceWorkerConfig;
|
|
16
25
|
private getAccessTokenUrl;
|
|
26
|
+
private getCdnUrl;
|
|
17
27
|
private deleteAccessToken;
|
|
18
28
|
private setAccessToken;
|
|
19
29
|
private getAccessToken;
|
|
@@ -24,11 +24,11 @@ export interface BytescaleApiClientConfig {
|
|
|
24
24
|
*/
|
|
25
25
|
apiKey: string;
|
|
26
26
|
/**
|
|
27
|
-
* The base URL of the Bytescale API.
|
|
27
|
+
* The base URL of the Bytescale API. (Excludes trailing "/".)
|
|
28
28
|
*/
|
|
29
29
|
apiUrl?: string;
|
|
30
30
|
/**
|
|
31
|
-
* The base URL of the Bytescale CDN.
|
|
31
|
+
* The base URL of the Bytescale CDN. (Excludes trailing "/".)
|
|
32
32
|
*/
|
|
33
33
|
cdnUrl?: string;
|
|
34
34
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bytescale/sdk",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.12.0",
|
|
4
4
|
"description": "Bytescale JavaScript SDK",
|
|
5
5
|
"author": "Bytescale <hello@bytescale.com> (https://www.bytescale.com)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -67,7 +67,9 @@
|
|
|
67
67
|
"publish:executeIfReleaseCommit": "bash -c 'COMMIT=$(git log -1 --pretty=%B) && [ \"${COMMIT:0:8}\" != \"Release \" ] || npm run publish:execute'",
|
|
68
68
|
"publish:execute": "npm run publish:cdn && npm publish && npm run publish:createGitHubRelease",
|
|
69
69
|
"publish:createGitHubRelease": "gh release create v$(node -p \"require('./package.json').version\")",
|
|
70
|
-
"publish:cdn": "npm run
|
|
70
|
+
"publish:cdn": "npm run publish:cdn:sdk && npm run publish:cdn:authSw",
|
|
71
|
+
"publish:cdn:sdk": "npm run prepack:cdn && aws s3 cp --recursive --content-type text/javascript dist/ s3://upload-js-releases/sdk/ && aws cloudfront create-invalidation --distribution-id E250290WAJ43YY --paths '/sdk/*'",
|
|
72
|
+
"publish:cdn:authSw": "aws s3 cp --content-type text/javascript src/index.auth-sw.js s3://upload-js-releases/auth-sw/v1 && aws cloudfront create-invalidation --distribution-id E250290WAJ43YY --paths '/auth-sw/*'"
|
|
71
73
|
},
|
|
72
74
|
"husky": {
|
|
73
75
|
"hooks": {
|