@faable/auth-js 1.2.1 → 1.3.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.
Files changed (55) hide show
  1. package/README.md +49 -2
  2. package/dist/faableauth.js +7 -0
  3. package/dist/faableauth.js.map +1 -0
  4. package/package.json +4 -24
  5. package/dist/Base.d.ts +0 -8
  6. package/dist/Base.d.ts.map +0 -1
  7. package/dist/Base.js +0 -16
  8. package/dist/BaseLog.d.ts +0 -11
  9. package/dist/BaseLog.d.ts.map +0 -1
  10. package/dist/BaseLog.js +0 -21
  11. package/dist/FaableAuthApi.d.ts +0 -14
  12. package/dist/FaableAuthApi.d.ts.map +0 -1
  13. package/dist/FaableAuthApi.js +0 -37
  14. package/dist/FaableAuthClient.d.ts +0 -227
  15. package/dist/FaableAuthClient.d.ts.map +0 -1
  16. package/dist/FaableAuthClient.js +0 -1073
  17. package/dist/index.d.ts +0 -7
  18. package/dist/index.d.ts.map +0 -1
  19. package/dist/index.js +0 -13
  20. package/dist/lib/constants.d.ts +0 -11
  21. package/dist/lib/constants.d.ts.map +0 -1
  22. package/dist/lib/constants.js +0 -13
  23. package/dist/lib/errors.d.ts +0 -83
  24. package/dist/lib/errors.d.ts.map +0 -1
  25. package/dist/lib/errors.js +0 -100
  26. package/dist/lib/fetch.d.ts +0 -8
  27. package/dist/lib/fetch.d.ts.map +0 -1
  28. package/dist/lib/fetch.js +0 -53
  29. package/dist/lib/helpers.d.ts +0 -42
  30. package/dist/lib/helpers.d.ts.map +0 -1
  31. package/dist/lib/helpers.js +0 -257
  32. package/dist/lib/jwt.d.ts +0 -56
  33. package/dist/lib/jwt.d.ts.map +0 -1
  34. package/dist/lib/jwt.js +0 -139
  35. package/dist/lib/local-storage.d.ts +0 -13
  36. package/dist/lib/local-storage.d.ts.map +0 -1
  37. package/dist/lib/local-storage.js +0 -45
  38. package/dist/lib/storage_helpers.d.ts +0 -5
  39. package/dist/lib/storage_helpers.d.ts.map +0 -1
  40. package/dist/lib/storage_helpers.js +0 -34
  41. package/dist/lib/types.d.ts +0 -384
  42. package/dist/lib/types.d.ts.map +0 -1
  43. package/dist/lib/types.js +0 -6
  44. package/dist/lib/version.d.ts +0 -2
  45. package/dist/lib/version.d.ts.map +0 -1
  46. package/dist/lib/version.js +0 -5
  47. package/dist/lock/Lock.d.ts +0 -20
  48. package/dist/lock/Lock.d.ts.map +0 -1
  49. package/dist/lock/Lock.js +0 -82
  50. package/dist/lock/locks.d.ts +0 -64
  51. package/dist/lock/locks.d.ts.map +0 -1
  52. package/dist/lock/locks.js +0 -137
  53. package/dist/utils.d.ts +0 -18
  54. package/dist/utils.d.ts.map +0 -1
  55. package/dist/utils.js +0 -88
package/dist/index.d.ts DELETED
@@ -1,7 +0,0 @@
1
- import { FaableAuthClient } from "./FaableAuthClient";
2
- import { FaableAuthClientConfig } from "./lib/types";
3
- import { Session, User } from "./lib/types";
4
- import { AuthError } from "./lib/errors";
5
- export declare const createClient: (config: FaableAuthClientConfig) => FaableAuthClient;
6
- export { Session, User, FaableAuthClient, AuthError };
7
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAEzC,eAAO,MAAM,YAAY,WAAY,sBAAsB,qBAE1D,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,gBAAgB,EAAE,SAAS,EAAE,CAAC"}
package/dist/index.js DELETED
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AuthError = exports.FaableAuthClient = exports.User = exports.createClient = void 0;
4
- const FaableAuthClient_1 = require("./FaableAuthClient");
5
- Object.defineProperty(exports, "FaableAuthClient", { enumerable: true, get: function () { return FaableAuthClient_1.FaableAuthClient; } });
6
- const types_1 = require("./lib/types");
7
- Object.defineProperty(exports, "User", { enumerable: true, get: function () { return types_1.User; } });
8
- const errors_1 = require("./lib/errors");
9
- Object.defineProperty(exports, "AuthError", { enumerable: true, get: function () { return errors_1.AuthError; } });
10
- const createClient = (config) => {
11
- return new FaableAuthClient_1.FaableAuthClient(config);
12
- };
13
- exports.createClient = createClient;
@@ -1,11 +0,0 @@
1
- export declare const GOTRUE_URL = "http://localhost:9999";
2
- export declare const STORAGE_KEY = "faable.auth.token";
3
- export declare const EXPIRY_MARGIN = 10;
4
- export declare const API_VERSION_HEADER_NAME = "X-Faableauth-Api-Version";
5
- export declare const API_VERSIONS: {
6
- "2024-01-01": {
7
- timestamp: number;
8
- name: string;
9
- };
10
- };
11
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":"AACA,eAAO,MAAM,UAAU,0BAA0B,CAAC;AAClD,eAAO,MAAM,WAAW,sBAAsB,CAAC;AAC/C,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,eAAO,MAAM,uBAAuB,6BAA6B,CAAC;AAClE,eAAO,MAAM,YAAY;;;;;CAKxB,CAAC"}
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.API_VERSIONS = exports.API_VERSION_HEADER_NAME = exports.EXPIRY_MARGIN = exports.STORAGE_KEY = exports.GOTRUE_URL = void 0;
4
- exports.GOTRUE_URL = "http://localhost:9999";
5
- exports.STORAGE_KEY = "faable.auth.token";
6
- exports.EXPIRY_MARGIN = 10; // in seconds
7
- exports.API_VERSION_HEADER_NAME = "X-Faableauth-Api-Version";
8
- exports.API_VERSIONS = {
9
- "2024-01-01": {
10
- timestamp: Date.parse("2024-01-01T00:00:00.0Z"),
11
- name: "2024-01-01",
12
- },
13
- };
@@ -1,83 +0,0 @@
1
- /**
2
- * Known error codes. Note that the server may also return other error codes
3
- * not included in this list (if the client library is older than the version
4
- * on the server).
5
- */
6
- export type ErrorCode = "unexpected_failure" | "validation_failed" | "bad_json" | "email_exists" | "phone_exists" | "bad_jwt" | "not_admin" | "no_authorization" | "user_not_found" | "session_not_found" | "flow_state_not_found" | "flow_state_expired" | "signup_disabled" | "user_banned" | "provider_email_needs_verification" | "invite_not_found" | "bad_oauth_state" | "bad_oauth_callback" | "oauth_provider_not_supported" | "unexpected_audience" | "single_identity_not_deletable" | "email_conflict_identity_not_deletable" | "identity_already_exists" | "email_provider_disabled" | "phone_provider_disabled" | "too_many_enrolled_mfa_factors" | "mfa_factor_name_conflict" | "mfa_factor_not_found" | "mfa_ip_address_mismatch" | "mfa_challenge_expired" | "mfa_verification_failed" | "mfa_verification_rejected" | "insufficient_aal" | "captcha_failed" | "saml_provider_disabled" | "manual_linking_disabled" | "sms_send_failed" | "email_not_confirmed" | "phone_not_confirmed" | "reauth_nonce_missing" | "saml_relay_state_not_found" | "saml_relay_state_expired" | "saml_idp_not_found" | "saml_assertion_no_user_id" | "saml_assertion_no_email" | "user_already_exists" | "sso_provider_not_found" | "saml_metadata_fetch_failed" | "saml_idp_already_exists" | "sso_domain_already_exists" | "saml_entity_id_mismatch" | "conflict" | "provider_disabled" | "user_sso_managed" | "reauthentication_needed" | "same_password" | "reauthentication_not_valid" | "otp_expired" | "otp_disabled" | "identity_not_found" | "weak_password" | "over_request_rate_limit" | "over_email_send_rate_limit" | "over_sms_send_rate_limit" | "bad_code_verifier";
7
- export declare class AuthError extends Error {
8
- /**
9
- * Error code associated with the error. Most errors coming from
10
- * HTTP responses will have a code, though some errors that occur
11
- * before a response is received will not have one present. In that
12
- * case {@link #status} will also be undefined.
13
- */
14
- code: ErrorCode | string | undefined;
15
- /** HTTP status code that caused the error. */
16
- status: number | undefined;
17
- protected __isAuthError: boolean;
18
- constructor(message: string, status?: number, code?: string);
19
- }
20
- export declare class CustomAuthError extends AuthError {
21
- name: string;
22
- status: number;
23
- constructor(message: string, name: string, status: number, code: string | undefined);
24
- }
25
- export declare class AuthSessionMissingError extends CustomAuthError {
26
- constructor();
27
- }
28
- export declare function isAuthError(error: unknown): error is AuthError;
29
- export declare class AuthApiError extends AuthError {
30
- status: number;
31
- constructor(message: string, status: number, code: string | undefined);
32
- }
33
- export declare function isAuthApiError(error: unknown): error is AuthApiError;
34
- export declare class AuthImplicitGrantRedirectError extends CustomAuthError {
35
- details: {
36
- error: string;
37
- code: string;
38
- } | null;
39
- constructor(message: string, details?: {
40
- error: string;
41
- code: string;
42
- } | null);
43
- toJSON(): {
44
- name: string;
45
- message: string;
46
- status: number;
47
- details: {
48
- error: string;
49
- code: string;
50
- } | null;
51
- };
52
- }
53
- export declare class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
54
- details: {
55
- error: string;
56
- code: string;
57
- } | null;
58
- constructor(message: string, details?: {
59
- error: string;
60
- code: string;
61
- } | null);
62
- toJSON(): {
63
- name: string;
64
- message: string;
65
- status: number;
66
- details: {
67
- error: string;
68
- code: string;
69
- } | null;
70
- };
71
- }
72
- export declare class AuthUnknownError extends AuthError {
73
- originalError: unknown;
74
- constructor(message: string, originalError: unknown);
75
- }
76
- export declare class AuthInvalidTokenResponseError extends CustomAuthError {
77
- constructor();
78
- }
79
- export declare class AuthRetryableFetchError extends CustomAuthError {
80
- constructor(message: string, status: number);
81
- }
82
- export declare function isAuthRetryableFetchError(error: unknown): error is AuthRetryableFetchError;
83
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,MAAM,SAAS,GACjB,oBAAoB,GACpB,mBAAmB,GACnB,UAAU,GACV,cAAc,GACd,cAAc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Ae;gBAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAG5C;AAED,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,OAAO,GACb,KAAK,IAAI,uBAAuB,CAElC"}
@@ -1,100 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isAuthRetryableFetchError = exports.AuthRetryableFetchError = exports.AuthInvalidTokenResponseError = exports.AuthUnknownError = exports.AuthPKCEGrantCodeExchangeError = exports.AuthImplicitGrantRedirectError = exports.isAuthApiError = exports.AuthApiError = exports.isAuthError = exports.AuthSessionMissingError = exports.CustomAuthError = exports.AuthError = void 0;
4
- class AuthError extends Error {
5
- constructor(message, status, code) {
6
- super(message);
7
- this.__isAuthError = true;
8
- this.name = "AuthError";
9
- this.status = status;
10
- this.code = code;
11
- }
12
- }
13
- exports.AuthError = AuthError;
14
- class CustomAuthError extends AuthError {
15
- constructor(message, name, status, code) {
16
- super(message, status, code);
17
- this.name = name;
18
- this.status = status;
19
- }
20
- }
21
- exports.CustomAuthError = CustomAuthError;
22
- class AuthSessionMissingError extends CustomAuthError {
23
- constructor() {
24
- super("Auth session missing!", "AuthSessionMissingError", 400, undefined);
25
- }
26
- }
27
- exports.AuthSessionMissingError = AuthSessionMissingError;
28
- function isAuthError(error) {
29
- return (typeof error === "object" && error !== null && "__isAuthError" in error);
30
- }
31
- exports.isAuthError = isAuthError;
32
- class AuthApiError extends AuthError {
33
- constructor(message, status, code) {
34
- super(message, status, code);
35
- this.name = "AuthApiError";
36
- this.status = status;
37
- this.code = code;
38
- }
39
- }
40
- exports.AuthApiError = AuthApiError;
41
- function isAuthApiError(error) {
42
- return isAuthError(error) && error.name === "AuthApiError";
43
- }
44
- exports.isAuthApiError = isAuthApiError;
45
- class AuthImplicitGrantRedirectError extends CustomAuthError {
46
- constructor(message, details = null) {
47
- super(message, "AuthImplicitGrantRedirectError", 500, undefined);
48
- this.details = null;
49
- this.details = details;
50
- }
51
- toJSON() {
52
- return {
53
- name: this.name,
54
- message: this.message,
55
- status: this.status,
56
- details: this.details,
57
- };
58
- }
59
- }
60
- exports.AuthImplicitGrantRedirectError = AuthImplicitGrantRedirectError;
61
- class AuthPKCEGrantCodeExchangeError extends CustomAuthError {
62
- constructor(message, details = null) {
63
- super(message, "AuthPKCEGrantCodeExchangeError", 500, undefined);
64
- this.details = null;
65
- this.details = details;
66
- }
67
- toJSON() {
68
- return {
69
- name: this.name,
70
- message: this.message,
71
- status: this.status,
72
- details: this.details,
73
- };
74
- }
75
- }
76
- exports.AuthPKCEGrantCodeExchangeError = AuthPKCEGrantCodeExchangeError;
77
- class AuthUnknownError extends AuthError {
78
- constructor(message, originalError) {
79
- super(message);
80
- this.name = "AuthUnknownError";
81
- this.originalError = originalError;
82
- }
83
- }
84
- exports.AuthUnknownError = AuthUnknownError;
85
- class AuthInvalidTokenResponseError extends CustomAuthError {
86
- constructor() {
87
- super("Auth session or user missing", "AuthInvalidTokenResponseError", 500, undefined);
88
- }
89
- }
90
- exports.AuthInvalidTokenResponseError = AuthInvalidTokenResponseError;
91
- class AuthRetryableFetchError extends CustomAuthError {
92
- constructor(message, status) {
93
- super(message, "AuthRetryableFetchError", status, undefined);
94
- }
95
- }
96
- exports.AuthRetryableFetchError = AuthRetryableFetchError;
97
- function isAuthRetryableFetchError(error) {
98
- return isAuthError(error) && error.name === "AuthRetryableFetchError";
99
- }
100
- exports.isAuthRetryableFetchError = isAuthRetryableFetchError;
@@ -1,8 +0,0 @@
1
- type RequestInitWithToken = RequestInit & {
2
- token?: string;
3
- transform?: (data: any) => any;
4
- };
5
- export declare const _post: (url: string, data: object, init?: RequestInitWithToken) => Promise<any>;
6
- export declare const _get: (url: string, init?: RequestInitWithToken) => Promise<any>;
7
- export {};
8
- //# sourceMappingURL=fetch.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"fetch.d.ts","sourceRoot":"","sources":["../../src/lib/fetch.ts"],"names":[],"mappings":"AAGA,KAAK,oBAAoB,GAAG,WAAW,GAAG;IACxC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,GAAG,CAAC;CAChC,CAAC;AAaF,eAAO,MAAM,KAAK,QACX,MAAM,QACL,MAAM,SACL,oBAAoB,iBAiB5B,CAAC;AAEF,eAAO,MAAM,IAAI,QAAe,MAAM,SAAS,oBAAoB,iBAelE,CAAC"}
package/dist/lib/fetch.js DELETED
@@ -1,53 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports._get = exports._post = void 0;
16
- const isomorphic_fetch_1 = __importDefault(require("isomorphic-fetch"));
17
- const headers = (init) => {
18
- let headers = {};
19
- if (init === null || init === void 0 ? void 0 : init.token) {
20
- headers = Object.assign(Object.assign({}, headers), { Authorization: `Bearer ${init === null || init === void 0 ? void 0 : init.token}` });
21
- }
22
- return Object.assign(Object.assign({}, init === null || init === void 0 ? void 0 : init.headers), headers);
23
- };
24
- const _post = (url, data, init) => __awaiter(void 0, void 0, void 0, function* () {
25
- try {
26
- const res = yield (0, isomorphic_fetch_1.default)(url, {
27
- method: "POST",
28
- body: JSON.stringify(data),
29
- headers: Object.assign(Object.assign({}, headers(init)), { "Content-Type": "application/json" }),
30
- });
31
- const body = yield res.json();
32
- return init && init.transform
33
- ? init.transform(body)
34
- : { data: body, error: null };
35
- }
36
- catch (e) {
37
- return { data: null, error: e };
38
- }
39
- });
40
- exports._post = _post;
41
- const _get = (url, init) => __awaiter(void 0, void 0, void 0, function* () {
42
- try {
43
- const res = yield (0, isomorphic_fetch_1.default)(url, Object.assign(Object.assign({}, init), { method: "GET", headers: headers(init) }));
44
- const body = yield res.json();
45
- return init && init.transform
46
- ? init.transform(body)
47
- : { data: body, error: null };
48
- }
49
- catch (e) {
50
- return { data: null, error: e };
51
- }
52
- });
53
- exports._get = _get;
@@ -1,42 +0,0 @@
1
- import { AuthResponse, SupportedStorage } from "./types";
2
- export declare function decodeBase64URL(value: string): string;
3
- export declare function generatePKCEVerifier(): string;
4
- export declare function generatePKCEChallenge(verifier: string): Promise<string>;
5
- export declare function getCodeChallengeAndMethod(storage: SupportedStorage, storageKey: string, isPasswordRecovery?: boolean): Promise<string[]>;
6
- export declare const isBrowser: () => boolean;
7
- /**
8
- * Checks whether localStorage is supported on this browser.
9
- */
10
- export declare const supportsLocalStorage: () => boolean;
11
- export declare function uuid(): string;
12
- /**
13
- * Extracts parameters encoded in the URL both in the query and fragment.
14
- */
15
- export declare function parseParametersFromURL(href: string): {
16
- [parameter: string]: string;
17
- };
18
- export declare function expiresAt(expiresIn: number): number;
19
- export declare function _sessionResponse(data: any): AuthResponse;
20
- /**
21
- * A deferred represents some asynchronous work that is not yet finished, which
22
- * may or may not culminate in a value.
23
- * Taken from: https://github.com/mike-north/types/blob/master/src/async.ts
24
- */
25
- export declare class Deferred<T = any> {
26
- static promiseConstructor: PromiseConstructor;
27
- readonly promise: PromiseLike<T>;
28
- readonly resolve: (value?: T | PromiseLike<T>) => void;
29
- readonly reject: (reason?: any) => any;
30
- constructor();
31
- }
32
- /**
33
- * Converts the provided async function into a retryable function. Each result
34
- * or thrown error is sent to the isRetryable function which should return true
35
- * if the function should run again.
36
- */
37
- export declare function retryable<T>(fn: (attempt: number) => Promise<T>, isRetryable: (attempt: number, error: any | null, result?: T) => boolean): Promise<T>;
38
- /**
39
- * Creates a promise that resolves to null after some time.
40
- */
41
- export declare function sleep(time: number): Promise<null>;
42
- //# sourceMappingURL=helpers.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/lib/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,gBAAgB,EAAsB,MAAM,SAAS,CAAC;AAO7E,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CA2BrD;AAGD,wBAAgB,oBAAoB,WAenC;AAiBD,wBAAsB,qBAAqB,CAAC,QAAQ,EAAE,MAAM,mBAc3D;AAED,wBAAsB,yBAAyB,CAC7C,OAAO,EAAE,gBAAgB,EACzB,UAAU,EAAE,MAAM,EAClB,kBAAkB,UAAQ,qBAe3B;AAED,eAAO,MAAM,SAAS,eAAwC,CAAC;AAO/D;;GAEG;AACH,eAAO,MAAM,oBAAoB,eAmChC,CAAC;AAEF,wBAAgB,IAAI,WAMnB;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CAAC,IAAI,EAAE,MAAM;;EAsBlD;AAWD,wBAAgB,SAAS,CAAC,SAAS,EAAE,MAAM,UAG1C;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,GAAG,GAAG,YAAY,CAYxD;AAED;;;;GAIG;AACH,qBAAa,QAAQ,CAAC,CAAC,GAAG,GAAG;IAC3B,OAAc,kBAAkB,EAAE,kBAAkB,CAAW;IAE/D,SAAgB,OAAO,EAAG,WAAW,CAAC,CAAC,CAAC,CAAC;IAEzC,SAAgB,OAAO,EAAG,CAAC,KAAK,CAAC,EAAE,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC;IAE/D,SAAgB,MAAM,EAAG,CAAC,MAAM,CAAC,EAAE,GAAG,KAAK,GAAG,CAAC;;CAWhD;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EACzB,EAAE,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,CAAC,CAAC,EACnC,WAAW,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,GAAG,IAAI,EAAE,MAAM,CAAC,EAAE,CAAC,KAAK,OAAO,GACvE,OAAO,CAAC,CAAC,CAAC,CAuBZ;AAED;;GAEG;AACH,wBAAsB,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIvD"}
@@ -1,257 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.sleep = exports.retryable = exports.Deferred = exports._sessionResponse = exports.expiresAt = exports.parseParametersFromURL = exports.uuid = exports.supportsLocalStorage = exports.isBrowser = exports.getCodeChallengeAndMethod = exports.generatePKCEChallenge = exports.generatePKCEVerifier = exports.decodeBase64URL = void 0;
13
- const storage_helpers_1 = require("./storage_helpers");
14
- function dec2hex(dec) {
15
- return ("0" + dec.toString(16)).substr(-2);
16
- }
17
- function decodeBase64URL(value) {
18
- const key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
19
- let base64 = "";
20
- let chr1, chr2, chr3;
21
- let enc1, enc2, enc3, enc4;
22
- let i = 0;
23
- value = value.replace("-", "+").replace("_", "/");
24
- while (i < value.length) {
25
- enc1 = key.indexOf(value.charAt(i++));
26
- enc2 = key.indexOf(value.charAt(i++));
27
- enc3 = key.indexOf(value.charAt(i++));
28
- enc4 = key.indexOf(value.charAt(i++));
29
- chr1 = (enc1 << 2) | (enc2 >> 4);
30
- chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
31
- chr3 = ((enc3 & 3) << 6) | enc4;
32
- base64 = base64 + String.fromCharCode(chr1);
33
- if (enc3 != 64 && chr2 != 0) {
34
- base64 = base64 + String.fromCharCode(chr2);
35
- }
36
- if (enc4 != 64 && chr3 != 0) {
37
- base64 = base64 + String.fromCharCode(chr3);
38
- }
39
- }
40
- return base64;
41
- }
42
- exports.decodeBase64URL = decodeBase64URL;
43
- // Functions below taken from: https://stackoverflow.com/questions/63309409/creating-a-code-verifier-and-challenge-for-pkce-auth-on-spotify-api-in-reactjs
44
- function generatePKCEVerifier() {
45
- const verifierLength = 56;
46
- const array = new Uint32Array(verifierLength);
47
- if (typeof crypto === "undefined") {
48
- const charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
49
- const charSetLen = charSet.length;
50
- let verifier = "";
51
- for (let i = 0; i < verifierLength; i++) {
52
- verifier += charSet.charAt(Math.floor(Math.random() * charSetLen));
53
- }
54
- return verifier;
55
- }
56
- crypto.getRandomValues(array);
57
- return Array.from(array, dec2hex).join("");
58
- }
59
- exports.generatePKCEVerifier = generatePKCEVerifier;
60
- function sha256(randomString) {
61
- return __awaiter(this, void 0, void 0, function* () {
62
- const encoder = new TextEncoder();
63
- const encodedData = encoder.encode(randomString);
64
- const hash = yield crypto.subtle.digest("SHA-256", encodedData);
65
- const bytes = new Uint8Array(hash);
66
- return Array.from(bytes)
67
- .map((c) => String.fromCharCode(c))
68
- .join("");
69
- });
70
- }
71
- function base64urlencode(str) {
72
- return btoa(str).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
73
- }
74
- function generatePKCEChallenge(verifier) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const hasCryptoSupport = typeof crypto !== "undefined" &&
77
- typeof crypto.subtle !== "undefined" &&
78
- typeof TextEncoder !== "undefined";
79
- if (!hasCryptoSupport) {
80
- console.warn("WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256.");
81
- return verifier;
82
- }
83
- const hashed = yield sha256(verifier);
84
- return base64urlencode(hashed);
85
- });
86
- }
87
- exports.generatePKCEChallenge = generatePKCEChallenge;
88
- function getCodeChallengeAndMethod(storage_1, storageKey_1) {
89
- return __awaiter(this, arguments, void 0, function* (storage, storageKey, isPasswordRecovery = false) {
90
- const codeVerifier = generatePKCEVerifier();
91
- let storedCodeVerifier = codeVerifier;
92
- if (isPasswordRecovery) {
93
- storedCodeVerifier += "/PASSWORD_RECOVERY";
94
- }
95
- yield (0, storage_helpers_1.setItemAsync)(storage, `${storageKey}-code-verifier`, storedCodeVerifier);
96
- const codeChallenge = yield generatePKCEChallenge(codeVerifier);
97
- const codeChallengeMethod = codeVerifier === codeChallenge ? "plain" : "S256";
98
- return [codeChallenge, codeChallengeMethod];
99
- });
100
- }
101
- exports.getCodeChallengeAndMethod = getCodeChallengeAndMethod;
102
- const isBrowser = () => typeof document !== "undefined";
103
- exports.isBrowser = isBrowser;
104
- const localStorageWriteTests = {
105
- tested: false,
106
- writable: false,
107
- };
108
- /**
109
- * Checks whether localStorage is supported on this browser.
110
- */
111
- const supportsLocalStorage = () => {
112
- if (!(0, exports.isBrowser)()) {
113
- return false;
114
- }
115
- try {
116
- if (typeof globalThis.localStorage !== "object") {
117
- return false;
118
- }
119
- }
120
- catch (e) {
121
- // DOM exception when accessing `localStorage`
122
- return false;
123
- }
124
- if (localStorageWriteTests.tested) {
125
- return localStorageWriteTests.writable;
126
- }
127
- const randomKey = `lswt-${Math.random()}${Math.random()}`;
128
- try {
129
- globalThis.localStorage.setItem(randomKey, randomKey);
130
- globalThis.localStorage.removeItem(randomKey);
131
- localStorageWriteTests.tested = true;
132
- localStorageWriteTests.writable = true;
133
- }
134
- catch (e) {
135
- // localStorage can't be written to
136
- // https://www.chromium.org/for-testers/bug-reporting-guidelines/uncaught-securityerror-failed-to-read-the-localstorage-property-from-window-access-is-denied-for-this-document
137
- localStorageWriteTests.tested = true;
138
- localStorageWriteTests.writable = false;
139
- }
140
- return localStorageWriteTests.writable;
141
- };
142
- exports.supportsLocalStorage = supportsLocalStorage;
143
- function uuid() {
144
- return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
145
- const r = (Math.random() * 16) | 0, v = c == "x" ? r : (r & 0x3) | 0x8;
146
- return v.toString(16);
147
- });
148
- }
149
- exports.uuid = uuid;
150
- /**
151
- * Extracts parameters encoded in the URL both in the query and fragment.
152
- */
153
- function parseParametersFromURL(href) {
154
- const result = {};
155
- const url = new URL(href);
156
- if (url.hash && url.hash[0] === "#") {
157
- try {
158
- const hashSearchParams = new URLSearchParams(url.hash.substring(1));
159
- hashSearchParams.forEach((value, key) => {
160
- result[key] = value;
161
- });
162
- }
163
- catch (e) {
164
- // hash is not a query string
165
- }
166
- }
167
- // search parameters take precedence over hash parameters
168
- url.searchParams.forEach((value, key) => {
169
- result[key] = value;
170
- });
171
- return result;
172
- }
173
- exports.parseParametersFromURL = parseParametersFromURL;
174
- /**
175
- * hasSession checks if the response object contains a valid session
176
- * @param data A response object
177
- * @returns true if a session is in the response
178
- */
179
- function hasSession(data) {
180
- return data.access_token && data.refresh_token && data.expires_in;
181
- }
182
- function expiresAt(expiresIn) {
183
- const timeNow = Math.round(Date.now() / 1000);
184
- return timeNow + expiresIn;
185
- }
186
- exports.expiresAt = expiresAt;
187
- function _sessionResponse(data) {
188
- var _a;
189
- let session = null;
190
- if (hasSession(data)) {
191
- session = Object.assign({}, data);
192
- if (!data.expires_at) {
193
- session.expires_at = expiresAt(data.expires_in);
194
- }
195
- }
196
- const user = (_a = data.user) !== null && _a !== void 0 ? _a : data;
197
- return { data: { session, user }, error: null };
198
- }
199
- exports._sessionResponse = _sessionResponse;
200
- /**
201
- * A deferred represents some asynchronous work that is not yet finished, which
202
- * may or may not culminate in a value.
203
- * Taken from: https://github.com/mike-north/types/blob/master/src/async.ts
204
- */
205
- class Deferred {
206
- constructor() {
207
- // eslint-disable-next-line @typescript-eslint/no-extra-semi
208
- this.promise = new Deferred.promiseConstructor((res, rej) => {
209
- // eslint-disable-next-line @typescript-eslint/no-extra-semi
210
- this.resolve = res;
211
- // eslint-disable-next-line @typescript-eslint/no-extra-semi
212
- this.reject = rej;
213
- });
214
- }
215
- }
216
- exports.Deferred = Deferred;
217
- Deferred.promiseConstructor = Promise;
218
- /**
219
- * Converts the provided async function into a retryable function. Each result
220
- * or thrown error is sent to the isRetryable function which should return true
221
- * if the function should run again.
222
- */
223
- function retryable(fn, isRetryable) {
224
- const promise = new Promise((accept, reject) => {
225
- // eslint-disable-next-line @typescript-eslint/no-extra-semi
226
- (() => __awaiter(this, void 0, void 0, function* () {
227
- for (let attempt = 0; attempt < Infinity; attempt++) {
228
- try {
229
- const result = yield fn(attempt);
230
- if (!isRetryable(attempt, null, result)) {
231
- accept(result);
232
- return;
233
- }
234
- }
235
- catch (e) {
236
- if (!isRetryable(attempt, e)) {
237
- reject(e);
238
- return;
239
- }
240
- }
241
- }
242
- }))();
243
- });
244
- return promise;
245
- }
246
- exports.retryable = retryable;
247
- /**
248
- * Creates a promise that resolves to null after some time.
249
- */
250
- function sleep(time) {
251
- return __awaiter(this, void 0, void 0, function* () {
252
- return new Promise((accept) => {
253
- setTimeout(() => accept(null), time);
254
- });
255
- });
256
- }
257
- exports.sleep = sleep;