@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.mjs CHANGED
@@ -11242,8 +11242,153 @@ Object.defineProperty(Emittery, "listenerRemoved", {
11242
11242
  configurable: false
11243
11243
  });
11244
11244
 
11245
- // src/Authenticator.ts
11246
- import { Credentials } from "@dstny/scp-credentials";
11245
+ // ../credentials/dist/index.mjs
11246
+ function base64decode(str) {
11247
+ const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
11248
+ let output = "";
11249
+ str = String(str).replace(/={1,10}$/, "");
11250
+ if (str.length % 4 === 1) {
11251
+ throw new Error("'atob' failed: The string to be decoded is not correctly encoded.");
11252
+ }
11253
+ for (
11254
+ let bc = 0, bs, buffer, idx = 0;
11255
+ // tslint:disable-next-line:no-conditional-assignment
11256
+ buffer = str.charAt(idx++);
11257
+ // tslint:disable-next-line:no-bitwise
11258
+ ~buffer && // tslint:disable-next-line:no-conditional-assignment
11259
+ (bs = bc % 4 ? bs * 64 + buffer : buffer, bc++ % 4) ? (
11260
+ // tslint:disable-next-line:no-bitwise
11261
+ output += String.fromCharCode(255 & bs >> (-2 * bc & 6))
11262
+ ) : 0
11263
+ ) {
11264
+ buffer = chars.indexOf(buffer);
11265
+ }
11266
+ return output;
11267
+ }
11268
+ async function sha1(str) {
11269
+ if (typeof crypto === "object") {
11270
+ const hash = await crypto.subtle.digest("SHA-1", new TextEncoder().encode(str));
11271
+ return Array.from(new Uint8Array(hash)).map((v) => v.toString(16).padStart(2, "0")).join("");
11272
+ } else {
11273
+ const Crypto = await import("crypto");
11274
+ return Crypto.createHash("sha1").update(str).digest("hex");
11275
+ }
11276
+ }
11277
+ var STORE_CREDENTILAS_KEY = "sdk-auth-credential";
11278
+ var Credentials = class _Credentials {
11279
+ static async fromStorage(storage) {
11280
+ const str = await storage.getItem(STORE_CREDENTILAS_KEY);
11281
+ if (str == null || str == void 0) {
11282
+ throw new Error("No credentials found in storage.");
11283
+ }
11284
+ return _Credentials.fromJSON(str);
11285
+ }
11286
+ static async toStorage(storage, credentials) {
11287
+ await storage.setItem(STORE_CREDENTILAS_KEY, JSON.stringify(credentials));
11288
+ }
11289
+ static async clearFromStorage(storage) {
11290
+ await storage.deleteItem(STORE_CREDENTILAS_KEY);
11291
+ }
11292
+ static fromJSON(str) {
11293
+ const { access_token, refresh_token, expires_in } = JSON.parse(str);
11294
+ return new _Credentials(access_token, refresh_token, expires_in);
11295
+ }
11296
+ static onStorageUpdate(storage, callback) {
11297
+ return storage.on(STORE_CREDENTILAS_KEY, (credentials) => {
11298
+ if (typeof credentials === "string") {
11299
+ callback(_Credentials.fromJSON(credentials));
11300
+ }
11301
+ });
11302
+ }
11303
+ static equals(a, b) {
11304
+ if (!a && !b) {
11305
+ return true;
11306
+ }
11307
+ if (a && !b || b && !a) {
11308
+ return false;
11309
+ }
11310
+ return a?.access_token === b?.access_token && a?._refresh_token === b?.refresh_token && a?.expires_in === b?.expires_in;
11311
+ }
11312
+ /**
11313
+ * Used to access resources.
11314
+ */
11315
+ _access_token;
11316
+ /**
11317
+ * Used to obtain a new access token.
11318
+ */
11319
+ _refresh_token;
11320
+ /**
11321
+ * Number of seconds until the access token expires.
11322
+ * This value is calculated at the moment the access token is generated.
11323
+ */
11324
+ _expires_in;
11325
+ constructor(access_token, refresh_token, expires_in) {
11326
+ this._access_token = access_token;
11327
+ this._refresh_token = refresh_token;
11328
+ this._expires_in = expires_in;
11329
+ }
11330
+ /**
11331
+ * Lists the claims present in the access token.
11332
+ */
11333
+ get claims() {
11334
+ const fallback = {};
11335
+ if (typeof this._access_token != "string") {
11336
+ return fallback;
11337
+ }
11338
+ const [, b64payload] = this._access_token.split(".");
11339
+ const payload = base64decode(b64payload);
11340
+ if (!payload) {
11341
+ return fallback;
11342
+ }
11343
+ try {
11344
+ return JSON.parse(payload);
11345
+ } catch (err) {
11346
+ return {};
11347
+ }
11348
+ }
11349
+ get access_token() {
11350
+ return this._access_token;
11351
+ }
11352
+ get refresh_token() {
11353
+ return this._refresh_token;
11354
+ }
11355
+ get expires_in() {
11356
+ return this._expires_in;
11357
+ }
11358
+ get token_type() {
11359
+ return "Bearer";
11360
+ }
11361
+ get expires_at() {
11362
+ if (typeof this.claims?.exp === "number") {
11363
+ return new Date(this.claims?.exp * 1e3);
11364
+ }
11365
+ const expiryDate = /* @__PURE__ */ new Date();
11366
+ expiryDate.setSeconds(expiryDate.getSeconds() + this._expires_in);
11367
+ return expiryDate;
11368
+ }
11369
+ isExpired() {
11370
+ return this.expires_at && (/* @__PURE__ */ new Date()).getTime() > new Date(this.expires_at).getTime();
11371
+ }
11372
+ async getUepId() {
11373
+ const { sub, uep_id: rawUepId } = this.claims;
11374
+ if (!sub) {
11375
+ throw new Error("Missing 'sub' claim.");
11376
+ }
11377
+ if (rawUepId) {
11378
+ return rawUepId;
11379
+ }
11380
+ return await sha1(sub);
11381
+ }
11382
+ toJSON() {
11383
+ return {
11384
+ access_token: this._access_token,
11385
+ refresh_token: this._refresh_token,
11386
+ expires_in: this._expires_in,
11387
+ expires_at: this.expires_at,
11388
+ token_type: this.token_type
11389
+ };
11390
+ }
11391
+ };
11247
11392
 
11248
11393
  // src/utils/jwt.ts
11249
11394
  function decodeToken(token) {
@@ -12502,7 +12647,7 @@ var transitional_default = {
12502
12647
  };
12503
12648
 
12504
12649
  // ../../node_modules/.pnpm/axios@1.9.0/node_modules/axios/lib/platform/node/index.js
12505
- import crypto from "crypto";
12650
+ import crypto2 from "crypto";
12506
12651
 
12507
12652
  // ../../node_modules/.pnpm/axios@1.9.0/node_modules/axios/lib/platform/node/classes/URLSearchParams.js
12508
12653
  import url from "url";
@@ -12520,7 +12665,7 @@ var generateString = (size = 16, alphabet = ALPHABET.ALPHA_DIGIT) => {
12520
12665
  let str = "";
12521
12666
  const { length } = alphabet;
12522
12667
  const randomValues = new Uint32Array(size);
12523
- crypto.randomFillSync(randomValues);
12668
+ crypto2.randomFillSync(randomValues);
12524
12669
  for (let i = 0; i < size; i++) {
12525
12670
  str += alphabet[randomValues[i] % length];
12526
12671
  }
@@ -15131,7 +15276,6 @@ var AbstractAuthenticationApi = class {
15131
15276
  };
15132
15277
 
15133
15278
  // src/api/OAuthApi.ts
15134
- import { Credentials as Credentials2 } from "@dstny/scp-credentials";
15135
15279
  var OAuthApi = class extends AbstractAuthenticationApi {
15136
15280
  clientId;
15137
15281
  scope;
@@ -15158,7 +15302,7 @@ var OAuthApi = class extends AbstractAuthenticationApi {
15158
15302
  redirect_uri: redirectUri
15159
15303
  })
15160
15304
  );
15161
- return new Credentials2(data.access_token, data.refresh_token, data.expires_in);
15305
+ return new Credentials(data.access_token, data.refresh_token, data.expires_in);
15162
15306
  }
15163
15307
  async loginWithUsernamePassword(username, password) {
15164
15308
  const { data } = await this.axiosInstance.post(
@@ -15170,7 +15314,7 @@ var OAuthApi = class extends AbstractAuthenticationApi {
15170
15314
  password
15171
15315
  })
15172
15316
  );
15173
- return new Credentials2(data.access_token, data.refresh_token, data.expires_in);
15317
+ return new Credentials(data.access_token, data.refresh_token, data.expires_in);
15174
15318
  }
15175
15319
  async refreshToken(refreshToken, accessToken) {
15176
15320
  try {
@@ -15182,7 +15326,7 @@ var OAuthApi = class extends AbstractAuthenticationApi {
15182
15326
  refresh_token: refreshToken
15183
15327
  })
15184
15328
  );
15185
- return new Credentials2(
15329
+ return new Credentials(
15186
15330
  response.data.access_token,
15187
15331
  response.data.refresh_token,
15188
15332
  response.data.expires_in
@@ -15209,7 +15353,6 @@ var OAuthApi = class extends AbstractAuthenticationApi {
15209
15353
  };
15210
15354
 
15211
15355
  // src/api/SmgAuthApi.ts
15212
- import { Credentials as Credentials3 } from "@dstny/scp-credentials";
15213
15356
  var SmgAuthApi = class extends AbstractAuthenticationApi {
15214
15357
  clientId;
15215
15358
  realm;
@@ -15244,7 +15387,7 @@ var SmgAuthApi = class extends AbstractAuthenticationApi {
15244
15387
  const expires_in = exp - iat;
15245
15388
  const expires_at = /* @__PURE__ */ new Date();
15246
15389
  expires_at.setSeconds(expires_at.getSeconds() + expires_in);
15247
- return new Credentials3(accessToken, refreshToken, expires_in);
15390
+ return new Credentials(accessToken, refreshToken, expires_in);
15248
15391
  }
15249
15392
  async refreshToken(refreshToken, accessToken) {
15250
15393
  try {
@@ -15260,7 +15403,7 @@ var SmgAuthApi = class extends AbstractAuthenticationApi {
15260
15403
  const { refreshToken: newRefreshToken, accessToken: newAccessToken } = data;
15261
15404
  const { iat = 0, exp = 0 } = decodeToken(newAccessToken);
15262
15405
  const expires_in = exp - iat;
15263
- return new Credentials3(newAccessToken, newRefreshToken, expires_in);
15406
+ return new Credentials(newAccessToken, newRefreshToken, expires_in);
15264
15407
  } catch (error) {
15265
15408
  if (axios_default.isAxiosError(error)) {
15266
15409
  if (error.message === "Network Error") {