wjec-one 4.0.21 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +71 -99
- package/auth/index.d.ts +183 -183
- package/auth/index.js +1 -3
- package/config/babel.config.js +3 -11
- package/config/eslint-config.js +3 -3
- package/config/utils.js +19 -7
- package/config/webpack.config.js +19 -3
- package/index.d.ts +1690 -1665
- package/index.js +44 -1
- package/layout/index.d.ts +55 -55
- package/layout/index.js +1 -1
- package/localization/index.d.ts +35 -37
- package/localization/index.js +1 -1
- package/package.json +25 -28
- package/portal/index.d.ts +264 -248
- package/portal/index.js +9 -3
- package/scripts/test.js +0 -1
- package/services/index.d.ts +102 -102
- package/services/index.js +1 -3
- package/store/index.d.ts +61 -61
- package/store/index.js +1 -1
- package/store/utils/index.d.ts +1 -1
- package/store/utils/index.js +1 -1
- package/test/setupTests.ts +1 -1
- package/test/setupTestsAfterEnv.ts +15 -0
- package/theme/index.d.ts +427 -79
- package/theme/index.js +1 -1
- package/umd/auth/index.js +1 -3
- package/umd/index.js +44 -1
- package/umd/layout/index.js +1 -1
- package/umd/localization/index.js +1 -1
- package/umd/portal/index.js +9 -3
- package/umd/services/index.js +1 -3
- package/umd/store/index.js +1 -1
- package/umd/store/utils/index.js +1 -1
- package/umd/theme/index.js +1 -1
- package/umd/utils/index.js +1 -3
- package/utils/index.d.ts +150 -140
- package/utils/index.js +1 -3
- package/wjec-one-project.d.ts +1 -0
package/umd/auth/index.js
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("aws-amplify"),require("wjec-one/utils"),require("react")):"function"==typeof define&&define.amd?define(["exports","aws-amplify","wjec-one/utils","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).WJECOneAuth={},e.aws_amplify,e.WJECOneUtils,e.React)}(this,(function(e,t,n,r){"use strict";function o(e,t){this.v=e,this.k=t}function u(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=Array(t);n<t;n++)r[n]=e[n];return r}function i(e,t,n,r,o,u,i){try{var a=e[u](i),c=a.value}catch(e){return void n(e)}a.done?t(c):Promise.resolve(c).then(r,o)}function a(e){return function(){var t=this,n=arguments;return new Promise((function(r,o){var u=e.apply(t,n);function a(e){i(u,r,o,a,c,"next",e)}function c(e){i(u,r,o,a,c,"throw",e)}a(void 0)}))}}function c(e,t,n){return(t=function(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:t+""}(t))in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function s(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function f(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?s(Object(n),!0).forEach((function(t){c(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):s(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function l(e,t){if(null==e)return{};var n,r,o=function(e,t){if(null==e)return{};var n={};for(var r in e)if({}.hasOwnProperty.call(e,r)){if(-1!==t.indexOf(r))continue;n[r]=e[r]}return n}(e,t);if(Object.getOwnPropertySymbols){var u=Object.getOwnPropertySymbols(e);for(r=0;r<u.length;r++)n=u[r],-1===t.indexOf(n)&&{}.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}function p(){
|
|
2
|
-
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */
|
|
3
|
-
var e,t,n="function"==typeof Symbol?Symbol:{},r=n.iterator||"@@iterator",o=n.toStringTag||"@@toStringTag";function u(n,r,o,u){var c=r&&r.prototype instanceof a?r:a,s=Object.create(c.prototype);return E(s,"_invoke",function(n,r,o){var u,a,c,s=0,f=o||[],l=!1,p={p:0,n:0,v:e,a:h,f:h.bind(e,4),d:function(t,n){return u=t,a=0,c=e,p.n=n,i}};function h(n,r){for(a=n,c=r,t=0;!l&&s&&!o&&t<f.length;t++){var o,u=f[t],h=p.p,d=u[2];n>3?(o=d===r)&&(c=u[(a=u[4])?5:(a=3,3)],u[4]=u[5]=e):u[0]<=h&&((o=n<2&&h<u[1])?(a=0,p.v=r,p.n=u[1]):h<d&&(o=n<3||u[0]>r||r>d)&&(u[4]=n,u[5]=r,p.n=d,a=0))}if(o||n>1)return i;throw l=!0,r}return function(o,f,d){if(s>1)throw TypeError("Generator is already running");for(l&&1===f&&h(f,d),a=f,c=d;(t=a<2?e:c)||!l;){u||(a?a<3?(a>1&&(p.n=-1),h(a,c)):p.n=c:p.v=c);try{if(s=2,u){if(a||(o="next"),t=u[o]){if(!(t=t.call(u,c)))throw TypeError("iterator result is not an object");if(!t.done)return t;c=t.value,a<2&&(a=0)}else 1===a&&(t=u.return)&&t.call(u),a<2&&(c=TypeError("The iterator does not provide a '"+o+"' method"),a=1);u=e}else if((t=(l=p.n<0)?c:n.call(r,p))!==i)break}catch(t){u=e,a=1,c=t}finally{s=1}}return{value:t,done:l}}}(n,o,u),!0),s}var i={};function a(){}function c(){}function s(){}t=Object.getPrototypeOf;var f=[][r]?t(t([][r]())):(E(t={},r,(function(){return this})),t),l=s.prototype=a.prototype=Object.create(f);function h(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,s):(e.__proto__=s,E(e,o,"GeneratorFunction")),e.prototype=Object.create(l),e}return c.prototype=s,E(l,"constructor",s),E(s,"constructor",c),c.displayName="GeneratorFunction",E(s,o,"GeneratorFunction"),E(l),E(l,o,"Generator"),E(l,r,(function(){return this})),E(l,"toString",(function(){return"[object Generator]"})),(p=function(){return{w:u,m:h}})()}function h(e,t,n,r,o){var u=d(e,t,n,r,o);return u.next().then((function(e){return e.done?e.value:u.next()}))}function d(e,t,n,r,o){return new A(p().w(e,t,n,r),o||Promise)}function A(e,t){function n(r,u,i,a){try{var c=e[r](u),s=c.value;return s instanceof o?t.resolve(s.v).then((function(e){n("next",e,i,a)}),(function(e){n("throw",e,i,a)})):t.resolve(s).then((function(e){c.value=e,i(c)}),(function(e){return n("throw",e,i,a)}))}catch(e){a(e)}}var r;this.next||(E(A.prototype),E(A.prototype,"function"==typeof Symbol&&Symbol.asyncIterator||"@asyncIterator",(function(){return this}))),E(this,"_invoke",(function(e,o,u){function i(){return new t((function(t,r){n(e,u,t,r)}))}return r=r?r.then(i,i):i()}),!0)}function E(e,t,n,r){var o=Object.defineProperty;try{o({},"",{})}catch(e){o=0}E=function(e,t,n,r){if(t)o?o(e,t,{value:n,enumerable:!r,configurable:!r,writable:!r}):e[t]=n;else{function u(t,n){E(e,t,(function(e){return this._invoke(t,n,e)}))}u("next",0),u("throw",1),u("return",2)}},E(e,t,n,r)}function S(e){var t=Object(e),n=[];for(var r in t)n.unshift(r);return function e(){for(;n.length;)if((r=n.pop())in t)return e.value=r,e.done=!1,e;return e.done=!0,e}}function T(e){if(null!=e){var t=e["function"==typeof Symbol&&Symbol.iterator||"@@iterator"],n=0;if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length))return{next:function(){return e&&n>=e.length&&(e=void 0),{value:e&&e[n++],done:!e}}}}throw new TypeError(typeof e+" is not iterable")}function m(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){var n=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=n){var r,o,u,i,a=[],c=!0,s=!1;try{if(u=(n=n.call(e)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(r=u.call(n)).done)&&(a.push(r.value),a.length!==t);c=!0);}catch(e){s=!0,o=e}finally{try{if(!c&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(s)throw o}}return a}}(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){return function(e){if(Array.isArray(e))return u(e)}(e)||function(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}(e)||_(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function y(e){return y="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},y(e)}function _(e,t){if(e){if("string"==typeof e)return u(e,t);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?u(e,t):void 0}}function b(){var e=p(),t=e.m(b),n=(Object.getPrototypeOf?Object.getPrototypeOf(t):t.__proto__).constructor;function r(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===n||"GeneratorFunction"===(t.displayName||t.name))}var u={throw:1,return:2,break:3,continue:3};function i(e){var t,n;return function(r){t||(t={stop:function(){return n(r.a,2)},catch:function(){return r.v},abrupt:function(e,t){return n(r.a,u[e],t)},delegateYield:function(e,o,u){return t.resultName=o,n(r.d,T(e),u)},finish:function(e){return n(r.f,e)}},n=function(e,n,o){r.p=t.prev,r.n=t.next;try{return e(n,o)}finally{t.next=r.n}}),t.resultName&&(t[t.resultName]=r.v,t.resultName=void 0),t.sent=r.v,t.next=r.n;try{return e.call(this,t)}finally{r.p=t.prev,r.n=t.next}}}return(b=function(){return{wrap:function(t,n,r,o){return e.w(i(t),n,r,o&&o.reverse())},isGeneratorFunction:r,mark:e.m,awrap:function(e,t){return new o(e,t)},AsyncIterator:A,async:function(e,t,n,o,u){return(r(t)?d:h)(i(e),t,n,o,u)},keys:S,values:T}})()}var g,O,N,R,P;e.AuthAction=void 0,(g=e.AuthAction||(e.AuthAction={})).CHANGE_PASSWORD="changePassword",g.COMPLETE_PASSWORD_RESET="completePasswordReset",g.FORCE_CHANGE_PASSWORD="forceChangePassword",g.GET_AUTHORIZATION_TOKEN="getAuthorizationToken",g.GET_USER_PREFERENCES="getUserPreferences",g.GET_USER_SESSION="getUserSession",g.INIT="init",g.REFRESH_USER_SESSION="refreshUserSession",g.REQUEST_PASSWORD_RESET="requestPasswordReset",g.REQUEST_TOTP_TOKEN="requestSoftwareToken",g.SET_MFA_TYPE="setMfaType",g.SIGN_IN="signIn",g.SIGN_OUT="signOut",g.SUBMIT_MFA_CODE="confirmSignIn",g.UPDATE_USER_ATTRIBUTES="updateUserAttributes",g.UPDATE_USER_PREFERENCES="updateUserPreferences",g.VERIFY_TELEPHONE_NUMBER="verifyTelephoneNumber",g.VERIFY_TOTP_TOKEN="verifySoftwareToken",e.ChallengeType=void 0,(O=e.ChallengeType||(e.ChallengeType={})).CUSTOM_CHALLENGE="CUSTOM_CHALLENGE",O.EMAIL_MFA="EMAIL",O.MFA_SETUP="MFA_SETUP",O.NEW_PASSWORD_REQUIRED="NEW_PASSWORD_REQUIRED",O.SMS_MFA="SMS",O.TOTP_MFA="TOTP",O.VERIFY_TELEPHONE_NUMBER="VERIFY_TELEPHONE_NUMBER",O.VERIFY_TOTP_TOKEN="VERIFY_TOTP_TOKEN",e.MFAType=void 0,(N=e.MFAType||(e.MFAType={})).EMAIL="EMAIL",N.NONE="NONE",N.SMS="SMS",N.TOTP="TOTP",e.Role=void 0,(R=e.Role||(e.Role={})).ADMINISTRATIVE_SUPPORT="administrativeSupport",R.EXAMS_OFFICER="examsOfficer",R.HEAD_OF_DEPARTMENT="headOfDepartment",R.HEAD_OF_YEAR="headOfYear",R.HEAD_TEACHER="headTeacher",R.LOCAL_AUTHORITY_ADVISOR="localAuthorityAdvisor",R.OTHER="other",R.STAKEHOLDER="stakeholder",R.TEACHER="teacher",R.TUTOR="tutor",e.Subject=void 0,(P=e.Subject||(e.Subject={})).ENGLISH="english",P.MATHS="maths",P.SCIENCE="science";var w=function(e,r){return function(){var o;if(q("isInitialized")){for(var u=arguments.length,i=new Array(u),a=0;a<u;a++)i[a]=arguments[a];(o=e.call.apply(e,[t.Auth].concat(i)))instanceof Promise&&o.catch((function(e){j(r,e)})).finally((function(){D()}))}else n.developerError("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 o}},I=function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};return e.then((function(){return!1!==n.updateStatus&&k(t,"done"),!0})).catch((function(e){return("function"==typeof n.isUnhandledError?n.isUnhandledError(e):e instanceof Error||"string"==typeof e)&&j(t,e),!1}))},C=function(e){return"object"===y(e)&&Object.hasOwn(e,"name")&&"NotAuthorizedException"===e.name},U=function(e){return"object"===y(e)&&Object.hasOwn(e,"name")&&"CodeMismatchException"===e.name},x=function(e){return Object.entries(e).reduce((function(e,t){var r=m(t,2),o=r[0],u=r[1];return f(f({},e),{},c({},function(e){return"telephoneNumber"===e?"phone_number":n.assertNever(e)}(o),u))}),{})},M=function(e){return f(f({},e),{},{emailMfaAccepted:e.emailMfaAccepted?new Date(e.emailMfaAccepted):void 0})},k=function(e,t){var n=q("requestStatuses");Q({requestStatuses:f(f({},n),{},c({},e,t))})},F=new Map,D=function(){F.clear()},H=function(e){t.Hub.dispatch("custom",{data:{challenge:e},event:"challenge"})},j=function(e,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"custom";F.get(e)===n||(F.set(e,n),t.Hub.dispatch(r,{data:{errorDetails:n,errorType:e},event:"error"}))},G=function(e,n){t.Hub.dispatch("custom",{data:n,event:e})},L=function(t){G(e.AuthAction.INIT,{session:t})},W=function(){var e=a(b().mark((function e(){var n,r,o;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return n=e.sent,r=n.getIdToken(),e.next=6,fetch("".concat(q("usersEndpoint")).concat(r.payload.sub,"/mfa/secret"),{headers:{Authorization:r.getJwtToken()}}).then((function(e){return e.json()})).then((function(e){return e.secret}));case 6:return o=e.sent,e.abrupt("return",o);case 8:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),V=function(){var e=a(b().mark((function e(){var n,r,o;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return n=e.sent,r=n.getIdToken(),e.next=6,fetch("".concat(q("usersEndpoint")).concat(r.payload.sub,"/preferences"),{headers:{Authorization:r.getJwtToken()}}).then((function(e){return null==e?void 0:e.json()}));case 6:return o=e.sent,e.abrupt("return",o||{});case 8:case"end":return e.stop()}}),e)})));return function(){return e.apply(this,arguments)}}(),Y=function(){var e=a(b().mark((function e(n){var r,o,u;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return r=e.sent,o=r.getIdToken(),e.next=6,fetch("".concat(q("usersEndpoint")).concat(o.payload.sub,"/mfa"),{body:JSON.stringify({mfaType:n}),headers:{Authorization:o.getJwtToken()},method:"POST"}).then((function(e){if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0}));case 6:return u=e.sent,e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),z=function(){var e=a(b().mark((function e(n){var r,o,u;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return r=e.sent,o=r.getIdToken(),e.next=6,fetch("".concat(q("usersEndpoint")).concat(o.payload.sub),{body:JSON.stringify(n),headers:{Authorization:o.getJwtToken()},method:"POST"}).then((function(e){if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0}));case 6:return u=e.sent,e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),B=function(){var e=a(b().mark((function e(n){var r,o,u;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return r=e.sent,o=r.getIdToken(),e.next=6,fetch("".concat(q("usersEndpoint")).concat(o.payload.sub,"/preferences"),{body:JSON.stringify(n),headers:{Authorization:o.getJwtToken()},method:"POST"}).then((function(e){return e.json()})).then((function(e){return e.success}));case 6:return u=e.sent,e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),K=function(){var e=a(b().mark((function e(n){var r,o,u;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return r=e.sent,o=r.getIdToken(),e.next=6,fetch("".concat(q("usersEndpoint")).concat(o.payload.sub,"/mfa/secret"),{body:JSON.stringify({totpCode:n}),headers:{Authorization:o.getJwtToken()},method:"POST"}).then((function(e){if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0}));case 6:return u=e.sent,e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),J={centrePermissions:{},debug:!1,emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,name:void 0,givenName:void 0,groups:[],id:void 0,isInitialized:!1,isSignedIn:!1,languagePreference:"en-gb",mfaType:void 0,middleName:void 0,preferences:{},provider:void 0,requestStatuses:Object.values(e.AuthAction).reduce((function(e,t){return f(f({},e),{},c({},t,"idle"))}),{}),telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1,usersEndpoint:void 0},q=function(e){return J[e]},Q=function(e){var n=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];Object.entries(e).forEach((function(e){var t=m(e,2),n=t[0],r=t[1];J[n]=r})),n&&t.Hub.dispatch("custom",{data:{updatedTimestamp:Date.now()},event:"cacheUpdated"})},Z=function(){var t=a(b().mark((function t(n){var r,o,u,i,a,c;return b().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return i=n.getIdToken().payload,k(e.AuthAction.GET_USER_PREFERENCES,"loading"),t.next=4,V().then((function(t){return k(e.AuthAction.GET_USER_PREFERENCES,"done"),t})).catch((function(t){return j(e.AuthAction.GET_USER_PREFERENCES,t),{}}));case 4:a=t.sent,c=M(a),Q({centrePermissions:JSON.parse(i["custom:centre_permissions"]||"{}"),emailAddress:i.email,emailAddressVerified:i.email_verified,familyName:i.family_name||i.familyName,name:i.name,givenName:i.given_name||i.givenName,groups:i["cognito:groups"]||[],id:i.sub,isInitialized:!0,isSignedIn:!0,languagePreference:i["custom:language_preference"],mfaType:i["custom:mfa_type"],middleName:i.middle_name||i.middleName,preferences:c,provider:(null===(r=i.identities)||void 0===r||null===(o=r[0])||void 0===o?void 0:o.providerName)||"Cognito",telephoneNumber:i.phone_number,telephoneNumberVerified:i.phone_number_verified,totpVerified:"true"===(null===(u=i["custom:totp_verified"])||void 0===u?void 0:u.toLowerCase())});case 7:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),X=function(){Q({centrePermissions:{},emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,name:void 0,givenName:void 0,groups:[],id:void 0,isSignedIn:!1,languagePreference:void 0,mfaType:void 0,middleName:void 0,preferences:{},provider:void 0,telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1})},$=function(n){return function(r){var o;return k(e.AuthAction.FORCE_CHANGE_PASSWORD,"loading"),new Promise((function(u,i){n.completeNewPasswordChallenge(r,void 0,{onSuccess:function(n){!function(e,n){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:"custom";t.Hub.dispatch(r,{data:n,event:e})}(e.AuthAction.FORCE_CHANGE_PASSWORD,n,"auth"),k(e.AuthAction.FORCE_CHANGE_PASSWORD,"done"),u(o)},onFailure:function(t){j(e.AuthAction.FORCE_CHANGE_PASSWORD,t,"auth"),i(t)},customChallenge:function(t){o=te(e.ChallengeType.CUSTOM_CHALLENGE,n,t),H(o),k(e.AuthAction.FORCE_CHANGE_PASSWORD,"done"),u(o)}})}))}},ee=function(n){return function(){var r=a(b().mark((function r(o){var u;return b().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:return k(e.AuthAction.SUBMIT_MFA_CODE,"loading"),r.next=3,t.Auth.sendCustomChallengeAnswer(n,o).then(function(){var t=a(b().mark((function t(r){return b().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(null==r||!r.challengeName){t.next=5;break}return t.next=3,te(r.challengeName,n,r.challengeParam);case 3:u=t.sent,H(u);case 5:k(e.AuthAction.SUBMIT_MFA_CODE,"done");case 6:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}()).catch((function(t){U(t)||j(e.AuthAction.SUBMIT_MFA_CODE,t)}));case 3:return r.abrupt("return",u);case 4:case"end":return r.stop()}}),r)})));return function(e){return r.apply(this,arguments)}}()},te=function(n,r){var o,u,i=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},c=ne(n,null==i?void 0:i.selectedMfaType);switch(c){case e.ChallengeType.EMAIL_MFA:o={attemptNumber:i.attemptNumber?parseInt(i.attemptNumber):void 0,challengeType:e.ChallengeType.EMAIL_MFA,completeChallenge:ee(r),emailAddress:i.emailAddress};break;case e.ChallengeType.NEW_PASSWORD_REQUIRED:o={challengeType:e.ChallengeType.NEW_PASSWORD_REQUIRED,completeChallenge:$(r)};break;case e.ChallengeType.SMS_MFA:o={attemptNumber:i.attemptNumber?parseInt(i.attemptNumber):void 0,challengeType:c,completeChallenge:ee(r),telephoneNumber:n===e.ChallengeType.CUSTOM_CHALLENGE?i.redactedPhoneNumber:i.CODE_DELIVERY_DESTINATION};break;case e.ChallengeType.TOTP_MFA:o={attemptNumber:i.attemptNumber?parseInt(i.attemptNumber):void 0,challengeType:c,completeChallenge:ee(r)};break;case e.ChallengeType.VERIFY_TELEPHONE_NUMBER:o={challengeType:c,completeChallenge:(u=i.currentMfaType,function(){var n=a(b().mark((function n(r){var o;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return k(e.AuthAction.VERIFY_TELEPHONE_NUMBER,"loading"),n.next=3,I(t.Auth.verifyCurrentUserAttributeSubmit("phone_number",r),e.AuthAction.VERIFY_TELEPHONE_NUMBER);case 3:if(!(o=n.sent)){n.next=9;break}if(Q({telephoneNumberVerified:!0}),q("mfaType")===e.MFAType.SMS||u!==e.MFAType.SMS){n.next=9;break}return n.next=9,de(e.MFAType.SMS,!0);case 9:return n.abrupt("return",o);case 10:case"end":return n.stop()}}),n)})));return function(e){return n.apply(this,arguments)}}())};break;case e.ChallengeType.VERIFY_TOTP_TOKEN:o={challengeType:c,completeChallenge:function(){var e=a(b().mark((function e(t){var n;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,ye(t);case 2:return n=e.sent,e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),secretKey:i.secretKey}}return o},ne=function(t,n){if(t===e.ChallengeType.CUSTOM_CHALLENGE)switch(n){case e.MFAType.EMAIL:return e.ChallengeType.EMAIL_MFA;case e.MFAType.SMS:return e.ChallengeType.SMS_MFA;case e.MFAType.TOTP:return e.ChallengeType.TOTP_MFA}return t},re=["amplifyConfig","debug","oAuthConfig","usersEndpoint"],oe=!1,ue=["telephoneNumber"],ie=w(function(){var n=a(b().mark((function n(r,o){var u;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return k(e.AuthAction.CHANGE_PASSWORD,"loading"),n.next=3,t.Auth.currentAuthenticatedUser();case 3:return u=n.sent,n.abrupt("return",I(t.Auth.changePassword(u,r,o),e.AuthAction.CHANGE_PASSWORD));case 5:case"end":return n.stop()}}),n)})));return function(e,t){return n.apply(this,arguments)}}(),e.AuthAction.CHANGE_PASSWORD),ae=w((function(n,r,o){return k(e.AuthAction.COMPLETE_PASSWORD_RESET,"loading"),I(t.Auth.forgotPasswordSubmit(n,r,o),e.AuthAction.COMPLETE_PASSWORD_RESET,{isUnhandledError:function(e){return!U(e)}})}),e.AuthAction.COMPLETE_PASSWORD_RESET),ce=w((function(e){t.Auth.federatedSignIn({customProvider:e})}),e.AuthAction.SIGN_IN),se=w(a(b().mark((function e(){var t,n;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,fe();case 2:return t=e.sent,n=t.getIdToken().getJwtToken(),e.abrupt("return",n);case 5:case"end":return e.stop()}}),e)}))),e.AuthAction.GET_AUTHORIZATION_TOKEN),fe=w(a(b().mark((function e(){var n;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.Auth.currentSession();case 2:return n=e.sent,e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)}))),e.AuthAction.GET_USER_SESSION),le=function(){var r=a(b().mark((function r(o,u,i){var c,s,f,l;return b().wrap((function(r){for(;;)switch(r.prev=r.next){case 0:if(s=(c="string"==typeof i?{debug:!1,usersEndpoint:i}:i).debug,f=c.usersEndpoint,!q("isInitialized")){r.next=5;break}n.developerWarning("WJEC One Authorization: Config cannot be changed after initialization - the config that was originally provided will continue to be used"),r.next=24;break;case 5:if(o&&u&&f){r.next=9;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 9:return r.prev=9,t.Amplify.configure(o),t.Auth.configure({oauth:u}),Q({debug:s,usersEndpoint:f},!1),t.Hub.listen("auth",function(){var t=a(b().mark((function t(n){var r,o,u;return b().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:r=n.payload,o=r.data,u=r.event,t.t0=u,t.next="completeNewPassword_failure"===t.t0?4:"forgotPassword_failure"===t.t0?7:"forgotPasswordSubmit_failure"===t.t0?10:"signIn"===t.t0?13:"signIn_failure"===t.t0?17:"signOut"===t.t0?20:"signOut_failure"===t.t0?22:"tokenRefresh_failure"===t.t0?25:28;break;case 4:return F.set(e.AuthAction.FORCE_CHANGE_PASSWORD,o),k(e.AuthAction.FORCE_CHANGE_PASSWORD,"failed"),t.abrupt("break",28);case 7:return F.set(e.AuthAction.REQUEST_PASSWORD_RESET,o),k(e.AuthAction.REQUEST_PASSWORD_RESET,"failed"),t.abrupt("break",28);case 10:return F.set(e.AuthAction.COMPLETE_PASSWORD_RESET,o),k(e.AuthAction.COMPLETE_PASSWORD_RESET,"failed"),t.abrupt("break",28);case 13:return t.next=15,Z(o.signInUserSession);case 15:return G(e.AuthAction.SIGN_IN,{session:o.signInUserSession}),t.abrupt("break",28);case 17:return F.set(e.AuthAction.SIGN_IN,o),k(e.AuthAction.SIGN_IN,"failed"),t.abrupt("break",28);case 20:return X(),t.abrupt("break",28);case 22:return F.set(e.AuthAction.SIGN_OUT,o),k(e.AuthAction.SIGN_OUT,"failed"),t.abrupt("break",28);case 25:return F.set(e.AuthAction.REFRESH_USER_SESSION,o),k(e.AuthAction.REFRESH_USER_SESSION,"failed"),t.abrupt("break",28);case 28:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}()),t.Hub.listen("custom",(function(e){var t=e.payload,n=t.data;if("error"===t.event){var r=n.errorType;"loading"===q("requestStatuses")[r]&&k(r,"failed")}})),r.next=16,t.Auth.currentAuthenticatedUser({bypassCache:!0}).then(function(){var e=a(b().mark((function e(t){var n;return b().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.getSignInUserSession(),e.next=3,Z(n);case 3:return e.abrupt("return",n);case 4:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}()).catch((function(){}));case 16:(l=r.sent)||Q({isInitialized:!0}),L(l),r.next=24;break;case 21:r.prev=21,r.t0=r.catch(9),j(e.AuthAction.INIT,r.t0);case 24:return r.abrupt("return",l);case 25:case"end":return r.stop()}}),r,null,[[9,21]])})));return function(e,t,n){return r.apply(this,arguments)}}(),pe=w(a(b().mark((function n(){var r,o,u;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,t.Auth.currentSession();case 2:return r=n.sent,n.next=5,t.Auth.currentAuthenticatedUser();case 5:return o=n.sent,n.next=8,new Promise((function(t,n){try{o.refreshSession(r.getRefreshToken(),(function(e,n){if(e)throw e;t(n)}))}catch(t){j(e.AuthAction.REFRESH_USER_SESSION,t),n(t)}}));case 8:return u=n.sent,n.abrupt("return",u);case 10:case"end":return n.stop()}}),n)}))),e.AuthAction.REFRESH_USER_SESSION),he=w((function(n){return k(e.AuthAction.REQUEST_PASSWORD_RESET,"loading"),I(t.Auth.forgotPassword(n),e.AuthAction.REQUEST_PASSWORD_RESET)}),e.AuthAction.REQUEST_PASSWORD_RESET),de=w(function(){var t=a(b().mark((function t(n){var r,o,u=arguments;return b().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=u.length>1&&void 0!==u[1]&&u[1],k(e.AuthAction.SET_MFA_TYPE,"loading"),t.next=4,Y(n).catch((function(t){return j(e.AuthAction.SET_MFA_TYPE,t),!1}));case 4:return(o=t.sent)&&(Q({mfaType:n},r),G(e.AuthAction.SET_MFA_TYPE,{mfaType:n}),k(e.AuthAction.SET_MFA_TYPE,"done"),n!==e.MFAType.EMAIL&&me({emailMfaAccepted:void 0})),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),e.AuthAction.SET_MFA_TYPE),Ae=w(a(b().mark((function n(){var r,o,u,i;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return k(e.AuthAction.REQUEST_TOTP_TOKEN,"loading"),o=q("mfaType"),n.next=4,t.Auth.currentAuthenticatedUser();case 4:if(u=n.sent,o!==e.MFAType.TOTP){n.next=8;break}return n.next=8,de(e.MFAType.EMAIL);case 8:return n.next=10,W().catch((function(t){j(e.AuthAction.REQUEST_TOTP_TOKEN,t)}));case 10:if(!(i=n.sent)){n.next=18;break}return n.next=14,te(e.ChallengeType.VERIFY_TOTP_TOKEN,u,{secretKey:i});case 14:r=n.sent,H(r),Q({totpVerified:!1},!1),k(e.AuthAction.REQUEST_TOTP_TOKEN,"done");case 18:return n.abrupt("return",{challenge:r,secretKey:i});case 19:case"end":return n.stop()}}),n)}))),e.AuthAction.REQUEST_TOTP_TOKEN),Ee=w(function(){var n=a(b().mark((function n(r,o){var u,i,a;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return k(e.AuthAction.SIGN_IN,"loading"),n.next=3,t.Auth.signIn(r,o).catch((function(t){C(t)||j(e.AuthAction.SIGN_IN,t)}));case 3:if(!(i=n.sent)){n.next=16;break}if(!i.challengeName){n.next=15;break}if((null===(a=i.challengeParam)||void 0===a?void 0:a.selectedMfaType)!==e.MFAType.NONE){n.next=11;break}return n.next=9,t.Auth.sendCustomChallengeAnswer(i,"N/A").catch((function(t){j(e.AuthAction.SIGN_IN,t)}));case 9:n.next=15;break;case 11:return n.next=13,te(i.challengeName,i,i.challengeParam);case 13:u=n.sent,H(u);case 15:k(e.AuthAction.SIGN_IN,"done");case 16:return n.abrupt("return",{challenge:u,user:i});case 17:case"end":return n.stop()}}),n)})));return function(e,t){return n.apply(this,arguments)}}(),e.AuthAction.SIGN_IN),Se=w((function(){return k(e.AuthAction.SIGN_OUT,"loading"),I(t.Auth.signOut(),e.AuthAction.SIGN_OUT)}),e.AuthAction.SIGN_OUT),Te=w(function(){var n=a(b().mark((function n(r){var o,u,i,a,c,s,f;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:if(o=r.telephoneNumber,u=l(r,ue),i=!0,k(e.AuthAction.UPDATE_USER_ATTRIBUTES,"loading"),!Object.hasOwn(r,"telephoneNumber")){n.next=13;break}return a=q("telephoneNumber"),c=q("telephoneNumberVerified"),n.next=8,t.Auth.currentAuthenticatedUser();case 8:return s=n.sent,n.next=11,I(t.Auth.updateUserAttributes(s,x({telephoneNumber:o})),e.AuthAction.UPDATE_USER_ATTRIBUTES,{updateStatus:!1});case 11:(i=n.sent)&&Q({telephoneNumber:o,telephoneNumberVerified:o===a&&c},!1);case 13:return n.next=15,z(u);case 15:return(f=n.sent)&&Q(u,!1),k(e.AuthAction.UPDATE_USER_ATTRIBUTES,i&&f?"done":"failed"),n.abrupt("return",i&&f);case 19:case"end":return n.stop()}}),n)})));return function(e){return n.apply(this,arguments)}}(),e.AuthAction.UPDATE_USER_ATTRIBUTES),me=w(function(){var t=a(b().mark((function t(n){var r,o;return b().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return k(e.AuthAction.UPDATE_USER_PREFERENCES,"loading"),r=f(f({},q("preferences")),n),t.next=4,B(r).catch((function(t){return j(e.AuthAction.UPDATE_USER_PREFERENCES,t),!1}));case 4:return(o=t.sent)&&(Q({preferences:r},!1),k(e.AuthAction.UPDATE_USER_PREFERENCES,"done")),t.abrupt("return",o);case 7:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),e.AuthAction.UPDATE_USER_PREFERENCES),ve=w(a(b().mark((function n(){var r,o,u;return b().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return k(e.AuthAction.VERIFY_TELEPHONE_NUMBER,"loading"),n.next=3,t.Auth.currentAuthenticatedUser();case 3:return o=n.sent,u=q("mfaType"),n.next=7,I(t.Auth.verifyCurrentUserAttribute("phone_number"),e.AuthAction.VERIFY_TELEPHONE_NUMBER);case 7:if(!n.sent){n.next=17;break}return n.next=11,te(e.ChallengeType.VERIFY_TELEPHONE_NUMBER,o,{currentMfaType:u});case 11:if(r=n.sent,u!==e.MFAType.SMS){n.next=15;break}return n.next=15,de(e.MFAType.EMAIL,!0);case 15:H(r),k(e.AuthAction.VERIFY_TELEPHONE_NUMBER,"done");case 17:return n.abrupt("return",r);case 18:case"end":return n.stop()}}),n)}))),e.AuthAction.VERIFY_TELEPHONE_NUMBER),ye=w(function(){var t=a(b().mark((function t(n){var r;return b().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return k(e.AuthAction.VERIFY_TOTP_TOKEN,"loading"),t.next=3,K(n).catch((function(t){return j(e.AuthAction.VERIFY_TOTP_TOKEN,t),!1}));case 3:return(r=t.sent)&&(Q({totpVerified:!0},!1),k(e.AuthAction.VERIFY_TOTP_TOKEN,"done")),t.abrupt("return",r);case 6:case"end":return t.stop()}}),t)})));return function(e){return t.apply(this,arguments)}}(),e.AuthAction.VERIFY_TOTP_TOKEN),_e=Object.freeze({get centrePermissions(){return q("centrePermissions")},get emailAddress(){return q("emailAddress")},get emailAddressVerified(){return q("emailAddressVerified")},get familyName(){return q("familyName")},get name(){return q("name")},get givenName(){return q("givenName")},get groups(){return q("groups")},get id(){return q("id")},get isInitialized(){return q("isInitialized")},get isSignedIn(){return q("isSignedIn")},get languagePreference(){return q("languagePreference")},get mfaType(){return q("mfaType")},get middleName(){return q("middleName")},get preferences(){return q("preferences")},get provider(){return q("provider")},get requestStatuses(){return q("requestStatuses")},get telephoneNumber(){return q("telephoneNumber")},get telephoneNumberVerified(){return q("telephoneNumberVerified")},get totpVerified(){return q("totpVerified")}});e.changePassword=ie,e.completePasswordReset=ae,e.externalSignIn=ce,e.getAuthorizationToken=se,e.getUserSession=fe,e.init=le,e.refreshUserSession=pe,e.requestPasswordReset=he,e.setMfaType=de,e.setupTotp=Ae,e.signIn=Ee,e.signOut=Se,e.state=_e,e.updateUserAttributes=Te,e.updateUserPreferences=me,e.useAuthorization=function(){var o=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},u=o.amplifyConfig,i=o.debug,a=o.oAuthConfig,c=o.usersEndpoint,s=l(o,re),p=m(r.useState(Date.now()),2);p[0];var h=p[1],d=r.useRef(!1),A=r.useRef(f({},s));r.useEffect((function(){A.current=f({},s)}),v(Object.values(s)));var E=function(e){if(n.isCallableFunction(A.current[e])){for(var t,r=arguments.length,o=new Array(r>1?r-1:0),u=1;u<r;u++)o[u-1]=arguments[u];(t=A.current[e]).call.apply(t,[void 0].concat(o))}};return r.useEffect((function(){if(u||a||c){try{le(u,a,{debug:i,usersEndpoint:c}),d.current=!0,oe=!0}catch(t){E("onError",e.AuthAction.INIT)}oe&&!d.current&&n.developerWarning("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)")}}),[u,a,c]),r.useEffect((function(){var r=!1,o=t.Hub.listen("auth",(function(t){var o=t.payload,u=o.data,i=o.event;if(!r)switch(i){case"completeNewPassword_failure":E("onError",e.AuthAction.FORCE_CHANGE_PASSWORD,u);break;case"forgotPassword":E("onRequestPasswordReset",u.username);break;case"forgotPassword_failure":E("onError",e.AuthAction.REQUEST_PASSWORD_RESET,u);break;case"forgotPasswordSubmit":E("onCompletePasswordReset");break;case"forgotPasswordSubmit_failure":E("onError",e.AuthAction.COMPLETE_PASSWORD_RESET,u);break;case"signIn_failure":E("onError",e.AuthAction.SIGN_IN,u);break;case"signOut":E("onSignOut");break;case"tokenRefresh":n.isCallableFunction(A.current.onRefreshSession)&&fe().then(A.current.onRefreshSession);break;case"tokenRefresh_failure":E("onError",e.AuthAction.REFRESH_USER_SESSION,u)}})),u=t.Hub.listen("custom",(function(t){var n=t.payload,o=n.data,u=n.event;if(!r)switch(u){case"cacheUpdated":h(o.updatedTimestamp);break;case"challenge":E("onChallenge",o.challenge);break;case"error":E("onError",o.errorType,o.errorDetails);break;case"init":E("onInit",o.session);break;case"setMfaType":E("onSetMfaType",o.mfaType);break;case e.AuthAction.SIGN_IN:E("onSignIn",o.session)}}));return function(){r=!0,o(),u()}}),[]),_e},e.verifyTelephoneNumber=ve,e.verifyTotp=ye}));
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("aws-amplify"),require("wjec-one/utils"),require("react")):"function"==typeof define&&define.amd?define(["exports","aws-amplify","wjec-one/utils","react"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).WJECOneAuth={},e.aws_amplify,e.WJECOneUtils,e.React)}(this,function(e,t,n,r){"use strict";var a,i,o,s,u;e.AuthAction=void 0,(a=e.AuthAction||(e.AuthAction={})).CHANGE_PASSWORD="changePassword",a.COMPLETE_PASSWORD_RESET="completePasswordReset",a.FORCE_CHANGE_PASSWORD="forceChangePassword",a.GET_AUTHORIZATION_TOKEN="getAuthorizationToken",a.GET_USER_PREFERENCES="getUserPreferences",a.GET_USER_SESSION="getUserSession",a.INIT="init",a.REFRESH_USER_SESSION="refreshUserSession",a.REQUEST_PASSWORD_RESET="requestPasswordReset",a.REQUEST_TOTP_TOKEN="requestSoftwareToken",a.SET_MFA_TYPE="setMfaType",a.SIGN_IN="signIn",a.SIGN_OUT="signOut",a.SUBMIT_MFA_CODE="confirmSignIn",a.UPDATE_USER_ATTRIBUTES="updateUserAttributes",a.UPDATE_USER_PREFERENCES="updateUserPreferences",a.VERIFY_TELEPHONE_NUMBER="verifyTelephoneNumber",a.VERIFY_TOTP_TOKEN="verifySoftwareToken",e.ChallengeType=void 0,(i=e.ChallengeType||(e.ChallengeType={})).CUSTOM_CHALLENGE="CUSTOM_CHALLENGE",i.EMAIL_MFA="EMAIL",i.MFA_SETUP="MFA_SETUP",i.NEW_PASSWORD_REQUIRED="NEW_PASSWORD_REQUIRED",i.SMS_MFA="SMS",i.TOTP_MFA="TOTP",i.VERIFY_TELEPHONE_NUMBER="VERIFY_TELEPHONE_NUMBER",i.VERIFY_TOTP_TOKEN="VERIFY_TOTP_TOKEN",e.MFAType=void 0,(o=e.MFAType||(e.MFAType={})).EMAIL="EMAIL",o.NONE="NONE",o.SMS="SMS",o.TOTP="TOTP",e.Role=void 0,(s=e.Role||(e.Role={})).ADMINISTRATIVE_SUPPORT="administrativeSupport",s.EXAMS_OFFICER="examsOfficer",s.HEAD_OF_DEPARTMENT="headOfDepartment",s.HEAD_OF_YEAR="headOfYear",s.HEAD_TEACHER="headTeacher",s.LOCAL_AUTHORITY_ADVISOR="localAuthorityAdvisor",s.OTHER="other",s.STAKEHOLDER="stakeholder",s.TEACHER="teacher",s.TUTOR="tutor",e.Subject=void 0,(u=e.Subject||(e.Subject={})).ENGLISH="english",u.MATHS="maths",u.SCIENCE="science";const c=(e,r)=>(...a)=>{let i;return m("isInitialized")?(i=e.call(t.Auth,...a),i instanceof Promise&&i.catch(e=>{_(r,e)}).finally(()=>{T()})):n.developerError(`WJEC One Authorization: You tried to call ${r}() before AWS Amplify was initialized. Please provide values for amplifyConfig & oAuthConfig to get set up`),i},E=(e,t,n={})=>e.then(()=>(!1!==n.updateStatus&&d(t,"done"),!0)).catch(e=>(("function"==typeof n.isUnhandledError?n.isUnhandledError(e):e instanceof Error||"string"==typeof e)&&_(t,e),!1)),A=e=>"object"==typeof e&&Object.hasOwn(e,"name")&&"CodeMismatchException"===e.name,h=e=>"telephoneNumber"===e?"phone_number":n.assertNever(e),d=(e,t)=>{const n=m("requestStatuses");g({requestStatuses:{...n,[e]:t}})},l=new Map,T=()=>{l.clear()},S=e=>{t.Hub.dispatch("custom",{data:{challenge:e},event:"challenge"})},_=(e,n,r="custom")=>{l.get(e)===n||(l.set(e,n),t.Hub.dispatch(r,{data:{errorDetails:n,errorType:e},event:"error"}))},p=(e,n)=>{t.Hub.dispatch("custom",{data:n,event:e})},f={centrePermissions:{},debug:!1,emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,name:void 0,givenName:void 0,groups:[],id:void 0,isInitialized:!1,isSignedIn:!1,languagePreference:"en-gb",mfaType:void 0,middleName:void 0,preferences:{},provider:void 0,requestStatuses:Object.values(e.AuthAction).reduce((e,t)=>({...e,[t]:"idle"}),{}),telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1,usersEndpoint:void 0},m=e=>f[e],g=(e,n=!0)=>{Object.entries(e).forEach(([e,t])=>{f[e]=t}),n&&t.Hub.dispatch("custom",{data:{updatedTimestamp:Date.now()},event:"cacheUpdated"})},R=async n=>{const r=n.getIdToken().payload;d(e.AuthAction.GET_USER_PREFERENCES,"loading");const a=(e=>({...e,emailMfaAccepted:e.emailMfaAccepted?new Date(e.emailMfaAccepted):void 0}))(await(async()=>{const e=(await t.Auth.currentSession()).getIdToken();return await fetch(`${m("usersEndpoint")}${e.payload.sub}/preferences`,{headers:{Authorization:e.getJwtToken()}}).then(e=>e?.json())||{}})().then(t=>(d(e.AuthAction.GET_USER_PREFERENCES,"done"),t)).catch(t=>(_(e.AuthAction.GET_USER_PREFERENCES,t),{})));g({centrePermissions:JSON.parse(r["custom:centre_permissions"]||"{}"),emailAddress:r.email,emailAddressVerified:r.email_verified,familyName:r.family_name||r.familyName,name:r.name,givenName:r.given_name||r.givenName,groups:r["cognito:groups"]||[],id:r.sub,isInitialized:!0,isSignedIn:!0,languagePreference:r["custom:language_preference"],mfaType:r["custom:mfa_type"],middleName:r.middle_name||r.middleName,preferences:a,provider:r.identities?.[0]?.providerName||"Cognito",telephoneNumber:r.phone_number,telephoneNumberVerified:r.phone_number_verified,totpVerified:"true"===r["custom:totp_verified"]?.toLowerCase()})},N=()=>{g({centrePermissions:{},emailAddress:void 0,emailAddressVerified:!1,familyName:void 0,name:void 0,givenName:void 0,groups:[],id:void 0,isSignedIn:!1,languagePreference:void 0,mfaType:void 0,middleName:void 0,preferences:{},provider:void 0,telephoneNumber:void 0,telephoneNumberVerified:!1,totpVerified:!1})},O=n=>r=>{let a;return d(e.AuthAction.FORCE_CHANGE_PASSWORD,"loading"),new Promise((i,o)=>{n.completeNewPasswordChallenge(r,void 0,{onSuccess:n=>{((e,n,r="custom")=>{t.Hub.dispatch(r,{data:n,event:e})})(e.AuthAction.FORCE_CHANGE_PASSWORD,n,"auth"),d(e.AuthAction.FORCE_CHANGE_PASSWORD,"done"),i(a)},onFailure:t=>{_(e.AuthAction.FORCE_CHANGE_PASSWORD,t,"auth"),o(t)},customChallenge:t=>{a=P(e.ChallengeType.CUSTOM_CHALLENGE,n,t),S(a),d(e.AuthAction.FORCE_CHANGE_PASSWORD,"done"),i(a)}})})},y=n=>async r=>{let a;return d(e.AuthAction.SUBMIT_MFA_CODE,"loading"),await t.Auth.sendCustomChallengeAnswer(n,r).then(async t=>{t?.challengeName&&(a=await P(t.challengeName,n,t.challengeParam),S(a)),d(e.AuthAction.SUBMIT_MFA_CODE,"done")}).catch(t=>{A(t)||_(e.AuthAction.SUBMIT_MFA_CODE,t)}),a},P=(n,r,a={})=>{const i=I(n,a?.selectedMfaType);let o;switch(i){case e.ChallengeType.EMAIL_MFA:o={attemptNumber:a.attemptNumber?parseInt(a.attemptNumber):void 0,challengeType:e.ChallengeType.EMAIL_MFA,completeChallenge:y(r),emailAddress:a.emailAddress};break;case e.ChallengeType.NEW_PASSWORD_REQUIRED:o={challengeType:e.ChallengeType.NEW_PASSWORD_REQUIRED,completeChallenge:O(r)};break;case e.ChallengeType.SMS_MFA:o={attemptNumber:a.attemptNumber?parseInt(a.attemptNumber):void 0,challengeType:i,completeChallenge:y(r),telephoneNumber:n===e.ChallengeType.CUSTOM_CHALLENGE?a.redactedPhoneNumber:a.CODE_DELIVERY_DESTINATION};break;case e.ChallengeType.TOTP_MFA:o={attemptNumber:a.attemptNumber?parseInt(a.attemptNumber):void 0,challengeType:i,completeChallenge:y(r)};break;case e.ChallengeType.VERIFY_TELEPHONE_NUMBER:o={challengeType:i,completeChallenge:(s=a.currentMfaType,async n=>{d(e.AuthAction.VERIFY_TELEPHONE_NUMBER,"loading");const r=await E(t.Auth.verifyCurrentUserAttributeSubmit("phone_number",n),e.AuthAction.VERIFY_TELEPHONE_NUMBER);return r&&(g({telephoneNumberVerified:!0}),m("mfaType")!==e.MFAType.SMS&&s===e.MFAType.SMS&&await k(e.MFAType.SMS,!0)),r})};break;case e.ChallengeType.VERIFY_TOTP_TOKEN:o={challengeType:i,completeChallenge:async e=>await B(e),secretKey:a.secretKey}}var s;return o},I=(t,n)=>{if(t===e.ChallengeType.CUSTOM_CHALLENGE)switch(n){case e.MFAType.EMAIL:return e.ChallengeType.EMAIL_MFA;case e.MFAType.SMS:return e.ChallengeType.SMS_MFA;case e.MFAType.TOTP:return e.ChallengeType.TOTP_MFA}return t};let b=!1;const C=c(async(n,r)=>{d(e.AuthAction.CHANGE_PASSWORD,"loading");const a=await t.Auth.currentAuthenticatedUser();return E(t.Auth.changePassword(a,n,r),e.AuthAction.CHANGE_PASSWORD)},e.AuthAction.CHANGE_PASSWORD),U=c((n,r,a)=>(d(e.AuthAction.COMPLETE_PASSWORD_RESET,"loading"),E(t.Auth.forgotPasswordSubmit(n,r,a),e.AuthAction.COMPLETE_PASSWORD_RESET,{isUnhandledError:e=>!A(e)})),e.AuthAction.COMPLETE_PASSWORD_RESET),w=c(e=>{t.Auth.federatedSignIn({customProvider:e})},e.AuthAction.SIGN_IN),M=c(async()=>(await F()).getIdToken().getJwtToken(),e.AuthAction.GET_AUTHORIZATION_TOKEN),F=c(async()=>await t.Auth.currentSession(),e.AuthAction.GET_USER_SESSION),v=async(r,a,i)=>{const{debug:o,usersEndpoint:s}="string"==typeof i?{debug:!1,usersEndpoint:i}:i;let u;if(m("isInitialized"))n.developerWarning("WJEC One Authorization: Config cannot be changed after initialization - the config that was originally provided will continue to be used");else{if(!(r&&a&&s))throw new Error("WJEC One Authorization: Both amplifyConfig and oAuthConfig objects must be provided, in addition to the endpoint URL for the users service");try{t.Amplify.configure(r),t.Auth.configure({oauth:a}),g({debug:o,usersEndpoint:s},!1),t.Hub.listen("auth",async({payload:{data:t,event:n}})=>{switch(n){case"completeNewPassword_failure":l.set(e.AuthAction.FORCE_CHANGE_PASSWORD,t),d(e.AuthAction.FORCE_CHANGE_PASSWORD,"failed");break;case"forgotPassword_failure":l.set(e.AuthAction.REQUEST_PASSWORD_RESET,t),d(e.AuthAction.REQUEST_PASSWORD_RESET,"failed");break;case"forgotPasswordSubmit_failure":l.set(e.AuthAction.COMPLETE_PASSWORD_RESET,t),d(e.AuthAction.COMPLETE_PASSWORD_RESET,"failed");break;case"signIn":await R(t.signInUserSession),p(e.AuthAction.SIGN_IN,{session:t.signInUserSession});break;case"signIn_failure":l.set(e.AuthAction.SIGN_IN,t),d(e.AuthAction.SIGN_IN,"failed");break;case"signOut":N();break;case"signOut_failure":l.set(e.AuthAction.SIGN_OUT,t),d(e.AuthAction.SIGN_OUT,"failed");break;case"tokenRefresh_failure":l.set(e.AuthAction.REFRESH_USER_SESSION,t),d(e.AuthAction.REFRESH_USER_SESSION,"failed")}}),t.Hub.listen("custom",({payload:{data:e,event:t}})=>{switch(t){case"error":{const t=e.errorType;"loading"===m("requestStatuses")[t]&&d(t,"failed");break}}}),u=await t.Auth.currentAuthenticatedUser({bypassCache:!0}).then(async e=>{const t=e.getSignInUserSession();return await R(t),t}).catch(()=>{}),u||g({isInitialized:!0}),(t=>{p(e.AuthAction.INIT,{session:t})})(u)}catch(t){_(e.AuthAction.INIT,t)}}return u},D=c(async()=>{const n=await t.Auth.currentSession(),r=await t.Auth.currentAuthenticatedUser();return await new Promise((t,a)=>{try{r.refreshSession(n.getRefreshToken(),(e,n)=>{if(e)throw e;t(n)})}catch(t){_(e.AuthAction.REFRESH_USER_SESSION,t),a(t)}})},e.AuthAction.REFRESH_USER_SESSION),H=c(n=>(d(e.AuthAction.REQUEST_PASSWORD_RESET,"loading"),E(t.Auth.forgotPassword(n),e.AuthAction.REQUEST_PASSWORD_RESET)),e.AuthAction.REQUEST_PASSWORD_RESET),k=c(async(n,r=!1)=>{d(e.AuthAction.SET_MFA_TYPE,"loading");const a=await(async e=>{const n=(await t.Auth.currentSession()).getIdToken();return await fetch(`${m("usersEndpoint")}${n.payload.sub}/mfa`,{body:JSON.stringify({mfaType:e}),headers:{Authorization:n.getJwtToken()},method:"POST"}).then(e=>{if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0})})(n).catch(t=>(_(e.AuthAction.SET_MFA_TYPE,t),!1));return a&&(g({mfaType:n},r),p(e.AuthAction.SET_MFA_TYPE,{mfaType:n}),d(e.AuthAction.SET_MFA_TYPE,"done"),n!==e.MFAType.EMAIL&&Y({emailMfaAccepted:void 0})),a},e.AuthAction.SET_MFA_TYPE),L=c(async()=>{let n;d(e.AuthAction.REQUEST_TOTP_TOKEN,"loading");const r=m("mfaType"),a=await t.Auth.currentAuthenticatedUser();r===e.MFAType.TOTP&&await k(e.MFAType.EMAIL);const i=await(async()=>{const e=(await t.Auth.currentSession()).getIdToken();return await fetch(`${m("usersEndpoint")}${e.payload.sub}/mfa/secret`,{headers:{Authorization:e.getJwtToken()}}).then(e=>e.json()).then(e=>e.secret)})().catch(t=>{_(e.AuthAction.REQUEST_TOTP_TOKEN,t)});return i&&(n=await P(e.ChallengeType.VERIFY_TOTP_TOKEN,a,{secretKey:i}),S(n),g({totpVerified:!1},!1),d(e.AuthAction.REQUEST_TOTP_TOKEN,"done")),{challenge:n,secretKey:i}},e.AuthAction.REQUEST_TOTP_TOKEN),G=c(async(n,r)=>{let a;d(e.AuthAction.SIGN_IN,"loading");const i=await t.Auth.signIn(n,r).catch(t=>{(e=>"object"==typeof e&&Object.hasOwn(e,"name")&&"NotAuthorizedException"===e.name)(t)||_(e.AuthAction.SIGN_IN,t)});return i&&(i.challengeName&&(i.challengeParam?.selectedMfaType===e.MFAType.NONE?await t.Auth.sendCustomChallengeAnswer(i,"N/A").catch(t=>{_(e.AuthAction.SIGN_IN,t)}):(a=await P(i.challengeName,i,i.challengeParam),S(a))),d(e.AuthAction.SIGN_IN,"done")),{challenge:a,user:i}},e.AuthAction.SIGN_IN),W=c(()=>(d(e.AuthAction.SIGN_OUT,"loading"),E(t.Auth.signOut(),e.AuthAction.SIGN_OUT)),e.AuthAction.SIGN_OUT),V=c(async n=>{const{telephoneNumber:r,...a}=n;let i=!0;if(d(e.AuthAction.UPDATE_USER_ATTRIBUTES,"loading"),Object.hasOwn(n,"telephoneNumber")){const n=m("telephoneNumber"),a=m("telephoneNumberVerified"),o=await t.Auth.currentAuthenticatedUser();i=await E(t.Auth.updateUserAttributes(o,(e=>Object.entries(e).reduce((e,[t,n])=>({...e,[h(t)]:n}),{}))({telephoneNumber:r})),e.AuthAction.UPDATE_USER_ATTRIBUTES,{updateStatus:!1}),i&&g({telephoneNumber:r,telephoneNumberVerified:r===n&&a},!1)}const o=await(async e=>{const n=(await t.Auth.currentSession()).getIdToken();return await fetch(`${m("usersEndpoint")}${n.payload.sub}`,{body:JSON.stringify(e),headers:{Authorization:n.getJwtToken()},method:"POST"}).then(e=>{if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0})})(a);return o&&g(a,!1),d(e.AuthAction.UPDATE_USER_ATTRIBUTES,i&&o?"done":"failed"),i&&o},e.AuthAction.UPDATE_USER_ATTRIBUTES),Y=c(async n=>{d(e.AuthAction.UPDATE_USER_PREFERENCES,"loading");const r={...m("preferences"),...n},a=await(async e=>{const n=(await t.Auth.currentSession()).getIdToken();return await fetch(`${m("usersEndpoint")}${n.payload.sub}/preferences`,{body:JSON.stringify(e),headers:{Authorization:n.getJwtToken()},method:"POST"}).then(e=>e.json()).then(e=>e.success)})(r).catch(t=>(_(e.AuthAction.UPDATE_USER_PREFERENCES,t),!1));return a&&(g({preferences:r},!1),d(e.AuthAction.UPDATE_USER_PREFERENCES,"done")),a},e.AuthAction.UPDATE_USER_PREFERENCES),z=c(async()=>{let n;d(e.AuthAction.VERIFY_TELEPHONE_NUMBER,"loading");const r=await t.Auth.currentAuthenticatedUser(),a=m("mfaType");return await E(t.Auth.verifyCurrentUserAttribute("phone_number"),e.AuthAction.VERIFY_TELEPHONE_NUMBER)&&(n=await P(e.ChallengeType.VERIFY_TELEPHONE_NUMBER,r,{currentMfaType:a}),a===e.MFAType.SMS&&await k(e.MFAType.EMAIL,!0),S(n),d(e.AuthAction.VERIFY_TELEPHONE_NUMBER,"done")),n},e.AuthAction.VERIFY_TELEPHONE_NUMBER),B=c(async n=>{d(e.AuthAction.VERIFY_TOTP_TOKEN,"loading");const r=await(async e=>{const n=(await t.Auth.currentSession()).getIdToken();return await fetch(`${m("usersEndpoint")}${n.payload.sub}/mfa/secret`,{body:JSON.stringify({totpCode:e}),headers:{Authorization:n.getJwtToken()},method:"POST"}).then(e=>{if(2!==Math.floor(e.status/100))throw new Error("Request failed");return!0})})(n).catch(t=>(_(e.AuthAction.VERIFY_TOTP_TOKEN,t),!1));return r&&(g({totpVerified:!0},!1),d(e.AuthAction.VERIFY_TOTP_TOKEN,"done")),r},e.AuthAction.VERIFY_TOTP_TOKEN),K=Object.freeze({get centrePermissions(){return m("centrePermissions")},get emailAddress(){return m("emailAddress")},get emailAddressVerified(){return m("emailAddressVerified")},get familyName(){return m("familyName")},get name(){return m("name")},get givenName(){return m("givenName")},get groups(){return m("groups")},get id(){return m("id")},get isInitialized(){return m("isInitialized")},get isSignedIn(){return m("isSignedIn")},get languagePreference(){return m("languagePreference")},get mfaType(){return m("mfaType")},get middleName(){return m("middleName")},get preferences(){return m("preferences")},get provider(){return m("provider")},get requestStatuses(){return m("requestStatuses")},get telephoneNumber(){return m("telephoneNumber")},get telephoneNumberVerified(){return m("telephoneNumberVerified")},get totpVerified(){return m("totpVerified")}});e.changePassword=C,e.completePasswordReset=U,e.externalSignIn=w,e.getAuthorizationToken=M,e.getUserSession=F,e.init=v,e.refreshUserSession=D,e.requestPasswordReset=H,e.setMfaType=k,e.setupTotp=L,e.signIn=G,e.signOut=W,e.state=K,e.updateUserAttributes=V,e.updateUserPreferences=Y,e.useAuthorization=({amplifyConfig:a,debug:i,oAuthConfig:o,usersEndpoint:s,...u}={})=>{const[c,E]=r.useState(Date.now()),A=r.useRef(!1),h=r.useRef({...u});r.useEffect(()=>{h.current={...u}},[...Object.values(u)]);const d=(e,...t)=>{n.isCallableFunction(h.current[e])&&h.current[e].call(void 0,...t)};return r.useEffect(()=>{if(a||o||s){try{v(a,o,{debug:i,usersEndpoint:s}),A.current=!0,b=!0}catch(t){d("onError",e.AuthAction.INIT)}b&&!A.current&&n.developerWarning("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)")}},[a,o,s]),r.useEffect(()=>{let r=!1;const a=t.Hub.listen("auth",({payload:{data:t,event:a}})=>{if(!r)switch(a){case"completeNewPassword_failure":d("onError",e.AuthAction.FORCE_CHANGE_PASSWORD,t);break;case"forgotPassword":d("onRequestPasswordReset",t.username);break;case"forgotPassword_failure":d("onError",e.AuthAction.REQUEST_PASSWORD_RESET,t);break;case"forgotPasswordSubmit":d("onCompletePasswordReset");break;case"forgotPasswordSubmit_failure":d("onError",e.AuthAction.COMPLETE_PASSWORD_RESET,t);break;case"signIn_failure":d("onError",e.AuthAction.SIGN_IN,t);break;case"signOut":d("onSignOut");break;case"tokenRefresh":n.isCallableFunction(h.current.onRefreshSession)&&F().then(h.current.onRefreshSession);break;case"tokenRefresh_failure":d("onError",e.AuthAction.REFRESH_USER_SESSION,t)}}),i=t.Hub.listen("custom",({payload:{data:t,event:n}})=>{if(!r)switch(n){case"cacheUpdated":E(t.updatedTimestamp);break;case"challenge":d("onChallenge",t.challenge);break;case"error":d("onError",t.errorType,t.errorDetails);break;case"init":d("onInit",t.session);break;case"setMfaType":d("onSetMfaType",t.mfaType);break;case e.AuthAction.SIGN_IN:d("onSignIn",t.session)}});return()=>{r=!0,a(),i()}},[]),K},e.verifyTelephoneNumber=z,e.verifyTotp=B});
|