@teamhanko/hanko-frontend-sdk 0.4.0-alpha → 0.5.4-beta

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.
@@ -1,8 +1,8 @@
1
1
  import { create as createWebauthnCredential, get as getWebauthnCredential } from "@github/webauthn-json";
2
2
  import { Client } from "./Client";
3
- import { PasscodeState } from "../state/PasscodeState";
4
- import { WebauthnState } from "../state/WebauthnState";
5
- import { User, WebauthnCredentials } from "../Dto";
3
+ import { PasscodeState } from "../state/users/PasscodeState";
4
+ import { WebauthnState } from "../state/users/WebauthnState";
5
+ import { User, WebauthnCredentials, WebauthnFinalized } from "../Dto";
6
6
  /**
7
7
  * A class that handles WebAuthn authentication and registration.
8
8
  *
@@ -32,8 +32,9 @@ declare class WebauthnClient extends Client {
32
32
  * @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/webauthnLoginInit
33
33
  * @see https://docs.hanko.io/api/public#tag/WebAuthn/operation/webauthnLoginFinal
34
34
  * @see https://www.w3.org/TR/webauthn-2/#authentication-ceremony
35
+ * @return {WebauthnFinalized}
35
36
  */
36
- login(userID?: string, useConditionalMediation?: boolean): Promise<void>;
37
+ login(userID?: string, useConditionalMediation?: boolean): Promise<WebauthnFinalized>;
37
38
  /**
38
39
  * Performs a WebAuthn registration ceremony.
39
40
  *
@@ -64,7 +65,6 @@ declare class WebauthnClient extends Client {
64
65
  * @param {string=} credentialID - The credential's UUID.
65
66
  * @param {string} name - The new credential name.
66
67
  * @return {Promise<void>}
67
- * @throws {NotFoundError}
68
68
  * @throws {UnauthorizedError}
69
69
  * @throws {RequestTimeoutError}
70
70
  * @throws {TechnicalError}
@@ -76,7 +76,6 @@ declare class WebauthnClient extends Client {
76
76
  *
77
77
  * @param {string=} credentialID - The credential's UUID.
78
78
  * @return {Promise<void>}
79
- * @throws {NotFoundError}
80
79
  * @throws {UnauthorizedError}
81
80
  * @throws {RequestTimeoutError}
82
81
  * @throws {TechnicalError}
@@ -0,0 +1,76 @@
1
+ /**
2
+ * The type of the `hanko-session-created` event.
3
+ * @typedef {string} sessionCreatedType
4
+ * @memberOf Listener
5
+ */
6
+ export declare const sessionCreatedType: "hanko-session-created";
7
+ /**
8
+ * The type of the `hanko-session-resumed` event.
9
+ * @typedef {string} sessionResumedType
10
+ * @memberOf Listener
11
+ */
12
+ export declare const sessionResumedType: "hanko-session-resumed";
13
+ /**
14
+ * The type of the `hanko-session-expired` event.
15
+ * @typedef {string} sessionExpiredType
16
+ * @memberOf Listener
17
+ */
18
+ export declare const sessionExpiredType: "hanko-session-expired";
19
+ /**
20
+ * The type of the `hanko-user-logged-out` event.
21
+ * @typedef {string} userLoggedOutType
22
+ * @memberOf Listener
23
+ */
24
+ export declare const userLoggedOutType: "hanko-user-logged-out";
25
+ /**
26
+ * The type of the `hanko-user-deleted` event.
27
+ * @typedef {string} userDeletedType
28
+ * @memberOf Listener
29
+ */
30
+ export declare const userDeletedType: "hanko-user-deleted";
31
+ /**
32
+ * The type of the `hanko-auth-flow-completed` event.
33
+ * @typedef {string} authFlowCompletedType
34
+ * @memberOf Listener
35
+ */
36
+ export declare const authFlowCompletedType: "hanko-auth-flow-completed";
37
+ export declare const sessionNotPresentType: "hanko-session-not-present";
38
+ /**
39
+ * The data passed in the `hanko-session-created` or `hanko-session-resumed` event.
40
+ *
41
+ * @interface
42
+ * @category SDK
43
+ * @subcategory Events
44
+ * @property {string=} jwt - The JSON web token associated with the session. Only present when the Hanko-API allows the JWT to be accessible client-side.
45
+ * @property {number} expirationSeconds - The number of seconds until the JWT expires.
46
+ * @property {string} userID - The user associated with the session.
47
+ */
48
+ export interface SessionEventDetail {
49
+ jwt?: string;
50
+ expirationSeconds: number;
51
+ userID: string;
52
+ }
53
+ /**
54
+ * The data passed in the `hanko-auth-flow-completed` event.
55
+ *
56
+ * @interface
57
+ * @category SDK
58
+ * @subcategory Events
59
+ * @property {string} userID - The user associated with the removed session.
60
+ */
61
+ export interface AuthFlowCompletedEventDetail {
62
+ userID: string;
63
+ }
64
+ /**
65
+ * A custom event that includes a detail object.
66
+ *
67
+ * @category SDK
68
+ * @subcategory Events
69
+ * @extends CustomEvent
70
+ * @ignore
71
+ * @param {string} type - The type of the event.
72
+ * @param {T} detail - The detail object to include in the event.
73
+ */
74
+ export declare class CustomEventWithDetail<T> extends CustomEvent<T> {
75
+ constructor(type: string, detail: T);
76
+ }
@@ -0,0 +1,52 @@
1
+ import { SessionEventDetail, AuthFlowCompletedEventDetail } from "./CustomEvents";
2
+ /**
3
+ * A class that dispatches custom events.
4
+ *
5
+ * @category SDK
6
+ * @subcategory Internal
7
+ */
8
+ export declare class Dispatcher {
9
+ _dispatchEvent: any;
10
+ /**
11
+ * Dispatches a custom event.
12
+ *
13
+ * @param {string} type
14
+ * @param {T} detail
15
+ * @private
16
+ */
17
+ private dispatch;
18
+ /**
19
+ * Dispatches a "hanko-session-resumed" event to the document with the specified detail.
20
+ *
21
+ * @param {SessionEventDetail} detail - The event detail.
22
+ */
23
+ dispatchSessionResumedEvent(detail: SessionEventDetail): void;
24
+ /**
25
+ * Dispatches a "hanko-session-created" event to the document with the specified detail.
26
+ *
27
+ * @param {SessionEventDetail} detail - The event detail.
28
+ */
29
+ dispatchSessionCreatedEvent(detail: SessionEventDetail): void;
30
+ /**
31
+ * Dispatches a "hanko-session-expired" event to the document.
32
+ */
33
+ dispatchSessionExpiredEvent(): void;
34
+ /**
35
+ * Dispatches a "hanko-user-logged-out" event to the document.
36
+ */
37
+ dispatchUserLoggedOutEvent(): void;
38
+ /**
39
+ * Dispatches a "hanko-user-deleted" event to the document.
40
+ */
41
+ dispatchUserDeletedEvent(): void;
42
+ /**
43
+ * Dispatches a "hanko-auth-flow-completed" event to the document with the specified detail.
44
+ *
45
+ * @param {AuthFlowCompletedEventDetail} detail - The event detail.
46
+ */
47
+ dispatchAuthFlowCompletedEvent(detail: AuthFlowCompletedEventDetail): void;
48
+ /**
49
+ * Dispatches a "hanko-session-not-present" event to the document.
50
+ */
51
+ dispatchSessionNotPresent(): void;
52
+ }
@@ -0,0 +1,137 @@
1
+ import { Throttle } from "../Throttle";
2
+ import { SessionEventDetail, AuthFlowCompletedEventDetail } from "./CustomEvents";
3
+ /**
4
+ * A callback function to be executed when an event is triggered.
5
+ *
6
+ * @alias CallbackFunc
7
+ * @typedef {function} CallbackFunc
8
+ * @memberOf Listener
9
+ */
10
+ type CallbackFunc<T> = (detail: T) => any;
11
+ /**
12
+ * A function returned when adding an event listener. The function can be called to remove the corresponding event
13
+ * listener.
14
+ *
15
+ * @alias CleanupFunc
16
+ * @typedef {function} CleanupFunc
17
+ * @memberOf Listener
18
+ */
19
+ type CleanupFunc = () => void;
20
+ /**
21
+ * A class to bind event listener for custom events.
22
+ *
23
+ * @category SDK
24
+ * @subcategory Events
25
+ */
26
+ export declare class Listener {
27
+ throttleLimit: number;
28
+ _cleanupFunctions: CleanupFunc[];
29
+ _addEventListener: any;
30
+ _removeEventListener: any;
31
+ _throttle: typeof Throttle.throttle;
32
+ /**
33
+ * Wraps the given callback.
34
+ *
35
+ * @param callback
36
+ * @param throttle
37
+ * @private
38
+ * @return {WrappedCallback}
39
+ */
40
+ private wrapCallback;
41
+ /**
42
+ * Adds an event listener with the specified type, callback function, and options.
43
+ *
44
+ * @private
45
+ * @param {EventListenerWithTypeParams<T>} params - The parameters for the event listener.
46
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
47
+ */
48
+ private addEventListenerWithType;
49
+ /**
50
+ * Removes all currently installed event listeners.
51
+ */
52
+ removeEventListeners(): void;
53
+ /**
54
+ * Maps the parameters for an event listener to the `EventListenerWithTypeParams` interface.
55
+ *
56
+ * @static
57
+ * @private
58
+ * @param {string} type - The type of the event.
59
+ * @param {EventListenerParams<T>} params - The parameters for the event listener.
60
+ * @param {boolean} [throttle=false] - Whether the event listener should be throttled.
61
+ * @returns {EventListenerWithTypeParams<T>}
62
+ **/
63
+ private static mapAddEventListenerParams;
64
+ /**
65
+ * Adds an event listener with the specified type, callback function, and options.
66
+ *
67
+ * @private
68
+ * @param {string} type - The type of the event.
69
+ * @param {EventListenerParams<T>} params - The parameters for the event listener.
70
+ * @param {boolean=} throttle - Whether the event listener should be throttled.
71
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
72
+ */
73
+ private addEventListener;
74
+ /**
75
+ * Adds an event listener for "hanko-session-created" events. Will be triggered across all browser windows, when the user
76
+ * logs in, or when the page has been loaded or refreshed and there is a valid session.
77
+ *
78
+ * @param {CallbackFunc<SessionEventDetail>} callback - The function to be called when the event is triggered.
79
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
80
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
81
+ */
82
+ onSessionCreated(callback: CallbackFunc<SessionEventDetail>, once?: boolean): CleanupFunc;
83
+ /**
84
+ * Adds an event listener for "hanko-session-resumed" events. Will be triggered, after the page has been loaded and
85
+ * the user has an active session.
86
+ *
87
+ * @param {CallbackFunc<SessionEventDetail>} callback - The function to be called when the event is triggered.
88
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
89
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
90
+ */
91
+ onSessionResumed(callback: CallbackFunc<SessionEventDetail>, once?: boolean): CleanupFunc;
92
+ /**
93
+ * Adds an event listener for "hanko-session-expired" events. The event will be triggered across all browser windows
94
+ * as soon as the current JWT expires or the user logs out. It also triggers, when the user deletes the account in
95
+ * another window.
96
+ *
97
+ * @param {CallbackFunc<null>} callback - The function to be called when the event is triggered.
98
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
99
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
100
+ */
101
+ onSessionExpired(callback: CallbackFunc<null>, once?: boolean): CleanupFunc;
102
+ /**
103
+ * Adds an event listener for hanko-user-deleted events. The event triggers, when the user has deleted the account in
104
+ * the browser window where the deletion happened.
105
+ *
106
+ * @param {CallbackFunc<null>} callback - The function to be called when the event is triggered.
107
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
108
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
109
+ */
110
+ onUserLoggedOut(callback: CallbackFunc<null>, once?: boolean): CleanupFunc;
111
+ /**
112
+ * Adds an event listener for hanko-user-deleted events. The event triggers, when the user has deleted the account.
113
+ *
114
+ * @param {CallbackFunc<null>} callback - The function to be called when the event is triggered.
115
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
116
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
117
+ */
118
+ onUserDeleted(callback: CallbackFunc<null>, once?: boolean): CleanupFunc;
119
+ /**
120
+ * Adds an event listener for hanko-auth-flow-completed events. Will be triggered after the login or registration flow has been completed.
121
+ *
122
+ * @param {CallbackFunc<AuthFlowCompletedEventDetail>} callback - The function to be called when the event is triggered.
123
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
124
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
125
+ */
126
+ onAuthFlowCompleted(callback: CallbackFunc<AuthFlowCompletedEventDetail>, once?: boolean): CleanupFunc;
127
+ /**
128
+ * Adds an event listener for hanko-session-not-present events. Will be triggered initially, after the page has been
129
+ * loaded and the user is logged out.
130
+ *
131
+ * @param {CallbackFunc<AuthFlowCompletedEventDetail>} callback - The function to be called when the event is triggered.
132
+ * @param {boolean=} once - Whether the event listener should be removed after being called once.
133
+ * @returns {CleanupFunc} This function can be called to remove the event listener.
134
+ */
135
+ onSessionNotPresent(callback: CallbackFunc<null>, once?: boolean): CleanupFunc;
136
+ }
137
+ export {};
@@ -0,0 +1,67 @@
1
+ import { SessionEventDetail } from "./CustomEvents";
2
+ import { Listener } from "./Listener";
3
+ import { SessionState } from "../state/session/SessionState";
4
+ import { Scheduler } from "./Scheduler";
5
+ import { Dispatcher } from "./Dispatcher";
6
+ import { Cookie } from "../Cookie";
7
+ /**
8
+ * A class that dispatches events and scheduled events, based on other events.
9
+ *
10
+ * @category SDK
11
+ * @subcategory Internal
12
+ * @extends Dispatcher
13
+ */
14
+ export declare class Relay extends Dispatcher {
15
+ _listener: Listener;
16
+ _scheduler: Scheduler;
17
+ _sessionState: SessionState;
18
+ _cookie: Cookie;
19
+ constructor();
20
+ /**
21
+ * Removes the scheduled "hanko-session-expired" event and re-schedules a new event with updated expirationSeconds, to
22
+ * ensure the "hanko-session-expired" event won't be triggered too early.
23
+ *
24
+ * @private
25
+ * @param {SessionEventDetail} detail - The event detail.
26
+ */
27
+ private scheduleSessionExpiredEvent;
28
+ /**
29
+ * Cancels scheduled "hanko-session-expired" events, to prevent it from being triggered again (e.g. when there are
30
+ * multiple SDK instances).
31
+ *
32
+ * @private
33
+ */
34
+ private cancelSessionExpiredEvent;
35
+ /**
36
+ * Returns the session detail currently stored in the local storage.
37
+ *
38
+ * @returns {SessionEventDetail}
39
+ */
40
+ getSessionDetail(): SessionEventDetail;
41
+ /**
42
+ * Handles the "storage" event in case the local storage entry, that contains the session detail has been changed by
43
+ * another window. Depending on the new value of `expirationSeconds`, it either dispatches a "hanko-session-created"
44
+ * or a "hanko-session-expired" event.
45
+ *
46
+ * @private
47
+ * @param {StorageEvent} event - The storage event object.
48
+ */
49
+ private handleStorageEvent;
50
+ /**
51
+ * Calls `func` when the document is ready.
52
+ *
53
+ * @param {function} func - The function to be called.
54
+ * @private
55
+ */
56
+ private static onDocumentReady;
57
+ /**
58
+ * Listens for events sent in the current browser.
59
+ *
60
+ * @private
61
+ */
62
+ private listenEventDependencies;
63
+ /**
64
+ * Restores the previous session details and dispatches initial events.
65
+ */
66
+ dispatchInitialEvents(): void;
67
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Represents a task to be scheduled in the scheduler.
3
+ *
4
+ * @ignore
5
+ * @param {number} timeoutID - the ID returned by `setTimeout()`
6
+ * @param {string} type - The type, helps to organize the tasks.
7
+ * @param {Function} func - The function to be called within the task.
8
+ */
9
+ export type Task = {
10
+ timeoutID: number;
11
+ type: string;
12
+ func: () => any;
13
+ };
14
+ /**
15
+ * A class that manages scheduled tasks.
16
+ *
17
+ * @category SDK
18
+ * @subcategory Internal
19
+ */
20
+ export declare class Scheduler {
21
+ _tasks: Task[];
22
+ /**
23
+ * Removes a task from the scheduler.
24
+ *
25
+ * @private
26
+ * @param {Task} task - The task to be removed.
27
+ */
28
+ private removeTask;
29
+ /**
30
+ * Removes all tasks with a given type from the scheduler.
31
+ *
32
+ * @param {string} type - The type of tasks to be removed.
33
+ */
34
+ removeTasksWithType(type: string): void;
35
+ /**
36
+ * Schedules a task to be executed after a given timeout.
37
+ *
38
+ * @param {string} type - The type of the task.
39
+ * @param {Function} func - The function to be executed when the task is triggered.
40
+ * @param {number} timeoutSeconds - The timeout after which the task should be executed, in seconds.
41
+ */
42
+ scheduleTask(type: string, func: () => any, timeoutSeconds: number): void;
43
+ }
@@ -1,4 +1,5 @@
1
- import { LocalStorageUsers } from "./UserState";
1
+ import { LocalStorageUsers } from "./users/UserState";
2
+ import { LocalStorageSession } from "./session/SessionState";
2
3
  /**
3
4
  * @interface
4
5
  * @category SDK
@@ -7,6 +8,7 @@ import { LocalStorageUsers } from "./UserState";
7
8
  */
8
9
  interface LocalStorage {
9
10
  users?: LocalStorageUsers;
11
+ session?: LocalStorageSession;
10
12
  }
11
13
  /**
12
14
  * A class to read and write local storage contents.
@@ -17,9 +19,9 @@ interface LocalStorage {
17
19
  * @subcategory Internal
18
20
  */
19
21
  declare abstract class State {
20
- private readonly key;
22
+ key: string;
21
23
  ls: LocalStorage;
22
- constructor(key?: string);
24
+ constructor(key: string);
23
25
  /**
24
26
  * Reads and decodes the locally stored data.
25
27
  *
@@ -0,0 +1,66 @@
1
+ import { State } from "../State";
2
+ /**
3
+ * @interface
4
+ * @category SDK
5
+ * @subcategory Internal
6
+ * @property {Object.<string, LocalStorageSession>} - A dictionary for mapping users to their states.
7
+ */
8
+ export interface LocalStorageSession {
9
+ expiry: number;
10
+ userID: string;
11
+ }
12
+ /**
13
+ * A class to read and write local storage contents regarding sessions.
14
+ *
15
+ * @extends State
16
+ * @param {string} key - The local storage key.
17
+ * @category SDK
18
+ * @subcategory Internal
19
+ */
20
+ declare class SessionState extends State {
21
+ constructor();
22
+ /**
23
+ * Reads the current state.
24
+ *
25
+ * @public
26
+ * @return {SessionState}
27
+ */
28
+ read(): SessionState;
29
+ /**
30
+ * Gets the session state.
31
+ *
32
+ * @return {LocalStorageSession}
33
+ */
34
+ getState(): LocalStorageSession;
35
+ /**
36
+ * Gets the number of seconds until the active session is valid.
37
+ *
38
+ * @return {number}
39
+ */
40
+ getExpirationSeconds(): number;
41
+ /**
42
+ * Sets the number of seconds until the active session is valid.
43
+ *
44
+ * @param {number} seconds - The number of seconds
45
+ * @return {SessionState}
46
+ */
47
+ setExpirationSeconds(seconds: number): SessionState;
48
+ /**
49
+ * Gets the user id.
50
+ */
51
+ getUserID(): string;
52
+ /**
53
+ * Sets the user id.
54
+ *
55
+ * @param {string} userID - The user id
56
+ * @return {SessionState}
57
+ */
58
+ setUserID(userID: string): SessionState;
59
+ /**
60
+ * Removes the session details.
61
+ *
62
+ * @return {SessionState}
63
+ */
64
+ reset(): SessionState;
65
+ }
66
+ export { SessionState };
@@ -100,7 +100,7 @@ declare class PasscodeState extends UserState {
100
100
  * Sets the number of seconds until a new passcode can be sent.
101
101
  *
102
102
  * @param {string} userID - The UUID of the user.
103
- * @param {string} seconds - Number of seconds the passcode is valid for.
103
+ * @param {number} seconds - Number of seconds the passcode is valid for.
104
104
  * @return {PasscodeState}
105
105
  */
106
106
  setResendAfter(userID: string, seconds: number): PasscodeState;
@@ -4,7 +4,7 @@
4
4
  * @subcategory Internal
5
5
  * @property {Object.<string, LocalStorageUser>} - A dictionary for mapping users to their states.
6
6
  */
7
- import { State } from "./State";
7
+ import { State } from "../State";
8
8
  import { LocalStorageWebauthn } from "./WebauthnState";
9
9
  import { LocalStoragePasscode } from "./PasscodeState";
10
10
  import { LocalStoragePassword } from "./PasswordState";
@@ -40,6 +40,7 @@ export interface LocalStorageUsers {
40
40
  * @subcategory Internal
41
41
  */
42
42
  declare abstract class UserState extends State {
43
+ constructor();
43
44
  /**
44
45
  * Gets the state of the specified user.
45
46
  *
@@ -1,5 +1,5 @@
1
1
  import { UserState } from "./UserState";
2
- import { Credential } from "../Dto";
2
+ import { Credential } from "../../Dto";
3
3
  /**
4
4
  * @interface
5
5
  * @category SDK