@teamhanko/hanko-frontend-sdk 0.2.1-alpha → 0.4.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/README.md +2 -0
- package/dist/Hanko.d.ts +2 -0
- package/dist/index.d.ts +2 -1
- package/dist/lib/Dto.d.ts +11 -0
- package/dist/lib/client/TokenClient.d.ts +22 -0
- package/dist/lib/client/UserClient.d.ts +7 -0
- package/dist/sdk.cjs +1 -1
- package/dist/sdk.cjs.map +1 -1
- package/dist/sdk.modern.js +1 -1
- package/dist/sdk.modern.js.map +1 -1
- package/dist/sdk.module.js +1 -1
- package/dist/sdk.module.js.map +1 -1
- package/dist/sdk.umd.js +1 -1
- package/dist/sdk.umd.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -54,6 +54,8 @@ To see the latest documentation, please click [here](https://docs.hanko.io/jsdoc
|
|
|
54
54
|
- `WebauthnClient` - A class to handle WebAuthn-related functionalities.
|
|
55
55
|
- `PasswordClient` - A class to manage passwords and password logins.
|
|
56
56
|
- `PasscodeClient` - A class to handle passcode logins.
|
|
57
|
+
- `ThirdPartyClient` - A class to handle social logins.
|
|
58
|
+
- `TokenClient` - A class that handles the exchange of one time tokens for session JWTs.
|
|
57
59
|
|
|
58
60
|
### Utilities
|
|
59
61
|
|
package/dist/Hanko.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { UserClient } from "./lib/client/UserClient";
|
|
|
5
5
|
import { WebauthnClient } from "./lib/client/WebauthnClient";
|
|
6
6
|
import { EmailClient } from "./lib/client/EmailClient";
|
|
7
7
|
import { ThirdPartyClient } from "./lib/client/ThirdPartyClient";
|
|
8
|
+
import { TokenClient } from "./lib/client/TokenClient";
|
|
8
9
|
/**
|
|
9
10
|
* A class that bundles all available SDK functions.
|
|
10
11
|
*
|
|
@@ -20,6 +21,7 @@ declare class Hanko {
|
|
|
20
21
|
passcode: PasscodeClient;
|
|
21
22
|
email: EmailClient;
|
|
22
23
|
thirdParty: ThirdPartyClient;
|
|
24
|
+
token: TokenClient;
|
|
23
25
|
constructor(api: string, timeout?: number);
|
|
24
26
|
}
|
|
25
27
|
export { Hanko };
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,8 @@ import { UserClient } from "./lib/client/UserClient";
|
|
|
7
7
|
import { WebauthnClient } from "./lib/client/WebauthnClient";
|
|
8
8
|
import { EmailClient } from "./lib/client/EmailClient";
|
|
9
9
|
import { ThirdPartyClient } from "./lib/client/ThirdPartyClient";
|
|
10
|
-
|
|
10
|
+
import { TokenClient } from "./lib/client/TokenClient";
|
|
11
|
+
export { ConfigClient, UserClient, WebauthnClient, PasswordClient, PasscodeClient, EmailClient, ThirdPartyClient, TokenClient, };
|
|
11
12
|
import { WebauthnSupport } from "./lib/WebauthnSupport";
|
|
12
13
|
export { WebauthnSupport };
|
|
13
14
|
import { PasswordConfig, Config, WebauthnFinalized, Credential, UserInfo, User, Email, Emails, WebauthnCredential, WebauthnCredentials, Passcode, Identity } from "./lib/Dto";
|
package/dist/lib/Dto.d.ts
CHANGED
|
@@ -21,6 +21,15 @@ interface EmailConfig {
|
|
|
21
21
|
require_verification: boolean;
|
|
22
22
|
max_num_of_addresses: number;
|
|
23
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* @interface
|
|
26
|
+
* @category SDK
|
|
27
|
+
* @subcategory DTO
|
|
28
|
+
* @property {boolean} allow_deletion - Indicates the current user is allowed to delete the account.
|
|
29
|
+
*/
|
|
30
|
+
interface AccountConfig {
|
|
31
|
+
allow_deletion: boolean;
|
|
32
|
+
}
|
|
24
33
|
/**
|
|
25
34
|
* @interface
|
|
26
35
|
* @category SDK
|
|
@@ -28,11 +37,13 @@ interface EmailConfig {
|
|
|
28
37
|
* @property {PasswordConfig} password - The password configuration.
|
|
29
38
|
* @property {EmailConfig} emails - The email configuration.
|
|
30
39
|
* @property {string[]} providers - The enabled third party providers.
|
|
40
|
+
* @property {AccountConfig} account - Controls the behaviour regarding user accounts.
|
|
31
41
|
*/
|
|
32
42
|
interface Config {
|
|
33
43
|
password: PasswordConfig;
|
|
34
44
|
emails: EmailConfig;
|
|
35
45
|
providers: string[];
|
|
46
|
+
account: AccountConfig;
|
|
36
47
|
}
|
|
37
48
|
/**
|
|
38
49
|
* @interface
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Client } from "./Client";
|
|
2
|
+
/**
|
|
3
|
+
* Client responsible for exchanging one time tokens for session JWTs.
|
|
4
|
+
*
|
|
5
|
+
* @constructor
|
|
6
|
+
* @category SDK
|
|
7
|
+
* @subcategory Clients
|
|
8
|
+
* @extends {Client}
|
|
9
|
+
*/
|
|
10
|
+
export declare class TokenClient extends Client {
|
|
11
|
+
constructor(api: string, timeout?: number);
|
|
12
|
+
/**
|
|
13
|
+
* Validate a one time token to retrieve a session JWT. Does nothing
|
|
14
|
+
* if the current window location does not contain a 'hanko_token' in the
|
|
15
|
+
* search query.
|
|
16
|
+
*
|
|
17
|
+
* @return {Promise<void>}
|
|
18
|
+
* @throws {TechnicalError}
|
|
19
|
+
* https://docs.hanko.io/api/api/public#tag/Token/operation/token
|
|
20
|
+
*/
|
|
21
|
+
validate(): Promise<void>;
|
|
22
|
+
}
|
|
@@ -44,6 +44,13 @@ declare class UserClient extends Client {
|
|
|
44
44
|
* @see https://docs.hanko.io/api/public#tag/User-Management/operation/listUser
|
|
45
45
|
*/
|
|
46
46
|
getCurrent(): Promise<User>;
|
|
47
|
+
/**
|
|
48
|
+
* Deletes the current user and expires the existing session cookie.
|
|
49
|
+
*
|
|
50
|
+
* @return {Promise<void>}
|
|
51
|
+
* @throws {TechnicalError}
|
|
52
|
+
*/
|
|
53
|
+
delete(): Promise<void>;
|
|
47
54
|
/**
|
|
48
55
|
* Logs out the current user and expires the existing session cookie. A valid session cookie is required to call the logout endpoint.
|
|
49
56
|
*
|
package/dist/sdk.cjs
CHANGED
|
@@ -1,2 +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(t,e,i){return n=function(){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}}()?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},n.apply(null,arguments)}function i(t){var o="function"==typeof Map?new Map:void 0;return i=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!==o){if(o.has(t))return o.get(t);o.set(t,i)}function i(){return n(t,arguments,e(this).constructor)}return i.prototype=Object.create(t.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),r(i,t)},i(t)}function o(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var s=/*#__PURE__*/function(e){function r(t,n,i){var s;return(s=e.call(this,t)||this).code=void 0,s.cause=void 0,s.code=n,s.cause=i,Object.setPrototypeOf(o(s),r.prototype),s}return t(r,e),r}(/*#__PURE__*/i(Error)),a=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Technical error","somethingWentWrong",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),u=/*#__PURE__*/function(e){function r(t,n){var i;return i=e.call(this,"Conflict error","conflict",n)||this,Object.setPrototypeOf(o(i),r.prototype),i}return t(r,e),r}(s),c=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Request timed out error","requestTimeout",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),h=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Request cancelled error","requestCancelled",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),l=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid password error","invalidPassword",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),d=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid Passcode error","invalidPasscode",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),f=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid WebAuthn credential error","invalidWebauthnCredential",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),p=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Passcode expired error","passcodeExpired",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),w=/*#__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(o(n),r.prototype),n}return t(r,e),r}(s),v=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Not found error","notFound",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),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(o(i),r.prototype),i}return t(r,e),r}(s),y=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Unauthorized error","unauthorized",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),g=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"User verification error","userVerification",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),P=/*#__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(o(n),r.prototype),n}return t(r,e),r}(s),b=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"The email address already exists","emailAddressAlreadyExistsError",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),C=/*#__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(o(i),r.prototype),i}return t(r,e),r}(s);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 S=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:"/"}),O=/*#__PURE__*/function(){function t(t){this._xhr=void 0,this._xhr=t}return t.prototype.get=function(t){return this._xhr.getResponseHeader(t)},t}(),j=/*#__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 O(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}(),x=/*#__PURE__*/function(){function t(t,e){void 0===e&&(e=13e3),this.timeout=void 0,this.api=void 0,this.authCookieName="hanko",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,i=this.api+t,o=this.timeout,s=this._getAuthCookie();return new Promise(function(t,u){r.open(e.method,i,!0),r.setRequestHeader("Accept","application/json"),r.setRequestHeader("Content-Type","application/json"),s&&r.setRequestHeader("Authorization","Bearer "+s),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");e&&n._setAuthCookie(e)}t(new j(r))},r.onerror=function(){u(new a)},r.ontimeout=function(){u(new c)},r.send(e.body?e.body.toString():null)})},e._getAuthCookie=function(){return S.get(this.authCookieName)},e._setAuthCookie=function(t){var e=!!this.api.match("^https://");S.set(this.authCookieName,t,{secure:e})},e.removeAuthCookie=function(){S.remove(this.authCookieName)},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}(),R=function(t,e){void 0===e&&(e=13e3),this.client=void 0,this.client=new x(t,e)},E=/*#__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 a;return t.json()})}catch(t){return Promise.reject(t)}},r}(R),k=/*#__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}(),_=/*#__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}(k),T=/*#__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 k.timeToRemainingSeconds(this.getState(t).ttl)},n.setTTL=function(t,e){return this.getState(t).ttl=k.remainingSecondsToTime(e),this},n.getResendAfter=function(t){return k.timeToRemainingSeconds(this.getState(t).resendAfter)},n.setResendAfter=function(t,e){return this.getState(t).resendAfter=k.remainingSecondsToTime(e),this},r}(_),I=/*#__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 T,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),u=n.state.getResendAfter(t);if(u>0)throw new m(u);if(!r&&i>0&&e===s)return Promise.resolve({id:o,ttl:i});var c={user_id:t};return e&&(c.email_id=e),Promise.resolve(n.client.post("/passcode/login/initialize",c)).then(function(r){if(429===r.status)throw u=r.parseRetryAfterHeader(),n.state.setResendAfter(t,u).write(),new m(u);if(401===r.status)throw new y;if(!r.ok)throw new a;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 p;return Promise.resolve(r.client.post("/passcode/login/finalize",{id:n,code:e})).then(function(e){if(401===e.status)throw new d;if(410===e.status)throw r.state.reset(t).write(),new w;if(!e.ok)throw new a;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}(R),U=/*#__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 k.timeToRemainingSeconds(this.getState(t).retryAfter)},n.setRetryAfter=function(t,e){return this.getState(t).retryAfter=k.remainingSecondsToTime(e),this},r}(_),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 U,n.passcodeState=new T,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 l;if(429===e.status){var n=e.parseRetryAfterHeader();throw r.passwordState.read().setRetryAfter(t,n).write(),new m(n)}if(!e.ok)throw new a;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 y;if(!t.ok)throw new a})}catch(t){return Promise.reject(t)}},n.getRetryAfter=function(t){return this.passwordState.read().getRetryAfter(t)},r}(R),D=/*#__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 v;if(!t.ok)throw new a;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 a;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 y;if(!e.ok)throw new a;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 y;if(!t.ok)throw new a;return t.json()})})}catch(t){return Promise.reject(t)}},n.logout=function(){try{var t=this;return Promise.resolve(t.client.post("/logout")).then(function(e){if(t.client.removeAuthCookie(),401===e.status);else if(!e.ok)throw new a})}catch(t){return Promise.reject(t)}},r}(R);function q(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 M(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,"")}var K="copy",W="convert";function z(t,e,r){if(e===K)return r;if(e===W)return t(r);if(e instanceof Array)return r.map(r=>z(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]?z(t,o.schema,r[i]):null;else if(o.required)throw new Error(`Missing key: ${i}`)}return n}}function H(t,e){return{required:!0,schema:t,derive:e}}function L(t){return{required:!0,schema:t}}function J(t){return{required:!1,schema:t}}var B={type:L(K),id:L(W),transports:J(K)},F={appid:J(K),appidExclude:J(K),credProps:J(K)},V={appid:J(K),appidExclude:J(K),credProps:J(K)},X={publicKey:L({rp:L(K),user:L({id:L(W),name:L(K),displayName:L(K)}),challenge:L(W),pubKeyCredParams:L(K),timeout:J(K),excludeCredentials:J([B]),authenticatorSelection:J(K),attestation:J(K),extensions:J(F)}),signal:J(K)},G={type:L(K),id:L(K),rawId:L(W),authenticatorAttachment:J(K),response:L({clientDataJSON:L(W),attestationObject:L(W),transports:H(K,t=>{var e;return(null==(e=t.getTransports)?void 0:e.call(t))||[]})}),clientExtensionResults:H(V,t=>t.getClientExtensionResults())},$={mediation:J(K),publicKey:L({challenge:L(W),timeout:J(K),rpId:J(K),allowCredentials:J([B]),userVerification:J(K),extensions:J(F)}),signal:J(K)},Q={type:L(K),id:L(K),rawId:L(W),authenticatorAttachment:J(K),response:L({clientDataJSON:L(W),authenticatorData:L(W),signature:L(W),userHandle:L(W)}),clientExtensionResults:H(V,t=>t.getClientExtensionResults())};async function Y(t){const e=await navigator.credentials.create(function(t){return z(q,X,t)}(t));return function(t){return z(M,G,t)}(e)}async function Z(t){const e=await navigator.credentials.get(function(t){return z(q,$,t)}(t));return function(t){return z(M,Q,t)}(e)}var tt=/*#__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}(),et=/*#__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}(_);function rt(t,e){try{var r=t()}catch(t){return e(t)}return r&&r.then?r.then(void 0,e):r}var nt=/*#__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=Z,n._createCredential=Y,n.webauthnState=new et,n.passcodeState=new T,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){var i;function o(e){return i?e:Promise.resolve(r.client.post("/webauthn/login/finalize",s)).then(function(e){if(400===e.status||401===e.status)throw new f;if(!e.ok)throw new a;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 a;var s,u=n.json();u.signal=r._createAbortSignal(),e&&(u.mediation="conditional");var c=rt(function(){return Promise.resolve(r._getCredential(u)).then(function(t){s=t})},function(t){throw new h(t)});return c&&c.then?c.then(o):o(c)})}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 g;throw new y}if(!e.ok)throw new a;var r=e.json();t.webauthnState.read().addCredential(r.user_id,r.credential_id).write()})}if(e.status>=400&&e.status<=499)throw new y;if(!e.ok)throw new a;var n,i=e.json();i.signal=t._createAbortSignal();var o=rt(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 y;if(!t.ok)throw new a;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 y;if(!t.ok)throw new a})}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 y;if(!t.ok)throw new a})}catch(t){return Promise.reject(t)}},n.shouldRegister=function(t){try{var e=tt.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}(R),it=/*#__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 y;if(!t.ok)throw new a;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 b;if(401===t.status)throw new y;if(409===t.status)throw new P;throw new a})}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 y;if(!t.ok)throw new a})}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 y;if(!t.ok)throw new a})}catch(t){return Promise.reject(t)}},r}(R),ot=/*#__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 C("somethingWentWrong",new Error("provider missing from request"));if(!e)throw new C("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 C(n,new Error(r))}},r}(R);exports.ConfigClient=E,exports.ConflictError=u,exports.EmailAddressAlreadyExistsError=b,exports.EmailClient=it,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 E(t,e),this.user=new D(t,e),this.webauthn=new nt(t,e),this.password=new N(t,e),this.passcode=new I(t,e),this.email=new it(t,e),this.thirdParty=new ot(t,e)},exports.HankoError=s,exports.InvalidPasscodeError=d,exports.InvalidPasswordError=l,exports.InvalidWebauthnCredentialError=f,exports.MaxNumOfEmailAddressesReachedError=P,exports.MaxNumOfPasscodeAttemptsReachedError=w,exports.NotFoundError=v,exports.PasscodeClient=I,exports.PasscodeExpiredError=p,exports.PasswordClient=N,exports.RequestTimeoutError=c,exports.TechnicalError=a,exports.ThirdPartyClient=ot,exports.ThirdPartyError=C,exports.TooManyRequestsError=m,exports.UnauthorizedError=y,exports.UserClient=D,exports.UserVerificationError=g,exports.WebauthnClient=nt,exports.WebauthnRequestCancelledError=h,exports.WebauthnSupport=tt;
|
|
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(t,e,i){return n=function(){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}}()?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},n.apply(null,arguments)}function i(t){var o="function"==typeof Map?new Map:void 0;return i=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!==o){if(o.has(t))return o.get(t);o.set(t,i)}function i(){return n(t,arguments,e(this).constructor)}return i.prototype=Object.create(t.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),r(i,t)},i(t)}function o(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}var s=/*#__PURE__*/function(e){function r(t,n,i){var s;return(s=e.call(this,t)||this).code=void 0,s.cause=void 0,s.code=n,s.cause=i,Object.setPrototypeOf(o(s),r.prototype),s}return t(r,e),r}(/*#__PURE__*/i(Error)),a=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Technical error","somethingWentWrong",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),u=/*#__PURE__*/function(e){function r(t,n){var i;return i=e.call(this,"Conflict error","conflict",n)||this,Object.setPrototypeOf(o(i),r.prototype),i}return t(r,e),r}(s),c=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Request timed out error","requestTimeout",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),h=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Request cancelled error","requestCancelled",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),l=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid password error","invalidPassword",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),d=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid Passcode error","invalidPasscode",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),f=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Invalid WebAuthn credential error","invalidWebauthnCredential",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),p=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Passcode expired error","passcodeExpired",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),w=/*#__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(o(n),r.prototype),n}return t(r,e),r}(s),v=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Not found error","notFound",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),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(o(i),r.prototype),i}return t(r,e),r}(s),y=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"Unauthorized error","unauthorized",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),g=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"User verification error","userVerification",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),P=/*#__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(o(n),r.prototype),n}return t(r,e),r}(s),b=/*#__PURE__*/function(e){function r(t){var n;return n=e.call(this,"The email address already exists","emailAddressAlreadyExistsError",t)||this,Object.setPrototypeOf(o(n),r.prototype),n}return t(r,e),r}(s),C=/*#__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(o(i),r.prototype),i}return t(r,e),r}(s);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 S=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}(),O=/*#__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}(),x=/*#__PURE__*/function(){function t(t,e){void 0===e&&(e=13e3),this.timeout=void 0,this.api=void 0,this.authCookieName="hanko",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,i=this.api+t,o=this.timeout,s=this._getAuthCookie();return new Promise(function(t,u){r.open(e.method,i,!0),r.setRequestHeader("Accept","application/json"),r.setRequestHeader("Content-Type","application/json"),s&&r.setRequestHeader("Authorization","Bearer "+s),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");e&&n._setAuthCookie(e)}t(new O(r))},r.onerror=function(){u(new a)},r.ontimeout=function(){u(new c)},r.send(e.body?e.body.toString():null)})},e._getAuthCookie=function(){return S.get(this.authCookieName)},e._setAuthCookie=function(t){var e=!!this.api.match("^https://");S.set(this.authCookieName,t,{secure:e})},e.removeAuthCookie=function(){S.remove(this.authCookieName)},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}(),k=function(t,e){void 0===e&&(e=13e3),this.client=void 0,this.client=new x(t,e)},R=/*#__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 a;return t.json()})}catch(t){return Promise.reject(t)}},r}(k),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}(),_=/*#__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}(E),T=/*#__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 E.timeToRemainingSeconds(this.getState(t).ttl)},n.setTTL=function(t,e){return this.getState(t).ttl=E.remainingSecondsToTime(e),this},n.getResendAfter=function(t){return E.timeToRemainingSeconds(this.getState(t).resendAfter)},n.setResendAfter=function(t,e){return this.getState(t).resendAfter=E.remainingSecondsToTime(e),this},r}(_),I=/*#__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 T,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),u=n.state.getResendAfter(t);if(u>0)throw new m(u);if(!r&&i>0&&e===s)return Promise.resolve({id:o,ttl:i});var c={user_id:t};return e&&(c.email_id=e),Promise.resolve(n.client.post("/passcode/login/initialize",c)).then(function(r){if(429===r.status)throw u=r.parseRetryAfterHeader(),n.state.setResendAfter(t,u).write(),new m(u);if(401===r.status)throw new y;if(!r.ok)throw new a;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 p;return Promise.resolve(r.client.post("/passcode/login/finalize",{id:n,code:e})).then(function(e){if(401===e.status)throw new d;if(410===e.status)throw r.state.reset(t).write(),new w;if(!e.ok)throw new a;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}(k),U=/*#__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 E.timeToRemainingSeconds(this.getState(t).retryAfter)},n.setRetryAfter=function(t,e){return this.getState(t).retryAfter=E.remainingSecondsToTime(e),this},r}(_),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 U,n.passcodeState=new T,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 l;if(429===e.status){var n=e.parseRetryAfterHeader();throw r.passwordState.read().setRetryAfter(t,n).write(),new m(n)}if(!e.ok)throw new a;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 y;if(!t.ok)throw new a})}catch(t){return Promise.reject(t)}},n.getRetryAfter=function(t){return this.passwordState.read().getRetryAfter(t)},r}(k),D=/*#__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 v;if(!t.ok)throw new a;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 a;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 y;if(!e.ok)throw new a;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 y;if(!t.ok)throw new a;return t.json()})})}catch(t){return Promise.reject(t)}},n.delete=function(){try{var t=this;return Promise.resolve(t.client.delete("/user")).then(function(e){if(!e.ok){if(401===e.status)throw new y;throw new a}t.client.removeAuthCookie()})}catch(t){return Promise.reject(t)}},n.logout=function(){try{var t=this;return Promise.resolve(t.client.post("/logout")).then(function(e){if(t.client.removeAuthCookie(),401===e.status);else if(!e.ok)throw new a})}catch(t){return Promise.reject(t)}},r}(k);function q(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 M(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,"")}var K="copy",W="convert";function z(t,e,r){if(e===K)return r;if(e===W)return t(r);if(e instanceof Array)return r.map(r=>z(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]?z(t,o.schema,r[i]):null;else if(o.required)throw new Error(`Missing key: ${i}`)}return n}}function H(t,e){return{required:!0,schema:t,derive:e}}function L(t){return{required:!0,schema:t}}function J(t){return{required:!1,schema:t}}var B={type:L(K),id:L(W),transports:J(K)},F={appid:J(K),appidExclude:J(K),credProps:J(K)},V={appid:J(K),appidExclude:J(K),credProps:J(K)},X={publicKey:L({rp:L(K),user:L({id:L(W),name:L(K),displayName:L(K)}),challenge:L(W),pubKeyCredParams:L(K),timeout:J(K),excludeCredentials:J([B]),authenticatorSelection:J(K),attestation:J(K),extensions:J(F)}),signal:J(K)},G={type:L(K),id:L(K),rawId:L(W),authenticatorAttachment:J(K),response:L({clientDataJSON:L(W),attestationObject:L(W),transports:H(K,t=>{var e;return(null==(e=t.getTransports)?void 0:e.call(t))||[]})}),clientExtensionResults:H(V,t=>t.getClientExtensionResults())},$={mediation:J(K),publicKey:L({challenge:L(W),timeout:J(K),rpId:J(K),allowCredentials:J([B]),userVerification:J(K),extensions:J(F)}),signal:J(K)},Q={type:L(K),id:L(K),rawId:L(W),authenticatorAttachment:J(K),response:L({clientDataJSON:L(W),authenticatorData:L(W),signature:L(W),userHandle:L(W)}),clientExtensionResults:H(V,t=>t.getClientExtensionResults())};async function Y(t){const e=await navigator.credentials.create(function(t){return z(q,X,t)}(t));return function(t){return z(M,G,t)}(e)}async function Z(t){const e=await navigator.credentials.get(function(t){return z(q,$,t)}(t));return function(t){return z(M,Q,t)}(e)}var tt=/*#__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}(),et=/*#__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}(_);function rt(t,e){try{var r=t()}catch(t){return e(t)}return r&&r.then?r.then(void 0,e):r}var nt=/*#__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=Z,n._createCredential=Y,n.webauthnState=new et,n.passcodeState=new T,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){var i;function o(e){return i?e:Promise.resolve(r.client.post("/webauthn/login/finalize",s)).then(function(e){if(400===e.status||401===e.status)throw new f;if(!e.ok)throw new a;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 a;var s,u=n.json();u.signal=r._createAbortSignal(),e&&(u.mediation="conditional");var c=rt(function(){return Promise.resolve(r._getCredential(u)).then(function(t){s=t})},function(t){throw new h(t)});return c&&c.then?c.then(o):o(c)})}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 g;throw new y}if(!e.ok)throw new a;var r=e.json();t.webauthnState.read().addCredential(r.user_id,r.credential_id).write()})}if(e.status>=400&&e.status<=499)throw new y;if(!e.ok)throw new a;var n,i=e.json();i.signal=t._createAbortSignal();var o=rt(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 y;if(!t.ok)throw new a;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 y;if(!t.ok)throw new a})}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 y;if(!t.ok)throw new a})}catch(t){return Promise.reject(t)}},n.shouldRegister=function(t){try{var e=tt.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}(k),it=/*#__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 y;if(!t.ok)throw new a;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 b;if(401===t.status)throw new y;if(409===t.status)throw new P;throw new a})}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 y;if(!t.ok)throw new a})}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 y;if(!t.ok)throw new a})}catch(t){return Promise.reject(t)}},r}(k),ot=/*#__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 C("somethingWentWrong",new Error("provider missing from request"));if(!e)throw new C("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 C(n,new Error(r))}},r}(k),st=/*#__PURE__*/function(e){function r(t,r){return void 0===r&&(r=13e3),e.call(this,t,r)||this}return t(r,e),r.prototype.validate=function(){try{var t=new URLSearchParams(window.location.search).get("hanko_token");return t?(window.history.replaceState(null,null,window.location.pathname),Promise.resolve(this.client.post("/token",{value:t})).then(function(t){if(!t.ok)throw new a})):Promise.resolve()}catch(t){return Promise.reject(t)}},r}(k);exports.ConfigClient=R,exports.ConflictError=u,exports.EmailAddressAlreadyExistsError=b,exports.EmailClient=it,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.token=void 0,this.api=t,this.config=new R(t,e),this.user=new D(t,e),this.webauthn=new nt(t,e),this.password=new N(t,e),this.passcode=new I(t,e),this.email=new it(t,e),this.thirdParty=new ot(t,e),this.token=new st(t,e)},exports.HankoError=s,exports.InvalidPasscodeError=d,exports.InvalidPasswordError=l,exports.InvalidWebauthnCredentialError=f,exports.MaxNumOfEmailAddressesReachedError=P,exports.MaxNumOfPasscodeAttemptsReachedError=w,exports.NotFoundError=v,exports.PasscodeClient=I,exports.PasscodeExpiredError=p,exports.PasswordClient=N,exports.RequestTimeoutError=c,exports.TechnicalError=a,exports.ThirdPartyClient=ot,exports.ThirdPartyError=C,exports.TokenClient=st,exports.TooManyRequestsError=m,exports.UnauthorizedError=y,exports.UserClient=D,exports.UserVerificationError=g,exports.WebauthnClient=nt,exports.WebauthnRequestCancelledError=h,exports.WebauthnSupport=tt;
|
|
2
2
|
//# sourceMappingURL=sdk.cjs.map
|