@teamhanko/hanko-frontend-sdk 0.1.0-alpha → 0.2.0-alpha
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/Hanko.d.ts +25 -0
- package/dist/index.d.ts +16 -0
- package/dist/lib/Dto.d.ts +191 -0
- package/dist/lib/Errors.d.ts +182 -0
- package/dist/lib/WebauthnSupport.d.ts +36 -0
- package/dist/lib/client/Client.d.ts +15 -0
- package/dist/lib/client/ConfigClient.d.ts +20 -0
- package/dist/lib/client/EmailClient.d.ts +58 -0
- package/dist/lib/client/HttpClient.d.ts +118 -0
- package/dist/lib/client/PasscodeClient.d.ts +57 -0
- package/dist/lib/client/PasswordClient.d.ts +48 -0
- package/dist/lib/client/ThirdPartyClient.d.ts +29 -0
- package/dist/lib/client/UserClient.d.ts +48 -0
- package/dist/lib/client/WebauthnClient.d.ts +97 -0
- package/dist/lib/state/PasscodeState.d.ts +108 -0
- package/dist/lib/state/PasswordState.d.ts +50 -0
- package/dist/lib/state/State.d.ts +53 -0
- package/dist/lib/state/UserState.d.ts +51 -0
- package/dist/lib/state/WebauthnState.d.ts +60 -0
- package/dist/sdk.cjs +2 -0
- package/dist/sdk.cjs.map +1 -0
- package/dist/sdk.modern.js +2 -0
- package/dist/sdk.modern.js.map +1 -0
- package/dist/sdk.module.js +2 -0
- package/dist/sdk.module.js.map +1 -0
- package/dist/sdk.umd.js +2 -0
- package/dist/sdk.umd.js.map +1 -0
- package/package.json +9 -9
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { PasscodeState } from "../state/PasscodeState";
|
|
2
|
+
import { Passcode } from "../Dto";
|
|
3
|
+
import { Client } from "./Client";
|
|
4
|
+
/**
|
|
5
|
+
* A class to handle passcodes.
|
|
6
|
+
*
|
|
7
|
+
* @constructor
|
|
8
|
+
* @category SDK
|
|
9
|
+
* @subcategory Clients
|
|
10
|
+
* @extends {Client}
|
|
11
|
+
*/
|
|
12
|
+
declare class PasscodeClient extends Client {
|
|
13
|
+
state: PasscodeState;
|
|
14
|
+
constructor(api: string, timeout?: number);
|
|
15
|
+
/**
|
|
16
|
+
* Causes the API to send a new passcode to the user's email address.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} userID - The UUID of the user.
|
|
19
|
+
* @param {string=} emailID - The UUID of the email address. If unspecified, the email will be sent to the primary email address.
|
|
20
|
+
* @param {boolean=} force - Indicates the passcode should be sent, even if there is another active passcode.
|
|
21
|
+
* @return {Promise<Passcode>}
|
|
22
|
+
* @throws {TooManyRequestsError}
|
|
23
|
+
* @throws {RequestTimeoutError}
|
|
24
|
+
* @throws {UnauthorizedError}
|
|
25
|
+
* @throws {TechnicalError}
|
|
26
|
+
* @see https://docs.hanko.io/api/public#tag/Passcode/operation/passcodeInit
|
|
27
|
+
*/
|
|
28
|
+
initialize(userID: string, emailID?: string, force?: boolean): Promise<Passcode>;
|
|
29
|
+
/**
|
|
30
|
+
* Validates the passcode obtained from the email.
|
|
31
|
+
*
|
|
32
|
+
* @param {string} userID - The UUID of the user.
|
|
33
|
+
* @param {string} code - The passcode digests.
|
|
34
|
+
* @return {Promise<void>}
|
|
35
|
+
* @throws {InvalidPasscodeError}
|
|
36
|
+
* @throws {MaxNumOfPasscodeAttemptsReachedError}
|
|
37
|
+
* @throws {RequestTimeoutError}
|
|
38
|
+
* @throws {TechnicalError}
|
|
39
|
+
* @see https://docs.hanko.io/api/public#tag/Passcode/operation/passcodeFinal
|
|
40
|
+
*/
|
|
41
|
+
finalize(userID: string, code: string): Promise<void>;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the number of seconds the current passcode is active for.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} userID - The UUID of the user.
|
|
46
|
+
* @return {number}
|
|
47
|
+
*/
|
|
48
|
+
getTTL(userID: string): number;
|
|
49
|
+
/**
|
|
50
|
+
* Returns the number of seconds the rate limiting is active for.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} userID - The UUID of the user.
|
|
53
|
+
* @return {number}
|
|
54
|
+
*/
|
|
55
|
+
getResendAfter(userID: string): number;
|
|
56
|
+
}
|
|
57
|
+
export { PasscodeClient };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { PasswordState } from "../state/PasswordState";
|
|
2
|
+
import { PasscodeState } from "../state/PasscodeState";
|
|
3
|
+
import { Client } from "./Client";
|
|
4
|
+
/**
|
|
5
|
+
* A class to handle passwords.
|
|
6
|
+
*
|
|
7
|
+
* @constructor
|
|
8
|
+
* @category SDK
|
|
9
|
+
* @subcategory Clients
|
|
10
|
+
* @extends {Client}
|
|
11
|
+
*/
|
|
12
|
+
declare class PasswordClient extends Client {
|
|
13
|
+
passwordState: PasswordState;
|
|
14
|
+
passcodeState: PasscodeState;
|
|
15
|
+
constructor(api: string, timeout?: number);
|
|
16
|
+
/**
|
|
17
|
+
* Logs in a user with a password.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} userID - The UUID of the user.
|
|
20
|
+
* @param {string} password - The password.
|
|
21
|
+
* @return {Promise<void>}
|
|
22
|
+
* @throws {TooManyRequestsError}
|
|
23
|
+
* @throws {RequestTimeoutError}
|
|
24
|
+
* @throws {TechnicalError}
|
|
25
|
+
* @see https://docs.hanko.io/api/public#tag/Password/operation/passwordLogin
|
|
26
|
+
*/
|
|
27
|
+
login(userID: string, password: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Updates a password.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} userID - The UUID of the user.
|
|
32
|
+
* @param {string} password - The new password.
|
|
33
|
+
* @return {Promise<void>}
|
|
34
|
+
* @throws {RequestTimeoutError}
|
|
35
|
+
* @throws {UnauthorizedError}
|
|
36
|
+
* @throws {TechnicalError}
|
|
37
|
+
* @see https://docs.hanko.io/api/public#tag/Password/operation/password
|
|
38
|
+
*/
|
|
39
|
+
update(userID: string, password: string): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Returns the number of seconds the rate limiting is active for.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} userID - The UUID of the user.
|
|
44
|
+
* @return {number}
|
|
45
|
+
*/
|
|
46
|
+
getRetryAfter(userID: string): number;
|
|
47
|
+
}
|
|
48
|
+
export { PasswordClient };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { Client } from "./Client";
|
|
2
|
+
import { ThirdPartyError } from "../Errors";
|
|
3
|
+
/**
|
|
4
|
+
* A class that handles communication with the Hanko API for the purposes
|
|
5
|
+
* of authenticating through a third party provider.
|
|
6
|
+
*
|
|
7
|
+
* @constructor
|
|
8
|
+
* @category SDK
|
|
9
|
+
* @subcategory Clients
|
|
10
|
+
* @extends {Client}
|
|
11
|
+
*/
|
|
12
|
+
export declare class ThirdPartyClient extends Client {
|
|
13
|
+
constructor(api: string, timeout?: number);
|
|
14
|
+
/**
|
|
15
|
+
* Performs a request to the Hanko API that redirects to the given
|
|
16
|
+
* third party provider.
|
|
17
|
+
*
|
|
18
|
+
* @param {string} provider - The name of the third party provider
|
|
19
|
+
* @param {string} redirectTo - The URL to redirect to after a successful third party authentication
|
|
20
|
+
* @throws {ThirdPartyError}
|
|
21
|
+
* @see http://docs.hanko.io/api/public#tag/Third-Party/operation/thirdPartyAuth
|
|
22
|
+
*/
|
|
23
|
+
auth(provider: string, redirectTo: string): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Get a third party error from the current location's query params.
|
|
26
|
+
* @returns {(ThirdPartyError|undefined)} The ThirdPartyError.
|
|
27
|
+
*/
|
|
28
|
+
getError(): ThirdPartyError;
|
|
29
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { User, UserInfo } from "../Dto";
|
|
2
|
+
import { Client } from "./Client";
|
|
3
|
+
/**
|
|
4
|
+
* A class to manage user information.
|
|
5
|
+
*
|
|
6
|
+
* @category SDK
|
|
7
|
+
* @subcategory Clients
|
|
8
|
+
* @extends {Client}
|
|
9
|
+
*/
|
|
10
|
+
declare class UserClient extends Client {
|
|
11
|
+
/**
|
|
12
|
+
* Fetches basic information about the user identified by the given email address. Can be used while the user is logged out
|
|
13
|
+
* and is helpful in deciding which type of login to choose. For example, if the user's email is not verified, you may
|
|
14
|
+
* want to log in with a passcode, or if no WebAuthn credentials are registered, you may not want to use WebAuthn.
|
|
15
|
+
*
|
|
16
|
+
* @param {string} email - The user's email address.
|
|
17
|
+
* @return {Promise<UserInfo>}
|
|
18
|
+
* @throws {NotFoundError}
|
|
19
|
+
* @throws {RequestTimeoutError}
|
|
20
|
+
* @throws {TechnicalError}
|
|
21
|
+
* @see https://docs.hanko.io/api/public#tag/User-Management/operation/getUserId
|
|
22
|
+
*/
|
|
23
|
+
getInfo(email: string): Promise<UserInfo>;
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new user. Afterwards, verify the email address via passcode. If a 'ConflictError'
|
|
26
|
+
* occurred, you may want to prompt the user to log in.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} email - The email address of the user to be created.
|
|
29
|
+
* @return {Promise<User>}
|
|
30
|
+
* @throws {ConflictError}
|
|
31
|
+
* @throws {RequestTimeoutError}
|
|
32
|
+
* @throws {TechnicalError}
|
|
33
|
+
* @see https://docs.hanko.io/api/public#tag/User-Management/operation/createUser
|
|
34
|
+
*/
|
|
35
|
+
create(email: string): Promise<User>;
|
|
36
|
+
/**
|
|
37
|
+
* Fetches the current user.
|
|
38
|
+
*
|
|
39
|
+
* @return {Promise<User>}
|
|
40
|
+
* @throws {UnauthorizedError}
|
|
41
|
+
* @throws {RequestTimeoutError}
|
|
42
|
+
* @throws {TechnicalError}
|
|
43
|
+
* @see https://docs.hanko.io/api/public#tag/User-Management/operation/IsUserAuthorized
|
|
44
|
+
* @see https://docs.hanko.io/api/public#tag/User-Management/operation/listUser
|
|
45
|
+
*/
|
|
46
|
+
getCurrent(): Promise<User>;
|
|
47
|
+
}
|
|
48
|
+
export { UserClient };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { create as createWebauthnCredential, get as getWebauthnCredential } from "@github/webauthn-json";
|
|
2
|
+
import { Client } from "./Client";
|
|
3
|
+
import { PasscodeState } from "../state/PasscodeState";
|
|
4
|
+
import { WebauthnState } from "../state/WebauthnState";
|
|
5
|
+
import { User, WebauthnCredentials } from "../Dto";
|
|
6
|
+
/**
|
|
7
|
+
* A class that handles WebAuthn authentication and registration.
|
|
8
|
+
*
|
|
9
|
+
* @constructor
|
|
10
|
+
* @category SDK
|
|
11
|
+
* @subcategory Clients
|
|
12
|
+
* @extends {Client}
|
|
13
|
+
*/
|
|
14
|
+
declare class WebauthnClient extends Client {
|
|
15
|
+
webauthnState: WebauthnState;
|
|
16
|
+
passcodeState: PasscodeState;
|
|
17
|
+
controller: AbortController;
|
|
18
|
+
_getCredential: typeof getWebauthnCredential;
|
|
19
|
+
_createCredential: typeof createWebauthnCredential;
|
|
20
|
+
constructor(api: string, timeout?: number);
|
|
21
|
+
/**
|
|
22
|
+
* Performs a WebAuthn authentication ceremony. When 'userID' is specified, the API provides a list of
|
|
23
|
+
* allowed credentials and the browser is able to present a list of suitable credentials to the user.
|
|
24
|
+
*
|
|
25
|
+
* @param {string=} userID - The user's UUID.
|
|
26
|
+
* @param {boolean=} useConditionalMediation - Enables autofill assisted login.
|
|
27
|
+
* @return {Promise<void>}
|
|
28
|
+
* @throws {WebauthnRequestCancelledError}
|
|
29
|
+
* @throws {InvalidWebauthnCredentialError}
|
|
30
|
+
* @throws {RequestTimeoutError}
|
|
31
|
+
* @throws {TechnicalError}
|
|
32
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/webauthnLoginInit
|
|
33
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/webauthnLoginFinal
|
|
34
|
+
* @see https://www.w3.org/TR/webauthn-2/#authentication-ceremony
|
|
35
|
+
*/
|
|
36
|
+
login(userID?: string, useConditionalMediation?: boolean): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Performs a WebAuthn registration ceremony.
|
|
39
|
+
*
|
|
40
|
+
* @return {Promise<void>}
|
|
41
|
+
* @throws {WebauthnRequestCancelledError}
|
|
42
|
+
* @throws {RequestTimeoutError}
|
|
43
|
+
* @throws {UnauthorizedError}
|
|
44
|
+
* @throws {TechnicalError}
|
|
45
|
+
* @throws {UserVerificationError}
|
|
46
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/webauthnRegInit
|
|
47
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/webauthnRegFinal
|
|
48
|
+
* @see https://www.w3.org/TR/webauthn-2/#sctn-registering-a-new-credential
|
|
49
|
+
*/
|
|
50
|
+
register(): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Returns a list of all WebAuthn credentials assigned to the current user.
|
|
53
|
+
*
|
|
54
|
+
* @return {Promise<WebauthnCredentials>}
|
|
55
|
+
* @throws {UnauthorizedError}
|
|
56
|
+
* @throws {RequestTimeoutError}
|
|
57
|
+
* @throws {TechnicalError}
|
|
58
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/listCredentials
|
|
59
|
+
*/
|
|
60
|
+
listCredentials(): Promise<WebauthnCredentials>;
|
|
61
|
+
/**
|
|
62
|
+
* Updates the WebAuthn credential.
|
|
63
|
+
*
|
|
64
|
+
* @param {string=} credentialID - The credential's UUID.
|
|
65
|
+
* @param {string} name - The new credential name.
|
|
66
|
+
* @return {Promise<void>}
|
|
67
|
+
* @throws {NotFoundError}
|
|
68
|
+
* @throws {UnauthorizedError}
|
|
69
|
+
* @throws {RequestTimeoutError}
|
|
70
|
+
* @throws {TechnicalError}
|
|
71
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/updateCredential
|
|
72
|
+
*/
|
|
73
|
+
updateCredential(credentialID: string, name: string): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Deletes the WebAuthn credential.
|
|
76
|
+
*
|
|
77
|
+
* @param {string=} credentialID - The credential's UUID.
|
|
78
|
+
* @return {Promise<void>}
|
|
79
|
+
* @throws {NotFoundError}
|
|
80
|
+
* @throws {UnauthorizedError}
|
|
81
|
+
* @throws {RequestTimeoutError}
|
|
82
|
+
* @throws {TechnicalError}
|
|
83
|
+
* @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/deleteCredential
|
|
84
|
+
*/
|
|
85
|
+
deleteCredential(credentialID: string): Promise<void>;
|
|
86
|
+
/**
|
|
87
|
+
* Determines whether a credential registration ceremony should be performed. Returns 'true' when WebAuthn
|
|
88
|
+
* is supported and the user's credentials do not intersect with the credentials already known on the
|
|
89
|
+
* current browser/device.
|
|
90
|
+
*
|
|
91
|
+
* @param {User} user - The user object.
|
|
92
|
+
* @return {Promise<boolean>}
|
|
93
|
+
*/
|
|
94
|
+
shouldRegister(user: User): Promise<boolean>;
|
|
95
|
+
_createAbortSignal(): AbortSignal;
|
|
96
|
+
}
|
|
97
|
+
export { WebauthnClient };
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { UserState } from "./UserState";
|
|
2
|
+
/**
|
|
3
|
+
* @interface
|
|
4
|
+
* @category SDK
|
|
5
|
+
* @subcategory Internal
|
|
6
|
+
* @property {string=} id - The UUID of the active passcode.
|
|
7
|
+
* @property {number=} ttl - Timestamp until when the passcode is valid in seconds (since January 1, 1970 00:00:00 UTC).
|
|
8
|
+
* @property {number=} resendAfter - Seconds until a passcode can be resent.
|
|
9
|
+
* @property {emailID=} emailID - The email address ID.
|
|
10
|
+
*/
|
|
11
|
+
export interface LocalStoragePasscode {
|
|
12
|
+
id?: string;
|
|
13
|
+
ttl?: number;
|
|
14
|
+
resendAfter?: number;
|
|
15
|
+
emailID?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A class that manages passcodes via local storage.
|
|
19
|
+
*
|
|
20
|
+
* @extends UserState
|
|
21
|
+
* @category SDK
|
|
22
|
+
* @subcategory Internal
|
|
23
|
+
*/
|
|
24
|
+
declare class PasscodeState extends UserState {
|
|
25
|
+
/**
|
|
26
|
+
* Get the passcode state.
|
|
27
|
+
*
|
|
28
|
+
* @private
|
|
29
|
+
* @param {string} userID - The UUID of the user.
|
|
30
|
+
* @return {LocalStoragePasscode}
|
|
31
|
+
*/
|
|
32
|
+
private getState;
|
|
33
|
+
/**
|
|
34
|
+
* Reads the current state.
|
|
35
|
+
*
|
|
36
|
+
* @public
|
|
37
|
+
* @return {PasscodeState}
|
|
38
|
+
*/
|
|
39
|
+
read(): PasscodeState;
|
|
40
|
+
/**
|
|
41
|
+
* Gets the UUID of the active passcode.
|
|
42
|
+
*
|
|
43
|
+
* @param {string} userID - The UUID of the user.
|
|
44
|
+
* @return {string}
|
|
45
|
+
*/
|
|
46
|
+
getActiveID(userID: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Sets the UUID of the active passcode.
|
|
49
|
+
*
|
|
50
|
+
* @param {string} userID - The UUID of the user.
|
|
51
|
+
* @param {string} passcodeID - The UUID of the passcode to be set as active.
|
|
52
|
+
* @return {PasscodeState}
|
|
53
|
+
*/
|
|
54
|
+
setActiveID(userID: string, passcodeID: string): PasscodeState;
|
|
55
|
+
/**
|
|
56
|
+
* Gets the UUID of the email address.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} userID - The UUID of the user.
|
|
59
|
+
* @return {string}
|
|
60
|
+
*/
|
|
61
|
+
getEmailID(userID: string): string;
|
|
62
|
+
/**
|
|
63
|
+
* Sets the UUID of the email address.
|
|
64
|
+
*
|
|
65
|
+
* @param {string} userID - The UUID of the user.
|
|
66
|
+
* @param {string} emailID - The UUID of the email address.
|
|
67
|
+
* @return {PasscodeState}
|
|
68
|
+
*/
|
|
69
|
+
setEmailID(userID: string, emailID: string): PasscodeState;
|
|
70
|
+
/**
|
|
71
|
+
* Removes the active passcode.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} userID - The UUID of the user.
|
|
74
|
+
* @return {PasscodeState}
|
|
75
|
+
*/
|
|
76
|
+
reset(userID: string): PasscodeState;
|
|
77
|
+
/**
|
|
78
|
+
* Gets the TTL in seconds. When the seconds expire, the code is invalid.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} userID - The UUID of the user.
|
|
81
|
+
* @return {number}
|
|
82
|
+
*/
|
|
83
|
+
getTTL(userID: string): number;
|
|
84
|
+
/**
|
|
85
|
+
* Sets the passcode's TTL and stores it to the local storage.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} userID - The UUID of the user.
|
|
88
|
+
* @param {string} seconds - Number of seconds the passcode is valid for.
|
|
89
|
+
* @return {PasscodeState}
|
|
90
|
+
*/
|
|
91
|
+
setTTL(userID: string, seconds: number): PasscodeState;
|
|
92
|
+
/**
|
|
93
|
+
* Gets the number of seconds until when the next passcode can be sent.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} userID - The UUID of the user.
|
|
96
|
+
* @return {number}
|
|
97
|
+
*/
|
|
98
|
+
getResendAfter(userID: string): number;
|
|
99
|
+
/**
|
|
100
|
+
* Sets the number of seconds until a new passcode can be sent.
|
|
101
|
+
*
|
|
102
|
+
* @param {string} userID - The UUID of the user.
|
|
103
|
+
* @param {string} seconds - Number of seconds the passcode is valid for.
|
|
104
|
+
* @return {PasscodeState}
|
|
105
|
+
*/
|
|
106
|
+
setResendAfter(userID: string, seconds: number): PasscodeState;
|
|
107
|
+
}
|
|
108
|
+
export { PasscodeState };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { UserState } from "./UserState";
|
|
2
|
+
/**
|
|
3
|
+
* @interface
|
|
4
|
+
* @category SDK
|
|
5
|
+
* @subcategory Internal
|
|
6
|
+
* @property {number=} retryAfter - Timestamp (in seconds since January 1, 1970 00:00:00 UTC) indicating when the next password login can be attempted.
|
|
7
|
+
*/
|
|
8
|
+
export interface LocalStoragePassword {
|
|
9
|
+
retryAfter?: number;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A class that manages the password login state.
|
|
13
|
+
*
|
|
14
|
+
* @extends UserState
|
|
15
|
+
* @category SDK
|
|
16
|
+
* @subcategory Internal
|
|
17
|
+
*/
|
|
18
|
+
declare class PasswordState extends UserState {
|
|
19
|
+
/**
|
|
20
|
+
* Get the password state.
|
|
21
|
+
*
|
|
22
|
+
* @private
|
|
23
|
+
* @param {string} userID - The UUID of the user.
|
|
24
|
+
* @return {LocalStoragePassword}
|
|
25
|
+
*/
|
|
26
|
+
private getState;
|
|
27
|
+
/**
|
|
28
|
+
* Reads the current state.
|
|
29
|
+
*
|
|
30
|
+
* @public
|
|
31
|
+
* @return {PasswordState}
|
|
32
|
+
*/
|
|
33
|
+
read(): PasswordState;
|
|
34
|
+
/**
|
|
35
|
+
* Gets the number of seconds until when a new password login can be attempted.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} userID - The UUID of the user.
|
|
38
|
+
* @return {number}
|
|
39
|
+
*/
|
|
40
|
+
getRetryAfter(userID: string): number;
|
|
41
|
+
/**
|
|
42
|
+
* Sets the number of seconds until a new password login can be attempted.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} userID - The UUID of the user.
|
|
45
|
+
* @param {string} seconds - Number of seconds the passcode is valid for.
|
|
46
|
+
* @return {PasswordState}
|
|
47
|
+
*/
|
|
48
|
+
setRetryAfter(userID: string, seconds: number): PasswordState;
|
|
49
|
+
}
|
|
50
|
+
export { PasswordState };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { LocalStorageUsers } from "./UserState";
|
|
2
|
+
/**
|
|
3
|
+
* @interface
|
|
4
|
+
* @category SDK
|
|
5
|
+
* @subcategory Internal
|
|
6
|
+
* @property {LocalStorageUsers=} users - The user states.
|
|
7
|
+
*/
|
|
8
|
+
interface LocalStorage {
|
|
9
|
+
users?: LocalStorageUsers;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* A class to read and write local storage contents.
|
|
13
|
+
*
|
|
14
|
+
* @abstract
|
|
15
|
+
* @param {string} key - The local storage key.
|
|
16
|
+
* @category SDK
|
|
17
|
+
* @subcategory Internal
|
|
18
|
+
*/
|
|
19
|
+
declare abstract class State {
|
|
20
|
+
private readonly key;
|
|
21
|
+
ls: LocalStorage;
|
|
22
|
+
constructor(key?: string);
|
|
23
|
+
/**
|
|
24
|
+
* Reads and decodes the locally stored data.
|
|
25
|
+
*
|
|
26
|
+
* @return {State}
|
|
27
|
+
*/
|
|
28
|
+
read(): State;
|
|
29
|
+
/**
|
|
30
|
+
* Encodes and writes the data to the local storage.
|
|
31
|
+
*
|
|
32
|
+
* @return {State}
|
|
33
|
+
*/
|
|
34
|
+
write(): State;
|
|
35
|
+
/**
|
|
36
|
+
* Converts a timestamp into remaining seconds that you can count down.
|
|
37
|
+
*
|
|
38
|
+
* @static
|
|
39
|
+
* @param {number} time - Timestamp in seconds (since January 1, 1970 00:00:00 UTC).
|
|
40
|
+
* @return {number}
|
|
41
|
+
*/
|
|
42
|
+
static timeToRemainingSeconds(time?: number): number;
|
|
43
|
+
/**
|
|
44
|
+
* Converts a number of seconds into a timestamp.
|
|
45
|
+
*
|
|
46
|
+
* @static
|
|
47
|
+
* @param {number} seconds - Remaining seconds to be converted into a timestamp.
|
|
48
|
+
* @return {number}
|
|
49
|
+
*/
|
|
50
|
+
static remainingSecondsToTime(seconds?: number): number;
|
|
51
|
+
}
|
|
52
|
+
export { State };
|
|
53
|
+
export type { LocalStorage };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @interface
|
|
3
|
+
* @category SDK
|
|
4
|
+
* @subcategory Internal
|
|
5
|
+
* @property {Object.<string, LocalStorageUser>} - A dictionary for mapping users to their states.
|
|
6
|
+
*/
|
|
7
|
+
import { State } from "./State";
|
|
8
|
+
import { LocalStorageWebauthn } from "./WebauthnState";
|
|
9
|
+
import { LocalStoragePasscode } from "./PasscodeState";
|
|
10
|
+
import { LocalStoragePassword } from "./PasswordState";
|
|
11
|
+
/**
|
|
12
|
+
* @interface
|
|
13
|
+
* @category SDK
|
|
14
|
+
* @subcategory Internal
|
|
15
|
+
* @property {LocalStorageWebauthn=} webauthn - Information about WebAuthn credentials.
|
|
16
|
+
* @property {LocalStoragePasscode=} passcode - Information about the active passcode.
|
|
17
|
+
* @property {LocalStoragePassword=} password - Information about the password login attempts.
|
|
18
|
+
*/
|
|
19
|
+
interface LocalStorageUser {
|
|
20
|
+
webauthn?: LocalStorageWebauthn;
|
|
21
|
+
passcode?: LocalStoragePasscode;
|
|
22
|
+
password?: LocalStoragePassword;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* @interface
|
|
26
|
+
* @category SDK
|
|
27
|
+
* @subcategory Internal
|
|
28
|
+
* @property {Object.<string, LocalStorageUser>} - A dictionary for mapping users to their states.
|
|
29
|
+
*/
|
|
30
|
+
export interface LocalStorageUsers {
|
|
31
|
+
[userID: string]: LocalStorageUser;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A class to read and write local storage contents.
|
|
35
|
+
*
|
|
36
|
+
* @abstract
|
|
37
|
+
* @extends State
|
|
38
|
+
* @param {string} key - The local storage key.
|
|
39
|
+
* @category SDK
|
|
40
|
+
* @subcategory Internal
|
|
41
|
+
*/
|
|
42
|
+
declare abstract class UserState extends State {
|
|
43
|
+
/**
|
|
44
|
+
* Gets the state of the specified user.
|
|
45
|
+
*
|
|
46
|
+
* @param {string} userID - The UUID of the user.
|
|
47
|
+
* @return {LocalStorageUser}
|
|
48
|
+
*/
|
|
49
|
+
getUserState(userID: string): LocalStorageUser;
|
|
50
|
+
}
|
|
51
|
+
export { UserState };
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { UserState } from "./UserState";
|
|
2
|
+
import { Credential } from "../Dto";
|
|
3
|
+
/**
|
|
4
|
+
* @interface
|
|
5
|
+
* @category SDK
|
|
6
|
+
* @subcategory Internal
|
|
7
|
+
* @property {string[]?} credentials - A list of known credential IDs on the current browser.
|
|
8
|
+
*/
|
|
9
|
+
export interface LocalStorageWebauthn {
|
|
10
|
+
credentials?: string[];
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A class that manages WebAuthn credentials via local storage.
|
|
14
|
+
*
|
|
15
|
+
* @extends UserState
|
|
16
|
+
* @category SDK
|
|
17
|
+
* @subcategory Internal
|
|
18
|
+
*/
|
|
19
|
+
declare class WebauthnState extends UserState {
|
|
20
|
+
/**
|
|
21
|
+
* Gets the WebAuthn state.
|
|
22
|
+
*
|
|
23
|
+
* @private
|
|
24
|
+
* @param {string} userID - The UUID of the user.
|
|
25
|
+
* @return {LocalStorageWebauthn}
|
|
26
|
+
*/
|
|
27
|
+
private getState;
|
|
28
|
+
/**
|
|
29
|
+
* Reads the current state.
|
|
30
|
+
*
|
|
31
|
+
* @public
|
|
32
|
+
* @return {WebauthnState}
|
|
33
|
+
*/
|
|
34
|
+
read(): WebauthnState;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the list of known credentials on the current browser.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} userID - The UUID of the user.
|
|
39
|
+
* @return {string[]}
|
|
40
|
+
*/
|
|
41
|
+
getCredentials(userID: string): string[];
|
|
42
|
+
/**
|
|
43
|
+
* Adds the credential to the list of known credentials.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} userID - The UUID of the user.
|
|
46
|
+
* @param {string} credentialID - The WebAuthn credential ID.
|
|
47
|
+
* @return {WebauthnState}
|
|
48
|
+
*/
|
|
49
|
+
addCredential(userID: string, credentialID: string): WebauthnState;
|
|
50
|
+
/**
|
|
51
|
+
* Returns the intersection between the specified list of credentials and the known credentials stored in
|
|
52
|
+
* the local storage.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} userID - The UUID of the user.
|
|
55
|
+
* @param {Credential[]} match - A list of credential IDs to be matched against the local storage.
|
|
56
|
+
* @return {Credential[]}
|
|
57
|
+
*/
|
|
58
|
+
matchCredentials(userID: string, match: Credential[]): Credential[];
|
|
59
|
+
}
|
|
60
|
+
export { WebauthnState };
|
package/dist/sdk.cjs
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
function t(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,r(t,e)}function e(t){return e=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(t){return t.__proto__||Object.getPrototypeOf(t)},e(t)}function r(t,e){return r=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t},r(t,e)}function n(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){})),!0}catch(t){return!1}}function i(t,e,o){return i=n()?Reflect.construct.bind():function(t,e,n){var i=[null];i.push.apply(i,e);var o=new(Function.bind.apply(t,i));return n&&r(o,n.prototype),o},i.apply(null,arguments)}function o(t){var n="function"==typeof Map?new Map:void 0;return o=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==n){if(n.has(t))return n.get(t);n.set(t,o)}function o(){return i(t,arguments,e(this).constructor)}return o.prototype=Object.create(t.prototype,{constructor:{value:o,enumerable:!1,writable:!0,configurable:!0}}),r(o,t)},o(t)}function s(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var a=/*#__PURE__*/function(e){function r(t,n,i){var o;return(o=e.call(this,t)||this).code=void 0,o.cause=void 0,o.code=n,o.cause=i,Object.setPrototypeOf(s(o),r.prototype),o}return t(r,e),r}(/*#__PURE__*/o(Error)),c=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Technical error","somethingWentWrong",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),u=/*#__PURE__*/function(e){function r(t,n){var i;return i=e.call(this,"Conflict error","conflict",n)||this,Object.setPrototypeOf(s(i),r.prototype),i}return t(r,e),r}(a),l=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Request timed out error","requestTimeout",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),h=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Request cancelled error","requestCancelled",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),d=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid password error","invalidPassword",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),f=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid Passcode error","invalidPasscode",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),p=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid WebAuthn credential error","invalidWebauthnCredential",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),w=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Passcode expired error","passcodeExpired",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),v=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Maximum number of Passcode attempts reached error","passcodeAttemptsReached",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),y=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Not found error","notFound",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),m=/*#__PURE__*/function(e){function r(t,n){var i;return(i=e.call(this,"Too many requests error","tooManyRequests",n)||this).retryAfter=void 0,i.retryAfter=t,Object.setPrototypeOf(s(i),r.prototype),i}return t(r,e),r}(a),g=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Unauthorized error","unauthorized",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),P=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"User verification error","userVerification",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),b=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Maximum number of email addresses reached error","maxNumOfEmailAddressesReached",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),S=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"The email address already exists","emailAddressAlreadyExistsError",t)||this,Object.setPrototypeOf(s(n),r.prototype),n}return t(r,e),r}(a),O=/*#__PURE__*/function(e){function r(t,n){var i;return i=e.call(this,"An error occurred during third party sign up/sign in",t,n)||this,Object.setPrototypeOf(s(i),r.prototype),i}return t(r,e),r}(a);function A(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)t[n]=r[n]}return t}var x=function t(e,r){function n(t,n,i){if("undefined"!=typeof document){"number"==typeof(i=A({},r,i)).expires&&(i.expires=new Date(Date.now()+864e5*i.expires)),i.expires&&(i.expires=i.expires.toUTCString()),t=encodeURIComponent(t).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var o="";for(var s in i)i[s]&&(o+="; "+s,!0!==i[s]&&(o+="="+i[s].split(";")[0]));return document.cookie=t+"="+e.write(n,t)+o}}return Object.create({set:n,get:function(t){if("undefined"!=typeof document&&(!arguments.length||t)){for(var r=document.cookie?document.cookie.split("; "):[],n={},i=0;i<r.length;i++){var o=r[i].split("="),s=o.slice(1).join("=");try{var a=decodeURIComponent(o[0]);if(n[a]=e.read(s,a),t===a)break}catch(t){}}return t?n[t]:n}},remove:function(t,e){n(t,"",A({},e,{expires:-1}))},withAttributes:function(e){return t(this.converter,A({},this.attributes,e))},withConverter:function(e){return t(A({},this.converter,e),this.attributes)}},{attributes:{value:Object.freeze(r)},converter:{value:Object.freeze(e)}})}({read:function(t){return'"'===t[0]&&(t=t.slice(1,-1)),t.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(t){return encodeURIComponent(t).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}},{path:"/"}),j=/*#__PURE__*/function(){function t(t){this._xhr=void 0,this._xhr=t}return t.prototype.get=function(t){return this._xhr.getResponseHeader(t)},t}(),C=/*#__PURE__*/function(){function t(t){this.headers=void 0,this.ok=void 0,this.status=void 0,this.statusText=void 0,this.url=void 0,this._decodedJSON=void 0,this.xhr=void 0,this.headers=new j(t),this.ok=t.status>=200&&t.status<=299,this.status=t.status,this.statusText=t.statusText,this.url=t.responseURL,this.xhr=t}var e=t.prototype;return e.json=function(){return this._decodedJSON||(this._decodedJSON=JSON.parse(this.xhr.response)),this._decodedJSON},e.parseRetryAfterHeader=function(){var t=parseInt(this.headers.get("Retry-After"),10);return isNaN(t)?0:t},t}(),R=/*#__PURE__*/function(){function t(t,e){void 0===e&&(e=13e3),this.timeout=void 0,this.api=void 0,this.api=t,this.timeout=e}var e=t.prototype;return e._fetch=function(t,e,r){void 0===r&&(r=new XMLHttpRequest);var n=this.api,i=n+t,o=this.timeout,s="hanko",a=x.get(s);return new Promise(function(t,u){r.open(e.method,i,!0),r.setRequestHeader("Accept","application/json"),r.setRequestHeader("Content-Type","application/json"),a&&r.setRequestHeader("Authorization","Bearer "+a),r.timeout=o,r.withCredentials=!0,r.onload=function(){if(r.getAllResponseHeaders().split("\r\n").filter(function(t){return t.toLowerCase().startsWith("x-auth-token")}).length){var e=r.getResponseHeader("X-Auth-Token");if(e){var i=!!n.match("^https://");x.set(s,e,{secure:i})}}t(new C(r))},r.onerror=function(){u(new c)},r.ontimeout=function(){u(new l)},r.send(e.body?e.body.toString():null)})},e.get=function(t){return this._fetch(t,{method:"GET"})},e.post=function(t,e){return this._fetch(t,{method:"POST",body:JSON.stringify(e)})},e.put=function(t,e){return this._fetch(t,{method:"PUT",body:JSON.stringify(e)})},e.patch=function(t,e){return this._fetch(t,{method:"PATCH",body:JSON.stringify(e)})},e.delete=function(t){return this._fetch(t,{method:"DELETE"})},t}(),E=function(t,e){void 0===e&&(e=13e3),this.client=void 0,this.client=new R(t,e)},T=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}return t(r,e),r.prototype.get=function(){try{return Promise.resolve(this.client.get("/.well-known/config")).then(function(t){if(!t.ok)throw new c;return t.json()})}catch(t){return Promise.reject(t)}},r}(E),_=/*#__PURE__*/function(){function t(t){void 0===t&&(t="hanko"),this.key=void 0,this.ls=void 0,this.key=t,this.ls={}}var e=t.prototype;return e.read=function(){var t;try{var e=localStorage.getItem(this.key),r=decodeURIComponent(decodeURI(window.atob(e)));t=JSON.parse(r)}catch(t){return this.ls={},this}return this.ls=t,this},e.write=function(){var t=JSON.stringify(this.ls),e=window.btoa(encodeURI(encodeURIComponent(t)));return localStorage.setItem(this.key,e),this},t.timeToRemainingSeconds=function(t){return void 0===t&&(t=0),t-Math.floor(Date.now()/1e3)},t.remainingSecondsToTime=function(t){return void 0===t&&(t=0),Math.floor(Date.now()/1e3)+t},t}(),k=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}return t(r,e),r.prototype.getUserState=function(t){var e;return(e=this.ls).users||(e.users={}),Object.prototype.hasOwnProperty.call(this.ls.users,t)||(this.ls.users[t]={}),this.ls.users[t]},r}(_),I=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}t(r,e);var n=r.prototype;return n.getState=function(t){var r;return(r=e.prototype.getUserState.call(this,t)).passcode||(r.passcode={})},n.read=function(){return e.prototype.read.call(this),this},n.getActiveID=function(t){return this.getState(t).id},n.setActiveID=function(t,e){return this.getState(t).id=e,this},n.getEmailID=function(t){return this.getState(t).emailID},n.setEmailID=function(t,e){return this.getState(t).emailID=e,this},n.reset=function(t){var e=this.getState(t);return delete e.id,delete e.ttl,delete e.resendAfter,delete e.emailID,this},n.getTTL=function(t){return _.timeToRemainingSeconds(this.getState(t).ttl)},n.setTTL=function(t,e){return this.getState(t).ttl=_.remainingSecondsToTime(e),this},n.getResendAfter=function(t){return _.timeToRemainingSeconds(this.getState(t).resendAfter)},n.setResendAfter=function(t,e){return this.getState(t).resendAfter=_.remainingSecondsToTime(e),this},r}(k),U=/*#__PURE__*/function(e){function r(t,r){var n;return void 0===r&&(r=13e3),(n=e.call(this,t,r)||this).state=void 0,n.state=new I,n}t(r,e);var n=r.prototype;return n.initialize=function(t,e,r){try{var n=this;n.state.read();var i=n.state.getTTL(t),o=n.state.getActiveID(t),s=n.state.getEmailID(t),a=n.state.getResendAfter(t);if(a>0)throw new m(a);if(!r&&i>0&&e===s)return Promise.resolve({id:o,ttl:i});var u={user_id:t};return e&&(u.email_id=e),Promise.resolve(n.client.post("/passcode/login/initialize",u)).then(function(r){if(429===r.status)throw a=r.parseRetryAfterHeader(),n.state.setResendAfter(t,a).write(),new m(a);if(401===r.status)throw new g;if(!r.ok)throw new c;var i=r.json();return n.state.setActiveID(t,i.id).setTTL(t,i.ttl),e&&n.state.setEmailID(t,e),n.state.write(),i})}catch(t){return Promise.reject(t)}},n.finalize=function(t,e){try{var r=this,n=r.state.read().getActiveID(t);if(r.state.getTTL(t)<=0)throw new w;return Promise.resolve(r.client.post("/passcode/login/finalize",{id:n,code:e})).then(function(e){if(401===e.status)throw new f;if(410===e.status)throw r.state.reset(t).write(),new v;if(!e.ok)throw new c;r.state.reset(t).write()})}catch(t){return Promise.reject(t)}},n.getTTL=function(t){return this.state.read().getTTL(t)},n.getResendAfter=function(t){return this.state.read().getResendAfter(t)},r}(E),D=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}t(r,e);var n=r.prototype;return n.getState=function(t){var r;return(r=e.prototype.getUserState.call(this,t)).password||(r.password={})},n.read=function(){return e.prototype.read.call(this),this},n.getRetryAfter=function(t){return _.timeToRemainingSeconds(this.getState(t).retryAfter)},n.setRetryAfter=function(t,e){return this.getState(t).retryAfter=_.remainingSecondsToTime(e),this},r}(k),N=/*#__PURE__*/function(e){function r(t,r){var n;return void 0===r&&(r=13e3),(n=e.call(this,t,r)||this).passwordState=void 0,n.passcodeState=void 0,n.passwordState=new D,n.passcodeState=new I,n}t(r,e);var n=r.prototype;return n.login=function(t,e){try{var r=this;return Promise.resolve(r.client.post("/password/login",{user_id:t,password:e})).then(function(e){if(401===e.status)throw new d;if(429===e.status){var n=e.parseRetryAfterHeader();throw r.passwordState.read().setRetryAfter(t,n).write(),new m(n)}if(!e.ok)throw new c;r.passcodeState.read().reset(t).write()})}catch(t){return Promise.reject(t)}},n.update=function(t,e){try{return Promise.resolve(this.client.put("/password",{user_id:t,password:e})).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c})}catch(t){return Promise.reject(t)}},n.getRetryAfter=function(t){return this.passwordState.read().getRetryAfter(t)},r}(E),q=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}t(r,e);var n=r.prototype;return n.getInfo=function(t){try{return Promise.resolve(this.client.post("/user",{email:t})).then(function(t){if(404===t.status)throw new y;if(!t.ok)throw new c;return t.json()})}catch(t){return Promise.reject(t)}},n.create=function(t){try{return Promise.resolve(this.client.post("/users",{email:t})).then(function(t){if(409===t.status)throw new u;if(!t.ok)throw new c;return t.json()})}catch(t){return Promise.reject(t)}},n.getCurrent=function(){try{var t=this;return Promise.resolve(t.client.get("/me")).then(function(e){if(400===e.status||401===e.status||404===e.status)throw new g;if(!e.ok)throw new c;var r=e.json();return Promise.resolve(t.client.get("/users/"+r.id)).then(function(t){if(400===t.status||401===t.status||404===t.status)throw new g;if(!t.ok)throw new c;return t.json()})})}catch(t){return Promise.reject(t)}},r}(E);function M(t){const e="==".slice(0,(4-t.length%4)%4),r=t.replace(/-/g,"+").replace(/_/g,"/")+e,n=atob(r),i=new ArrayBuffer(n.length),o=new Uint8Array(i);for(let t=0;t<n.length;t++)o[t]=n.charCodeAt(t);return i}function K(t){const e=new Uint8Array(t);let r="";for(const t of e)r+=String.fromCharCode(t);return btoa(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=/g,"")}function W(t,e,r){if("copy"===e)return r;if("convert"===e)return t(r);if(e instanceof Array)return r.map(r=>W(t,e[0],r));if(e instanceof Object){const n={};for(const[i,o]of Object.entries(e)){if(o.derive){const t=o.derive(r);void 0!==t&&(r[i]=t)}if(i in r)n[i]=null!=r[i]?W(t,o.schema,r[i]):null;else if(o.required)throw new Error(`Missing key: ${i}`)}return n}}function z(t,e){return{required:!0,schema:t,derive:e}}function H(t){return{required:!0,schema:t}}function L(t){return{required:!1,schema:t}}var J={type:H("copy"),id:H("convert"),transports:L("copy")},B={appid:L("copy"),appidExclude:L("copy"),credProps:L("copy")},F={appid:L("copy"),appidExclude:L("copy"),credProps:L("copy")},V={publicKey:H({rp:H("copy"),user:H({id:H("convert"),name:H("copy"),displayName:H("copy")}),challenge:H("convert"),pubKeyCredParams:H("copy"),timeout:L("copy"),excludeCredentials:L([J]),authenticatorSelection:L("copy"),attestation:L("copy"),extensions:L(B)}),signal:L("copy")},X={type:H("copy"),id:H("copy"),rawId:H("convert"),authenticatorAttachment:L("copy"),response:H({clientDataJSON:H("convert"),attestationObject:H("convert"),transports:z("copy",t=>{var e;return(null==(e=t.getTransports)?void 0:e.call(t))||[]})}),clientExtensionResults:z(F,t=>t.getClientExtensionResults())},G={mediation:L("copy"),publicKey:H({challenge:H("convert"),timeout:L("copy"),rpId:L("copy"),allowCredentials:L([J]),userVerification:L("copy"),extensions:L(B)}),signal:L("copy")},$={type:H("copy"),id:H("copy"),rawId:H("convert"),authenticatorAttachment:L("copy"),response:H({clientDataJSON:H("convert"),authenticatorData:H("convert"),signature:H("convert"),userHandle:H("convert")}),clientExtensionResults:z(F,t=>t.getClientExtensionResults())};async function Q(t){const e=await navigator.credentials.create(function(t){return W(M,V,t)}(t));return function(t){return W(K,X,t)}(e)}async function Y(t){const e=await navigator.credentials.get(function(t){return W(M,G,t)}(t));return function(t){return W(K,$,t)}(e)}var Z=/*#__PURE__*/function(){function t(){}return t.supported=function(){return!!(navigator.credentials&&navigator.credentials.create&&navigator.credentials.get&&window.PublicKeyCredential)},t.isPlatformAuthenticatorAvailable=function(){try{return this.supported()&&window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable?Promise.resolve(window.PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable()):Promise.resolve(!1)}catch(t){return Promise.reject(t)}},t.isSecurityKeySupported=function(){try{return void 0!==window.PublicKeyCredential&&window.PublicKeyCredential.isExternalCTAP2SecurityKeySupported?Promise.resolve(window.PublicKeyCredential.isExternalCTAP2SecurityKeySupported()):Promise.resolve(this.supported())}catch(t){return Promise.reject(t)}},t.isConditionalMediationAvailable=function(){try{return window.PublicKeyCredential&&window.PublicKeyCredential.isConditionalMediationAvailable?Promise.resolve(window.PublicKeyCredential.isConditionalMediationAvailable()):Promise.resolve(!1)}catch(t){return Promise.reject(t)}},t}(),tt=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}t(r,e);var n=r.prototype;return n.getState=function(t){var r;return(r=e.prototype.getUserState.call(this,t)).webauthn||(r.webauthn={})},n.read=function(){return e.prototype.read.call(this),this},n.getCredentials=function(t){var e;return(e=this.getState(t)).credentials||(e.credentials=[])},n.addCredential=function(t,e){return this.getCredentials(t).push(e),this},n.matchCredentials=function(t,e){return this.getCredentials(t).filter(function(t){return e.find(function(e){return e.id===t})}).map(function(t){return{id:t}})},r}(k);function et(t,e){try{var r=t()}catch(t){return e(t)}return r&&r.then?r.then(void 0,e):r}var rt=/*#__PURE__*/function(e){function r(t,r){var n;return void 0===r&&(r=13e3),(n=e.call(this,t,r)||this).webauthnState=void 0,n.passcodeState=void 0,n.controller=void 0,n._getCredential=Y,n._createCredential=Q,n.webauthnState=new tt,n.passcodeState=new I,n}t(r,e);var n=r.prototype;return n.login=function(t,e){try{var r=this;return Promise.resolve(r.client.post("/webauthn/login/initialize",{user_id:t})).then(function(n){function i(e){return Promise.resolve(r.client.post("/webauthn/login/finalize",o)).then(function(e){if(400===e.status||401===e.status)throw new p;if(!e.ok)throw new c;var n=e.json();r.webauthnState.read().addCredential(n.user_id,n.credential_id).write(),r.passcodeState.read().reset(t).write()})}if(!n.ok)throw new c;var o,s=n.json();s.signal=r._createAbortSignal(),e&&(s.mediation="conditional");var a=et(function(){return Promise.resolve(r._getCredential(s)).then(function(t){o=t})},function(t){throw new h(t)});return a&&a.then?a.then(i):i()})}catch(t){return Promise.reject(t)}},n.register=function(){try{var t=this;return Promise.resolve(t.client.post("/webauthn/registration/initialize")).then(function(e){function r(e){return n.transports=n.response.transports,Promise.resolve(t.client.post("/webauthn/registration/finalize",n)).then(function(e){if(e.status>=400&&e.status<=499){if(422===e.status)throw new P;throw new g}if(!e.ok)throw new c;var r=e.json();t.webauthnState.read().addCredential(r.user_id,r.credential_id).write()})}if(e.status>=400&&e.status<=499)throw new g;if(!e.ok)throw new c;var n,i=e.json();i.signal=t._createAbortSignal();var o=et(function(){return Promise.resolve(t._createCredential(i)).then(function(t){n=t})},function(t){throw new h(t)});return o&&o.then?o.then(r):r()})}catch(t){return Promise.reject(t)}},n.listCredentials=function(){try{return Promise.resolve(this.client.get("/webauthn/credentials")).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c;return t.json()})}catch(t){return Promise.reject(t)}},n.updateCredential=function(t,e){try{return Promise.resolve(this.client.patch("/webauthn/credentials/"+t,{name:e})).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c})}catch(t){return Promise.reject(t)}},n.deleteCredential=function(t){try{return Promise.resolve(this.client.delete("/webauthn/credentials/"+t)).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c})}catch(t){return Promise.reject(t)}},n.shouldRegister=function(t){try{var e=Z.supported();if(!t.webauthn_credentials||!t.webauthn_credentials.length)return Promise.resolve(e);var r=this.webauthnState.read().matchCredentials(t.id,t.webauthn_credentials);return Promise.resolve(e&&!r.length)}catch(t){return Promise.reject(t)}},n._createAbortSignal=function(){return this.controller&&this.controller.abort(),this.controller=new AbortController,this.controller.signal},r}(E),nt=/*#__PURE__*/function(e){function r(){return e.apply(this,arguments)||this}t(r,e);var n=r.prototype;return n.list=function(){try{return Promise.resolve(this.client.get("/emails")).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c;return t.json()})}catch(t){return Promise.reject(t)}},n.create=function(t){try{return Promise.resolve(this.client.post("/emails",{address:t})).then(function(t){if(t.ok)return t.json();if(400===t.status)throw new S;if(401===t.status)throw new g;if(409===t.status)throw new b;throw new c})}catch(t){return Promise.reject(t)}},n.setPrimaryEmail=function(t){try{return Promise.resolve(this.client.post("/emails/"+t+"/set_primary")).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c})}catch(t){return Promise.reject(t)}},n.delete=function(t){try{return Promise.resolve(this.client.delete("/emails/"+t)).then(function(t){if(401===t.status)throw new g;if(!t.ok)throw new c})}catch(t){return Promise.reject(t)}},r}(E),it=/*#__PURE__*/function(e){function r(t,r){return void 0===r&&(r=13e3),e.call(this,t,r)||this}t(r,e);var n=r.prototype;return n.auth=function(t,e){try{var r=new URL("/thirdparty/auth",this.client.api);if(!t)throw new O("somethingWentWrong",new Error("provider missing from request"));if(!e)throw new O("somethingWentWrong",new Error("redirectTo missing from request"));return r.searchParams.append("provider",t),r.searchParams.append("redirect_to",e),window.location.assign(r.href),Promise.resolve()}catch(t){return Promise.reject(t)}},n.getError=function(){var t=new URLSearchParams(window.location.search),e=t.get("error"),r=t.get("error_description");if(e){var n="";switch(e){case"access_denied":n="thirdPartyAccessDenied";break;case"user_conflict":n="emailAddressAlreadyExistsError";break;case"multiple_accounts":n="thirdPartyMultipleAccounts";break;case"unverified_email":n="thirdPartyUnverifiedEmail";break;case"email_maxnum":n="maxNumOfEmailAddressesReached";break;default:n="somethingWentWrong"}return new O(n,new Error(r))}},r}(E);exports.ConfigClient=T,exports.ConflictError=u,exports.EmailAddressAlreadyExistsError=S,exports.EmailClient=nt,exports.Hanko=function(t,e){void 0===e&&(e=13e3),this.api=void 0,this.config=void 0,this.user=void 0,this.webauthn=void 0,this.password=void 0,this.passcode=void 0,this.email=void 0,this.thirdParty=void 0,this.api=t,this.config=new T(t,e),this.user=new q(t,e),this.webauthn=new rt(t,e),this.password=new N(t,e),this.passcode=new U(t,e),this.email=new nt(t,e),this.thirdParty=new it(t,e)},exports.HankoError=a,exports.InvalidPasscodeError=f,exports.InvalidPasswordError=d,exports.InvalidWebauthnCredentialError=p,exports.MaxNumOfEmailAddressesReachedError=b,exports.MaxNumOfPasscodeAttemptsReachedError=v,exports.NotFoundError=y,exports.PasscodeClient=U,exports.PasscodeExpiredError=w,exports.PasswordClient=N,exports.RequestTimeoutError=l,exports.TechnicalError=c,exports.ThirdPartyClient=it,exports.ThirdPartyError=O,exports.TooManyRequestsError=m,exports.UnauthorizedError=g,exports.UserClient=q,exports.UserVerificationError=P,exports.WebauthnClient=rt,exports.WebauthnRequestCancelledError=h,exports.WebauthnSupport=Z;
|
|
2
|
+
//# sourceMappingURL=sdk.cjs.map
|