wjec-one 4.0.0-alpha.21 → 4.0.0-alpha.23

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/auth/index.d.ts CHANGED
@@ -15,9 +15,11 @@ declare enum AuthAction {
15
15
  COMPLETE_PASSWORD_RESET = "completePasswordReset",
16
16
  FORCE_PASSWORD_CHANGE = "forcePasswordChange",
17
17
  GET_AUTHORIZATION_TOKEN = "getAuthorizationToken",
18
+ GET_USER_PREFERENCES = "getUserPreferences",
18
19
  GET_USER_SESSION = "getUserSession",
19
20
  INIT = "init",
20
21
  REQUEST_PASSWORD_RESET = "requestPasswordReset",
22
+ REQUEST_TOTP_TOKEN = "requestSoftwareToken",
21
23
  SET_MFA_TYPE = "setMfaType",
22
24
  SIGN_IN = "signIn",
23
25
  SIGN_OUT = "signOut",
@@ -34,7 +36,8 @@ declare enum ChallengeType {
34
36
  NEW_PASSWORD_REQUIRED = "NEW_PASSWORD_REQUIRED",
35
37
  SMS_MFA = "SMS",
36
38
  TOTP_MFA = "TOTP",
37
- VERIFY_TELEPHONE_NUMBER = "VERIFY_TELEPHONE_NUMBER"
39
+ VERIFY_TELEPHONE_NUMBER = "VERIFY_TELEPHONE_NUMBER",
40
+ VERIFY_TOTP_TOKEN = "VERIFY_TOTP_TOKEN"
38
41
  }
39
42
  declare enum MFAType {
40
43
  EMAIL = "EMAIL",
@@ -90,7 +93,12 @@ declare type VerifyTelephoneNumberChallenge = {
90
93
  challengeType: ChallengeType.VERIFY_TELEPHONE_NUMBER;
91
94
  completeChallenge: (code: string) => Promise<boolean>;
92
95
  };
93
- declare type Challenge = EmailMfaChallenge | NewPasswordChallenge | SmsMfaChallenge | TotpMfaChallenge | VerifyTelephoneNumberChallenge;
96
+ declare type VerifyTotpTokenChallenge = {
97
+ challengeType: ChallengeType.VERIFY_TOTP_TOKEN;
98
+ completeChallenge: (code: string) => Promise<boolean>;
99
+ secretKey: string;
100
+ };
101
+ declare type Challenge = EmailMfaChallenge | NewPasswordChallenge | SmsMfaChallenge | TotpMfaChallenge | VerifyTelephoneNumberChallenge | VerifyTotpTokenChallenge;
94
102
  declare enum Role {
95
103
  EXAMS_OFFICER = "examsOfficer",
96
104
  HEAD_TEACHER = "headTeacher",
@@ -102,7 +110,7 @@ declare enum Subject {
102
110
  SCIENCE = "science"
103
111
  }
104
112
  declare type UserPreferences = {
105
- emailMfaAccepted?: boolean;
113
+ emailMfaAccepted?: Date;
106
114
  favouriteLinks?: string[];
107
115
  role?: Role;
108
116
  subjects?: Subject[];
@@ -129,7 +137,11 @@ declare const getAuthorizationToken: () => Promise<string>;
129
137
  declare const getUserSession: () => Promise<CognitoUserSession>;
130
138
  declare const init: (amplifyConfig: Record<string, any>, oAuthConfig: Record<string, any>, usersEndpoint: string) => Promise<CognitoUserSession>;
131
139
  declare const requestPasswordReset: (emailAddress: string) => Promise<boolean>;
132
- declare const setMfaType: (mfaType: MFAType.EMAIL | MFAType.SMS | MFAType.TOTP) => Promise<void>;
140
+ declare const setMfaType: (mfaType: MFAType.EMAIL | MFAType.SMS | MFAType.TOTP) => Promise<boolean>;
141
+ declare const setupTotp: () => Promise<{
142
+ challenge?: Challenge;
143
+ secretKey: string;
144
+ }>;
133
145
  declare const signIn: (emailAddress: string, password: string) => Promise<{
134
146
  challenge?: Challenge;
135
147
  user: CognitoUser;
@@ -139,7 +151,8 @@ declare const updateUserAttributes: (attributes: Partial<EditableCognitoUserAttr
139
151
  challenge: Challenge;
140
152
  success: boolean;
141
153
  }>;
142
- declare const updateUserPreferences: (preferences: Partial<UserPreferences>) => Promise<void>;
154
+ declare const updateUserPreferences: (preferences: Partial<UserPreferences>) => Promise<boolean>;
155
+ declare const verifyTotp: (code: string) => Promise<boolean>;
143
156
  declare const state: Readonly<{
144
157
  readonly centrePermissions: Record<string, string>;
145
158
  readonly emailAddress: string;
@@ -161,4 +174,4 @@ declare const state: Readonly<{
161
174
  readonly totpVerified: boolean;
162
175
  }>;
163
176
 
164
- export { AuthAction, Cache, Challenge, ChallengeType, CognitoUserAttributes, EditableCognitoUserAttributes, EmailMfaChallenge, MFAType, NewPasswordChallenge, ReadonlyCognitoUserAttributes, Role, SmsMfaChallenge, Subject, TotpMfaChallenge, UserPreferences, VerifyTelephoneNumberChallenge, completePasswordReset, getAuthorizationToken, getUserSession, init, requestPasswordReset, setMfaType, signIn, signOut, state, updateUserAttributes, updateUserPreferences, useAuthorization };
177
+ export { AuthAction, Cache, Challenge, ChallengeType, CognitoUserAttributes, EditableCognitoUserAttributes, EmailMfaChallenge, MFAType, NewPasswordChallenge, ReadonlyCognitoUserAttributes, Role, SmsMfaChallenge, Subject, TotpMfaChallenge, UserPreferences, VerifyTelephoneNumberChallenge, VerifyTotpTokenChallenge, completePasswordReset, getAuthorizationToken, getUserSession, init, requestPasswordReset, setMfaType, setupTotp, signIn, signOut, state, updateUserAttributes, updateUserPreferences, useAuthorization, verifyTotp };
package/auth/index.js CHANGED
@@ -1 +1 @@
1
- import{Hub as e,Auth as t,Amplify as r}from"aws-amplify";import{assertNever as n,isCallableFunction as o,developerWarning as a}from"../utils";import{useState as i,useRef as u,useEffect as c}from"react";function s(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function f(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?s(Object(r),!0).forEach((function(t){m(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):s(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function l(){l=function(){return e};var e={},t=Object.prototype,r=t.hasOwnProperty,n=Object.defineProperty||function(e,t,r){e[t]=r.value},o="function"==typeof Symbol?Symbol:{},a=o.iterator||"@@iterator",i=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(e){c=function(e,t,r){return e[t]=r}}function s(e,t,r,o){var a=t&&t.prototype instanceof d?t:d,i=Object.create(a.prototype),u=new O(o||[]);return n(i,"_invoke",{value:_(e,r,u)}),i}function f(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var p={};function d(){}function h(){}function m(){}var g={};c(g,a,(function(){return this}));var v=Object.getPrototypeOf,y=v&&v(v(A([])));y&&y!==t&&r.call(y,a)&&(g=y);var E=m.prototype=d.prototype=Object.create(g);function S(e){["next","throw","return"].forEach((function(t){c(e,t,(function(e){return this._invoke(t,e)}))}))}function b(e,t){function o(n,a,i,u){var c=f(e[n],e,a);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&r.call(l,"__await")?t.resolve(l.__await).then((function(e){o("next",e,i,u)}),(function(e){o("throw",e,i,u)})):t.resolve(l).then((function(e){s.value=e,i(s)}),(function(e){return o("throw",e,i,u)}))}u(c.arg)}var a;n(this,"_invoke",{value:function(e,r){function n(){return new t((function(t,n){o(e,r,t,n)}))}return a=a?a.then(n,n):n()}})}function _(e,t,r){var n="suspendedStart";return function(o,a){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw a;return P()}for(r.method=o,r.arg=a;;){var i=r.delegate;if(i){var u=w(i,r);if(u){if(u===p)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=f(e,t,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===p)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function w(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,w(e,t),"throw"===t.method))return p;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var n=f(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,p;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,p):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,p)}function T(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function N(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function O(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(T,this),this.reset(!0)}function A(e){if(e){var t=e[a];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,o=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return o.next=o}}return{next:P}}function P(){return{value:void 0,done:!0}}return h.prototype=m,n(E,"constructor",{value:m,configurable:!0}),n(m,"constructor",{value:h,configurable:!0}),h.displayName=c(m,u,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,m):(e.__proto__=m,c(e,u,"GeneratorFunction")),e.prototype=Object.create(E),e},e.awrap=function(e){return{__await:e}},S(b.prototype),c(b.prototype,i,(function(){return this})),e.AsyncIterator=b,e.async=function(t,r,n,o,a){void 0===a&&(a=Promise);var i=new b(s(t,r,n,o),a);return e.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},S(E),c(E,u,"Generator"),c(E,a,(function(){return this})),c(E,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},e.values=A,O.prototype={constructor:O,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(N),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return i.type="throw",i.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return n("end");if(a.tryLoc<=this.prev){var u=r.call(a,"catchLoc"),c=r.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,p):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),p},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),N(r),p}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;N(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:A(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},e}function p(e){return p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p(e)}function d(e,t,r,n,o,a,i){try{var u=e[a](i),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,o)}function h(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function i(e){d(a,n,o,i,u,"next",e)}function u(e){d(a,n,o,i,u,"throw",e)}i(void 0)}))}}function m(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function g(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==r)return;var n,o,a=[],i=!0,u=!1;try{for(r=r.call(e);!(i=(n=r.next()).done)&&(a.push(n.value),!t||a.length!==t);i=!0);}catch(e){u=!0,o=e}finally{try{i||null==r.return||r.return()}finally{if(u)throw o}}return a}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return v(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return v(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function v(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}var y,E,S,b,_,w=function(){var e=h(l().mark((function e(){var t,r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,J();case 2:return t=e.sent,e.next=5,fetch("".concat(A("usersEndpoint")).concat(A("id"),"/preferences"),{headers:{Authorization:t}}).then((function(e){return e.json()})).catch((function(){}));case 5:return r=e.sent,e.abrupt("return",r||{});case 7:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),T=function(){var e=h(l().mark((function e(t){var r,n;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,J();case 2:return r=e.sent,e.next=5,fetch("".concat(A("usersEndpoint")).concat(A("id"),"/mfa"),{body:JSON.stringify({mfaType:t}),headers:{Authorization:r},method:"POST"}).then((function(){return!0})).catch((function(){return!1}));case 5:return n=e.sent,e.abrupt("return",n);case 7:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),N=function(){var e=h(l().mark((function e(t){var r,n;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,J();case 2:return r=e.sent,e.next=5,fetch("".concat(A("usersEndpoint")).concat(A("id"),"/preferences"),{body:JSON.stringify(t),headers:{Authorization:r},method:"POST"}).then((function(e){return e.json()})).then((function(e){return e.success})).catch((function(){return!1}));case 5:return n=e.sent,e.abrupt("return",n);case 7:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),O={centrePermissions:{},emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,fullName:void 0,givenName:void 0,groups:[],id:void 0,isInitialized:!1,isSignedIn:!1,languagePreference:"en-gb",mfaType:void 0,middleName:void 0,preferences:{},requestStatus:"idle",telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1,usersEndpoint:void 0},A=function(e){return O[e]},P=function(t){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];Object.entries(t).forEach((function(e){var t=g(e,2),r=t[0],n=t[1];O[r]=n})),r&&e.dispatch("custom",{data:{updatedTimestamp:Date.now()},event:"cacheUpdated"})},I=function(){var e=h(l().mark((function e(t){var r,n,o;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.getIdToken().payload,P({centrePermissions:JSON.parse(n["custom:centre_permissions"]||"{}"),emailAddress:n.email,emailAddressVerified:n.email_verified,familyName:n.family_name||n.familyName,fullName:n.name,givenName:n.given_name||n.givenName,groups:n["cognito:groups"]||[],id:n.sub,isSignedIn:!0,languagePreference:n["custom:language_preference"],mfaType:n["custom:mfa_type"],middleName:n.middle_name||n.middleName,telephoneNumber:n.phone_number,telephoneNumberVerified:n.phone_number_verified,totpVerified:"true"===(null===(r=n["custom:totp_verified"])||void 0===r?void 0:r.toLowerCase())}),e.next=4,w();case 4:o=e.sent,P({preferences:o});case 6:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),x=function(t){e.dispatch("custom",{data:{challenge:t},event:"challenge"})},R=function(t,r){e.dispatch("custom",{data:{errorDetails:r,errorType:t},event:"error"})},M=function(t){e.dispatch("custom",{data:{session:t},event:"init"})},L=function(){e.listen("auth",(function(e){var t=e.payload,r=t.data;switch(t.event){case"completeNewPassword_failure":case"forgotPassword_failure":case"forgotPasswordSubmit_failure":case"signIn_failure":case"signOut_failure":P({requestStatus:"failed"});break;case"signIn":I(r.signInUserSession);break;case"signOut":P({centrePermissions:{},emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,fullName:void 0,givenName:void 0,groups:[],id:void 0,isSignedIn:!1,languagePreference:void 0,mfaType:void 0,middleName:void 0,preferences:{},telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1})}})),e.listen("custom",(function(e){if("error"===e.payload.event)"loading"===A("requestStatus")&&P({requestStatus:"failed"})}))};!function(e){e.COMPLETE_PASSWORD_RESET="completePasswordReset",e.FORCE_PASSWORD_CHANGE="forcePasswordChange",e.GET_AUTHORIZATION_TOKEN="getAuthorizationToken",e.GET_USER_SESSION="getUserSession",e.INIT="init",e.REQUEST_PASSWORD_RESET="requestPasswordReset",e.SET_MFA_TYPE="setMfaType",e.SIGN_IN="signIn",e.SIGN_OUT="signOut",e.SUBMIT_MFA_CODE="confirmSignIn",e.UPDATE_USER_ATTRIBUTES="updateUserAttributes",e.UPDATE_USER_PREFERENCES="updateUserPreferences",e.VERIFY_TELEPHONE_NUMBER="verifyTelephoneNumber",e.VERIFY_TOTP_TOKEN="verifySoftwareToken"}(y||(y={})),function(e){e.CUSTOM_CHALLENGE="CUSTOM_CHALLENGE",e.EMAIL_MFA="EMAIL",e.MFA_SETUP="MFA_SETUP",e.NEW_PASSWORD_REQUIRED="NEW_PASSWORD_REQUIRED",e.SMS_MFA="SMS",e.TOTP_MFA="TOTP",e.VERIFY_TELEPHONE_NUMBER="VERIFY_TELEPHONE_NUMBER"}(E||(E={})),function(e){e.EMAIL="EMAIL",e.NONE="NONE",e.SMS="SMS",e.TOTP="TOTP"}(S||(S={})),function(e){e.EXAMS_OFFICER="examsOfficer",e.HEAD_TEACHER="headTeacher",e.TEACHER="teacher"}(b||(b={})),function(e){e.ENGLISH="english",e.MATHS="maths",e.SCIENCE="science"}(_||(_={}));var U=function(e,r){return function(){var n;if(A("isInitialized")){for(var o=arguments.length,a=new Array(o),i=0;i<o;i++)a[i]=arguments[i];(n=e.call.apply(e,[t].concat(a)))instanceof Promise&&n.catch((function(e){R(r,e)}))}else console.error("WJEC One Authorization: You tried to call ".concat(r,"() before AWS Amplify was initialized. Please provide values for amplifyConfig & oAuthConfig to get set up"));return n}},k=function(e,t){return e.then((function(){return P({requestStatus:"done"}),!0})).catch((function(e){return(e instanceof Error||"string"==typeof e)&&R(t,e),!1}))},C=function(e){return"object"===p(e)&&Object.hasOwn(e,"name")&&"NotAuthorizedException"===e.name},j=function(e){return"object"===p(e)&&Object.hasOwn(e,"name")&&"InvalidPasswordException"===e.name},D=function(e){return Object.entries(e).reduce((function(e,t){var r=g(t,2),o=r[0],a=r[1];return f(f({},e),{},m({},function(e){switch(e){case"familyName":return"family_name";case"fullName":return"name";case"givenName":return"given_name";case"languagePreference":return"custom:language_preference";case"middleName":return"middle_name";case"telephoneNumber":return"phone_number";default:return n(e)}}(o),a))}),{})},F=function(){var e=h(l().mark((function e(r){var n,o;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=A("mfaType"),e.next=3,W(E.VERIFY_TELEPHONE_NUMBER,r,{currentMfaType:n});case 3:return o=e.sent,t.verifyCurrentUserAttribute("phone_number"),n===S.SMS&&T(S.EMAIL).then((function(){P({mfaType:S.EMAIL})})),x(o),e.abrupt("return",o);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),G=function(e){return function(){var r=h(l().mark((function r(n){var o,a,i;return l().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return P({requestStatus:"loading"}),r.next=3,t.completeNewPassword(e,n).catch((function(e){C(e)||j(e)||R(y.FORCE_PASSWORD_CHANGE,e),o=!1}));case 3:if(!(a=r.sent)){r.next=12;break}if(!a.challengeName){r.next=10;break}return r.next=8,W(a.challengeName,a,a.challengeParam);case 8:i=r.sent,x(i);case 10:P({requestStatus:"done"}),o=!0;case 12:return r.abrupt("return",o);case 13:case"end":return r.stop()}}),r)})));return function(e){return r.apply(this,arguments)}}()},q=function(e){return function(r){return P({requestStatus:"loading"}),k(t.sendCustomChallengeAnswer(e,r),y.SUBMIT_MFA_CODE)}},V=function(e){return function(){var r=h(l().mark((function r(n){var o;return l().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return P({requestStatus:"loading"}),r.next=3,k(t.verifyCurrentUserAttributeSubmit("phone_number",n),y.VERIFY_TELEPHONE_NUMBER);case 3:return(o=r.sent)&&(P({telephoneNumberVerified:!0}),A("mfaType")!==S.SMS&&e===S.SMS&&T(S.SMS).then((function(){P({mfaType:S.SMS})}))),r.abrupt("return",o);case 6:case"end":return r.stop()}}),r)})));return function(e){return r.apply(this,arguments)}}()},W=function(e,t){var r,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=z(e,null==n?void 0:n.selectedMfaType);switch(o){case E.EMAIL_MFA:r={challengeType:E.EMAIL_MFA,completeChallenge:q(t),emailAddress:n.emailAddress};break;case E.NEW_PASSWORD_REQUIRED:r={challengeType:E.NEW_PASSWORD_REQUIRED,completeChallenge:G(t)};break;case E.SMS_MFA:r={challengeType:o,completeChallenge:q(t),telephoneNumber:e===E.CUSTOM_CHALLENGE?n.redactedPhoneNumber:n.CODE_DELIVERY_DESTINATION};break;case E.TOTP_MFA:r={challengeType:o,completeChallenge:q(t)};break;case E.VERIFY_TELEPHONE_NUMBER:r={challengeType:o,completeChallenge:V(n.currentMfaType)}}return r},z=function(e,t){if(e===E.CUSTOM_CHALLENGE)switch(t){case S.EMAIL:return E.EMAIL_MFA;case S.SMS:return E.SMS_MFA;case S.TOTP:return E.TOTP_MFA}return e},H=!1,Y=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.amplifyConfig,n=t.oAuthConfig,s=t.onChallenge,f=t.onCompletePasswordReset,l=t.onError,p=t.onInit,d=t.onRequestPasswordReset,h=t.onSignIn,m=t.onSignOut,v=t.usersEndpoint,E=i(Date.now()),S=g(E,2);S[0];var b=S[1],_=u(!1);return c((function(){var t=!1,r=e.listen("auth",(function(e){var r=e.payload,n=r.data,a=r.event;if(!t)switch(a){case"completeNewPassword_failure":o(l)&&l(y.FORCE_PASSWORD_CHANGE,n);break;case"forgotPassword":o(d)&&d(n.username);break;case"forgotPassword_failure":o(l)&&l(y.REQUEST_PASSWORD_RESET,n);break;case"forgotPasswordSubmit":o(f)&&f();break;case"forgotPasswordSubmit_failure":o(l)&&l(y.COMPLETE_PASSWORD_RESET,n);break;case"signIn":o(h)&&h(n.signInUserSession);break;case"signIn_failure":o(l)&&l(y.SIGN_IN,n);break;case"signOut":o(m)&&m()}})),n=e.listen("custom",(function(e){var r=e.payload,n=r.data,a=r.event;if(!t)switch(a){case"cacheUpdated":b(n.updatedTimestamp);break;case"challenge":o(s)&&s(n.challenge);break;case"error":o(l)&&l(n.errorType,n.errorDetails);break;case"init":o(p)&&p(n.session)}}));return function(){t=!0,r(),n()}}),[s,f,l,p,d,h,m]),c((function(){if(r||n||v){try{K(r,n,v),_.current=!0,H=!0}catch(e){o(l)&&l(y.INIT)}H&&!_.current&&a("WJEC One useAuthorization: Config should only be provided to the first instance of the useAuthorization hook in your component tree (recommendation is to include this in your entry point component, even if you don't need authorization tools at that point)")}}),[r,n,v]),ne},B=U((function(e,r,n){return P({requestStatus:"loading"}),k(t.forgotPasswordSubmit(e,r,n),y.COMPLETE_PASSWORD_RESET)}),y.COMPLETE_PASSWORD_RESET),J=U(h(l().mark((function e(){var t,r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Q();case 2:return t=e.sent,r=t.getIdToken().getJwtToken(),e.abrupt("return",r);case 5:case"end":return e.stop()}}),e)}))),y.GET_AUTHORIZATION_TOKEN),Q=U(h(l().mark((function e(){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return r=e.sent,e.abrupt("return",r);case 4:case"end":return e.stop()}}),e)}))),y.GET_USER_SESSION),K=function(){var e=h(l().mark((function e(n,o,i){var u;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!A("isInitialized")){e.next=4;break}a("WJEC One Authorization: Config cannot be changed after initialization - the config that was originally provided will continue to be used"),e.next=22;break;case 4:if(n&&o&&i){e.next=8;break}throw new Error("WJEC One Authorization: Both amplifyConfig and oAuthConfig objects must be provided, in addition to the endpoint URL for the users service");case 8:return e.prev=8,r.configure(n),t.configure({oauth:o}),P({isInitialized:!0,usersEndpoint:i}),L(),e.next=15,t.currentAuthenticatedUser({bypassCache:!0}).then((function(e){var t=e.getSignInUserSession();return I(t),t})).catch((function(){}));case 15:u=e.sent,M(u),e.next=22;break;case 19:e.prev=19,e.t0=e.catch(8),R(y.INIT,e.t0);case 22:return e.abrupt("return",u);case 23:case"end":return e.stop()}}),e,null,[[8,19]])})));return function(t,r,n){return e.apply(this,arguments)}}(),Z=U((function(e){return P({requestStatus:"loading"}),k(t.forgotPassword(e),y.REQUEST_PASSWORD_RESET)}),y.REQUEST_PASSWORD_RESET),X=U(function(){var e=h(l().mark((function e(t){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return P({requestStatus:"loading"}),e.next=3,T(t);case 3:r=e.sent,P(r?{mfaType:t,requestStatus:"done"}:{requestStatus:"failed"});case 5:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),y.SET_MFA_TYPE),$=U(function(){var e=h(l().mark((function e(r,n){var o,a;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return P({requestStatus:"loading"}),e.next=3,t.signIn(r,n).catch((function(e){C(e)||R(y.SIGN_IN,e)}));case 3:if(!(a=e.sent)){e.next=11;break}if(!a.challengeName){e.next=10;break}return e.next=8,W(a.challengeName,a,a.challengeParam);case 8:o=e.sent,x(o);case 10:P({requestStatus:"done"});case 11:return e.abrupt("return",{challenge:o,user:a});case 12:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}(),y.SIGN_IN),ee=U((function(){return P({requestStatus:"loading"}),k(t.signOut(),y.SIGN_OUT)}),y.SIGN_OUT),te=U(function(){var e=h(l().mark((function e(r){var n,o,a,i,u,c;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=A("telephoneNumber"),a=A("telephoneNumberVerified"),P({requestStatus:"loading"}),e.next=5,t.currentAuthenticatedUser();case 5:return i=e.sent,e.next=8,k(t.updateUserAttributes(i,D(r)),y.UPDATE_USER_ATTRIBUTES);case 8:if(!(u=e.sent)){e.next=17;break}if(P(r),!Object.hasOwn(r,"telephoneNumber")){e.next=17;break}if(c=r.telephoneNumber,o===c&&a){e.next=17;break}return e.next=16,F(i);case 16:n=e.sent;case 17:return e.abrupt("return",{challenge:n,success:u});case 18:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),y.UPDATE_USER_ATTRIBUTES),re=U(function(){var e=h(l().mark((function e(t){var r,n;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return P({requestStatus:"loading"}),r=f(f({},A("preferences")),t),e.next=4,N(r);case 4:n=e.sent,P(n?{preferences:r,requestStatus:"done"}:{requestStatus:"failed"});case 6:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),y.UPDATE_USER_PREFERENCES),ne=Object.freeze({get centrePermissions(){return A("centrePermissions")},get emailAddress(){return A("emailAddress")},get emailAddressVerified(){return A("emailAddressVerified")},get familyName(){return A("familyName")},get fullName(){return A("fullName")},get givenName(){return A("givenName")},get groups(){return A("groups")},get id(){return A("id")},get isInitialized(){return A("isInitialized")},get isSignedIn(){return A("isSignedIn")},get languagePreference(){return A("languagePreference")},get mfaType(){return A("mfaType")},get middleName(){return A("middleName")},get preferences(){return A("preferences")},get requestStatus(){return A("requestStatus")},get telephoneNumber(){return A("telephoneNumber")},get telephoneNumberVerified(){return A("telephoneNumberVerified")},get totpVerified(){return A("totpVerified")}});export{y as AuthAction,E as ChallengeType,S as MFAType,b as Role,_ as Subject,B as completePasswordReset,J as getAuthorizationToken,Q as getUserSession,K as init,Z as requestPasswordReset,X as setMfaType,$ as signIn,ee as signOut,ne as state,te as updateUserAttributes,re as updateUserPreferences,Y as useAuthorization};
1
+ import{Hub as e,Auth as t,Amplify as r}from"aws-amplify";import{assertNever as n,isCallableFunction as a,developerWarning as o}from"../utils";import{useState as i,useRef as u,useEffect as c}from"react";function s(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function f(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?s(Object(r),!0).forEach((function(t){m(e,t,r[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):s(Object(r)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))}))}return e}function l(){l=function(){return e};var e={},t=Object.prototype,r=t.hasOwnProperty,n=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},o=a.iterator||"@@iterator",i=a.asyncIterator||"@@asyncIterator",u=a.toStringTag||"@@toStringTag";function c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(e){c=function(e,t,r){return e[t]=r}}function s(e,t,r,a){var o=t&&t.prototype instanceof d?t:d,i=Object.create(o.prototype),u=new N(a||[]);return n(i,"_invoke",{value:_(e,r,u)}),i}function f(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=s;var p={};function d(){}function h(){}function m(){}var E={};c(E,o,(function(){return this}));var g=Object.getPrototypeOf,v=g&&g(g(A([])));v&&v!==t&&r.call(v,o)&&(E=v);var y=m.prototype=d.prototype=Object.create(E);function S(e){["next","throw","return"].forEach((function(t){c(e,t,(function(e){return this._invoke(t,e)}))}))}function T(e,t){function a(n,o,i,u){var c=f(e[n],e,o);if("throw"!==c.type){var s=c.arg,l=s.value;return l&&"object"==typeof l&&r.call(l,"__await")?t.resolve(l.__await).then((function(e){a("next",e,i,u)}),(function(e){a("throw",e,i,u)})):t.resolve(l).then((function(e){s.value=e,i(s)}),(function(e){return a("throw",e,i,u)}))}u(c.arg)}var o;n(this,"_invoke",{value:function(e,r){function n(){return new t((function(t,n){a(e,r,t,n)}))}return o=o?o.then(n,n):n()}})}function _(e,t,r){var n="suspendedStart";return function(a,o){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===a)throw o;return P()}for(r.method=a,r.arg=o;;){var i=r.delegate;if(i){var u=b(i,r);if(u){if(u===p)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=f(e,t,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===p)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function b(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,b(e,t),"throw"===t.method))return p;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return p}var n=f(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,p;var a=n.arg;return a?a.done?(t[e.resultName]=a.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,p):a:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,p)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function O(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function N(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function A(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,a=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return a.next=a}}return{next:P}}function P(){return{value:void 0,done:!0}}return h.prototype=m,n(y,"constructor",{value:m,configurable:!0}),n(m,"constructor",{value:h,configurable:!0}),h.displayName=c(m,u,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,m):(e.__proto__=m,c(e,u,"GeneratorFunction")),e.prototype=Object.create(y),e},e.awrap=function(e){return{__await:e}},S(T.prototype),c(T.prototype,i,(function(){return this})),e.AsyncIterator=T,e.async=function(t,r,n,a,o){void 0===o&&(o=Promise);var i=new T(s(t,r,n,a),o);return e.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},S(y),c(y,u,"Generator"),c(y,o,(function(){return this})),c(y,"toString",(function(){return"[object Generator]"})),e.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},e.values=A,N.prototype={constructor:N,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(O),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return i.type="throw",i.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var a=this.tryEntries.length-1;a>=0;--a){var o=this.tryEntries[a],i=o.completion;if("root"===o.tryLoc)return n("end");if(o.tryLoc<=this.prev){var u=r.call(o,"catchLoc"),c=r.call(o,"finallyLoc");if(u&&c){if(this.prev<o.catchLoc)return n(o.catchLoc,!0);if(this.prev<o.finallyLoc)return n(o.finallyLoc)}else if(u){if(this.prev<o.catchLoc)return n(o.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<o.finallyLoc)return n(o.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var a=this.tryEntries[n];if(a.tryLoc<=this.prev&&r.call(a,"finallyLoc")&&this.prev<a.finallyLoc){var o=a;break}}o&&("break"===e||"continue"===e)&&o.tryLoc<=t&&t<=o.finallyLoc&&(o=null);var i=o?o.completion:{};return i.type=e,i.arg=t,o?(this.method="next",this.next=o.finallyLoc,p):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),p},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),O(r),p}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var a=n.arg;O(r)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:A(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),p}},e}function p(e){return p="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},p(e)}function d(e,t,r,n,a,o,i){try{var u=e[o](i),c=u.value}catch(e){return void r(e)}u.done?t(c):Promise.resolve(c).then(n,a)}function h(e){return function(){var t=this,r=arguments;return new Promise((function(n,a){var o=e.apply(t,r);function i(e){d(o,n,a,i,u,"next",e)}function u(e){d(o,n,a,i,u,"throw",e)}i(void 0)}))}}function m(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function E(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null==r)return;var n,a,o=[],i=!0,u=!1;try{for(r=r.call(e);!(i=(n=r.next()).done)&&(o.push(n.value),!t||o.length!==t);i=!0);}catch(e){u=!0,a=e}finally{try{i||null==r.return||r.return()}finally{if(u)throw a}}return o}(e,t)||function(e,t){if(!e)return;if("string"==typeof e)return g(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);"Object"===r&&e.constructor&&(r=e.constructor.name);if("Map"===r||"Set"===r)return Array.from(e);if("Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r))return g(e,t)}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function g(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}var v,y,S,T,_,b=function(t){e.dispatch("custom",{data:{challenge:t},event:"challenge"})},w=function(t,r){e.dispatch("custom",{data:{errorDetails:r,errorType:t},event:"error"})},O=function(t){e.dispatch("custom",{data:{session:t},event:"init"})},N=function(){var e=h(l().mark((function e(){var r,n,a;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return r=e.sent,n=r.getIdToken(),e.next=6,fetch("".concat(z("usersEndpoint")).concat(n.payload.sub,"/mfa/secret"),{headers:{Authorization:n.getJwtToken()}}).then((function(e){return e.json()})).then((function(e){return e.secret}));case 6:return a=e.sent,e.abrupt("return",a);case 8:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),A=function(){var e=h(l().mark((function e(){var r,n,a;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return r=e.sent,n=r.getIdToken(),e.next=6,fetch("".concat(z("usersEndpoint")).concat(n.payload.sub,"/preferences"),{headers:{Authorization:n.getJwtToken()}}).then((function(e){return e.json()}));case 6:return a=e.sent,e.abrupt("return",a||{});case 8:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),P=function(){var e=h(l().mark((function e(r){var n,a,o;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return n=e.sent,a=n.getIdToken(),e.next=6,fetch("".concat(z("usersEndpoint")).concat(a.payload.sub,"/mfa"),{body:JSON.stringify({mfaType:r}),headers:{Authorization:a.getJwtToken()},method:"POST"}).then((function(e){if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0}));case 6:return o=e.sent,e.abrupt("return",o);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),I=function(){var e=h(l().mark((function e(r){var n,a,o;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return n=e.sent,a=n.getIdToken(),e.next=6,fetch("".concat(z("usersEndpoint")).concat(a.payload.sub,"/preferences"),{body:JSON.stringify(r),headers:{Authorization:a.getJwtToken()},method:"POST"}).then((function(e){return e.json()})).then((function(e){return e.success}));case 6:return o=e.sent,e.abrupt("return",o);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),R=function(){var e=h(l().mark((function e(r){var n,a,o;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return n=e.sent,a=n.getIdToken(),e.next=6,fetch("".concat(z("usersEndpoint")).concat(a.payload.sub,"/mfa/secret"),{body:JSON.stringify({totpCode:r}),headers:{Authorization:a.getJwtToken()},method:"POST"}).then((function(e){if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0}));case 6:return o=e.sent,e.abrupt("return",o);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}();!function(e){e.COMPLETE_PASSWORD_RESET="completePasswordReset",e.FORCE_PASSWORD_CHANGE="forcePasswordChange",e.GET_AUTHORIZATION_TOKEN="getAuthorizationToken",e.GET_USER_PREFERENCES="getUserPreferences",e.GET_USER_SESSION="getUserSession",e.INIT="init",e.REQUEST_PASSWORD_RESET="requestPasswordReset",e.REQUEST_TOTP_TOKEN="requestSoftwareToken",e.SET_MFA_TYPE="setMfaType",e.SIGN_IN="signIn",e.SIGN_OUT="signOut",e.SUBMIT_MFA_CODE="confirmSignIn",e.UPDATE_USER_ATTRIBUTES="updateUserAttributes",e.UPDATE_USER_PREFERENCES="updateUserPreferences",e.VERIFY_TELEPHONE_NUMBER="verifyTelephoneNumber",e.VERIFY_TOTP_TOKEN="verifySoftwareToken"}(v||(v={})),function(e){e.CUSTOM_CHALLENGE="CUSTOM_CHALLENGE",e.EMAIL_MFA="EMAIL",e.MFA_SETUP="MFA_SETUP",e.NEW_PASSWORD_REQUIRED="NEW_PASSWORD_REQUIRED",e.SMS_MFA="SMS",e.TOTP_MFA="TOTP",e.VERIFY_TELEPHONE_NUMBER="VERIFY_TELEPHONE_NUMBER",e.VERIFY_TOTP_TOKEN="VERIFY_TOTP_TOKEN"}(y||(y={})),function(e){e.EMAIL="EMAIL",e.NONE="NONE",e.SMS="SMS",e.TOTP="TOTP"}(S||(S={})),function(e){e.EXAMS_OFFICER="examsOfficer",e.HEAD_TEACHER="headTeacher",e.TEACHER="teacher"}(T||(T={})),function(e){e.ENGLISH="english",e.MATHS="maths",e.SCIENCE="science"}(_||(_={}));var x=function(e){return function(){var r=h(l().mark((function r(n){var a,o,i;return l().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return Y({requestStatus:"loading"}),r.next=3,t.completeNewPassword(e,n).catch((function(e){D(e)||q(e)||w(v.FORCE_PASSWORD_CHANGE,e),a=!1}));case 3:if(!(o=r.sent)){r.next=12;break}if(!o.challengeName){r.next=10;break}return r.next=8,L(o.challengeName,o,o.challengeParam);case 8:i=r.sent,b(i);case 10:Y({requestStatus:"done"}),a=!0;case 12:return r.abrupt("return",a);case 13:case"end":return r.stop()}}),r)})));return function(e){return r.apply(this,arguments)}}()},k=function(e){return function(r){return Y({requestStatus:"loading"}),j(t.sendCustomChallengeAnswer(e,r),v.SUBMIT_MFA_CODE)}},M=function(e){return function(){var r=h(l().mark((function r(n){var a;return l().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return Y({requestStatus:"loading"}),r.next=3,j(t.verifyCurrentUserAttributeSubmit("phone_number",n),v.VERIFY_TELEPHONE_NUMBER);case 3:return(a=r.sent)&&(Y({telephoneNumberVerified:!0}),z("mfaType")!==S.SMS&&e===S.SMS&&P(S.SMS).then((function(){Y({mfaType:S.SMS})}))),r.abrupt("return",a);case 6:case"end":return r.stop()}}),r)})));return function(e){return r.apply(this,arguments)}}()},U=function(){return function(){var e=h(l().mark((function e(t){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,ue(t);case 2:return r=e.sent,e.abrupt("return",r);case 4:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}()},L=function(e,t){var r,n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},a=C(e,null==n?void 0:n.selectedMfaType);switch(a){case y.EMAIL_MFA:r={challengeType:y.EMAIL_MFA,completeChallenge:k(t),emailAddress:n.emailAddress};break;case y.NEW_PASSWORD_REQUIRED:r={challengeType:y.NEW_PASSWORD_REQUIRED,completeChallenge:x(t)};break;case y.SMS_MFA:r={challengeType:a,completeChallenge:k(t),telephoneNumber:e===y.CUSTOM_CHALLENGE?n.redactedPhoneNumber:n.CODE_DELIVERY_DESTINATION};break;case y.TOTP_MFA:r={challengeType:a,completeChallenge:k(t)};break;case y.VERIFY_TELEPHONE_NUMBER:r={challengeType:a,completeChallenge:M(n.currentMfaType)};break;case y.VERIFY_TOTP_TOKEN:r={challengeType:a,completeChallenge:U(),secretKey:n.secretKey}}return r},C=function(e,t){if(e===y.CUSTOM_CHALLENGE)switch(t){case S.EMAIL:return y.EMAIL_MFA;case S.SMS:return y.SMS_MFA;case S.TOTP:return y.TOTP_MFA}return e},F=function(e,r){return function(){var n;if(z("isInitialized")){for(var a=arguments.length,o=new Array(a),i=0;i<a;i++)o[i]=arguments[i];(n=e.call.apply(e,[t].concat(o)))instanceof Promise&&n.catch((function(e){w(r,e)}))}else console.error("WJEC One Authorization: You tried to call ".concat(r,"() before AWS Amplify was initialized. Please provide values for amplifyConfig & oAuthConfig to get set up"));return n}},j=function(e,t){return e.then((function(){return Y({requestStatus:"done"}),!0})).catch((function(e){return(e instanceof Error||"string"==typeof e)&&w(t,e),!1}))},D=function(e){return"object"===p(e)&&Object.hasOwn(e,"name")&&"NotAuthorizedException"===e.name},q=function(e){return"object"===p(e)&&Object.hasOwn(e,"name")&&"InvalidPasswordException"===e.name},V=function(e){return Object.entries(e).reduce((function(e,t){var r=E(t,2),a=r[0],o=r[1];return f(f({},e),{},m({},function(e){switch(e){case"familyName":return"family_name";case"fullName":return"name";case"givenName":return"given_name";case"languagePreference":return"custom:language_preference";case"middleName":return"middle_name";case"telephoneNumber":return"phone_number";default:return n(e)}}(a),o))}),{})},G=function(){var e=h(l().mark((function e(r){var n,a;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=z("mfaType"),e.next=3,L(y.VERIFY_TELEPHONE_NUMBER,r,{currentMfaType:n});case 3:return a=e.sent,t.verifyCurrentUserAttribute("phone_number"),n===S.SMS&&P(S.EMAIL).then((function(){Y({mfaType:S.EMAIL})})).catch((function(e){w(v.SET_MFA_TYPE,e)})),b(a),e.abrupt("return",a);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),W={centrePermissions:{},emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,fullName:void 0,givenName:void 0,groups:[],id:void 0,isInitialized:!1,isSignedIn:!1,languagePreference:"en-gb",mfaType:void 0,middleName:void 0,preferences:{},requestStatus:"idle",telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1,usersEndpoint:void 0},z=function(e){return W[e]},Y=function(t){var r=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];Object.entries(t).forEach((function(e){var t=E(e,2),r=t[0],n=t[1];W[r]=n})),r&&e.dispatch("custom",{data:{updatedTimestamp:Date.now()},event:"cacheUpdated"})},H=function(){var e=h(l().mark((function e(t){var r,n,a;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.getIdToken().payload,Y({requestStatus:"loading"}),e.next=4,A().then((function(e){return Y({requestStatus:"done"}),e})).catch((function(e){return w(v.GET_USER_PREFERENCES,e),Y({requestStatus:"failed"}),{}}));case 4:a=e.sent,Y({centrePermissions:JSON.parse(n["custom:centre_permissions"]||"{}"),emailAddress:n.email,emailAddressVerified:n.email_verified,familyName:n.family_name||n.familyName,fullName:n.name,givenName:n.given_name||n.givenName,groups:n["cognito:groups"]||[],id:n.sub,isSignedIn:!0,languagePreference:n["custom:language_preference"],mfaType:n["custom:mfa_type"],middleName:n.middle_name||n.middleName,preferences:(o=a,f(f({},o),{},{emailMfaAccepted:o.emailMfaAccepted?new Date(o.emailMfaAccepted):void 0})),telephoneNumber:n.phone_number,telephoneNumberVerified:n.phone_number_verified,totpVerified:"true"===(null===(r=n["custom:totp_verified"])||void 0===r?void 0:r.toLowerCase())});case 6:case"end":return e.stop()}var o}),e)})));return function(t){return e.apply(this,arguments)}}(),K=function(){Y({centrePermissions:{},emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,fullName:void 0,givenName:void 0,groups:[],id:void 0,isSignedIn:!1,languagePreference:void 0,mfaType:void 0,middleName:void 0,preferences:{},telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1})},J=!1,B=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},r=t.amplifyConfig,n=t.oAuthConfig,s=t.onChallenge,f=t.onCompletePasswordReset,l=t.onError,p=t.onInit,d=t.onRequestPasswordReset,h=t.onSignIn,m=t.onSignOut,g=t.usersEndpoint,y=i(Date.now()),S=E(y,2);S[0];var T=S[1],_=u(!1);return c((function(){var t=!1,r=e.listen("auth",(function(e){var r=e.payload,n=r.data,o=r.event;if(!t)switch(o){case"completeNewPassword_failure":a(l)&&l(v.FORCE_PASSWORD_CHANGE,n);break;case"forgotPassword":a(d)&&d(n.username);break;case"forgotPassword_failure":a(l)&&l(v.REQUEST_PASSWORD_RESET,n);break;case"forgotPasswordSubmit":a(f)&&f();break;case"forgotPasswordSubmit_failure":a(l)&&l(v.COMPLETE_PASSWORD_RESET,n);break;case"signIn":a(h)&&h(n.signInUserSession);break;case"signIn_failure":a(l)&&l(v.SIGN_IN,n);break;case"signOut":a(m)&&m()}})),n=e.listen("custom",(function(e){var r=e.payload,n=r.data,o=r.event;if(!t)switch(o){case"cacheUpdated":T(n.updatedTimestamp);break;case"challenge":a(s)&&s(n.challenge);break;case"error":a(l)&&l(n.errorType,n.errorDetails);break;case"init":a(p)&&p(n.session)}}));return function(){t=!0,r(),n()}}),[s,f,l,p,d,h,m]),c((function(){if(r||n||g){try{$(r,n,g),_.current=!0,J=!0}catch(e){a(l)&&l(v.INIT)}J&&!_.current&&o("WJEC One useAuthorization: Config should only be provided to the first instance of the useAuthorization hook in your component tree (recommendation is to include this in your entry point component, even if you don't need authorization tools at that point)")}}),[r,n,g]),ce},Q=F((function(e,r,n){return Y({requestStatus:"loading"}),j(t.forgotPasswordSubmit(e,r,n),v.COMPLETE_PASSWORD_RESET)}),v.COMPLETE_PASSWORD_RESET),Z=F(h(l().mark((function e(){var t,r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,X();case 2:return t=e.sent,r=t.getIdToken().getJwtToken(),e.abrupt("return",r);case 5:case"end":return e.stop()}}),e)}))),v.GET_AUTHORIZATION_TOKEN),X=F(h(l().mark((function e(){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentSession();case 2:return r=e.sent,e.abrupt("return",r);case 4:case"end":return e.stop()}}),e)}))),v.GET_USER_SESSION),$=function(){var n=h(l().mark((function n(a,i,u){var c;return l().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(!z("isInitialized")){n.next=4;break}o("WJEC One Authorization: Config cannot be changed after initialization - the config that was originally provided will continue to be used"),n.next=22;break;case 4:if(a&&i&&u){n.next=8;break}throw new Error("WJEC One Authorization: Both amplifyConfig and oAuthConfig objects must be provided, in addition to the endpoint URL for the users service");case 8:return n.prev=8,r.configure(a),t.configure({oauth:i}),Y({isInitialized:!0,usersEndpoint:u}),e.listen("auth",(function(e){var t=e.payload,r=t.data;switch(t.event){case"completeNewPassword_failure":case"forgotPassword_failure":case"forgotPasswordSubmit_failure":case"signIn_failure":case"signOut_failure":Y({requestStatus:"failed"});break;case"signIn":H(r.signInUserSession);break;case"signOut":K()}})),e.listen("custom",(function(e){"error"===e.payload.event&&"loading"===z("requestStatus")&&Y({requestStatus:"failed"})})),n.next=15,t.currentAuthenticatedUser({bypassCache:!0}).then(function(){var e=h(l().mark((function e(t){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.getSignInUserSession(),e.next=3,H(r);case 3:return e.abrupt("return",r);case 4:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}()).catch((function(){}));case 15:c=n.sent,O(c),n.next=22;break;case 19:n.prev=19,n.t0=n.catch(8),w(v.INIT,n.t0);case 22:return n.abrupt("return",c);case 23:case"end":return n.stop()}}),n,null,[[8,19]])})));return function(e,t,r){return n.apply(this,arguments)}}(),ee=F((function(e){return Y({requestStatus:"loading"}),j(t.forgotPassword(e),v.REQUEST_PASSWORD_RESET)}),v.REQUEST_PASSWORD_RESET),te=F(function(){var e=h(l().mark((function e(t){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return Y({requestStatus:"loading"}),e.next=3,P(t).catch((function(e){return w(v.SET_MFA_TYPE,e),Y({requestStatus:"failed"}),!1}));case 3:return(r=e.sent)&&(Y({mfaType:t,requestStatus:"done"}),t!==S.EMAIL&&I({emailMfaAccepted:void 0})),e.abrupt("return",r);case 6:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),v.SET_MFA_TYPE),re=F(h(l().mark((function e(){var r,n,a;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.currentAuthenticatedUser();case 2:return n=e.sent,Y({requestStatus:"loading"}),e.next=6,N().catch((function(e){w(v.REQUEST_TOTP_TOKEN,e),Y({requestStatus:"failed"})}));case 6:if(!(a=e.sent)){e.next=15;break}return z("mfaType")===S.SMS&&P(S.EMAIL).then((function(){Y({mfaType:S.EMAIL})})).catch((function(e){w(v.SET_MFA_TYPE,e)})),e.next=12,L(y.VERIFY_TOTP_TOKEN,n,{secretKey:a});case 12:r=e.sent,b(r),Y({requestStatus:"done"});case 15:return e.abrupt("return",{challenge:r,secretKey:a});case 16:case"end":return e.stop()}}),e)}))),v.REQUEST_TOTP_TOKEN),ne=F(function(){var e=h(l().mark((function e(r,n){var a,o;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return Y({requestStatus:"loading"}),e.next=3,t.signIn(r,n).catch((function(e){D(e)||w(v.SIGN_IN,e)}));case 3:if(!(o=e.sent)){e.next=11;break}if(!o.challengeName){e.next=10;break}return e.next=8,L(o.challengeName,o,o.challengeParam);case 8:a=e.sent,b(a);case 10:Y({requestStatus:"done"});case 11:return e.abrupt("return",{challenge:a,user:o});case 12:case"end":return e.stop()}}),e)})));return function(t,r){return e.apply(this,arguments)}}(),v.SIGN_IN),ae=F((function(){return Y({requestStatus:"loading"}),j(t.signOut(),v.SIGN_OUT)}),v.SIGN_OUT),oe=F(function(){var e=h(l().mark((function e(r){var n,a,o,i,u,c;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=z("telephoneNumber"),o=z("telephoneNumberVerified"),Y({requestStatus:"loading"}),e.next=5,t.currentAuthenticatedUser();case 5:return i=e.sent,e.next=8,j(t.updateUserAttributes(i,V(r)),v.UPDATE_USER_ATTRIBUTES);case 8:if(!(u=e.sent)){e.next=17;break}if(Y(r),!Object.hasOwn(r,"telephoneNumber")){e.next=17;break}if(c=r.telephoneNumber,a===c&&o){e.next=17;break}return e.next=16,G(i);case 16:n=e.sent;case 17:return e.abrupt("return",{challenge:n,success:u});case 18:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),v.UPDATE_USER_ATTRIBUTES),ie=F(function(){var e=h(l().mark((function e(t){var r,n;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return Y({requestStatus:"loading"}),r=f(f({},z("preferences")),t),e.next=4,I(r).catch((function(e){return w(v.UPDATE_USER_PREFERENCES,e),Y({requestStatus:"failed"}),!1}));case 4:return(n=e.sent)&&Y({preferences:r,requestStatus:"done"}),e.abrupt("return",n);case 7:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),v.UPDATE_USER_PREFERENCES),ue=F(function(){var e=h(l().mark((function e(t){var r;return l().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return Y({requestStatus:"loading"}),e.next=3,R(t).catch((function(e){return w(v.VERIFY_TOTP_TOKEN,e),Y({requestStatus:"failed"}),!1}));case 3:return(r=e.sent)&&Y({requestStatus:"done"}),e.abrupt("return",r);case 6:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),v.VERIFY_TOTP_TOKEN),ce=Object.freeze({get centrePermissions(){return z("centrePermissions")},get emailAddress(){return z("emailAddress")},get emailAddressVerified(){return z("emailAddressVerified")},get familyName(){return z("familyName")},get fullName(){return z("fullName")},get givenName(){return z("givenName")},get groups(){return z("groups")},get id(){return z("id")},get isInitialized(){return z("isInitialized")},get isSignedIn(){return z("isSignedIn")},get languagePreference(){return z("languagePreference")},get mfaType(){return z("mfaType")},get middleName(){return z("middleName")},get preferences(){return z("preferences")},get requestStatus(){return z("requestStatus")},get telephoneNumber(){return z("telephoneNumber")},get telephoneNumberVerified(){return z("telephoneNumberVerified")},get totpVerified(){return z("totpVerified")}});export{v as AuthAction,y as ChallengeType,S as MFAType,T as Role,_ as Subject,Q as completePasswordReset,Z as getAuthorizationToken,X as getUserSession,$ as init,ee as requestPasswordReset,te as setMfaType,re as setupTotp,ne as signIn,ae as signOut,ce as state,oe as updateUserAttributes,ie as updateUserPreferences,B as useAuthorization,ue as verifyTotp};