@rent-the-runway/rtr-react-okta-auth
Version:
React hooks library for interacting with Okta. Lock out UI and Routes based on user authentication and the okta-user-groups the user belongs to. Can also lock out UI or Routes based on Claims.
3 lines (2 loc) • 13.5 kB
JavaScript
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t,e=require("react"),n=(t=e)&&"object"==typeof t&&"default"in t?t.default:t,r=require("react-router-dom");function o(t,e,n,r,o,a,i){try{var u=t[a](i),c=u.value}catch(t){return void n(t)}u.done?e(c):Promise.resolve(c).then(r,o)}function a(t){return function(){var e=this,n=arguments;return new Promise((function(r,a){var i=t.apply(e,n);function u(t){o(i,r,a,u,c,"next",t)}function c(t){o(i,r,a,u,c,"throw",t)}u(void 0)}))}}function i(t,e){if(null==t)return{};var n,r,o={},a=Object.keys(t);for(r=0;r<a.length;r++)e.indexOf(n=a[r])>=0||(o[n]=t[n]);return o}function u(t,e){var n=t.map((function(t){return t.toLowerCase().trim()})),r=e.map((function(t){return t.toLowerCase().trim()}));return n.filter((function(t){return r.includes(t)}))}function c(t,e){return u(t,e).length>0}function s(t,e){for(var n=0;n<e.length;n++)if(t.hasOwnProperty(e[n]))return!0;return!1}var h,l=(function(t){var e=function(t){var e=Object.prototype,n=e.hasOwnProperty,r="function"==typeof Symbol?Symbol:{},o=r.iterator||"@@iterator",a=r.asyncIterator||"@@asyncIterator",i=r.toStringTag||"@@toStringTag";function u(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{u({},"")}catch(t){u=function(t,e,n){return t[e]=n}}function c(t,e,n,r){var o=Object.create((e&&e.prototype instanceof l?e:l).prototype),a=new b(r||[]);return o._invoke=function(t,e,n){var r="suspendedStart";return function(o,a){if("executing"===r)throw new Error("Generator is already running");if("completed"===r){if("throw"===o)throw a;return{value:void 0,done:!0}}for(n.method=o,n.arg=a;;){var i=n.delegate;if(i){var u=w(i,n);if(u){if(u===h)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if("suspendedStart"===r)throw r="completed",n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r="executing";var c=s(t,e,n);if("normal"===c.type){if(r=n.done?"completed":"suspendedYield",c.arg===h)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(r="completed",n.method="throw",n.arg=c.arg)}}}(t,n,a),o}function s(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=c;var h={};function l(){}function f(){}function p(){}var m={};m[o]=function(){return this};var d=Object.getPrototypeOf,v=d&&d(d(O([])));v&&v!==e&&n.call(v,o)&&(m=v);var y=p.prototype=l.prototype=Object.create(m);function g(t){["next","throw","return"].forEach((function(e){u(t,e,(function(t){return this._invoke(e,t)}))}))}function x(t,e){var r;this._invoke=function(o,a){function i(){return new e((function(r,i){!function r(o,a,i,u){var c=s(t[o],t,a);if("throw"!==c.type){var h=c.arg,l=h.value;return l&&"object"==typeof l&&n.call(l,"__await")?e.resolve(l.__await).then((function(t){r("next",t,i,u)}),(function(t){r("throw",t,i,u)})):e.resolve(l).then((function(t){h.value=t,i(h)}),(function(t){return r("throw",t,i,u)}))}u(c.arg)}(o,a,r,i)}))}return r=r?r.then(i,i):i()}}function w(t,e){var n=t.iterator[e.method];if(void 0===n){if(e.delegate=null,"throw"===e.method){if(t.iterator.return&&(e.method="return",e.arg=void 0,w(t,e),"throw"===e.method))return h;e.method="throw",e.arg=new TypeError("The iterator does not provide a 'throw' method")}return h}var r=s(n,t.iterator,e.arg);if("throw"===r.type)return e.method="throw",e.arg=r.arg,e.delegate=null,h;var o=r.arg;return o?o.done?(e[t.resultName]=o.value,e.next=t.nextLoc,"return"!==e.method&&(e.method="next",e.arg=void 0),e.delegate=null,h):o:(e.method="throw",e.arg=new TypeError("iterator result is not an object"),e.delegate=null,h)}function C(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function A(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function b(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(C,this),this.reset(!0)}function O(t){if(t){var e=t[o];if(e)return e.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var r=-1,a=function e(){for(;++r<t.length;)if(n.call(t,r))return e.value=t[r],e.done=!1,e;return e.value=void 0,e.done=!0,e};return a.next=a}}return{next:S}}function S(){return{value:void 0,done:!0}}return f.prototype=y.constructor=p,p.constructor=f,f.displayName=u(p,i,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===f||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,p):(t.__proto__=p,u(t,i,"GeneratorFunction")),t.prototype=Object.create(y),t},t.awrap=function(t){return{__await:t}},g(x.prototype),x.prototype[a]=function(){return this},t.AsyncIterator=x,t.async=function(e,n,r,o,a){void 0===a&&(a=Promise);var i=new x(c(e,n,r,o),a);return t.isGeneratorFunction(n)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},g(y),u(y,i,"Generator"),y[o]=function(){return this},y.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var n in t)e.push(n);return e.reverse(),function n(){for(;e.length;){var r=e.pop();if(r in t)return n.value=r,n.done=!1,n}return n.done=!0,n}},t.values=O,b.prototype={constructor:b,reset:function(t){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(A),!t)for(var e in this)"t"===e.charAt(0)&&n.call(this,e)&&!isNaN(+e.slice(1))&&(this[e]=void 0)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var e=this;function r(n,r){return i.type="throw",i.arg=t,e.next=n,r&&(e.method="next",e.arg=void 0),!!r}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],i=a.completion;if("root"===a.tryLoc)return r("end");if(a.tryLoc<=this.prev){var u=n.call(a,"catchLoc"),c=n.call(a,"finallyLoc");if(u&&c){if(this.prev<a.catchLoc)return r(a.catchLoc,!0);if(this.prev<a.finallyLoc)return r(a.finallyLoc)}else if(u){if(this.prev<a.catchLoc)return r(a.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<a.finallyLoc)return r(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===t||"continue"===t)&&a.tryLoc<=e&&e<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=t,i.arg=e,a?(this.method="next",this.next=a.finallyLoc,h):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),h},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),A(n),h}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var o=r.arg;A(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,e,n){return this.delegate={iterator:O(t),resultName:e,nextLoc:n},"next"===this.method&&(this.arg=void 0),h}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}(h={exports:{}}),h.exports),f=e.createContext({user:null,userGroups:[],authorizationStateKnown:!1,authCtx:{}});function p(){var t=e.useContext(f),n=t.user,r=t.userGroups;return{user:n,userGroups:r,authorizationStateKnown:t.authorizationStateKnown,authCtx:t.authCtx,isMemberOf:function(t){return!!n&&c([t],r)},isMemberOfAny:function(t){return!!n&&c(t,r)},isMemberOfAll:function(t){return!!n&&u(e=t,r).length===e.length&&e.length>0;var e},hasClaim:function(t){return!!n&&s(n,[t])},hasAnyClaims:function(t){return!!n&&s(n,t)},hasAllClaims:function(t){return!!n&&(e=n,o=(r=t).filter((function(t){return e.hasOwnProperty(t)})),!!r.length&&o.length===r.length);var e,r,o}}}var m=function(){return p().authCtx.authState.isAuthenticated?null:n.createElement("div",{"data-testid":"default-unauthenticated"},n.createElement("div",null,"Unauthenticated"))},d=function(){return n.createElement("div",{"data-testid":"default-unauthorized"},n.createElement("div",null,"Unauthorized"))},v=function(t){var e=t.groups,o=t.component,a=t.unauthenticatedComponent,u=t.unauthorizedComponent,c=i(t,["groups","component","unauthenticatedComponent","unauthorizedComponent"]),s=p(),h=s.authCtx.authState.isAuthenticated,l=(0,s.isMemberOfAll)(e);return n.createElement(r.Route,Object.assign({},c,{component:h?l?o:u||d:a||m}))},y=function(t){var e=p();return e.authCtx.authState.isAuthenticated&&e.authorizationStateKnown&&(0,e.isMemberOfAll)(t.groups)?t.children:null},g=function(t){var e=p();return e.authorizationStateKnown?e.authCtx.authState.isAuthenticated&&(0,e.isMemberOfAll)(t.groups)?null:t.children:null},x=function(t){var e=p();return e.authCtx.authState.isAuthenticated&&e.authorizationStateKnown&&(0,e.hasAllClaims)(t.claims)?t.children:null},w=function(t){var e=p();return e.authorizationStateKnown?e.authCtx.authState.isAuthenticated&&(0,e.hasAllClaims)(t.claims)?null:t.children:null},C=function(t){var e=t.claims,o=t.component,a=t.unauthenticatedComponent,u=t.unauthorizedComponent,c=i(t,["claims","component","unauthenticatedComponent","unauthorizedComponent"]),s=p(),h=s.authCtx.authState.isAuthenticated,l=(0,s.hasAllClaims)(e);return n.createElement(r.Route,Object.assign({component:h?l?o:u||d:a||m},c))};function A(){var t=p().authCtx.authState.isAuthenticated;return{when:function(e){return!!t&&e()}}}var b=function(t){return(0,A().when)(t.isTrue)?t.children:null},O=function(t){var e=t.component,o=t.unauthenticatedComponent,a=t.unauthorizedComponent,u=t.isTrue,c=i(t,["component","unauthenticatedComponent","unauthorizedComponent","isTrue"]),s=p().authCtx.authState.isAuthenticated,h=u();return n.createElement(r.Route,Object.assign({component:s?h?e:a||d:o||m},c))},S=b,E=O,L=A;exports.RouteWhen=O,exports.RouteWhenAuthenticatedAnd=E,exports.RouteWhenHasAllClaims=C,exports.RouteWhenHasAnyClaims=function(t){var e=t.claims,o=t.component,a=t.unauthenticatedComponent,u=t.unauthorizedComponent,c=i(t,["claims","component","unauthenticatedComponent","unauthorizedComponent"]),s=p(),h=s.authCtx.authState.isAuthenticated,l=(0,s.hasAnyClaims)(e);return n.createElement(r.Route,Object.assign({component:h?l?o:u||d:a||m},c))},exports.RouteWhenHasClaim=function(t){var e=t.claim,r=i(t,["claim"]);return n.createElement(C,Object.assign({claims:[e]},r))},exports.RouteWhenMemberOf=function(t){var e=t.group,r=i(t,["group"]);return n.createElement(v,Object.assign({groups:[e]},r))},exports.RouteWhenMemberOfAll=v,exports.RouteWhenMemberOfAny=function(t){var e=t.groups,o=t.component,a=t.unauthenticatedComponent,u=t.unauthorizedComponent,c=i(t,["groups","component","unauthenticatedComponent","unauthorizedComponent"]),s=p(),h=s.authCtx.authState.isAuthenticated,l=(0,s.isMemberOfAny)(e);return n.createElement(r.Route,Object.assign({},c,{component:h?l?o:u||d:a||m}))},exports.RtrOktaAuth=function(t){var n=t.children,r=function(t){var n=t.authCtx,r=e.useState(null),o=r[0],i=r[1],u=e.useState(!1),c=u[0],s=u[1],h=e.useState([]),f=h[0],p=h[1],m=n.authState,d=n.oktaAuth;return e.useEffect((function(){function t(){return(t=a(l.mark((function t(){var e;return l.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(m.isAuthenticated){t.next=5;break}return i(null),p([]),s(!0),t.abrupt("return");case 5:return s(!1),t.next=8,d.token.getUserInfo();case 8:i(e=t.sent),p(e.groups||[]),s(!0);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}m.isPending||function(){t.apply(this,arguments)}()}),[m.isAuthenticated]),{user:o,userGroups:f,authorizationStateKnown:c,authCtx:n}}({authCtx:t.authCtx});return e.createElement(f.Provider,{value:r},n)},exports.When=b,exports.WhenAuthStatePending=function(t){return p().authorizationStateKnown?t.children:null},exports.WhenAuthenticatedAnd=S,exports.WhenHasAllClaims=x,exports.WhenHasAnyClaims=function(t){var e=p();return e.authCtx.authState.isAuthenticated&&e.authorizationStateKnown&&(0,e.hasAnyClaims)(t.claims)?t.children:null},exports.WhenHasClaim=function(t){var e=t.claim,r=i(t,["claim"]);return n.createElement(x,Object.assign({claims:[e]},r))},exports.WhenMemberOf=function(t){var e=t.group,r=i(t,["group"]);return n.createElement(y,Object.assign({groups:[e]},r))},exports.WhenMemberOfAll=y,exports.WhenMemberOfAny=function(t){var e=p();return e.authCtx.authState.isAuthenticated&&e.authorizationStateKnown&&(0,e.isMemberOfAny)(t.groups)?t.children:null},exports.WhenNotHasAllClaims=w,exports.WhenNotHasAnyClaims=function(t){var e=p();return e.authorizationStateKnown?e.authCtx.authState.isAuthenticated&&(0,e.hasAnyClaims)(t.claims)?null:t.children:null},exports.WhenNotHasClaim=function(t){var e=t.claim,r=i(t,["claim"]);return n.createElement(w,Object.assign({claims:[e]},r))},exports.WhenNotMemberOf=function(t){var e=t.group,r=i(t,["group"]);return n.createElement(g,Object.assign({groups:[e]},r))},exports.WhenNotMemberOfAll=g,exports.WhenNotMemberOfAny=function(t){var e=p();return e.authorizationStateKnown?e.authCtx.authState.isAuthenticated&&(0,e.isMemberOfAny)(t.groups)?null:t.children:null},exports.useRtrOktaAuth=p,exports.useWhen=A,exports.useWhenAuthenticatedAnd=L;
//# sourceMappingURL=rtr-react-okta-auth.cjs.production.min.js.map