@bytescale/sdk 3.56.0 → 3.58.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 +138 -56
- package/dist/browser/esm/main.mjs +138 -56
- package/dist/types/private/dtos/AuthSwSetConfigDto.d.ts +2 -0
- package/dist/types/private/model/AuthManagerInterface.d.ts +40 -26
- package/dist/types/private/model/AuthSession.d.ts +5 -1
- package/dist/types/public/browser/AuthManagerBrowser.d.ts +4 -2
- package/dist/types/public/node/AuthManagerNode.d.ts +1 -1
- package/package.json +1 -1
- package/tests/AuthManagerBrowser.test.ts +278 -114
- package/tests/AuthServiceWorkerRewrite.test.ts +276 -0
|
@@ -3156,11 +3156,12 @@ function AuthManagerBrowser_awaitIgnored(value, direct) {
|
|
|
3156
3156
|
return value && value.then ? value.then(AuthManagerBrowser_empty) : Promise.resolve();
|
|
3157
3157
|
}
|
|
3158
3158
|
}
|
|
3159
|
-
function
|
|
3159
|
+
function AuthManagerBrowser_invoke(body, then) {
|
|
3160
3160
|
var result = body();
|
|
3161
3161
|
if (result && result.then) {
|
|
3162
|
-
return result.then(
|
|
3162
|
+
return result.then(then);
|
|
3163
3163
|
}
|
|
3164
|
+
return then(result);
|
|
3164
3165
|
}
|
|
3165
3166
|
function AuthManagerBrowser_await(value, then, direct) {
|
|
3166
3167
|
if (direct) {
|
|
@@ -3183,12 +3184,11 @@ function AuthManagerBrowser_async(f) {
|
|
|
3183
3184
|
}
|
|
3184
3185
|
};
|
|
3185
3186
|
}
|
|
3186
|
-
function
|
|
3187
|
+
function AuthManagerBrowser_invokeIgnored(body) {
|
|
3187
3188
|
var result = body();
|
|
3188
3189
|
if (result && result.then) {
|
|
3189
|
-
return result.then(
|
|
3190
|
+
return result.then(AuthManagerBrowser_empty);
|
|
3190
3191
|
}
|
|
3191
|
-
return then(result);
|
|
3192
3192
|
}
|
|
3193
3193
|
function AuthManagerBrowser_catch(body, recover) {
|
|
3194
3194
|
try {
|
|
@@ -3221,14 +3221,29 @@ function AuthManagerBrowser_continueIgnored(value) {
|
|
|
3221
3221
|
return value.then(AuthManagerBrowser_empty);
|
|
3222
3222
|
}
|
|
3223
3223
|
}
|
|
3224
|
+
function AuthManagerBrowser_call(body, then, direct) {
|
|
3225
|
+
if (direct) {
|
|
3226
|
+
return then ? then(body()) : body();
|
|
3227
|
+
}
|
|
3228
|
+
try {
|
|
3229
|
+
var result = Promise.resolve(body());
|
|
3230
|
+
return then ? result.then(then) : result;
|
|
3231
|
+
} catch (e) {
|
|
3232
|
+
return Promise.reject(e);
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3224
3235
|
function AuthManagerBrowser_continue(value, then) {
|
|
3225
3236
|
return value && value.then ? value.then(then) : then(value);
|
|
3226
3237
|
}
|
|
3227
|
-
function AuthManagerBrowser_typeof(o) { "@babel/helpers - typeof"; return AuthManagerBrowser_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, AuthManagerBrowser_typeof(o); }
|
|
3228
3238
|
function AuthManagerBrowser_defineProperty(e, r, t) { return (r = AuthManagerBrowser_toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
3229
3239
|
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = AuthManagerBrowser_unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
|
|
3240
|
+
function AuthManagerBrowser_toConsumableArray(r) { return AuthManagerBrowser_arrayWithoutHoles(r) || AuthManagerBrowser_iterableToArray(r) || AuthManagerBrowser_unsupportedIterableToArray(r) || AuthManagerBrowser_nonIterableSpread(); }
|
|
3241
|
+
function AuthManagerBrowser_nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
3230
3242
|
function AuthManagerBrowser_unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return AuthManagerBrowser_arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? AuthManagerBrowser_arrayLikeToArray(r, a) : void 0; } }
|
|
3243
|
+
function AuthManagerBrowser_iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
3244
|
+
function AuthManagerBrowser_arrayWithoutHoles(r) { if (Array.isArray(r)) return AuthManagerBrowser_arrayLikeToArray(r); }
|
|
3231
3245
|
function AuthManagerBrowser_arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
3246
|
+
function AuthManagerBrowser_typeof(o) { "@babel/helpers - typeof"; return AuthManagerBrowser_typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, AuthManagerBrowser_typeof(o); }
|
|
3232
3247
|
function AuthManagerBrowser_classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
3233
3248
|
function AuthManagerBrowser_defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, AuthManagerBrowser_toPropertyKey(o.key), o); } }
|
|
3234
3249
|
function AuthManagerBrowser_createClass(e, r, t) { return r && AuthManagerBrowser_defineProperties(e.prototype, r), t && AuthManagerBrowser_defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
@@ -3262,8 +3277,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3262
3277
|
}, {
|
|
3263
3278
|
key: "isAuthSessionReady",
|
|
3264
3279
|
value: function isAuthSessionReady() {
|
|
3280
|
+
var _a;
|
|
3265
3281
|
var session = AuthSessionState.getSession();
|
|
3266
|
-
return (session === null || session === void 0 ? void 0 : session.isReady)
|
|
3282
|
+
return (_a = session === null || session === void 0 ? void 0 : session.isReady) !== null && _a !== void 0 ? _a : (session === null || session === void 0 ? void 0 : session.accessToken) !== undefined;
|
|
3267
3283
|
}
|
|
3268
3284
|
}, {
|
|
3269
3285
|
key: "beginAuthSession",
|
|
@@ -3276,8 +3292,13 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3276
3292
|
throw new Error("Auth session already active. Please call 'await endAuthSession()' and then call 'await beginAuthSession(...)' to start a new auth session.");
|
|
3277
3293
|
}
|
|
3278
3294
|
var canUseServiceWorkers = _this.serviceWorkerUtils.canUseServiceWorkers();
|
|
3279
|
-
if (
|
|
3280
|
-
|
|
3295
|
+
if (params.serviceWorkerConfig !== undefined) {
|
|
3296
|
+
if (params.serviceWorkerScript === undefined) {
|
|
3297
|
+
throw new Error("The 'serviceWorkerScript' field is required when 'serviceWorkerConfig' is provided.");
|
|
3298
|
+
}
|
|
3299
|
+
if (!canUseServiceWorkers) {
|
|
3300
|
+
throw new Error("The 'serviceWorkerConfig' field requires service workers, but this browser does not support them.");
|
|
3301
|
+
}
|
|
3281
3302
|
}
|
|
3282
3303
|
var newSession = {
|
|
3283
3304
|
accessToken: undefined,
|
|
@@ -3288,18 +3309,21 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3288
3309
|
authServiceWorker: params.serviceWorkerScript !== undefined && canUseServiceWorkers ? {
|
|
3289
3310
|
serviceWorkerScript: params.serviceWorkerScript,
|
|
3290
3311
|
type: "Uninitialized"
|
|
3291
|
-
} : undefined
|
|
3312
|
+
} : undefined,
|
|
3313
|
+
primaryAuthSwConfig: undefined,
|
|
3314
|
+
serviceWorkerConfig: undefined,
|
|
3315
|
+
serviceWorkerConfigRefreshHandle: undefined
|
|
3292
3316
|
};
|
|
3293
3317
|
AuthSessionState.setSession(newSession);
|
|
3294
3318
|
return newSession;
|
|
3295
3319
|
})), function (session) {
|
|
3296
3320
|
// IMPORTANT: must be called outside the above, else re-entrant deadlock will occur.
|
|
3297
|
-
return
|
|
3298
|
-
if (
|
|
3321
|
+
return AuthManagerBrowser_invoke(function () {
|
|
3322
|
+
if (session.params.serviceWorkerConfig !== undefined) {
|
|
3299
3323
|
return AuthManagerBrowser_awaitIgnored(_this.refreshServiceWorkerConfig(session, session.params));
|
|
3300
|
-
} else {
|
|
3301
|
-
return AuthManagerBrowser_awaitIgnored(_this.refreshAccessToken(session, session.params));
|
|
3302
3324
|
}
|
|
3325
|
+
}, function () {
|
|
3326
|
+
return AuthManagerBrowser_awaitIgnored(_this.refreshAccessToken(session, session.params));
|
|
3303
3327
|
});
|
|
3304
3328
|
});
|
|
3305
3329
|
} catch (e) {
|
|
@@ -3321,8 +3345,13 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3321
3345
|
if (session.accessTokenRefreshHandle !== undefined) {
|
|
3322
3346
|
_this2.scheduler.unschedule(session.accessTokenRefreshHandle);
|
|
3323
3347
|
}
|
|
3348
|
+
if (session.serviceWorkerConfigRefreshHandle !== undefined) {
|
|
3349
|
+
_this2.scheduler.unschedule(session.serviceWorkerConfigRefreshHandle);
|
|
3350
|
+
}
|
|
3351
|
+
// AuthSessionState is shared between bundled SDK versions. Service-worker-only sessions from 3.56.0 did not
|
|
3352
|
+
// contain an accountId, so allow a newer SDK instance to tear those sessions down without calling this endpoint.
|
|
3324
3353
|
return AuthManagerBrowser_invoke(function () {
|
|
3325
|
-
if (
|
|
3354
|
+
if (typeof session.params.accountId === "string") {
|
|
3326
3355
|
return AuthManagerBrowser_awaitIgnored(_this2.deleteAccessToken(session.params));
|
|
3327
3356
|
}
|
|
3328
3357
|
}, function () {
|
|
@@ -3364,21 +3393,24 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3364
3393
|
return AuthManagerBrowser_await(_this3.setAccessToken(params, jwt, setCookie), function (setTokenResult) {
|
|
3365
3394
|
return AuthManagerBrowser_invoke(function () {
|
|
3366
3395
|
if (session.authServiceWorker !== undefined) {
|
|
3367
|
-
|
|
3396
|
+
var primaryAuthSwConfig = {
|
|
3368
3397
|
headers: [{
|
|
3369
3398
|
key: "Authorization",
|
|
3370
3399
|
value: "Bearer ".concat(jwt)
|
|
3371
3400
|
}],
|
|
3372
3401
|
expires: secondsFromNow(setTokenResult.ttlSeconds),
|
|
3373
|
-
sourceUrlPrefixes: params.sourceUrlPrefixes,
|
|
3374
3402
|
urlPrefix: "".concat(_this3.getCdnUrl(params), "/").concat(params.accountId, "/")
|
|
3375
|
-
}
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
return
|
|
3381
|
-
|
|
3403
|
+
};
|
|
3404
|
+
// Fail closed until the initial serviceWorkerConfig callback succeeds: without its result, we do not know
|
|
3405
|
+
// the source-page restrictions intended for the primary JWT.
|
|
3406
|
+
return AuthManagerBrowser_invoke(function () {
|
|
3407
|
+
if (params.serviceWorkerConfig === undefined || session.serviceWorkerConfig !== undefined) {
|
|
3408
|
+
return AuthManagerBrowser_await(_this3.sendMergedServiceWorkerConfig(session, primaryAuthSwConfig, session.serviceWorkerConfig), function () {
|
|
3409
|
+
return AuthManagerBrowser_awaitIgnored(_this3.waitForServiceWorker());
|
|
3410
|
+
});
|
|
3411
|
+
}
|
|
3412
|
+
}, function () {
|
|
3413
|
+
session.primaryAuthSwConfig = primaryAuthSwConfig;
|
|
3382
3414
|
});
|
|
3383
3415
|
}
|
|
3384
3416
|
}, function () {
|
|
@@ -3390,7 +3422,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3390
3422
|
expires = secondsFromNow(actualTtl);
|
|
3391
3423
|
// Set this at the end, as it's also used to signal 'isAuthSessionReady', so must be set after configuring the Service Worker, etc.
|
|
3392
3424
|
session.accessToken = setTokenResult.accessToken;
|
|
3393
|
-
session
|
|
3425
|
+
_this3.updateSessionReadiness(session);
|
|
3394
3426
|
});
|
|
3395
3427
|
});
|
|
3396
3428
|
});
|
|
@@ -3421,6 +3453,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3421
3453
|
value: function refreshServiceWorkerConfig(session, params) {
|
|
3422
3454
|
try {
|
|
3423
3455
|
var _this4 = this;
|
|
3456
|
+
var getServiceWorkerConfig = params.serviceWorkerConfig;
|
|
3457
|
+
if (getServiceWorkerConfig === undefined) {
|
|
3458
|
+
return AuthManagerBrowser_await();
|
|
3459
|
+
}
|
|
3424
3460
|
return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(_this4.authSessionMutex.safe(AuthManagerBrowser_async(function () {
|
|
3425
3461
|
var _exit = false;
|
|
3426
3462
|
if (!session.isActive) {
|
|
@@ -3428,17 +3464,35 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3428
3464
|
}
|
|
3429
3465
|
var refreshAt;
|
|
3430
3466
|
return AuthManagerBrowser_continue(AuthManagerBrowser_catch(function () {
|
|
3431
|
-
return
|
|
3432
|
-
if (!Array.isArray(
|
|
3433
|
-
throw new Error("The '
|
|
3467
|
+
return AuthManagerBrowser_call(getServiceWorkerConfig, function (result) {
|
|
3468
|
+
if (result === null || AuthManagerBrowser_typeof(result) !== "object" || !Array.isArray(result.additionalConfig)) {
|
|
3469
|
+
throw new Error("The 'serviceWorkerConfig' callback must return an object containing 'additionalConfig'.");
|
|
3434
3470
|
}
|
|
3435
|
-
|
|
3436
|
-
return
|
|
3437
|
-
|
|
3438
|
-
|
|
3439
|
-
|
|
3440
|
-
|
|
3441
|
-
|
|
3471
|
+
if (result.sourceUrlPrefixes !== undefined && (!Array.isArray(result.sourceUrlPrefixes) || !result.sourceUrlPrefixes.every(function (prefix) {
|
|
3472
|
+
return typeof prefix === "string";
|
|
3473
|
+
}))) {
|
|
3474
|
+
throw new Error("The 'sourceUrlPrefixes' field returned by 'serviceWorkerConfig' must be an array of strings.");
|
|
3475
|
+
}
|
|
3476
|
+
if (result.urlRewriteRules !== undefined && (!Array.isArray(result.urlRewriteRules) || !result.urlRewriteRules.every(function (rule) {
|
|
3477
|
+
return rule !== null && AuthManagerBrowser_typeof(rule) === "object" && typeof rule.fromUrlPrefix === "string" && typeof rule.toUrlPrefix === "string";
|
|
3478
|
+
}))) {
|
|
3479
|
+
throw new Error("The 'urlRewriteRules' field returned by 'serviceWorkerConfig' must be an array of URL rewrite rules.");
|
|
3480
|
+
}
|
|
3481
|
+
var config = {
|
|
3482
|
+
additionalConfig: result.additionalConfig,
|
|
3483
|
+
sourceUrlPrefixes: result.sourceUrlPrefixes,
|
|
3484
|
+
urlRewriteRules: result.urlRewriteRules
|
|
3485
|
+
};
|
|
3486
|
+
return AuthManagerBrowser_invoke(function () {
|
|
3487
|
+
if (session.primaryAuthSwConfig !== undefined) {
|
|
3488
|
+
return AuthManagerBrowser_await(_this4.sendMergedServiceWorkerConfig(session, session.primaryAuthSwConfig, config), function () {
|
|
3489
|
+
return AuthManagerBrowser_awaitIgnored(_this4.waitForServiceWorker());
|
|
3490
|
+
});
|
|
3491
|
+
}
|
|
3492
|
+
}, function () {
|
|
3493
|
+
session.serviceWorkerConfig = config;
|
|
3494
|
+
_this4.updateSessionReadiness(session);
|
|
3495
|
+
refreshAt = _this4.getServiceWorkerConfigRefreshEpoch(config.additionalConfig);
|
|
3442
3496
|
});
|
|
3443
3497
|
});
|
|
3444
3498
|
}, function (e) {
|
|
@@ -3447,7 +3501,7 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3447
3501
|
}), function (_result2) {
|
|
3448
3502
|
if (_exit) return _result2;
|
|
3449
3503
|
if (refreshAt !== undefined) {
|
|
3450
|
-
session.
|
|
3504
|
+
session.serviceWorkerConfigRefreshHandle = _this4.scheduler.schedule(refreshAt, function () {
|
|
3451
3505
|
_this4.refreshServiceWorkerConfig(session, params).then(function () {}, function (e) {
|
|
3452
3506
|
return ConsoleUtils.error("Unexpected error when refreshing service worker auth config: ".concat(e));
|
|
3453
3507
|
});
|
|
@@ -3459,6 +3513,19 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3459
3513
|
return Promise.reject(e);
|
|
3460
3514
|
}
|
|
3461
3515
|
}
|
|
3516
|
+
}, {
|
|
3517
|
+
key: "sendMergedServiceWorkerConfig",
|
|
3518
|
+
value: function sendMergedServiceWorkerConfig(session, primaryConfig, serviceWorkerConfig) {
|
|
3519
|
+
try {
|
|
3520
|
+
var _this5 = this;
|
|
3521
|
+
var _a;
|
|
3522
|
+
return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(_this5.sendServiceWorkerConfig(session, [Object.assign(Object.assign({}, primaryConfig), {
|
|
3523
|
+
sourceUrlPrefixes: serviceWorkerConfig === null || serviceWorkerConfig === void 0 ? void 0 : serviceWorkerConfig.sourceUrlPrefixes
|
|
3524
|
+
})].concat(AuthManagerBrowser_toConsumableArray((_a = serviceWorkerConfig === null || serviceWorkerConfig === void 0 ? void 0 : serviceWorkerConfig.additionalConfig) !== null && _a !== void 0 ? _a : [])), serviceWorkerConfig === null || serviceWorkerConfig === void 0 ? void 0 : serviceWorkerConfig.urlRewriteRules)));
|
|
3525
|
+
} catch (e) {
|
|
3526
|
+
return Promise.reject(e);
|
|
3527
|
+
}
|
|
3528
|
+
}
|
|
3462
3529
|
}, {
|
|
3463
3530
|
key: "getServiceWorkerConfigRefreshEpoch",
|
|
3464
3531
|
value: function getServiceWorkerConfigRefreshEpoch(config) {
|
|
@@ -3481,26 +3548,46 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3481
3548
|
}
|
|
3482
3549
|
}, {
|
|
3483
3550
|
key: "sendServiceWorkerConfig",
|
|
3484
|
-
value: function sendServiceWorkerConfig(session, config) {
|
|
3551
|
+
value: function sendServiceWorkerConfig(session, config, urlRewriteRules) {
|
|
3485
3552
|
try {
|
|
3486
|
-
var
|
|
3553
|
+
var _this6 = this;
|
|
3487
3554
|
if (session.authServiceWorker === undefined) {
|
|
3488
3555
|
throw new Error("Service worker configuration cannot be applied because service workers are unavailable.");
|
|
3489
3556
|
}
|
|
3490
|
-
return AuthManagerBrowser_await(
|
|
3557
|
+
return AuthManagerBrowser_await(_this6.serviceWorkerUtils.sendMessage(Object.assign({
|
|
3491
3558
|
type: "SET_BYTESCALE_AUTH_CONFIG",
|
|
3492
3559
|
config: config.map(function (entry) {
|
|
3493
3560
|
return Object.assign(Object.assign({}, entry), {
|
|
3494
|
-
urlPrefix: "".concat(entry.sourceUrlPrefixes === undefined ? "" :
|
|
3561
|
+
urlPrefix: "".concat(entry.sourceUrlPrefixes === undefined ? "" : _this6.sourceScopedUrlPrefixMarker).concat(entry.urlPrefix)
|
|
3495
3562
|
});
|
|
3496
3563
|
})
|
|
3497
|
-
},
|
|
3498
|
-
|
|
3564
|
+
}, urlRewriteRules === undefined ? {} : {
|
|
3565
|
+
urlRewriteRules: urlRewriteRules
|
|
3566
|
+
}), session.authServiceWorker, _this6.serviceWorkerScriptFieldName), function (_this6$serviceWorkerU) {
|
|
3567
|
+
session.authServiceWorker = _this6$serviceWorkerU;
|
|
3499
3568
|
});
|
|
3500
3569
|
} catch (e) {
|
|
3501
3570
|
return Promise.reject(e);
|
|
3502
3571
|
}
|
|
3503
3572
|
}
|
|
3573
|
+
}, {
|
|
3574
|
+
key: "updateSessionReadiness",
|
|
3575
|
+
value: function updateSessionReadiness(session) {
|
|
3576
|
+
session.isReady = session.accessToken !== undefined && (session.params.serviceWorkerConfig === undefined || session.serviceWorkerConfig !== undefined);
|
|
3577
|
+
}
|
|
3578
|
+
}, {
|
|
3579
|
+
key: "waitForServiceWorker",
|
|
3580
|
+
value: function waitForServiceWorker() {
|
|
3581
|
+
try {
|
|
3582
|
+
// Message delivery is asynchronous and has no acknowledgement, so allow the worker time to apply the config before
|
|
3583
|
+
// beginAuthSession reports that authenticated requests are ready.
|
|
3584
|
+
return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(new Promise(function (resolve) {
|
|
3585
|
+
return setTimeout(resolve, 100);
|
|
3586
|
+
})));
|
|
3587
|
+
} catch (e) {
|
|
3588
|
+
return Promise.reject(e);
|
|
3589
|
+
}
|
|
3590
|
+
}
|
|
3504
3591
|
}, {
|
|
3505
3592
|
key: "getAccessTokenUrl",
|
|
3506
3593
|
value: function getAccessTokenUrl(params, setCookie) {
|
|
@@ -3516,9 +3603,9 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3516
3603
|
key: "deleteAccessToken",
|
|
3517
3604
|
value: function deleteAccessToken(params) {
|
|
3518
3605
|
try {
|
|
3519
|
-
var
|
|
3606
|
+
var _this7 = this;
|
|
3520
3607
|
var _a;
|
|
3521
|
-
return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(BaseAPI.fetch(
|
|
3608
|
+
return AuthManagerBrowser_await(AuthManagerBrowser_awaitIgnored(BaseAPI.fetch(_this7.getAccessTokenUrl(params, true), {
|
|
3522
3609
|
method: "DELETE",
|
|
3523
3610
|
credentials: "include",
|
|
3524
3611
|
headers: {}
|
|
@@ -3534,15 +3621,15 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3534
3621
|
key: "setAccessToken",
|
|
3535
3622
|
value: function setAccessToken(params, jwt, setCookie) {
|
|
3536
3623
|
try {
|
|
3537
|
-
var
|
|
3624
|
+
var _this8 = this;
|
|
3538
3625
|
var _a;
|
|
3539
3626
|
var request = {
|
|
3540
3627
|
accessToken: jwt
|
|
3541
3628
|
};
|
|
3542
|
-
return AuthManagerBrowser_await(BaseAPI.fetch(
|
|
3629
|
+
return AuthManagerBrowser_await(BaseAPI.fetch(_this8.getAccessTokenUrl(params, setCookie), {
|
|
3543
3630
|
method: "PUT",
|
|
3544
3631
|
credentials: "include",
|
|
3545
|
-
headers: AuthManagerBrowser_defineProperty({},
|
|
3632
|
+
headers: AuthManagerBrowser_defineProperty({}, _this8.contentType, _this8.contentTypeJson),
|
|
3546
3633
|
body: JSON.stringify(request)
|
|
3547
3634
|
}, {
|
|
3548
3635
|
isBytescaleApi: true,
|
|
@@ -3558,10 +3645,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3558
3645
|
key: "getAccessToken",
|
|
3559
3646
|
value: function getAccessToken(params, headers) {
|
|
3560
3647
|
try {
|
|
3561
|
-
var
|
|
3648
|
+
var _this9 = this;
|
|
3562
3649
|
var _a, _b;
|
|
3563
3650
|
var endpointName = "Your auth API endpoint";
|
|
3564
|
-
var requiredContentType =
|
|
3651
|
+
var requiredContentType = _this9.contentTypeText;
|
|
3565
3652
|
return AuthManagerBrowser_await(BaseAPI.fetch(params.authUrl, {
|
|
3566
3653
|
method: "GET",
|
|
3567
3654
|
headers: headers
|
|
@@ -3569,10 +3656,10 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3569
3656
|
isBytescaleApi: false,
|
|
3570
3657
|
fetchApi: (_a = params.options) === null || _a === void 0 ? void 0 : _a.fetchApi
|
|
3571
3658
|
}), function (result) {
|
|
3572
|
-
var actualContentType = (_b = result.headers.get(
|
|
3659
|
+
var actualContentType = (_b = result.headers.get(_this9.contentType)) !== null && _b !== void 0 ? _b : "";
|
|
3573
3660
|
// Support content types like "text/plain; charset=utf-8" and "text/plain"
|
|
3574
3661
|
if (actualContentType.split(";")[0] !== requiredContentType) {
|
|
3575
|
-
throw new Error("".concat(endpointName, " returned \"").concat(actualContentType, "\" for the ").concat(
|
|
3662
|
+
throw new Error("".concat(endpointName, " returned \"").concat(actualContentType, "\" for the ").concat(_this9.contentType, " response header, but the Bytescale SDK requires \"").concat(requiredContentType, "\"."));
|
|
3576
3663
|
}
|
|
3577
3664
|
return AuthManagerBrowser_await(result.text(), function (jwt) {
|
|
3578
3665
|
if (jwt.length === 0) {
|
|
@@ -3589,11 +3676,6 @@ var AuthManagerImpl = /*#__PURE__*/function () {
|
|
|
3589
3676
|
return Promise.reject(e);
|
|
3590
3677
|
}
|
|
3591
3678
|
}
|
|
3592
|
-
}, {
|
|
3593
|
-
key: "isV2",
|
|
3594
|
-
value: function isV2(params) {
|
|
3595
|
-
return "getServiceWorkerConfig" in params;
|
|
3596
|
-
}
|
|
3597
3679
|
}]);
|
|
3598
3680
|
}();
|
|
3599
3681
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AuthSwConfigDto } from "./AuthSwConfigDto";
|
|
2
|
+
import { UrlRewriteRule } from "../model/AuthManagerInterface";
|
|
2
3
|
export interface AuthSwSetConfigDto {
|
|
3
4
|
config: AuthSwConfigDto;
|
|
4
5
|
/**
|
|
@@ -8,4 +9,5 @@ export interface AuthSwSetConfigDto {
|
|
|
8
9
|
* with the user's events.
|
|
9
10
|
*/
|
|
10
11
|
type: "SET_BYTESCALE_AUTH_CONFIG";
|
|
12
|
+
urlRewriteRules?: UrlRewriteRule[];
|
|
11
13
|
}
|
|
@@ -1,7 +1,30 @@
|
|
|
1
1
|
import { BytescaleApiClientConfig } from "../../public/shared";
|
|
2
2
|
import { AuthSwConfigEntryDto } from "../dtos/AuthSwConfigEntryDto";
|
|
3
|
-
export
|
|
4
|
-
|
|
3
|
+
export interface UrlRewriteRule {
|
|
4
|
+
/** URL prefix to replace. This URL must be covered by the service worker's origin and scope. */
|
|
5
|
+
fromUrlPrefix: string;
|
|
6
|
+
/** Replacement URL prefix. The remainder of the original URL is appended unchanged. */
|
|
7
|
+
toUrlPrefix: string;
|
|
8
|
+
}
|
|
9
|
+
export interface AuthManagerServiceWorkerConfig {
|
|
10
|
+
/**
|
|
11
|
+
* Additional download-only service-worker authorization rules.
|
|
12
|
+
* An empty array clears all additional rules.
|
|
13
|
+
*/
|
|
14
|
+
additionalConfig: AuthSwConfigEntryDto[];
|
|
15
|
+
/**
|
|
16
|
+
* Restricts the primary JWT to pages and iframes whose URL starts with one of these prefixes.
|
|
17
|
+
* An empty array matches no clients. Omit this field to preserve the existing service-worker behavior.
|
|
18
|
+
* History API changes do not update the matched client URL.
|
|
19
|
+
*/
|
|
20
|
+
sourceUrlPrefixes?: string[];
|
|
21
|
+
/**
|
|
22
|
+
* Rewrites matching service-worker requests before applying authorization rules. The first matching rule wins and
|
|
23
|
+
* rewritten URLs are not processed recursively.
|
|
24
|
+
*/
|
|
25
|
+
urlRewriteRules?: UrlRewriteRule[];
|
|
26
|
+
}
|
|
27
|
+
export interface BeginAuthSessionParams {
|
|
5
28
|
/**
|
|
6
29
|
* The account ID to authorize requests for.
|
|
7
30
|
*/
|
|
@@ -20,6 +43,19 @@ export interface BeginAuthSessionParamsV1 {
|
|
|
20
43
|
* Optional configuration.
|
|
21
44
|
*/
|
|
22
45
|
options?: Pick<BytescaleApiClientConfig, "fetchApi" | "cdnUrl">;
|
|
46
|
+
/**
|
|
47
|
+
* Returns configuration for service-worker authorization. The primary JWT returned by `authUrl` is restricted using
|
|
48
|
+
* `sourceUrlPrefixes`; `additionalConfig` contains download-only rules and never authenticates SDK API operations;
|
|
49
|
+
* `urlRewriteRules` rewrites in-scope URLs before download authorization is evaluated.
|
|
50
|
+
* Called when the session begins and again 20 seconds before the earliest additional entry expires. Entries without
|
|
51
|
+
* an expiry do not trigger a refresh.
|
|
52
|
+
*
|
|
53
|
+
* Requires `serviceWorkerScript` and browser service-worker support. Additional rules cannot use the CDN cookie
|
|
54
|
+
* fallback.
|
|
55
|
+
*
|
|
56
|
+
* IMPORTANT: do not call 'AuthManager.beginAuthSession' or 'AuthManager.endAuthSession' inside this callback, as this will cause a deadlock.
|
|
57
|
+
*/
|
|
58
|
+
serviceWorkerConfig?: () => Promise<AuthManagerServiceWorkerConfig>;
|
|
23
59
|
/**
|
|
24
60
|
* Enables support for modern browsers that block third-party cookies (like Safari).
|
|
25
61
|
*
|
|
@@ -66,35 +102,13 @@ export interface BeginAuthSessionParamsV1 {
|
|
|
66
102
|
* script must be hosted on your website's domain in the root directory.
|
|
67
103
|
*/
|
|
68
104
|
serviceWorkerScript?: string;
|
|
69
|
-
/**
|
|
70
|
-
* Restricts service-worker authorization to pages and iframes whose URL starts with one of these prefixes.
|
|
71
|
-
* An empty array matches no clients. Omit this field to preserve the existing service-worker behavior.
|
|
72
|
-
* Applies only with `serviceWorkerScript`; History API changes do not update the matched client URL.
|
|
73
|
-
*/
|
|
74
|
-
sourceUrlPrefixes?: string[];
|
|
75
|
-
}
|
|
76
|
-
export interface BeginAuthSessionParamsV2 {
|
|
77
|
-
/**
|
|
78
|
-
* Returns the complete replacement service-worker authorization config. Called when the session begins and again
|
|
79
|
-
* 20 seconds before the earliest configured expiry. Entries without an expiry do not trigger a refresh.
|
|
80
|
-
*
|
|
81
|
-
* IMPORTANT: do not call 'AuthManager.beginAuthSession' or 'AuthManager.endAuthSession' inside this callback, as this will cause a deadlock.
|
|
82
|
-
*/
|
|
83
|
-
getServiceWorkerConfig: () => Promise<AuthSwConfigEntryDto[]>;
|
|
84
|
-
/**
|
|
85
|
-
* The path to the service worker JavaScript file hosted at the root of your website.
|
|
86
|
-
*
|
|
87
|
-
* Unlike V1 sessions, V2 sessions require service-worker support and do not create an SDK API access token or use
|
|
88
|
-
* CDN cookies as a fallback.
|
|
89
|
-
*/
|
|
90
|
-
serviceWorkerScript: string;
|
|
91
105
|
}
|
|
92
106
|
export interface AuthManagerInterface {
|
|
93
107
|
/**
|
|
94
108
|
* Begins a JWT auth session with the Bytescale API and Bytescale CDN.
|
|
95
109
|
*
|
|
96
|
-
*
|
|
97
|
-
* service-worker
|
|
110
|
+
* The primary JWT authenticates Bytescale API operations and downloads for `accountId`. Optional additional
|
|
111
|
+
* service-worker rules authenticate downloads only.
|
|
98
112
|
*
|
|
99
113
|
* Specifically, calling this method will cause the SDK to periodically acquire a JWT from your JWT endpoint. The SDK will then automatically include this JWT in all subsequent Bytescale API requests (via the 'authorization-token' request header) and also in all Bytescale CDN download requests (via a session cookie, or an 'authorization' header if service workers are being used).
|
|
100
114
|
*
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { BeginAuthSessionParams } from "./AuthManagerInterface";
|
|
1
|
+
import { AuthManagerServiceWorkerConfig, BeginAuthSessionParams } from "./AuthManagerInterface";
|
|
2
|
+
import { AuthSwConfigEntryDto } from "../dtos/AuthSwConfigEntryDto";
|
|
2
3
|
import { ServiceWorkerConfig } from "./ServiceWorkerConfig";
|
|
3
4
|
export interface AuthSession {
|
|
4
5
|
accessToken: string | undefined;
|
|
@@ -7,4 +8,7 @@ export interface AuthSession {
|
|
|
7
8
|
isActive: boolean;
|
|
8
9
|
isReady?: boolean;
|
|
9
10
|
params: BeginAuthSessionParams;
|
|
11
|
+
primaryAuthSwConfig: AuthSwConfigEntryDto | undefined;
|
|
12
|
+
serviceWorkerConfig: AuthManagerServiceWorkerConfig | undefined;
|
|
13
|
+
serviceWorkerConfigRefreshHandle: number | undefined;
|
|
10
14
|
}
|
|
@@ -3,7 +3,7 @@ import { AuthSwSetConfigDto } from "../../private/dtos/AuthSwSetConfigDto";
|
|
|
3
3
|
import { ServiceWorkerUtils } from "../../private/ServiceWorkerUtils";
|
|
4
4
|
export type { AuthSwConfigEntryDto } from "../../private/dtos/AuthSwConfigEntryDto";
|
|
5
5
|
export type { AuthSwHeaderDto } from "../../private/dtos/AuthSwHeaderDto";
|
|
6
|
-
export type {
|
|
6
|
+
export type { AuthManagerServiceWorkerConfig, BeginAuthSessionParams, UrlRewriteRule } from "../../private/model/AuthManagerInterface";
|
|
7
7
|
declare class AuthManagerImpl implements AuthManagerInterface {
|
|
8
8
|
private readonly serviceWorkerUtils;
|
|
9
9
|
private readonly authSessionMutex;
|
|
@@ -23,14 +23,16 @@ declare class AuthManagerImpl implements AuthManagerInterface {
|
|
|
23
23
|
endAuthSession(): Promise<void>;
|
|
24
24
|
private refreshAccessToken;
|
|
25
25
|
private refreshServiceWorkerConfig;
|
|
26
|
+
private sendMergedServiceWorkerConfig;
|
|
26
27
|
private getServiceWorkerConfigRefreshEpoch;
|
|
27
28
|
private sendServiceWorkerConfig;
|
|
29
|
+
private updateSessionReadiness;
|
|
30
|
+
private waitForServiceWorker;
|
|
28
31
|
private getAccessTokenUrl;
|
|
29
32
|
private getCdnUrl;
|
|
30
33
|
private deleteAccessToken;
|
|
31
34
|
private setAccessToken;
|
|
32
35
|
private getAccessToken;
|
|
33
|
-
private isV2;
|
|
34
36
|
}
|
|
35
37
|
/**
|
|
36
38
|
* Alternative way of implementing a static class (i.e. all methods static). We do this so we can use a interface on the class (interfaces can't define static methods).
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AuthManagerInterface, BeginAuthSessionParams } from "../../private/model/AuthManagerInterface";
|
|
2
2
|
export type { AuthSwConfigEntryDto } from "../../private/dtos/AuthSwConfigEntryDto";
|
|
3
3
|
export type { AuthSwHeaderDto } from "../../private/dtos/AuthSwHeaderDto";
|
|
4
|
-
export type {
|
|
4
|
+
export type { AuthManagerServiceWorkerConfig, BeginAuthSessionParams, UrlRewriteRule } from "../../private/model/AuthManagerInterface";
|
|
5
5
|
declare class AuthManagerImpl implements AuthManagerInterface {
|
|
6
6
|
beginAuthSession(_params: BeginAuthSessionParams): Promise<void>;
|
|
7
7
|
endAuthSession(): Promise<void>;
|