@dstny/scp-authenticator 0.0.2 → 0.0.3
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/index.js +155 -12
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +154 -11
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -5
package/dist/index.js
CHANGED
|
@@ -11250,8 +11250,153 @@ Object.defineProperty(Emittery, "listenerRemoved", {
|
|
|
11250
11250
|
configurable: false
|
|
11251
11251
|
});
|
|
11252
11252
|
|
|
11253
|
-
//
|
|
11254
|
-
|
|
11253
|
+
// ../credentials/dist/index.mjs
|
|
11254
|
+
function base64decode(str) {
|
|
11255
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
11256
|
+
let output = "";
|
|
11257
|
+
str = String(str).replace(/={1,10}$/, "");
|
|
11258
|
+
if (str.length % 4 === 1) {
|
|
11259
|
+
throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
|
|
11260
|
+
}
|
|
11261
|
+
for (
|
|
11262
|
+
let bc = 0, bs, buffer, idx = 0;
|
|
11263
|
+
// tslint:disable-next-line:no-conditional-assignment
|
|
11264
|
+
buffer = str.charAt(idx++);
|
|
11265
|
+
// tslint:disable-next-line:no-bitwise
|
|
11266
|
+
~buffer && // tslint:disable-next-line:no-conditional-assignment
|
|
11267
|
+
(bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? (
|
|
11268
|
+
// tslint:disable-next-line:no-bitwise
|
|
11269
|
+
output += String.fromCharCode(255 & bs >> (-2 * bc & 6))
|
|
11270
|
+
) : 0
|
|
11271
|
+
) {
|
|
11272
|
+
buffer = chars.indexOf(buffer);
|
|
11273
|
+
}
|
|
11274
|
+
return output;
|
|
11275
|
+
}
|
|
11276
|
+
async function sha1(str) {
|
|
11277
|
+
if (typeof crypto === "object") {
|
|
11278
|
+
const hash = await crypto.subtle.digest("SHA-1", new TextEncoder().encode(str));
|
|
11279
|
+
return Array.from(new Uint8Array(hash)).map((v) => v.toString(16).padStart(2, "0")).join("");
|
|
11280
|
+
} else {
|
|
11281
|
+
const Crypto = await import("crypto");
|
|
11282
|
+
return Crypto.createHash("sha1").update(str).digest("hex");
|
|
11283
|
+
}
|
|
11284
|
+
}
|
|
11285
|
+
var STORE_CREDENTILAS_KEY = "sdk-auth-credential";
|
|
11286
|
+
var Credentials = class _Credentials {
|
|
11287
|
+
static async fromStorage(storage) {
|
|
11288
|
+
const str = await storage.getItem(STORE_CREDENTILAS_KEY);
|
|
11289
|
+
if (str == null || str == void 0) {
|
|
11290
|
+
throw new Error("No credentials found in storage.");
|
|
11291
|
+
}
|
|
11292
|
+
return _Credentials.fromJSON(str);
|
|
11293
|
+
}
|
|
11294
|
+
static async toStorage(storage, credentials) {
|
|
11295
|
+
await storage.setItem(STORE_CREDENTILAS_KEY, JSON.stringify(credentials));
|
|
11296
|
+
}
|
|
11297
|
+
static async clearFromStorage(storage) {
|
|
11298
|
+
await storage.deleteItem(STORE_CREDENTILAS_KEY);
|
|
11299
|
+
}
|
|
11300
|
+
static fromJSON(str) {
|
|
11301
|
+
const { access_token, refresh_token, expires_in } = JSON.parse(str);
|
|
11302
|
+
return new _Credentials(access_token, refresh_token, expires_in);
|
|
11303
|
+
}
|
|
11304
|
+
static onStorageUpdate(storage, callback) {
|
|
11305
|
+
return storage.on(STORE_CREDENTILAS_KEY, (credentials) => {
|
|
11306
|
+
if (typeof credentials === "string") {
|
|
11307
|
+
callback(_Credentials.fromJSON(credentials));
|
|
11308
|
+
}
|
|
11309
|
+
});
|
|
11310
|
+
}
|
|
11311
|
+
static equals(a, b) {
|
|
11312
|
+
if (!a && !b) {
|
|
11313
|
+
return true;
|
|
11314
|
+
}
|
|
11315
|
+
if (a && !b || b && !a) {
|
|
11316
|
+
return false;
|
|
11317
|
+
}
|
|
11318
|
+
return a?.access_token === b?.access_token && a?._refresh_token === b?.refresh_token && a?.expires_in === b?.expires_in;
|
|
11319
|
+
}
|
|
11320
|
+
/**
|
|
11321
|
+
* Used to access resources.
|
|
11322
|
+
*/
|
|
11323
|
+
_access_token;
|
|
11324
|
+
/**
|
|
11325
|
+
* Used to obtain a new access token.
|
|
11326
|
+
*/
|
|
11327
|
+
_refresh_token;
|
|
11328
|
+
/**
|
|
11329
|
+
* Number of seconds until the access token expires.
|
|
11330
|
+
* This value is calculated at the moment the access token is generated.
|
|
11331
|
+
*/
|
|
11332
|
+
_expires_in;
|
|
11333
|
+
constructor(access_token, refresh_token, expires_in) {
|
|
11334
|
+
this._access_token = access_token;
|
|
11335
|
+
this._refresh_token = refresh_token;
|
|
11336
|
+
this._expires_in = expires_in;
|
|
11337
|
+
}
|
|
11338
|
+
/**
|
|
11339
|
+
* Lists the claims present in the access token.
|
|
11340
|
+
*/
|
|
11341
|
+
get claims() {
|
|
11342
|
+
const fallback = {};
|
|
11343
|
+
if (typeof this._access_token != "string") {
|
|
11344
|
+
return fallback;
|
|
11345
|
+
}
|
|
11346
|
+
const [, b64payload] = this._access_token.split(".");
|
|
11347
|
+
const payload = base64decode(b64payload);
|
|
11348
|
+
if (!payload) {
|
|
11349
|
+
return fallback;
|
|
11350
|
+
}
|
|
11351
|
+
try {
|
|
11352
|
+
return JSON.parse(payload);
|
|
11353
|
+
} catch (err) {
|
|
11354
|
+
return {};
|
|
11355
|
+
}
|
|
11356
|
+
}
|
|
11357
|
+
get access_token() {
|
|
11358
|
+
return this._access_token;
|
|
11359
|
+
}
|
|
11360
|
+
get refresh_token() {
|
|
11361
|
+
return this._refresh_token;
|
|
11362
|
+
}
|
|
11363
|
+
get expires_in() {
|
|
11364
|
+
return this._expires_in;
|
|
11365
|
+
}
|
|
11366
|
+
get token_type() {
|
|
11367
|
+
return "Bearer";
|
|
11368
|
+
}
|
|
11369
|
+
get expires_at() {
|
|
11370
|
+
if (typeof this.claims?.exp === "number") {
|
|
11371
|
+
return new Date(this.claims?.exp * 1e3);
|
|
11372
|
+
}
|
|
11373
|
+
const expiryDate = /* @__PURE__ */ new Date();
|
|
11374
|
+
expiryDate.setSeconds(expiryDate.getSeconds() + this._expires_in);
|
|
11375
|
+
return expiryDate;
|
|
11376
|
+
}
|
|
11377
|
+
isExpired() {
|
|
11378
|
+
return this.expires_at && (/* @__PURE__ */ new Date()).getTime() > new Date(this.expires_at).getTime();
|
|
11379
|
+
}
|
|
11380
|
+
async getUepId() {
|
|
11381
|
+
const { sub, uep_id: rawUepId } = this.claims;
|
|
11382
|
+
if (!sub) {
|
|
11383
|
+
throw new Error("Missing 'sub' claim.");
|
|
11384
|
+
}
|
|
11385
|
+
if (rawUepId) {
|
|
11386
|
+
return rawUepId;
|
|
11387
|
+
}
|
|
11388
|
+
return await sha1(sub);
|
|
11389
|
+
}
|
|
11390
|
+
toJSON() {
|
|
11391
|
+
return {
|
|
11392
|
+
access_token: this._access_token,
|
|
11393
|
+
refresh_token: this._refresh_token,
|
|
11394
|
+
expires_in: this._expires_in,
|
|
11395
|
+
expires_at: this.expires_at,
|
|
11396
|
+
token_type: this.token_type
|
|
11397
|
+
};
|
|
11398
|
+
}
|
|
11399
|
+
};
|
|
11255
11400
|
|
|
11256
11401
|
// src/utils/jwt.ts
|
|
11257
11402
|
function decodeToken(token) {
|
|
@@ -11429,14 +11574,14 @@ var Authenticator = class extends Emittery {
|
|
|
11429
11574
|
} else {
|
|
11430
11575
|
const localStorageCredentials = await this.secureStorage.getItem(STORE_CREDENTIALS_KEY);
|
|
11431
11576
|
if (typeof localStorageCredentials === "string") {
|
|
11432
|
-
credentials =
|
|
11577
|
+
credentials = Credentials.fromJSON(localStorageCredentials);
|
|
11433
11578
|
}
|
|
11434
11579
|
}
|
|
11435
11580
|
if (credentials) {
|
|
11436
11581
|
this.logger?.log("Has credentials:", credentials);
|
|
11437
11582
|
await this._signInWithCredentials(credentials);
|
|
11438
11583
|
}
|
|
11439
|
-
this.removeCredentialStorageUpdates =
|
|
11584
|
+
this.removeCredentialStorageUpdates = Credentials.onStorageUpdate(
|
|
11440
11585
|
this.secureStorage,
|
|
11441
11586
|
(credentials2) => {
|
|
11442
11587
|
this._signInWithCredentials(credentials2, true);
|
|
@@ -11455,7 +11600,7 @@ var Authenticator = class extends Emittery {
|
|
|
11455
11600
|
this.unsubscribeUpdateState = this.on(
|
|
11456
11601
|
INTERNAL_CREDENTIALS,
|
|
11457
11602
|
async ({ credentials: credentials2, isFromOtherInstance }) => {
|
|
11458
|
-
if (
|
|
11603
|
+
if (Credentials.equals(this._credentials, credentials2)) {
|
|
11459
11604
|
return;
|
|
11460
11605
|
}
|
|
11461
11606
|
this._credentials = credentials2;
|
|
@@ -15139,7 +15284,6 @@ var AbstractAuthenticationApi = class {
|
|
|
15139
15284
|
};
|
|
15140
15285
|
|
|
15141
15286
|
// src/api/OAuthApi.ts
|
|
15142
|
-
var import_scp_credentials2 = require("@dstny/scp-credentials");
|
|
15143
15287
|
var OAuthApi = class extends AbstractAuthenticationApi {
|
|
15144
15288
|
clientId;
|
|
15145
15289
|
scope;
|
|
@@ -15166,7 +15310,7 @@ var OAuthApi = class extends AbstractAuthenticationApi {
|
|
|
15166
15310
|
redirect_uri: redirectUri
|
|
15167
15311
|
})
|
|
15168
15312
|
);
|
|
15169
|
-
return new
|
|
15313
|
+
return new Credentials(data.access_token, data.refresh_token, data.expires_in);
|
|
15170
15314
|
}
|
|
15171
15315
|
async loginWithUsernamePassword(username, password) {
|
|
15172
15316
|
const { data } = await this.axiosInstance.post(
|
|
@@ -15178,7 +15322,7 @@ var OAuthApi = class extends AbstractAuthenticationApi {
|
|
|
15178
15322
|
password
|
|
15179
15323
|
})
|
|
15180
15324
|
);
|
|
15181
|
-
return new
|
|
15325
|
+
return new Credentials(data.access_token, data.refresh_token, data.expires_in);
|
|
15182
15326
|
}
|
|
15183
15327
|
async refreshToken(refreshToken, accessToken) {
|
|
15184
15328
|
try {
|
|
@@ -15190,7 +15334,7 @@ var OAuthApi = class extends AbstractAuthenticationApi {
|
|
|
15190
15334
|
refresh_token: refreshToken
|
|
15191
15335
|
})
|
|
15192
15336
|
);
|
|
15193
|
-
return new
|
|
15337
|
+
return new Credentials(
|
|
15194
15338
|
response.data.access_token,
|
|
15195
15339
|
response.data.refresh_token,
|
|
15196
15340
|
response.data.expires_in
|
|
@@ -15217,7 +15361,6 @@ var OAuthApi = class extends AbstractAuthenticationApi {
|
|
|
15217
15361
|
};
|
|
15218
15362
|
|
|
15219
15363
|
// src/api/SmgAuthApi.ts
|
|
15220
|
-
var import_scp_credentials3 = require("@dstny/scp-credentials");
|
|
15221
15364
|
var SmgAuthApi = class extends AbstractAuthenticationApi {
|
|
15222
15365
|
clientId;
|
|
15223
15366
|
realm;
|
|
@@ -15252,7 +15395,7 @@ var SmgAuthApi = class extends AbstractAuthenticationApi {
|
|
|
15252
15395
|
const expires_in = exp - iat;
|
|
15253
15396
|
const expires_at = /* @__PURE__ */ new Date();
|
|
15254
15397
|
expires_at.setSeconds(expires_at.getSeconds() + expires_in);
|
|
15255
|
-
return new
|
|
15398
|
+
return new Credentials(accessToken, refreshToken, expires_in);
|
|
15256
15399
|
}
|
|
15257
15400
|
async refreshToken(refreshToken, accessToken) {
|
|
15258
15401
|
try {
|
|
@@ -15268,7 +15411,7 @@ var SmgAuthApi = class extends AbstractAuthenticationApi {
|
|
|
15268
15411
|
const { refreshToken: newRefreshToken, accessToken: newAccessToken } = data;
|
|
15269
15412
|
const { iat = 0, exp = 0 } = decodeToken(newAccessToken);
|
|
15270
15413
|
const expires_in = exp - iat;
|
|
15271
|
-
return new
|
|
15414
|
+
return new Credentials(newAccessToken, newRefreshToken, expires_in);
|
|
15272
15415
|
} catch (error) {
|
|
15273
15416
|
if (axios_default.isAxiosError(error)) {
|
|
15274
15417
|
if (error.message === "Network Error") {
|