@frontegg/rest-api
Advanced tools
Comparing version 1.3.1-alpha.0 to 1.4.0
@@ -10,1 +10,2 @@ export declare const AUTH_SERVICE_URL_V1 = "/identity/resources/auth/v1"; | ||
export declare const TEAMS_STATS_SERVICE_URL_V1 = "/team/resources/stats/v1/members"; | ||
export declare const NOTIFICATIONS_SERVICE_URL = "/notification"; |
@@ -5,2 +5,3 @@ import * as auth from './auth'; | ||
import * as reports from './reports'; | ||
import * as notifications from './notifications'; | ||
import * as fetch from './fetch'; | ||
@@ -13,2 +14,3 @@ import { ContextHolder, FronteggContext } from './ContextHolder'; | ||
export * from './reports/interfaces'; | ||
export * from './notifications/interfaces'; | ||
declare const api: { | ||
@@ -19,3 +21,4 @@ auth: typeof auth; | ||
reports: typeof reports; | ||
notifications: typeof notifications; | ||
}; | ||
export { fetch, ContextHolder, FronteggContext, api }; |
926
index.es.js
@@ -1,2 +0,926 @@ | ||
import{__awaiter as e,__generator as n,__assign as t}from"tslib";import r from"jwt-decode";var o=function(){function e(){this.context=null,this.accessToken=null,this.user=null,this.onRedirectTo=function(e){return window.location.href=e}}return e.getInstance=function(){return e.instance||(e.instance=new e),e.instance},e.setContext=function(n){e.getInstance().context=n},e.setAccessToken=function(n){e.getInstance().accessToken=n},e.setUser=function(n){e.getInstance().user=n},e.setOnRedirectTo=function(n){e.getInstance().onRedirectTo=n},e.getContext=function(){var n;return null!==(n=e.getInstance().context)&&void 0!==n?n:{baseUrl:window.location.href,tokenResolver:function(){return"my-authentication-token"}}},e.getAccessToken=function(){return e.getInstance().accessToken},e.getUser=function(){return e.getInstance().user},e.onRedirectTo=function(n,t){return e.getInstance().onRedirectTo(n,t)},e}(),i={getContext:function(){return o.getContext()},getAccessToken:function(){return o.getAccessToken()},getUser:function(){return o.getUser()}};function s(t){return e(this,void 0,void 0,(function(){var e,r;return n(this,(function(n){return e=t.baseUrl,r=t.urlPrefix||"frontegg",e.endsWith("/")||(e+="/"),e.endsWith(r)||(e+=r),[2,e]}))}))}function u(t,r,o){return e(this,void 0,void 0,(function(){var e,i,u,c,d;return n(this,(function(n){switch(n.label){case 0:return[4,s(t)];case 1:return e=n.sent(),[4,a(t,o)];case 2:return i=n.sent(),u=r.startsWith("http")?r:""+e+r,c=Object.keys(i).length>0,i&&c&&(d=new URLSearchParams(i),u+="?"+d),[2,u]}}))}))}function c(t,r){var i;return void 0===r&&(r="application/json"),e(this,void 0,void 0,(function(){var e,s,u,c,a;return n(this,(function(n){switch(n.label){case 0:return[4,(null!==(i=null==t?void 0:t.tokenResolver)&&void 0!==i?i:o.getAccessToken)()];case 1:return e=n.sent(),s={},e&&(s.Authorization="Bearer "+e),r&&(s["Content-Type"]=r),u=0,[4,l(t)];case 2:c=n.sent(),n.label=3;case 3:if(!(u<c.length))return[3,5];a=c[u],s[""+a.key]=""+a.value,n.label=4;case 4:return u++,[3,3];case 5:return s["x-frontegg-source"]="frontegg-react",[2,s]}}))}))}function a(t,r){return e(this,void 0,void 0,(function(){var e,o,i,s,u,c,a,l,f;return n(this,(function(n){switch(n.label){case 0:return r||(r={}),[4,d(t)];case 1:for(e=n.sent(),o=0,i=e;o<i.length;o++)s=i[o],r[s.key]=s.value;for(u=Object.keys(r),c=0,a=u;c<a.length;c++)l=a[c],f=r[l],r[l]="object"==typeof f?JSON.stringify(f):f;return[2,r]}}))}))}function d(t){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return e=[],t.additionalQueryParamsResolver?[4,t.additionalQueryParamsResolver()]:[3,2];case 1:e=n.sent(),n.label=2;case 2:return[2,e]}}))}))}function l(t){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return e=[],t.additionalHeadersResolver?[4,t.additionalHeadersResolver()]:[3,2];case 1:e=n.sent(),n.label=2;case 2:return[2,e]}}))}))}var f=function(r){return e(void 0,void 0,void 0,(function(){var e,i,s,a,d,l,f,v;return n(this,(function(n){switch(n.label){case 0:return[4,c(e=o.getContext(),r.contentType)];case 1:return i=n.sent(),[4,u(e,r.url,r.params)];case 2:return s=n.sent(),[4,fetch(s,{body:r.body?JSON.stringify(r.body):null,method:null!==(l=r.method)&&void 0!==l?l:"GET",headers:t(t({},i),r.headers),credentials:null!==(v=null!==(f=r.credentials)&&void 0!==f?f:e.requestCredentials)&&void 0!==v?v:"same-origin"})];case 3:if((a=n.sent()).ok)return[3,8];d=void 0,n.label=4;case 4:return n.trys.push([4,6,,7]),[4,a.text()];case 5:return d=n.sent(),d=JSON.parse(d),[3,7];case 6:return n.sent(),[3,7];case 7:throw d.errors?d=d.errors.join(", "):"string"!=typeof d&&(d="Error "+a.status+" - "+a.statusText),new Error(d);case 8:if(r.responseType&&"json"!==r.responseType)return[3,13];n.label=9;case 9:return n.trys.push([9,11,,12]),[4,a.json()];case 10:return[2,n.sent()];case 11:return n.sent(),[2,{}];case 12:return[3,17];case 13:return"blob"!==r.responseType?[3,15]:[4,a.blob()];case 14:return[2,n.sent()];case 15:return[4,a.text()];case 16:return[2,n.sent()];case 17:return[2]}}))}))},v=function(r,o,i){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,f(t({url:r,method:"GET",contentType:"application/json",params:o},i))]}))}))},h=function(r,o,i){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,f(t({url:r,method:"POST",contentType:"application/json",body:o},i))]}))}))},g=function(r,o,i){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,f(t({url:r,method:"PUT",contentType:"application/json",body:o},i))]}))}))},b=function(r,o,i){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,f(t({url:r,method:"DELETE",contentType:"application/json",body:o},i))]}))}))},m=Object.freeze({__proto__:null,Get:v,Post:h,Patch:function(r,o,i){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,f(t({url:r,method:"PATCH",contentType:"application/json",body:o},i))]}))}))},Put:g,Delete:b,PostDownload:function(r,o,i){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,f(t({url:r,method:"GET",contentType:"application/json",responseType:"blob",body:o},i))]}))}))}}),p="/identity/resources/auth/v1",y="/identity/resources/users/v1";function T(i){return e(this,void 0,void 0,(function(){var e,s,u;return n(this,(function(n){switch(n.label){case 0:return i.accessToken?(o.setAccessToken(i.accessToken),[4,v("/identity/resources/users/v2/me")]):[2,i];case 1:return e=n.sent(),s=i.accessToken?r(i.accessToken):{},u=t(t(t({},i),s),e),o.setUser(u),[2,u]}}))}))}function w(t){var r,o;return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:return[4,v("/metadata",t)];case 1:if(e=n.sent(),null===(r=null==e?void 0:e.rows)||void 0===r?void 0:r[0])return[2,null===(o=null==e?void 0:e.rows)||void 0===o?void 0:o[0]];throw new Error("metadata not found: "+t.entityName)}}))}))}var k={auth:Object.freeze({__proto__:null,preLogin:function(t){return e(this,void 0,void 0,(function(){var e;return n(this,(function(n){switch(n.label){case 0:console.info("preLogin()",t),n.label=1;case 1:return n.trys.push([1,3,,4]),[4,h(p+"/user/saml/prelogin",t)];case 2:return[2,n.sent().address];case 3:return e=n.sent(),console.error("preLogin()",e),[2,null];case 4:return[2]}}))}))},postLogin:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return console.debug("postLogin()"),[4,h(p+"/user/saml/postlogin",t)];case 1:return[2,T(e.sent())]}}))}))},login:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return console.debug("login()"),[4,h(p+"/user",t)];case 1:return[2,T(e.sent())]}}))}))},loginWithMfa:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return console.debug("loginWithMfa()"),[4,h(p+"/user/mfa/verify",t)];case 1:return[2,T(e.sent())]}}))}))},activateAccount:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("activateAccount()"),[2,h(y+"/activate",t)]}))}))},acceptInvitation:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("acceptInvitation()"),[2,h(y+"/invitation/accept",t)]}))}))},refreshToken:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){switch(e.label){case 0:return console.debug("refreshToken()"),[4,h(p+"/user/token/refresh")];case 1:return[2,T(e.sent())]}}))}))},logout:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("logout()"),[2,h(p+"/logout")]}))}))},forgotPassword:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("forgotPassword()",t),[2,h(y+"/passwords/reset",t)]}))}))},resetPassword:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("resetPassword()"),[2,h(y+"/passwords/reset/verify",t)]}))}))},recoverMfaToken:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("recoverMfaToken()",t),[2,h(p+"/user/mfa/recover",t)]}))}))},enrollMfa:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("enrollMfa()"),[2,h(y+"/mfa/enroll")]}))}))},verifyMfa:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("verifyMfa()",t),[2,h(y+"/mfa/enroll/verify",t)]}))}))},disableMfa:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("disableMfa()",t),[2,h(y+"/mfa/disable",t)]}))}))},getSamlConfiguration:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("getSamlConfiguration()"),[2,v("/team/resources/sso/v1/saml/configurations")]}))}))},updateSamlConfiguration:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("updateSamlConfiguration()",t),[2,h("/team/resources/sso/v1/saml/configurations",t)]}))}))},getSamlVendorConfiguration:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("getSamlVendorConfiguration()"),[2,v("/team/resources/sso/v1/saml/configurations/vendor-config")]}))}))},updateSamlVendorMetadata:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("updateSamlVendorMetadata()",t),[2,g("/team/resources/sso/v1/saml/configurations/metadata",t)]}))}))},validateSamlDomain:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("validateSamlDomain()"),[2,g("/team/resources/sso/v1/saml/validations/domain")]}))}))}}),teams:Object.freeze({__proto__:null,getProfile:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("getProfile()"),[2,v("/identity/resources/users/v2/me",null!=t?t:{addRoles:!0})]}))}))},updateProfile:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("updateProfile()",t),[2,g("/identity/resources/users/v2/me",t)]}))}))},changePassword:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("changePassword()"),[2,h(y+"/passwords/change",t)]}))}))},loadUsers:function(r){var o,i,s,u,c,a;return e(this,void 0,void 0,(function(){var e,d;return n(this,(function(n){return console.debug("loadUsers()",r),e=(r.filter||[]).reduce((function(e,n){var r;return t(t({},e),((r={})[n.id]=n.value,r))}),{}),d=(null===(o=r.sort)||void 0===o?void 0:o.length)?{sortBy:null!==(u=null===(s=null===(i=r.sort)||void 0===i?void 0:i[0])||void 0===s?void 0:s.id)&&void 0!==u?u:"name",sortDirection:(null===(a=null===(c=r.sort)||void 0===c?void 0:c[0])||void 0===a?void 0:a.desc)?"desc":"asc"}:null,[2,v("/team/resources/members/v1",t(t({pageOffset:r.pageOffset,pageSize:r.pageSize},e),d))]}))}))},addUser:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("addUser()",t),[2,h("/team/resources/members/v1",t)]}))}))},deleteUser:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("deleteUser()",t),[2,b("/team/resources/members/v1/"+t.userId)]}))}))},updateUser:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("editUser()",t),[2,g("/team/resources/members/v1",t)]}))}))},loadAvailableRoles:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("loadAvailableRoles()"),[2,v("/team/resources/roles/v1")]}))}))},loadStats:function(){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("loadStats()"),[2,v("/team/resources/stats/v1/members")]}))}))},resendActivationLink:function(t){return e(this,void 0,void 0,(function(){return n(this,(function(e){return console.debug("resendActivationLink()",t),[2,h("/team/resources/members/v1/"+t.userId+"/resendActivationEmail",{})]}))}))}}),metadata:Object.freeze({__proto__:null,getNotificationsMetadata:function(){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,w({entityName:"notifications"})]}))}))},getSamlMetadata:function(){return e(void 0,void 0,void 0,(function(){return n(this,(function(e){return[2,w({entityName:"saml"})]}))}))}}),reports:Object.freeze({__proto__:null})};export{o as ContextHolder,i as FronteggContext,k as api,m as fetch}; | ||
import { __awaiter, __generator, __assign } from 'tslib'; | ||
import jwtDecode from 'jwt-decode'; | ||
var ContextHolder = /** @class */ (function () { | ||
function ContextHolder() { | ||
this.context = null; | ||
this.accessToken = null; | ||
this.user = null; | ||
this.onRedirectTo = function (path) { return (window.location.href = path); }; | ||
} | ||
ContextHolder.getInstance = function () { | ||
if (!ContextHolder.instance) { | ||
ContextHolder.instance = new ContextHolder(); | ||
} | ||
return ContextHolder.instance; | ||
}; | ||
ContextHolder.setContext = function (context) { | ||
ContextHolder.getInstance().context = context; | ||
}; | ||
ContextHolder.setAccessToken = function (accessToken) { | ||
ContextHolder.getInstance().accessToken = accessToken; | ||
}; | ||
ContextHolder.setUser = function (user) { | ||
ContextHolder.getInstance().user = user; | ||
}; | ||
ContextHolder.setOnRedirectTo = function (onRedirectTo) { | ||
ContextHolder.getInstance().onRedirectTo = onRedirectTo; | ||
}; | ||
ContextHolder.getContext = function () { | ||
var _a; | ||
return ((_a = ContextHolder.getInstance().context) !== null && _a !== void 0 ? _a : { | ||
baseUrl: window.location.href, | ||
tokenResolver: function () { return 'my-authentication-token'; }, | ||
}); | ||
}; | ||
ContextHolder.getAccessToken = function () { | ||
return ContextHolder.getInstance().accessToken; | ||
}; | ||
ContextHolder.getUser = function () { | ||
return ContextHolder.getInstance().user; | ||
}; | ||
ContextHolder.onRedirectTo = function (path, opts) { | ||
return ContextHolder.getInstance().onRedirectTo(path, opts); | ||
}; | ||
return ContextHolder; | ||
}()); | ||
var FronteggContext = { | ||
getContext: function () { return ContextHolder.getContext(); }, | ||
getAccessToken: function () { return ContextHolder.getAccessToken(); }, | ||
getUser: function () { return ContextHolder.getUser(); }, | ||
}; | ||
function getBaseUrl(context) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var baseUrl, prefix; | ||
return __generator(this, function (_a) { | ||
baseUrl = context.baseUrl; | ||
prefix = context.urlPrefix || 'frontegg'; | ||
if (!baseUrl.endsWith('/')) { | ||
baseUrl += '/'; | ||
} | ||
if (!baseUrl.endsWith(prefix)) { | ||
baseUrl += prefix; | ||
} | ||
return [2 /*return*/, baseUrl]; | ||
}); | ||
}); | ||
} | ||
function prepareUrl(context, url, params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var baseUrl, paramsToSend, finalUrl, hasKeys, urlParams; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, getBaseUrl(context)]; | ||
case 1: | ||
baseUrl = _a.sent(); | ||
return [4 /*yield*/, buildQueryParams(context, params)]; | ||
case 2: | ||
paramsToSend = _a.sent(); | ||
finalUrl = url.startsWith('http') ? url : "" + baseUrl + url; | ||
hasKeys = Object.keys(paramsToSend).length > 0; | ||
if (paramsToSend && hasKeys) { | ||
urlParams = new URLSearchParams(paramsToSend); | ||
finalUrl += "?" + urlParams; | ||
} | ||
return [2 /*return*/, finalUrl]; | ||
} | ||
}); | ||
}); | ||
} | ||
function buildRequestHeaders(context, contentType) { | ||
var _a; | ||
if (contentType === void 0) { contentType = 'application/json'; } | ||
return __awaiter(this, void 0, void 0, function () { | ||
var authToken, headers, _i, _b, additionalHeader; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, ((_a = context === null || context === void 0 ? void 0 : context.tokenResolver) !== null && _a !== void 0 ? _a : ContextHolder.getAccessToken)()]; | ||
case 1: | ||
authToken = _c.sent(); | ||
headers = {}; | ||
if (authToken) { | ||
headers.Authorization = "Bearer " + authToken; | ||
} | ||
if (contentType) { | ||
headers['Content-Type'] = contentType; | ||
} | ||
_i = 0; | ||
return [4 /*yield*/, getAdditionalHeaders(context)]; | ||
case 2: | ||
_b = _c.sent(); | ||
_c.label = 3; | ||
case 3: | ||
if (!(_i < _b.length)) return [3 /*break*/, 5]; | ||
additionalHeader = _b[_i]; | ||
headers["" + additionalHeader.key] = "" + additionalHeader.value; | ||
_c.label = 4; | ||
case 4: | ||
_i++; | ||
return [3 /*break*/, 3]; | ||
case 5: | ||
headers['x-frontegg-source'] = 'frontegg-react'; | ||
return [2 /*return*/, headers]; | ||
} | ||
}); | ||
}); | ||
} | ||
function buildQueryParams(context, params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var additionalQueryParams, _i, additionalQueryParams_1, queryParam, keys, _a, keys_1, key, value; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!params) { | ||
params = {}; | ||
} | ||
return [4 /*yield*/, getAdditionalQueryParams(context)]; | ||
case 1: | ||
additionalQueryParams = _b.sent(); | ||
for (_i = 0, additionalQueryParams_1 = additionalQueryParams; _i < additionalQueryParams_1.length; _i++) { | ||
queryParam = additionalQueryParams_1[_i]; | ||
params[queryParam.key] = queryParam.value; | ||
} | ||
keys = Object.keys(params); | ||
for (_a = 0, keys_1 = keys; _a < keys_1.length; _a++) { | ||
key = keys_1[_a]; | ||
value = params[key]; | ||
params[key] = typeof value === 'object' ? JSON.stringify(value) : value; | ||
} | ||
return [2 /*return*/, params]; | ||
} | ||
}); | ||
}); | ||
} | ||
function getAdditionalQueryParams(context) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var output; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
output = []; | ||
if (!context.additionalQueryParamsResolver) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, context.additionalQueryParamsResolver()]; | ||
case 1: | ||
output = _a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, output]; | ||
} | ||
}); | ||
}); | ||
} | ||
function getAdditionalHeaders(context) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var output; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
output = []; | ||
if (!context.additionalHeadersResolver) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, context.additionalHeadersResolver()]; | ||
case 1: | ||
output = _a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, output]; | ||
} | ||
}); | ||
}); | ||
} | ||
var sendRequest = function (opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
var context, headers, url, response, errorMessage, e_1, e_2; | ||
var _a, _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
context = ContextHolder.getContext(); | ||
return [4 /*yield*/, buildRequestHeaders(context, opts.contentType)]; | ||
case 1: | ||
headers = _d.sent(); | ||
return [4 /*yield*/, prepareUrl(context, opts.url, opts.params)]; | ||
case 2: | ||
url = _d.sent(); | ||
return [4 /*yield*/, fetch(url, { | ||
body: opts.body ? JSON.stringify(opts.body) : null, | ||
method: (_a = opts.method) !== null && _a !== void 0 ? _a : 'GET', | ||
headers: __assign(__assign({}, headers), opts.headers), | ||
credentials: (_c = (_b = opts.credentials) !== null && _b !== void 0 ? _b : context.requestCredentials) !== null && _c !== void 0 ? _c : 'same-origin', | ||
})]; | ||
case 3: | ||
response = _d.sent(); | ||
if (!!response.ok) return [3 /*break*/, 8]; | ||
errorMessage = void 0; | ||
_d.label = 4; | ||
case 4: | ||
_d.trys.push([4, 6, , 7]); | ||
return [4 /*yield*/, response.text()]; | ||
case 5: | ||
errorMessage = _d.sent(); | ||
errorMessage = JSON.parse(errorMessage); | ||
return [3 /*break*/, 7]; | ||
case 6: | ||
e_1 = _d.sent(); | ||
return [3 /*break*/, 7]; | ||
case 7: | ||
if (errorMessage.errors) { | ||
errorMessage = errorMessage.errors.join(', '); | ||
} | ||
else if (typeof errorMessage !== 'string') { | ||
errorMessage = "Error " + response.status + " - " + response.statusText; | ||
} | ||
throw new Error(errorMessage); | ||
case 8: | ||
if (!(!opts.responseType || opts.responseType === 'json')) return [3 /*break*/, 13]; | ||
_d.label = 9; | ||
case 9: | ||
_d.trys.push([9, 11, , 12]); | ||
return [4 /*yield*/, response.json()]; | ||
case 10: return [2 /*return*/, _d.sent()]; | ||
case 11: | ||
e_2 = _d.sent(); | ||
return [2 /*return*/, {}]; | ||
case 12: return [3 /*break*/, 17]; | ||
case 13: | ||
if (!(opts.responseType === 'blob')) return [3 /*break*/, 15]; | ||
return [4 /*yield*/, response.blob()]; | ||
case 14: return [2 /*return*/, _d.sent()]; | ||
case 15: return [4 /*yield*/, response.text()]; | ||
case 16: return [2 /*return*/, _d.sent()]; | ||
case 17: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var Get = function (url, params, opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(__assign({ url: url, method: 'GET', contentType: 'application/json', params: params }, opts))]; | ||
}); | ||
}); }; | ||
var Post = function (url, body, opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(__assign({ url: url, method: 'POST', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var Patch = function (url, body, opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(__assign({ url: url, method: 'PATCH', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var Put = function (url, body, opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(__assign({ url: url, method: 'PUT', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var Delete = function (url, body, opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(__assign({ url: url, method: 'DELETE', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var PostDownload = function (url, body, opts) { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(__assign({ url: url, method: 'GET', contentType: 'application/json', responseType: 'blob', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var fetch$1 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
Get: Get, | ||
Post: Post, | ||
Patch: Patch, | ||
Put: Put, | ||
Delete: Delete, | ||
PostDownload: PostDownload | ||
}); | ||
var AUTH_SERVICE_URL_V1 = '/identity/resources/auth/v1'; | ||
var USERS_SERVICE_URL_V1 = '/identity/resources/users/v1'; | ||
var USERS_SERVICE_URL_V2 = '/identity/resources/users/v2'; | ||
var SSO_SERVICE_URL_V1 = '/team/resources/sso/v1'; | ||
var TEAMS_USERS_SERVICE_URL_V1 = '/team/resources/members/v1'; | ||
var TEAMS_ROLES_SERVICE_URL_V1 = '/team/resources/roles/v1'; | ||
var TEAMS_STATS_SERVICE_URL_V1 = '/team/resources/stats/v1/members'; | ||
var NOTIFICATIONS_SERVICE_URL = '/notification'; | ||
/* tslint:disable:no-console */ | ||
/***************************************** | ||
* Authentication | ||
*****************************************/ | ||
function generateLoginResponse(loginResponse) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var me, decodedContent, user; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!loginResponse.accessToken) { | ||
return [2 /*return*/, loginResponse]; | ||
} | ||
ContextHolder.setAccessToken(loginResponse.accessToken); | ||
return [4 /*yield*/, Get(USERS_SERVICE_URL_V2 + "/me")]; | ||
case 1: | ||
me = _a.sent(); | ||
decodedContent = loginResponse.accessToken ? jwtDecode(loginResponse.accessToken) : {}; | ||
user = __assign(__assign(__assign({}, loginResponse), decodedContent), me); | ||
ContextHolder.setUser(user); | ||
return [2 /*return*/, user]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Check if requested email address has sso configuration | ||
* If true, this function will return the sso address to navigate to | ||
* else, return null | ||
*/ | ||
function preLogin(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var address, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.info('preLogin()', body); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/saml/prelogin", body)]; | ||
case 2: | ||
address = (_a.sent()).address; | ||
return [2 /*return*/, address]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
console.error('preLogin()', e_1); | ||
return [2 /*return*/, null]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* After calling preLogin and navigated to SSO url, the IDP will redirect you to the ACS Url | ||
* Which configured in the SSO configuraation | ||
* else, return null | ||
*/ | ||
function postLogin(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('postLogin()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/saml/postlogin", body)]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* login with username and password. | ||
* if the user has two factor authentication | ||
* the server will return mfaToken with mfaRequired: true, | ||
* and then ``loginWithMfa`` should be called with the mfaToken and and generated code | ||
* else, the server will accessToken and refreshToken. | ||
* the refresh should be used to renew your access token by calling ``refreshToken`` | ||
* | ||
* @throw exception if login failed | ||
*/ | ||
function login(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('login()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user", body)]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* after login succeeded with mfaRequired token response, this function should be called | ||
* with the mfaToken and the generated code from your authenticator app. | ||
* | ||
* @throw exception if generated code or mfaToken are incorrect | ||
*/ | ||
function loginWithMfa(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('loginWithMfa()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/mfa/verify", body)]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* activating account should be called after registering new user of deactivate account | ||
* ``activateAccount`` should contains userId and the token that has been sent to the user after activation requested. | ||
* | ||
* @throws exception if activation failed | ||
*/ | ||
function activateAccount(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('activateAccount()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/activate", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* activating account should be called after registering new user of deactivate account | ||
* ``activateAccount`` should contains userId and the token that has been sent to the user after activation requested. | ||
* | ||
* @throws exception if activation failed | ||
*/ | ||
function acceptInvitation(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('acceptInvitation()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/invitation/accept", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* refresh token called as authenticated use, access and refresh tokens resolved by the cookies. | ||
* the server will return ILoginResponse with new access Token and refresh token and store it in the browser cookies. | ||
*/ | ||
function refreshToken() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('refreshToken()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/token/refresh")]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* logout from server, invalidate access and refresh token, remove it from cookies. | ||
*/ | ||
function logout() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('logout()'); | ||
return [2 /*return*/, Post(AUTH_SERVICE_URL_V1 + "/logout")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* calling forgot password request will send email with link to reset user's password. | ||
* | ||
* @throws exception if the user not found | ||
*/ | ||
function forgotPassword(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('forgotPassword()', body); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/passwords/reset", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* reset password should be called after forget password request. | ||
* userId, new password and the token has been sent to the user email. | ||
* | ||
* @throws exception if the user not found, password validation failed or invalid token. | ||
*/ | ||
function resetPassword(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('resetPassword()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/passwords/reset/verify", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* recover Multi-Factor authentication by providing the recoveryCode | ||
* that has been received when activated it | ||
* | ||
* @throws exception if recovery code is not valid | ||
*/ | ||
function recoverMfaToken(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('recoverMfaToken()', body); | ||
return [2 /*return*/, Post(AUTH_SERVICE_URL_V1 + "/user/mfa/recover", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Multi-Factor Settings | ||
*/ | ||
/** | ||
* enroll Multi-Factor Auth Code to use in 3rd party apps like Google Authenticator. | ||
* the server returns qrCode as png image in base64 format | ||
* | ||
* @throws exception if the user already have mfa-enabled | ||
* ``authorized user`` | ||
*/ | ||
function enrollMfa() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('enrollMfa()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/mfa/enroll")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* after enrolling MFA QR code, verify function should be called | ||
* with the generated code in the Authenticator App | ||
* | ||
* @return recoveryCode to use to disable mfa if your device is lost, this code won't show it again. | ||
* @throws exception if the generated token is invalid | ||
* ``authorized user`` | ||
*/ | ||
function verifyMfa(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('verifyMfa()', body); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/mfa/enroll/verify", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* disable MFA by either passing the recoveryCode or the generated code from the Authenticator App. | ||
* | ||
* @throws exception if the generated token or the recoveryCode are incorrect. | ||
* * ``authorized user`` | ||
*/ | ||
function disableMfa(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('disableMfa()', body); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/mfa/disable", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* SSO Configurations | ||
*/ | ||
/** | ||
* Retrieve SAML configurations from server by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function getSamlConfiguration() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('getSamlConfiguration()'); | ||
return [2 /*return*/, Get(SSO_SERVICE_URL_V1 + "/saml/configurations")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Update SAML configuration by logged in user (tenantId, vendorId) | ||
* * ``authorized user`` | ||
*/ | ||
function updateSamlConfiguration(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('updateSamlConfiguration()', body); | ||
return [2 /*return*/, Post(SSO_SERVICE_URL_V1 + "/saml/configurations", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Retrieve 'Vendor' Saml config from server by logged in user (tenantId, vendorId) | ||
* | ||
* @throws exception 'ACS url information not found' if no saml vendor configuration found. | ||
* * ``authorized user`` | ||
*/ | ||
function getSamlVendorConfiguration() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('getSamlVendorConfiguration()'); | ||
return [2 /*return*/, Get(SSO_SERVICE_URL_V1 + "/saml/configurations/vendor-config")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Update Vendor Saml metadata by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function updateSamlVendorMetadata(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('updateSamlVendorMetadata()', body); | ||
return [2 /*return*/, Put(SSO_SERVICE_URL_V1 + "/saml/configurations/metadata", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Validate Saml configuration's domain by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function validateSamlDomain() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('validateSamlDomain()'); | ||
return [2 /*return*/, Put(SSO_SERVICE_URL_V1 + "/saml/validations/domain")]; | ||
}); | ||
}); | ||
} | ||
var auth = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
preLogin: preLogin, | ||
postLogin: postLogin, | ||
login: login, | ||
loginWithMfa: loginWithMfa, | ||
activateAccount: activateAccount, | ||
acceptInvitation: acceptInvitation, | ||
refreshToken: refreshToken, | ||
logout: logout, | ||
forgotPassword: forgotPassword, | ||
resetPassword: resetPassword, | ||
recoverMfaToken: recoverMfaToken, | ||
enrollMfa: enrollMfa, | ||
verifyMfa: verifyMfa, | ||
disableMfa: disableMfa, | ||
getSamlConfiguration: getSamlConfiguration, | ||
updateSamlConfiguration: updateSamlConfiguration, | ||
getSamlVendorConfiguration: getSamlVendorConfiguration, | ||
updateSamlVendorMetadata: updateSamlVendorMetadata, | ||
validateSamlDomain: validateSamlDomain | ||
}); | ||
/***************************************** | ||
* Profile Api | ||
*****************************************/ | ||
/** | ||
* change user password by providing current password and the new password. | ||
* | ||
* @throws exception if the current password incorrect or new password validation failed. | ||
* ``authorized user`` | ||
*/ | ||
function getProfile(params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('getProfile()'); | ||
return [2 /*return*/, Get(USERS_SERVICE_URL_V2 + "/me", params !== null && params !== void 0 ? params : { addRoles: true })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* update user profile by providing updated fields. | ||
* ``authorized user`` | ||
*/ | ||
function updateProfile(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('updateProfile()', body); | ||
return [2 /*return*/, Put(USERS_SERVICE_URL_V2 + "/me", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* change user password by providing current password and the new password. | ||
* | ||
* @throws exception if the current password incorrect or new password validation failed. | ||
* ``authorized user`` | ||
*/ | ||
function changePassword(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('changePassword()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/passwords/change", body)]; | ||
}); | ||
}); | ||
} | ||
/***************************************** | ||
* Team Api | ||
*****************************************/ | ||
/** | ||
* load users from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadUsers(params) { | ||
var _a, _b, _c, _d, _e, _f; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var filters, sorts; | ||
return __generator(this, function (_g) { | ||
console.debug('loadUsers()', params); | ||
filters = (params.filter || []).reduce(function (p, n) { | ||
var _a; | ||
return (__assign(__assign({}, p), (_a = {}, _a[n.id] = n.value, _a))); | ||
}, {}); | ||
sorts = ((_a = params.sort) === null || _a === void 0 ? void 0 : _a.length) ? { | ||
sortBy: (_d = (_c = (_b = params.sort) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 'name', | ||
sortDirection: ((_f = (_e = params.sort) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.desc) ? 'desc' : 'asc', | ||
} | ||
: null; | ||
return [2 /*return*/, Get(TEAMS_USERS_SERVICE_URL_V1, __assign(__assign({ pageOffset: params.pageOffset, pageSize: params.pageSize }, filters), sorts))]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* add user to team, invitation link will be sent to the added user. | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function addUser(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('addUser()', body); | ||
return [2 /*return*/, Post(TEAMS_USERS_SERVICE_URL_V1, body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* delete user from team, user will be deleted. | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function deleteUser(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('deleteUser()', body); | ||
return [2 /*return*/, Delete(TEAMS_USERS_SERVICE_URL_V1 + "/" + body.userId)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* edit user roles in team | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function updateUser(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('editUser()', body); | ||
return [2 /*return*/, Put(TEAMS_USERS_SERVICE_URL_V1, body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* load available roles that can be assigned to team users from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadAvailableRoles() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('loadAvailableRoles()'); | ||
return [2 /*return*/, Get(TEAMS_ROLES_SERVICE_URL_V1)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* load users stats from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadStats() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('loadStats()'); | ||
return [2 /*return*/, Get(TEAMS_STATS_SERVICE_URL_V1)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* resend activation link to specific user | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function resendActivationLink(body) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('resendActivationLink()', body); | ||
return [2 /*return*/, Post(TEAMS_USERS_SERVICE_URL_V1 + "/" + body.userId + "/resendActivationEmail", {})]; | ||
}); | ||
}); | ||
} | ||
var teams = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getProfile: getProfile, | ||
updateProfile: updateProfile, | ||
changePassword: changePassword, | ||
loadUsers: loadUsers, | ||
addUser: addUser, | ||
deleteUser: deleteUser, | ||
updateUser: updateUser, | ||
loadAvailableRoles: loadAvailableRoles, | ||
loadStats: loadStats, | ||
resendActivationLink: resendActivationLink | ||
}); | ||
function getMetadata(body) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, Get('/metadata', body)]; | ||
case 1: | ||
data = _c.sent(); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.rows) === null || _a === void 0 ? void 0 : _a[0]) | ||
return [2 /*return*/, (_b = data === null || data === void 0 ? void 0 : data.rows) === null || _b === void 0 ? void 0 : _b[0]]; | ||
throw new Error("metadata not found: " + body.entityName); | ||
} | ||
}); | ||
}); | ||
} | ||
var getNotificationsMetadata = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, getMetadata({ entityName: 'notifications' })]; | ||
}); }); }; | ||
var getSamlMetadata = function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, getMetadata({ entityName: 'saml' })]; | ||
}); }); }; | ||
var metadata = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getNotificationsMetadata: getNotificationsMetadata, | ||
getSamlMetadata: getSamlMetadata | ||
}); | ||
var reports = /*#__PURE__*/Object.freeze({ | ||
__proto__: null | ||
}); | ||
/***************************************** | ||
* Notifications Api | ||
*****************************************/ | ||
/** | ||
* get Notifications | ||
*/ | ||
function getNotifications(params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Get("" + NOTIFICATIONS_SERVICE_URL, { params: params })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* update Notification status | ||
*/ | ||
function updateNotificationStatus(params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Put(NOTIFICATIONS_SERVICE_URL + "/status", { params: params })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* update Notification pin status | ||
*/ | ||
function updateNotificationIsPinned(params) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var notificationId, pinStatus; | ||
return __generator(this, function (_a) { | ||
notificationId = params.notificationId, pinStatus = params.pinStatus; | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Put(NOTIFICATIONS_SERVICE_URL + "/" + pinStatus, { notificationId: notificationId })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* mark all notifications as read | ||
*/ | ||
function markAllAsRead() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Post(NOTIFICATIONS_SERVICE_URL + "/status/mark-all-read")]; | ||
}); | ||
}); | ||
} | ||
var notifications = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getNotifications: getNotifications, | ||
updateNotificationStatus: updateNotificationStatus, | ||
updateNotificationIsPinned: updateNotificationIsPinned, | ||
markAllAsRead: markAllAsRead | ||
}); | ||
var api = { | ||
auth: auth, | ||
teams: teams, | ||
metadata: metadata, | ||
reports: reports, | ||
notifications: notifications, | ||
}; | ||
export { ContextHolder, FronteggContext, api, fetch$1 as fetch }; | ||
//# sourceMappingURL=index.es.js.map |
712
index.esm.js
@@ -1,2 +0,712 @@ | ||
import{__awaiter as e}from"tslib";import t from"jwt-decode";class o{constructor(){this.context=null,this.accessToken=null,this.user=null,this.onRedirectTo=e=>window.location.href=e}static getInstance(){return o.instance||(o.instance=new o),o.instance}static setContext(e){o.getInstance().context=e}static setAccessToken(e){o.getInstance().accessToken=e}static setUser(e){o.getInstance().user=e}static setOnRedirectTo(e){o.getInstance().onRedirectTo=e}static getContext(){var e;return null!==(e=o.getInstance().context)&&void 0!==e?e:{baseUrl:window.location.href,tokenResolver:()=>"my-authentication-token"}}static getAccessToken(){return o.getInstance().accessToken}static getUser(){return o.getInstance().user}static onRedirectTo(e,t){return o.getInstance().onRedirectTo(e,t)}}const n={getContext:()=>o.getContext(),getAccessToken:()=>o.getAccessToken(),getUser:()=>o.getUser()};function i(t,o,n){return e(this,void 0,void 0,(function*(){const i=yield function(t){return e(this,void 0,void 0,(function*(){let e=t.baseUrl;const o=t.urlPrefix||"frontegg";return e.endsWith("/")||(e+="/"),e.endsWith(o)||(e+=o),e}))}(t),r=yield function(t,o){return e(this,void 0,void 0,(function*(){o||(o={});const n=yield function(t){return e(this,void 0,void 0,(function*(){let e=[];return t.additionalQueryParamsResolver&&(e=yield t.additionalQueryParamsResolver()),e}))}(t);for(const e of n)o[e.key]=e.value;const i=Object.keys(o);for(const e of i){const t=o[e];o[e]="object"==typeof t?JSON.stringify(t):t}return o}))}(t,n);let s=o.startsWith("http")?o:`${i}${o}`;const u=Object.keys(r).length>0;if(r&&u){s+="?"+new URLSearchParams(r)}return s}))}function r(t,n="application/json"){var i;return e(this,void 0,void 0,(function*(){const r=yield(null!==(i=null==t?void 0:t.tokenResolver)&&void 0!==i?i:o.getAccessToken)(),s={};r&&(s.Authorization="Bearer "+r),n&&(s["Content-Type"]=n);for(const o of yield function(t){return e(this,void 0,void 0,(function*(){let e=[];return t.additionalHeadersResolver&&(e=yield t.additionalHeadersResolver()),e}))}(t))s[""+o.key]=""+o.value;return s["x-frontegg-source"]="frontegg-react",s}))}const s=t=>e(void 0,void 0,void 0,(function*(){var e,n,s;const u=o.getContext(),c=yield r(u,t.contentType),d=yield i(u,t.url,t.params),a=yield fetch(d,{body:t.body?JSON.stringify(t.body):null,method:null!==(e=t.method)&&void 0!==e?e:"GET",headers:Object.assign(Object.assign({},c),t.headers),credentials:null!==(s=null!==(n=t.credentials)&&void 0!==n?n:u.requestCredentials)&&void 0!==s?s:"same-origin"});if(!a.ok){let e;try{e=yield a.text(),e=JSON.parse(e)}catch(e){}throw e.errors?e=e.errors.join(", "):"string"!=typeof e&&(e=`Error ${a.status} - ${a.statusText}`),new Error(e)}if(t.responseType&&"json"!==t.responseType)return"blob"===t.responseType?yield a.blob():yield a.text();try{return yield a.json()}catch(e){return{}}})),u=(t,o,n)=>e(void 0,void 0,void 0,(function*(){return s(Object.assign({url:t,method:"GET",contentType:"application/json",params:o},n))})),c=(t,o,n)=>e(void 0,void 0,void 0,(function*(){return s(Object.assign({url:t,method:"POST",contentType:"application/json",body:o},n))})),d=(t,o,n)=>e(void 0,void 0,void 0,(function*(){return s(Object.assign({url:t,method:"PUT",contentType:"application/json",body:o},n))})),a=(t,o,n)=>e(void 0,void 0,void 0,(function*(){return s(Object.assign({url:t,method:"DELETE",contentType:"application/json",body:o},n))}));var l=Object.freeze({__proto__:null,Get:u,Post:c,Patch:(t,o,n)=>e(void 0,void 0,void 0,(function*(){return s(Object.assign({url:t,method:"PATCH",contentType:"application/json",body:o},n))})),Put:d,Delete:a,PostDownload:(t,o,n)=>e(void 0,void 0,void 0,(function*(){return s(Object.assign({url:t,method:"GET",contentType:"application/json",responseType:"blob",body:o},n))}))});const v="/identity/resources/auth/v1",f="/identity/resources/users/v1";function g(n){return e(this,void 0,void 0,(function*(){if(!n.accessToken)return n;o.setAccessToken(n.accessToken);const e=yield u("/identity/resources/users/v2/me"),i=n.accessToken?t(n.accessToken):{},r=Object.assign(Object.assign(Object.assign({},n),i),e);return o.setUser(r),r}))}function m(t){var o,n;return e(this,void 0,void 0,(function*(){const e=yield u("/metadata",t);if(null===(o=null==e?void 0:e.rows)||void 0===o?void 0:o[0])return null===(n=null==e?void 0:e.rows)||void 0===n?void 0:n[0];throw new Error("metadata not found: "+t.entityName)}))}const h={auth:Object.freeze({__proto__:null,preLogin:function(t){return e(this,void 0,void 0,(function*(){console.info("preLogin()",t);try{const{address:e}=yield c(v+"/user/saml/prelogin",t);return e}catch(e){return console.error("preLogin()",e),null}}))},postLogin:function(t){return e(this,void 0,void 0,(function*(){console.debug("postLogin()");return g(yield c(v+"/user/saml/postlogin",t))}))},login:function(t){return e(this,void 0,void 0,(function*(){console.debug("login()");return g(yield c(v+"/user",t))}))},loginWithMfa:function(t){return e(this,void 0,void 0,(function*(){console.debug("loginWithMfa()");return g(yield c(v+"/user/mfa/verify",t))}))},activateAccount:function(t){return e(this,void 0,void 0,(function*(){return console.debug("activateAccount()"),c(f+"/activate",t)}))},acceptInvitation:function(t){return e(this,void 0,void 0,(function*(){return console.debug("acceptInvitation()"),c(f+"/invitation/accept",t)}))},refreshToken:function(){return e(this,void 0,void 0,(function*(){console.debug("refreshToken()");return g(yield c(v+"/user/token/refresh"))}))},logout:function(){return e(this,void 0,void 0,(function*(){return console.debug("logout()"),c(v+"/logout")}))},forgotPassword:function(t){return e(this,void 0,void 0,(function*(){return console.debug("forgotPassword()",t),c(f+"/passwords/reset",t)}))},resetPassword:function(t){return e(this,void 0,void 0,(function*(){return console.debug("resetPassword()"),c(f+"/passwords/reset/verify",t)}))},recoverMfaToken:function(t){return e(this,void 0,void 0,(function*(){return console.debug("recoverMfaToken()",t),c(v+"/user/mfa/recover",t)}))},enrollMfa:function(){return e(this,void 0,void 0,(function*(){return console.debug("enrollMfa()"),c(f+"/mfa/enroll")}))},verifyMfa:function(t){return e(this,void 0,void 0,(function*(){return console.debug("verifyMfa()",t),c(f+"/mfa/enroll/verify",t)}))},disableMfa:function(t){return e(this,void 0,void 0,(function*(){return console.debug("disableMfa()",t),c(f+"/mfa/disable",t)}))},getSamlConfiguration:function(){return e(this,void 0,void 0,(function*(){return console.debug("getSamlConfiguration()"),u("/team/resources/sso/v1/saml/configurations")}))},updateSamlConfiguration:function(t){return e(this,void 0,void 0,(function*(){return console.debug("updateSamlConfiguration()",t),c("/team/resources/sso/v1/saml/configurations",t)}))},getSamlVendorConfiguration:function(){return e(this,void 0,void 0,(function*(){return console.debug("getSamlVendorConfiguration()"),u("/team/resources/sso/v1/saml/configurations/vendor-config")}))},updateSamlVendorMetadata:function(t){return e(this,void 0,void 0,(function*(){return console.debug("updateSamlVendorMetadata()",t),d("/team/resources/sso/v1/saml/configurations/metadata",t)}))},validateSamlDomain:function(){return e(this,void 0,void 0,(function*(){return console.debug("validateSamlDomain()"),d("/team/resources/sso/v1/saml/validations/domain")}))}}),teams:Object.freeze({__proto__:null,getProfile:function(t){return e(this,void 0,void 0,(function*(){return console.debug("getProfile()"),u("/identity/resources/users/v2/me",null!=t?t:{addRoles:!0})}))},updateProfile:function(t){return e(this,void 0,void 0,(function*(){return console.debug("updateProfile()",t),d("/identity/resources/users/v2/me",t)}))},changePassword:function(t){return e(this,void 0,void 0,(function*(){return console.debug("changePassword()"),c(f+"/passwords/change",t)}))},loadUsers:function(t){var o,n,i,r,s,c;return e(this,void 0,void 0,(function*(){console.debug("loadUsers()",t);const e=(t.filter||[]).reduce((e,t)=>Object.assign(Object.assign({},e),{[t.id]:t.value}),{}),d=(null===(o=t.sort)||void 0===o?void 0:o.length)?{sortBy:null!==(r=null===(i=null===(n=t.sort)||void 0===n?void 0:n[0])||void 0===i?void 0:i.id)&&void 0!==r?r:"name",sortDirection:(null===(c=null===(s=t.sort)||void 0===s?void 0:s[0])||void 0===c?void 0:c.desc)?"desc":"asc"}:null;return u("/team/resources/members/v1",Object.assign(Object.assign({pageOffset:t.pageOffset,pageSize:t.pageSize},e),d))}))},addUser:function(t){return e(this,void 0,void 0,(function*(){return console.debug("addUser()",t),c("/team/resources/members/v1",t)}))},deleteUser:function(t){return e(this,void 0,void 0,(function*(){return console.debug("deleteUser()",t),a("/team/resources/members/v1/"+t.userId)}))},updateUser:function(t){return e(this,void 0,void 0,(function*(){return console.debug("editUser()",t),d("/team/resources/members/v1",t)}))},loadAvailableRoles:function(){return e(this,void 0,void 0,(function*(){return console.debug("loadAvailableRoles()"),u("/team/resources/roles/v1")}))},loadStats:function(){return e(this,void 0,void 0,(function*(){return console.debug("loadStats()"),u("/team/resources/stats/v1/members")}))},resendActivationLink:function(t){return e(this,void 0,void 0,(function*(){return console.debug("resendActivationLink()",t),c(`/team/resources/members/v1/${t.userId}/resendActivationEmail`,{})}))}}),metadata:Object.freeze({__proto__:null,getNotificationsMetadata:()=>e(void 0,void 0,void 0,(function*(){return m({entityName:"notifications"})})),getSamlMetadata:()=>e(void 0,void 0,void 0,(function*(){return m({entityName:"saml"})}))}),reports:Object.freeze({__proto__:null})};export{o as ContextHolder,n as FronteggContext,h as api,l as fetch}; | ||
import { __awaiter } from 'tslib'; | ||
import jwtDecode from 'jwt-decode'; | ||
class ContextHolder { | ||
constructor() { | ||
this.context = null; | ||
this.accessToken = null; | ||
this.user = null; | ||
this.onRedirectTo = (path) => (window.location.href = path); | ||
} | ||
static getInstance() { | ||
if (!ContextHolder.instance) { | ||
ContextHolder.instance = new ContextHolder(); | ||
} | ||
return ContextHolder.instance; | ||
} | ||
static setContext(context) { | ||
ContextHolder.getInstance().context = context; | ||
} | ||
static setAccessToken(accessToken) { | ||
ContextHolder.getInstance().accessToken = accessToken; | ||
} | ||
static setUser(user) { | ||
ContextHolder.getInstance().user = user; | ||
} | ||
static setOnRedirectTo(onRedirectTo) { | ||
ContextHolder.getInstance().onRedirectTo = onRedirectTo; | ||
} | ||
static getContext() { | ||
var _a; | ||
return ((_a = ContextHolder.getInstance().context) !== null && _a !== void 0 ? _a : { | ||
baseUrl: window.location.href, | ||
tokenResolver: () => 'my-authentication-token', | ||
}); | ||
} | ||
static getAccessToken() { | ||
return ContextHolder.getInstance().accessToken; | ||
} | ||
static getUser() { | ||
return ContextHolder.getInstance().user; | ||
} | ||
static onRedirectTo(path, opts) { | ||
return ContextHolder.getInstance().onRedirectTo(path, opts); | ||
} | ||
} | ||
const FronteggContext = { | ||
getContext: () => ContextHolder.getContext(), | ||
getAccessToken: () => ContextHolder.getAccessToken(), | ||
getUser: () => ContextHolder.getUser(), | ||
}; | ||
function getBaseUrl(context) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let baseUrl = context.baseUrl; | ||
const prefix = context.urlPrefix || 'frontegg'; | ||
if (!baseUrl.endsWith('/')) { | ||
baseUrl += '/'; | ||
} | ||
if (!baseUrl.endsWith(prefix)) { | ||
baseUrl += prefix; | ||
} | ||
return baseUrl; | ||
}); | ||
} | ||
function prepareUrl(context, url, params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const baseUrl = yield getBaseUrl(context); | ||
const paramsToSend = yield buildQueryParams(context, params); | ||
let finalUrl = url.startsWith('http') ? url : `${baseUrl}${url}`; | ||
const hasKeys = Object.keys(paramsToSend).length > 0; | ||
if (paramsToSend && hasKeys) { | ||
const urlParams = new URLSearchParams(paramsToSend); | ||
finalUrl += `?${urlParams}`; | ||
} | ||
return finalUrl; | ||
}); | ||
} | ||
function buildRequestHeaders(context, contentType = 'application/json') { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const authToken = yield ((_a = context === null || context === void 0 ? void 0 : context.tokenResolver) !== null && _a !== void 0 ? _a : ContextHolder.getAccessToken)(); | ||
const headers = {}; | ||
if (authToken) { | ||
headers.Authorization = `Bearer ${authToken}`; | ||
} | ||
if (contentType) { | ||
headers['Content-Type'] = contentType; | ||
} | ||
for (const additionalHeader of yield getAdditionalHeaders(context)) { | ||
headers[`${additionalHeader.key}`] = `${additionalHeader.value}`; | ||
} | ||
headers['x-frontegg-source'] = 'frontegg-react'; | ||
return headers; | ||
}); | ||
} | ||
function buildQueryParams(context, params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!params) { | ||
params = {}; | ||
} | ||
const additionalQueryParams = yield getAdditionalQueryParams(context); | ||
for (const queryParam of additionalQueryParams) { | ||
params[queryParam.key] = queryParam.value; | ||
} | ||
const keys = Object.keys(params); | ||
for (const key of keys) { | ||
const value = params[key]; | ||
params[key] = typeof value === 'object' ? JSON.stringify(value) : value; | ||
} | ||
return params; | ||
}); | ||
} | ||
function getAdditionalQueryParams(context) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let output = []; | ||
if (context.additionalQueryParamsResolver) { | ||
output = yield context.additionalQueryParamsResolver(); | ||
} | ||
return output; | ||
}); | ||
} | ||
function getAdditionalHeaders(context) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let output = []; | ||
if (context.additionalHeadersResolver) { | ||
output = yield context.additionalHeadersResolver(); | ||
} | ||
return output; | ||
}); | ||
} | ||
const sendRequest = (opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
var _a, _b, _c; | ||
const context = ContextHolder.getContext(); | ||
const headers = yield buildRequestHeaders(context, opts.contentType); | ||
const url = yield prepareUrl(context, opts.url, opts.params); | ||
const response = yield fetch(url, { | ||
body: opts.body ? JSON.stringify(opts.body) : null, | ||
method: (_a = opts.method) !== null && _a !== void 0 ? _a : 'GET', | ||
headers: Object.assign(Object.assign({}, headers), opts.headers), | ||
credentials: (_c = (_b = opts.credentials) !== null && _b !== void 0 ? _b : context.requestCredentials) !== null && _c !== void 0 ? _c : 'same-origin', | ||
}); | ||
if (!response.ok) { | ||
let errorMessage; | ||
try { | ||
errorMessage = yield response.text(); | ||
errorMessage = JSON.parse(errorMessage); | ||
} | ||
catch (e) { } | ||
if (errorMessage.errors) { | ||
errorMessage = errorMessage.errors.join(', '); | ||
} | ||
else if (typeof errorMessage !== 'string') { | ||
errorMessage = `Error ${response.status} - ${response.statusText}`; | ||
} | ||
throw new Error(errorMessage); | ||
} | ||
if (!opts.responseType || opts.responseType === 'json') { | ||
try { | ||
return yield response.json(); | ||
} | ||
catch (e) { | ||
return {}; | ||
} | ||
} | ||
else if (opts.responseType === 'blob') { | ||
return yield response.blob(); | ||
} | ||
else { | ||
return yield response.text(); | ||
} | ||
}); | ||
const Get = (url, params, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
return sendRequest(Object.assign({ url, method: 'GET', contentType: 'application/json', params }, opts)); | ||
}); | ||
const Post = (url, body, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
return sendRequest(Object.assign({ url, method: 'POST', contentType: 'application/json', body }, opts)); | ||
}); | ||
const Patch = (url, body, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
return sendRequest(Object.assign({ url, method: 'PATCH', contentType: 'application/json', body }, opts)); | ||
}); | ||
const Put = (url, body, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
return sendRequest(Object.assign({ url, method: 'PUT', contentType: 'application/json', body }, opts)); | ||
}); | ||
const Delete = (url, body, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
return sendRequest(Object.assign({ url, method: 'DELETE', contentType: 'application/json', body }, opts)); | ||
}); | ||
const PostDownload = (url, body, opts) => __awaiter(void 0, void 0, void 0, function* () { | ||
return sendRequest(Object.assign({ url, method: 'GET', contentType: 'application/json', responseType: 'blob', body }, opts)); | ||
}); | ||
var fetch$1 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
Get: Get, | ||
Post: Post, | ||
Patch: Patch, | ||
Put: Put, | ||
Delete: Delete, | ||
PostDownload: PostDownload | ||
}); | ||
const AUTH_SERVICE_URL_V1 = '/identity/resources/auth/v1'; | ||
const USERS_SERVICE_URL_V1 = '/identity/resources/users/v1'; | ||
const USERS_SERVICE_URL_V2 = '/identity/resources/users/v2'; | ||
const SSO_SERVICE_URL_V1 = '/team/resources/sso/v1'; | ||
const TEAMS_USERS_SERVICE_URL_V1 = '/team/resources/members/v1'; | ||
const TEAMS_ROLES_SERVICE_URL_V1 = '/team/resources/roles/v1'; | ||
const TEAMS_STATS_SERVICE_URL_V1 = '/team/resources/stats/v1/members'; | ||
const NOTIFICATIONS_SERVICE_URL = '/notification'; | ||
/* tslint:disable:no-console */ | ||
/***************************************** | ||
* Authentication | ||
*****************************************/ | ||
function generateLoginResponse(loginResponse) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!loginResponse.accessToken) { | ||
return loginResponse; | ||
} | ||
ContextHolder.setAccessToken(loginResponse.accessToken); | ||
const me = yield Get(`${USERS_SERVICE_URL_V2}/me`); | ||
const decodedContent = loginResponse.accessToken ? jwtDecode(loginResponse.accessToken) : {}; | ||
const user = Object.assign(Object.assign(Object.assign({}, loginResponse), decodedContent), me); | ||
ContextHolder.setUser(user); | ||
return user; | ||
}); | ||
} | ||
/** | ||
* Check if requested email address has sso configuration | ||
* If true, this function will return the sso address to navigate to | ||
* else, return null | ||
*/ | ||
function preLogin(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.info('preLogin()', body); | ||
try { | ||
const { address } = yield Post(`${AUTH_SERVICE_URL_V1}/user/saml/prelogin`, body); | ||
return address; | ||
} | ||
catch (e) { | ||
console.error('preLogin()', e); | ||
return null; | ||
} | ||
}); | ||
} | ||
/** | ||
* After calling preLogin and navigated to SSO url, the IDP will redirect you to the ACS Url | ||
* Which configured in the SSO configuraation | ||
* else, return null | ||
*/ | ||
function postLogin(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('postLogin()'); | ||
const data = yield Post(`${AUTH_SERVICE_URL_V1}/user/saml/postlogin`, body); | ||
return generateLoginResponse(data); | ||
}); | ||
} | ||
/** | ||
* login with username and password. | ||
* if the user has two factor authentication | ||
* the server will return mfaToken with mfaRequired: true, | ||
* and then ``loginWithMfa`` should be called with the mfaToken and and generated code | ||
* else, the server will accessToken and refreshToken. | ||
* the refresh should be used to renew your access token by calling ``refreshToken`` | ||
* | ||
* @throw exception if login failed | ||
*/ | ||
function login(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('login()'); | ||
const data = yield Post(`${AUTH_SERVICE_URL_V1}/user`, body); | ||
return generateLoginResponse(data); | ||
}); | ||
} | ||
/** | ||
* after login succeeded with mfaRequired token response, this function should be called | ||
* with the mfaToken and the generated code from your authenticator app. | ||
* | ||
* @throw exception if generated code or mfaToken are incorrect | ||
*/ | ||
function loginWithMfa(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('loginWithMfa()'); | ||
const data = yield Post(`${AUTH_SERVICE_URL_V1}/user/mfa/verify`, body); | ||
return generateLoginResponse(data); | ||
}); | ||
} | ||
/** | ||
* activating account should be called after registering new user of deactivate account | ||
* ``activateAccount`` should contains userId and the token that has been sent to the user after activation requested. | ||
* | ||
* @throws exception if activation failed | ||
*/ | ||
function activateAccount(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('activateAccount()'); | ||
return Post(`${USERS_SERVICE_URL_V1}/activate`, body); | ||
}); | ||
} | ||
/** | ||
* activating account should be called after registering new user of deactivate account | ||
* ``activateAccount`` should contains userId and the token that has been sent to the user after activation requested. | ||
* | ||
* @throws exception if activation failed | ||
*/ | ||
function acceptInvitation(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('acceptInvitation()'); | ||
return Post(`${USERS_SERVICE_URL_V1}/invitation/accept`, body); | ||
}); | ||
} | ||
/** | ||
* refresh token called as authenticated use, access and refresh tokens resolved by the cookies. | ||
* the server will return ILoginResponse with new access Token and refresh token and store it in the browser cookies. | ||
*/ | ||
function refreshToken() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('refreshToken()'); | ||
const data = yield Post(`${AUTH_SERVICE_URL_V1}/user/token/refresh`); | ||
return generateLoginResponse(data); | ||
}); | ||
} | ||
/** | ||
* logout from server, invalidate access and refresh token, remove it from cookies. | ||
*/ | ||
function logout() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('logout()'); | ||
return Post(`${AUTH_SERVICE_URL_V1}/logout`); | ||
}); | ||
} | ||
/** | ||
* calling forgot password request will send email with link to reset user's password. | ||
* | ||
* @throws exception if the user not found | ||
*/ | ||
function forgotPassword(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('forgotPassword()', body); | ||
return Post(`${USERS_SERVICE_URL_V1}/passwords/reset`, body); | ||
}); | ||
} | ||
/** | ||
* reset password should be called after forget password request. | ||
* userId, new password and the token has been sent to the user email. | ||
* | ||
* @throws exception if the user not found, password validation failed or invalid token. | ||
*/ | ||
function resetPassword(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('resetPassword()'); | ||
return Post(`${USERS_SERVICE_URL_V1}/passwords/reset/verify`, body); | ||
}); | ||
} | ||
/** | ||
* recover Multi-Factor authentication by providing the recoveryCode | ||
* that has been received when activated it | ||
* | ||
* @throws exception if recovery code is not valid | ||
*/ | ||
function recoverMfaToken(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('recoverMfaToken()', body); | ||
return Post(`${AUTH_SERVICE_URL_V1}/user/mfa/recover`, body); | ||
}); | ||
} | ||
/** | ||
* Multi-Factor Settings | ||
*/ | ||
/** | ||
* enroll Multi-Factor Auth Code to use in 3rd party apps like Google Authenticator. | ||
* the server returns qrCode as png image in base64 format | ||
* | ||
* @throws exception if the user already have mfa-enabled | ||
* ``authorized user`` | ||
*/ | ||
function enrollMfa() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('enrollMfa()'); | ||
return Post(`${USERS_SERVICE_URL_V1}/mfa/enroll`); | ||
}); | ||
} | ||
/** | ||
* after enrolling MFA QR code, verify function should be called | ||
* with the generated code in the Authenticator App | ||
* | ||
* @return recoveryCode to use to disable mfa if your device is lost, this code won't show it again. | ||
* @throws exception if the generated token is invalid | ||
* ``authorized user`` | ||
*/ | ||
function verifyMfa(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('verifyMfa()', body); | ||
return Post(`${USERS_SERVICE_URL_V1}/mfa/enroll/verify`, body); | ||
}); | ||
} | ||
/** | ||
* disable MFA by either passing the recoveryCode or the generated code from the Authenticator App. | ||
* | ||
* @throws exception if the generated token or the recoveryCode are incorrect. | ||
* * ``authorized user`` | ||
*/ | ||
function disableMfa(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('disableMfa()', body); | ||
return Post(`${USERS_SERVICE_URL_V1}/mfa/disable`, body); | ||
}); | ||
} | ||
/** | ||
* SSO Configurations | ||
*/ | ||
/** | ||
* Retrieve SAML configurations from server by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function getSamlConfiguration() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('getSamlConfiguration()'); | ||
return Get(`${SSO_SERVICE_URL_V1}/saml/configurations`); | ||
}); | ||
} | ||
/** | ||
* Update SAML configuration by logged in user (tenantId, vendorId) | ||
* * ``authorized user`` | ||
*/ | ||
function updateSamlConfiguration(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('updateSamlConfiguration()', body); | ||
return Post(`${SSO_SERVICE_URL_V1}/saml/configurations`, body); | ||
}); | ||
} | ||
/** | ||
* Retrieve 'Vendor' Saml config from server by logged in user (tenantId, vendorId) | ||
* | ||
* @throws exception 'ACS url information not found' if no saml vendor configuration found. | ||
* * ``authorized user`` | ||
*/ | ||
function getSamlVendorConfiguration() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('getSamlVendorConfiguration()'); | ||
return Get(`${SSO_SERVICE_URL_V1}/saml/configurations/vendor-config`); | ||
}); | ||
} | ||
/** | ||
* Update Vendor Saml metadata by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function updateSamlVendorMetadata(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('updateSamlVendorMetadata()', body); | ||
return Put(`${SSO_SERVICE_URL_V1}/saml/configurations/metadata`, body); | ||
}); | ||
} | ||
/** | ||
* Validate Saml configuration's domain by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function validateSamlDomain() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('validateSamlDomain()'); | ||
return Put(`${SSO_SERVICE_URL_V1}/saml/validations/domain`); | ||
}); | ||
} | ||
var auth = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
preLogin: preLogin, | ||
postLogin: postLogin, | ||
login: login, | ||
loginWithMfa: loginWithMfa, | ||
activateAccount: activateAccount, | ||
acceptInvitation: acceptInvitation, | ||
refreshToken: refreshToken, | ||
logout: logout, | ||
forgotPassword: forgotPassword, | ||
resetPassword: resetPassword, | ||
recoverMfaToken: recoverMfaToken, | ||
enrollMfa: enrollMfa, | ||
verifyMfa: verifyMfa, | ||
disableMfa: disableMfa, | ||
getSamlConfiguration: getSamlConfiguration, | ||
updateSamlConfiguration: updateSamlConfiguration, | ||
getSamlVendorConfiguration: getSamlVendorConfiguration, | ||
updateSamlVendorMetadata: updateSamlVendorMetadata, | ||
validateSamlDomain: validateSamlDomain | ||
}); | ||
/***************************************** | ||
* Profile Api | ||
*****************************************/ | ||
/** | ||
* change user password by providing current password and the new password. | ||
* | ||
* @throws exception if the current password incorrect or new password validation failed. | ||
* ``authorized user`` | ||
*/ | ||
function getProfile(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('getProfile()'); | ||
return Get(`${USERS_SERVICE_URL_V2}/me`, params !== null && params !== void 0 ? params : { addRoles: true }); | ||
}); | ||
} | ||
/** | ||
* update user profile by providing updated fields. | ||
* ``authorized user`` | ||
*/ | ||
function updateProfile(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('updateProfile()', body); | ||
return Put(`${USERS_SERVICE_URL_V2}/me`, body); | ||
}); | ||
} | ||
/** | ||
* change user password by providing current password and the new password. | ||
* | ||
* @throws exception if the current password incorrect or new password validation failed. | ||
* ``authorized user`` | ||
*/ | ||
function changePassword(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('changePassword()'); | ||
return Post(`${USERS_SERVICE_URL_V1}/passwords/change`, body); | ||
}); | ||
} | ||
/***************************************** | ||
* Team Api | ||
*****************************************/ | ||
/** | ||
* load users from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadUsers(params) { | ||
var _a, _b, _c, _d, _e, _f; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('loadUsers()', params); | ||
const filters = (params.filter || []).reduce((p, n) => (Object.assign(Object.assign({}, p), { [n.id]: n.value })), {}); | ||
const sorts = ((_a = params.sort) === null || _a === void 0 ? void 0 : _a.length) ? { | ||
sortBy: (_d = (_c = (_b = params.sort) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 'name', | ||
sortDirection: ((_f = (_e = params.sort) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.desc) ? 'desc' : 'asc', | ||
} | ||
: null; | ||
return Get(TEAMS_USERS_SERVICE_URL_V1, Object.assign(Object.assign({ pageOffset: params.pageOffset, pageSize: params.pageSize }, filters), sorts)); | ||
}); | ||
} | ||
/** | ||
* add user to team, invitation link will be sent to the added user. | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function addUser(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('addUser()', body); | ||
return Post(TEAMS_USERS_SERVICE_URL_V1, body); | ||
}); | ||
} | ||
/** | ||
* delete user from team, user will be deleted. | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function deleteUser(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('deleteUser()', body); | ||
return Delete(`${TEAMS_USERS_SERVICE_URL_V1}/${body.userId}`); | ||
}); | ||
} | ||
/** | ||
* edit user roles in team | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function updateUser(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('editUser()', body); | ||
return Put(TEAMS_USERS_SERVICE_URL_V1, body); | ||
}); | ||
} | ||
/** | ||
* load available roles that can be assigned to team users from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadAvailableRoles() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('loadAvailableRoles()'); | ||
return Get(TEAMS_ROLES_SERVICE_URL_V1); | ||
}); | ||
} | ||
/** | ||
* load users stats from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadStats() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('loadStats()'); | ||
return Get(TEAMS_STATS_SERVICE_URL_V1); | ||
}); | ||
} | ||
/** | ||
* resend activation link to specific user | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function resendActivationLink(body) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('resendActivationLink()', body); | ||
return Post(`${TEAMS_USERS_SERVICE_URL_V1}/${body.userId}/resendActivationEmail`, {}); | ||
}); | ||
} | ||
var teams = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getProfile: getProfile, | ||
updateProfile: updateProfile, | ||
changePassword: changePassword, | ||
loadUsers: loadUsers, | ||
addUser: addUser, | ||
deleteUser: deleteUser, | ||
updateUser: updateUser, | ||
loadAvailableRoles: loadAvailableRoles, | ||
loadStats: loadStats, | ||
resendActivationLink: resendActivationLink | ||
}); | ||
function getMetadata(body) { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const data = yield Get('/metadata', body); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.rows) === null || _a === void 0 ? void 0 : _a[0]) | ||
return (_b = data === null || data === void 0 ? void 0 : data.rows) === null || _b === void 0 ? void 0 : _b[0]; | ||
throw new Error(`metadata not found: ${body.entityName}`); | ||
}); | ||
} | ||
const getNotificationsMetadata = () => __awaiter(void 0, void 0, void 0, function* () { return getMetadata({ entityName: 'notifications' }); }); | ||
const getSamlMetadata = () => __awaiter(void 0, void 0, void 0, function* () { return getMetadata({ entityName: 'saml' }); }); | ||
var metadata = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getNotificationsMetadata: getNotificationsMetadata, | ||
getSamlMetadata: getSamlMetadata | ||
}); | ||
var reports = /*#__PURE__*/Object.freeze({ | ||
__proto__: null | ||
}); | ||
/***************************************** | ||
* Notifications Api | ||
*****************************************/ | ||
/** | ||
* get Notifications | ||
*/ | ||
function getNotifications(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('getNotifications()'); | ||
return Get(`${NOTIFICATIONS_SERVICE_URL}`, { params }); | ||
}); | ||
} | ||
/** | ||
* update Notification status | ||
*/ | ||
function updateNotificationStatus(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('getNotifications()'); | ||
return Put(`${NOTIFICATIONS_SERVICE_URL}/status`, { params }); | ||
}); | ||
} | ||
/** | ||
* update Notification pin status | ||
*/ | ||
function updateNotificationIsPinned(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { notificationId, pinStatus } = params; | ||
console.debug('getNotifications()'); | ||
return Put(`${NOTIFICATIONS_SERVICE_URL}/${pinStatus}`, { notificationId }); | ||
}); | ||
} | ||
/** | ||
* mark all notifications as read | ||
*/ | ||
function markAllAsRead() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
console.debug('getNotifications()'); | ||
return Post(`${NOTIFICATIONS_SERVICE_URL}/status/mark-all-read`); | ||
}); | ||
} | ||
var notifications = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getNotifications: getNotifications, | ||
updateNotificationStatus: updateNotificationStatus, | ||
updateNotificationIsPinned: updateNotificationIsPinned, | ||
markAllAsRead: markAllAsRead | ||
}); | ||
const api = { | ||
auth, | ||
teams, | ||
metadata, | ||
reports, | ||
notifications, | ||
}; | ||
export { ContextHolder, FronteggContext, api, fetch$1 as fetch }; | ||
//# sourceMappingURL=index.esm.js.map |
935
index.js
@@ -1,2 +0,935 @@ | ||
Object.defineProperty(exports,"__esModule",{value:!0});var e,t=require("tslib"),n=(e=require("jwt-decode"))&&"object"==typeof e&&"default"in e?e.default:e,r=function(){function e(){this.context=null,this.accessToken=null,this.user=null,this.onRedirectTo=function(e){return window.location.href=e}}return e.getInstance=function(){return e.instance||(e.instance=new e),e.instance},e.setContext=function(t){e.getInstance().context=t},e.setAccessToken=function(t){e.getInstance().accessToken=t},e.setUser=function(t){e.getInstance().user=t},e.setOnRedirectTo=function(t){e.getInstance().onRedirectTo=t},e.getContext=function(){var t;return null!==(t=e.getInstance().context)&&void 0!==t?t:{baseUrl:window.location.href,tokenResolver:function(){return"my-authentication-token"}}},e.getAccessToken=function(){return e.getInstance().accessToken},e.getUser=function(){return e.getInstance().user},e.onRedirectTo=function(t,n){return e.getInstance().onRedirectTo(t,n)},e}(),o={getContext:function(){return r.getContext()},getAccessToken:function(){return r.getAccessToken()},getUser:function(){return r.getUser()}};function i(e){return t.__awaiter(this,void 0,void 0,(function(){var n,r;return t.__generator(this,(function(t){return n=e.baseUrl,r=e.urlPrefix||"frontegg",n.endsWith("/")||(n+="/"),n.endsWith(r)||(n+=r),[2,n]}))}))}function s(e,n,r){return t.__awaiter(this,void 0,void 0,(function(){var o,s,a,c,d;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,i(e)];case 1:return o=t.sent(),[4,u(e,r)];case 2:return s=t.sent(),a=n.startsWith("http")?n:""+o+n,c=Object.keys(s).length>0,s&&c&&(d=new URLSearchParams(s),a+="?"+d),[2,a]}}))}))}function a(e,n){var o;return void 0===n&&(n="application/json"),t.__awaiter(this,void 0,void 0,(function(){var i,s,a,u,c;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,(null!==(o=null==e?void 0:e.tokenResolver)&&void 0!==o?o:r.getAccessToken)()];case 1:return i=t.sent(),s={},i&&(s.Authorization="Bearer "+i),n&&(s["Content-Type"]=n),a=0,[4,d(e)];case 2:u=t.sent(),t.label=3;case 3:if(!(a<u.length))return[3,5];c=u[a],s[""+c.key]=""+c.value,t.label=4;case 4:return a++,[3,3];case 5:return s["x-frontegg-source"]="frontegg-react",[2,s]}}))}))}function u(e,n){return t.__awaiter(this,void 0,void 0,(function(){var r,o,i,s,a,u,d,l,_;return t.__generator(this,(function(t){switch(t.label){case 0:return n||(n={}),[4,c(e)];case 1:for(r=t.sent(),o=0,i=r;o<i.length;o++)s=i[o],n[s.key]=s.value;for(a=Object.keys(n),u=0,d=a;u<d.length;u++)l=d[u],_=n[l],n[l]="object"==typeof _?JSON.stringify(_):_;return[2,n]}}))}))}function c(e){return t.__awaiter(this,void 0,void 0,(function(){var n;return t.__generator(this,(function(t){switch(t.label){case 0:return n=[],e.additionalQueryParamsResolver?[4,e.additionalQueryParamsResolver()]:[3,2];case 1:n=t.sent(),t.label=2;case 2:return[2,n]}}))}))}function d(e){return t.__awaiter(this,void 0,void 0,(function(){var n;return t.__generator(this,(function(t){switch(t.label){case 0:return n=[],e.additionalHeadersResolver?[4,e.additionalHeadersResolver()]:[3,2];case 1:n=t.sent(),t.label=2;case 2:return[2,n]}}))}))}var l=function(e){return t.__awaiter(void 0,void 0,void 0,(function(){var n,o,i,u,c,d,l,_;return t.__generator(this,(function(f){switch(f.label){case 0:return[4,a(n=r.getContext(),e.contentType)];case 1:return o=f.sent(),[4,s(n,e.url,e.params)];case 2:return i=f.sent(),[4,fetch(i,{body:e.body?JSON.stringify(e.body):null,method:null!==(d=e.method)&&void 0!==d?d:"GET",headers:t.__assign(t.__assign({},o),e.headers),credentials:null!==(_=null!==(l=e.credentials)&&void 0!==l?l:n.requestCredentials)&&void 0!==_?_:"same-origin"})];case 3:if((u=f.sent()).ok)return[3,8];c=void 0,f.label=4;case 4:return f.trys.push([4,6,,7]),[4,u.text()];case 5:return c=f.sent(),c=JSON.parse(c),[3,7];case 6:return f.sent(),[3,7];case 7:throw c.errors?c=c.errors.join(", "):"string"!=typeof c&&(c="Error "+u.status+" - "+u.statusText),new Error(c);case 8:if(e.responseType&&"json"!==e.responseType)return[3,13];f.label=9;case 9:return f.trys.push([9,11,,12]),[4,u.json()];case 10:return[2,f.sent()];case 11:return f.sent(),[2,{}];case 12:return[3,17];case 13:return"blob"!==e.responseType?[3,15]:[4,u.blob()];case 14:return[2,f.sent()];case 15:return[4,u.text()];case 16:return[2,f.sent()];case 17:return[2]}}))}))},_=function(e,n,r){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(o){return[2,l(t.__assign({url:e,method:"GET",contentType:"application/json",params:n},r))]}))}))},f=function(e,n,r){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(o){return[2,l(t.__assign({url:e,method:"POST",contentType:"application/json",body:n},r))]}))}))},v=function(e,n,r){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(o){return[2,l(t.__assign({url:e,method:"PUT",contentType:"application/json",body:n},r))]}))}))},g=function(e,n,r){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(o){return[2,l(t.__assign({url:e,method:"DELETE",contentType:"application/json",body:n},r))]}))}))},h=Object.freeze({__proto__:null,Get:_,Post:f,Patch:function(e,n,r){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(o){return[2,l(t.__assign({url:e,method:"PATCH",contentType:"application/json",body:n},r))]}))}))},Put:v,Delete:g,PostDownload:function(e,n,r){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(o){return[2,l(t.__assign({url:e,method:"GET",contentType:"application/json",responseType:"blob",body:n},r))]}))}))}}),b="/identity/resources/auth/v1",w="/identity/resources/users/v1";function m(e){return t.__awaiter(this,void 0,void 0,(function(){var o,i,s;return t.__generator(this,(function(a){switch(a.label){case 0:return e.accessToken?(r.setAccessToken(e.accessToken),[4,_("/identity/resources/users/v2/me")]):[2,e];case 1:return o=a.sent(),i=e.accessToken?n(e.accessToken):{},s=t.__assign(t.__assign(t.__assign({},e),i),o),r.setUser(s),[2,s]}}))}))}function p(e){var n,r;return t.__awaiter(this,void 0,void 0,(function(){var o;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,_("/metadata",e)];case 1:if(o=t.sent(),null===(n=null==o?void 0:o.rows)||void 0===n?void 0:n[0])return[2,null===(r=null==o?void 0:o.rows)||void 0===r?void 0:r[0]];throw new Error("metadata not found: "+e.entityName)}}))}))}var y={auth:Object.freeze({__proto__:null,preLogin:function(e){return t.__awaiter(this,void 0,void 0,(function(){var n;return t.__generator(this,(function(t){switch(t.label){case 0:console.info("preLogin()",e),t.label=1;case 1:return t.trys.push([1,3,,4]),[4,f(b+"/user/saml/prelogin",e)];case 2:return[2,t.sent().address];case 3:return n=t.sent(),console.error("preLogin()",n),[2,null];case 4:return[2]}}))}))},postLogin:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:return console.debug("postLogin()"),[4,f(b+"/user/saml/postlogin",e)];case 1:return[2,m(t.sent())]}}))}))},login:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:return console.debug("login()"),[4,f(b+"/user",e)];case 1:return[2,m(t.sent())]}}))}))},loginWithMfa:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:return console.debug("loginWithMfa()"),[4,f(b+"/user/mfa/verify",e)];case 1:return[2,m(t.sent())]}}))}))},activateAccount:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("activateAccount()"),[2,f(w+"/activate",e)]}))}))},acceptInvitation:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("acceptInvitation()"),[2,f(w+"/invitation/accept",e)]}))}))},refreshToken:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){switch(e.label){case 0:return console.debug("refreshToken()"),[4,f(b+"/user/token/refresh")];case 1:return[2,m(e.sent())]}}))}))},logout:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("logout()"),[2,f(b+"/logout")]}))}))},forgotPassword:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("forgotPassword()",e),[2,f(w+"/passwords/reset",e)]}))}))},resetPassword:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("resetPassword()"),[2,f(w+"/passwords/reset/verify",e)]}))}))},recoverMfaToken:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("recoverMfaToken()",e),[2,f(b+"/user/mfa/recover",e)]}))}))},enrollMfa:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("enrollMfa()"),[2,f(w+"/mfa/enroll")]}))}))},verifyMfa:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("verifyMfa()",e),[2,f(w+"/mfa/enroll/verify",e)]}))}))},disableMfa:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("disableMfa()",e),[2,f(w+"/mfa/disable",e)]}))}))},getSamlConfiguration:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("getSamlConfiguration()"),[2,_("/team/resources/sso/v1/saml/configurations")]}))}))},updateSamlConfiguration:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("updateSamlConfiguration()",e),[2,f("/team/resources/sso/v1/saml/configurations",e)]}))}))},getSamlVendorConfiguration:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("getSamlVendorConfiguration()"),[2,_("/team/resources/sso/v1/saml/configurations/vendor-config")]}))}))},updateSamlVendorMetadata:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("updateSamlVendorMetadata()",e),[2,v("/team/resources/sso/v1/saml/configurations/metadata",e)]}))}))},validateSamlDomain:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("validateSamlDomain()"),[2,v("/team/resources/sso/v1/saml/validations/domain")]}))}))}}),teams:Object.freeze({__proto__:null,getProfile:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("getProfile()"),[2,_("/identity/resources/users/v2/me",null!=e?e:{addRoles:!0})]}))}))},updateProfile:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("updateProfile()",e),[2,v("/identity/resources/users/v2/me",e)]}))}))},changePassword:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("changePassword()"),[2,f(w+"/passwords/change",e)]}))}))},loadUsers:function(e){var n,r,o,i,s,a;return t.__awaiter(this,void 0,void 0,(function(){var u,c;return t.__generator(this,(function(d){return console.debug("loadUsers()",e),u=(e.filter||[]).reduce((function(e,n){var r;return t.__assign(t.__assign({},e),((r={})[n.id]=n.value,r))}),{}),c=(null===(n=e.sort)||void 0===n?void 0:n.length)?{sortBy:null!==(i=null===(o=null===(r=e.sort)||void 0===r?void 0:r[0])||void 0===o?void 0:o.id)&&void 0!==i?i:"name",sortDirection:(null===(a=null===(s=e.sort)||void 0===s?void 0:s[0])||void 0===a?void 0:a.desc)?"desc":"asc"}:null,[2,_("/team/resources/members/v1",t.__assign(t.__assign({pageOffset:e.pageOffset,pageSize:e.pageSize},u),c))]}))}))},addUser:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("addUser()",e),[2,f("/team/resources/members/v1",e)]}))}))},deleteUser:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("deleteUser()",e),[2,g("/team/resources/members/v1/"+e.userId)]}))}))},updateUser:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("editUser()",e),[2,v("/team/resources/members/v1",e)]}))}))},loadAvailableRoles:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("loadAvailableRoles()"),[2,_("/team/resources/roles/v1")]}))}))},loadStats:function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(e){return console.debug("loadStats()"),[2,_("/team/resources/stats/v1/members")]}))}))},resendActivationLink:function(e){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return console.debug("resendActivationLink()",e),[2,f("/team/resources/members/v1/"+e.userId+"/resendActivationEmail",{})]}))}))}}),metadata:Object.freeze({__proto__:null,getNotificationsMetadata:function(){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(e){return[2,p({entityName:"notifications"})]}))}))},getSamlMetadata:function(){return t.__awaiter(void 0,void 0,void 0,(function(){return t.__generator(this,(function(e){return[2,p({entityName:"saml"})]}))}))}}),reports:Object.freeze({__proto__:null})};exports.ContextHolder=r,exports.FronteggContext=o,exports.api=y,exports.fetch=h; | ||
'use strict'; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var tslib = require('tslib'); | ||
var jwtDecode = _interopDefault(require('jwt-decode')); | ||
var ContextHolder = /** @class */ (function () { | ||
function ContextHolder() { | ||
this.context = null; | ||
this.accessToken = null; | ||
this.user = null; | ||
this.onRedirectTo = function (path) { return (window.location.href = path); }; | ||
} | ||
ContextHolder.getInstance = function () { | ||
if (!ContextHolder.instance) { | ||
ContextHolder.instance = new ContextHolder(); | ||
} | ||
return ContextHolder.instance; | ||
}; | ||
ContextHolder.setContext = function (context) { | ||
ContextHolder.getInstance().context = context; | ||
}; | ||
ContextHolder.setAccessToken = function (accessToken) { | ||
ContextHolder.getInstance().accessToken = accessToken; | ||
}; | ||
ContextHolder.setUser = function (user) { | ||
ContextHolder.getInstance().user = user; | ||
}; | ||
ContextHolder.setOnRedirectTo = function (onRedirectTo) { | ||
ContextHolder.getInstance().onRedirectTo = onRedirectTo; | ||
}; | ||
ContextHolder.getContext = function () { | ||
var _a; | ||
return ((_a = ContextHolder.getInstance().context) !== null && _a !== void 0 ? _a : { | ||
baseUrl: window.location.href, | ||
tokenResolver: function () { return 'my-authentication-token'; }, | ||
}); | ||
}; | ||
ContextHolder.getAccessToken = function () { | ||
return ContextHolder.getInstance().accessToken; | ||
}; | ||
ContextHolder.getUser = function () { | ||
return ContextHolder.getInstance().user; | ||
}; | ||
ContextHolder.onRedirectTo = function (path, opts) { | ||
return ContextHolder.getInstance().onRedirectTo(path, opts); | ||
}; | ||
return ContextHolder; | ||
}()); | ||
var FronteggContext = { | ||
getContext: function () { return ContextHolder.getContext(); }, | ||
getAccessToken: function () { return ContextHolder.getAccessToken(); }, | ||
getUser: function () { return ContextHolder.getUser(); }, | ||
}; | ||
function getBaseUrl(context) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var baseUrl, prefix; | ||
return tslib.__generator(this, function (_a) { | ||
baseUrl = context.baseUrl; | ||
prefix = context.urlPrefix || 'frontegg'; | ||
if (!baseUrl.endsWith('/')) { | ||
baseUrl += '/'; | ||
} | ||
if (!baseUrl.endsWith(prefix)) { | ||
baseUrl += prefix; | ||
} | ||
return [2 /*return*/, baseUrl]; | ||
}); | ||
}); | ||
} | ||
function prepareUrl(context, url, params) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var baseUrl, paramsToSend, finalUrl, hasKeys, urlParams; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, getBaseUrl(context)]; | ||
case 1: | ||
baseUrl = _a.sent(); | ||
return [4 /*yield*/, buildQueryParams(context, params)]; | ||
case 2: | ||
paramsToSend = _a.sent(); | ||
finalUrl = url.startsWith('http') ? url : "" + baseUrl + url; | ||
hasKeys = Object.keys(paramsToSend).length > 0; | ||
if (paramsToSend && hasKeys) { | ||
urlParams = new URLSearchParams(paramsToSend); | ||
finalUrl += "?" + urlParams; | ||
} | ||
return [2 /*return*/, finalUrl]; | ||
} | ||
}); | ||
}); | ||
} | ||
function buildRequestHeaders(context, contentType) { | ||
var _a; | ||
if (contentType === void 0) { contentType = 'application/json'; } | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var authToken, headers, _i, _b, additionalHeader; | ||
return tslib.__generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, ((_a = context === null || context === void 0 ? void 0 : context.tokenResolver) !== null && _a !== void 0 ? _a : ContextHolder.getAccessToken)()]; | ||
case 1: | ||
authToken = _c.sent(); | ||
headers = {}; | ||
if (authToken) { | ||
headers.Authorization = "Bearer " + authToken; | ||
} | ||
if (contentType) { | ||
headers['Content-Type'] = contentType; | ||
} | ||
_i = 0; | ||
return [4 /*yield*/, getAdditionalHeaders(context)]; | ||
case 2: | ||
_b = _c.sent(); | ||
_c.label = 3; | ||
case 3: | ||
if (!(_i < _b.length)) return [3 /*break*/, 5]; | ||
additionalHeader = _b[_i]; | ||
headers["" + additionalHeader.key] = "" + additionalHeader.value; | ||
_c.label = 4; | ||
case 4: | ||
_i++; | ||
return [3 /*break*/, 3]; | ||
case 5: | ||
headers['x-frontegg-source'] = 'frontegg-react'; | ||
return [2 /*return*/, headers]; | ||
} | ||
}); | ||
}); | ||
} | ||
function buildQueryParams(context, params) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var additionalQueryParams, _i, additionalQueryParams_1, queryParam, keys, _a, keys_1, key, value; | ||
return tslib.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: | ||
if (!params) { | ||
params = {}; | ||
} | ||
return [4 /*yield*/, getAdditionalQueryParams(context)]; | ||
case 1: | ||
additionalQueryParams = _b.sent(); | ||
for (_i = 0, additionalQueryParams_1 = additionalQueryParams; _i < additionalQueryParams_1.length; _i++) { | ||
queryParam = additionalQueryParams_1[_i]; | ||
params[queryParam.key] = queryParam.value; | ||
} | ||
keys = Object.keys(params); | ||
for (_a = 0, keys_1 = keys; _a < keys_1.length; _a++) { | ||
key = keys_1[_a]; | ||
value = params[key]; | ||
params[key] = typeof value === 'object' ? JSON.stringify(value) : value; | ||
} | ||
return [2 /*return*/, params]; | ||
} | ||
}); | ||
}); | ||
} | ||
function getAdditionalQueryParams(context) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var output; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
output = []; | ||
if (!context.additionalQueryParamsResolver) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, context.additionalQueryParamsResolver()]; | ||
case 1: | ||
output = _a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, output]; | ||
} | ||
}); | ||
}); | ||
} | ||
function getAdditionalHeaders(context) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var output; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
output = []; | ||
if (!context.additionalHeadersResolver) return [3 /*break*/, 2]; | ||
return [4 /*yield*/, context.additionalHeadersResolver()]; | ||
case 1: | ||
output = _a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/, output]; | ||
} | ||
}); | ||
}); | ||
} | ||
var sendRequest = function (opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
var context, headers, url, response, errorMessage, e_1, e_2; | ||
var _a, _b, _c; | ||
return tslib.__generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
context = ContextHolder.getContext(); | ||
return [4 /*yield*/, buildRequestHeaders(context, opts.contentType)]; | ||
case 1: | ||
headers = _d.sent(); | ||
return [4 /*yield*/, prepareUrl(context, opts.url, opts.params)]; | ||
case 2: | ||
url = _d.sent(); | ||
return [4 /*yield*/, fetch(url, { | ||
body: opts.body ? JSON.stringify(opts.body) : null, | ||
method: (_a = opts.method) !== null && _a !== void 0 ? _a : 'GET', | ||
headers: tslib.__assign(tslib.__assign({}, headers), opts.headers), | ||
credentials: (_c = (_b = opts.credentials) !== null && _b !== void 0 ? _b : context.requestCredentials) !== null && _c !== void 0 ? _c : 'same-origin', | ||
})]; | ||
case 3: | ||
response = _d.sent(); | ||
if (!!response.ok) return [3 /*break*/, 8]; | ||
errorMessage = void 0; | ||
_d.label = 4; | ||
case 4: | ||
_d.trys.push([4, 6, , 7]); | ||
return [4 /*yield*/, response.text()]; | ||
case 5: | ||
errorMessage = _d.sent(); | ||
errorMessage = JSON.parse(errorMessage); | ||
return [3 /*break*/, 7]; | ||
case 6: | ||
e_1 = _d.sent(); | ||
return [3 /*break*/, 7]; | ||
case 7: | ||
if (errorMessage.errors) { | ||
errorMessage = errorMessage.errors.join(', '); | ||
} | ||
else if (typeof errorMessage !== 'string') { | ||
errorMessage = "Error " + response.status + " - " + response.statusText; | ||
} | ||
throw new Error(errorMessage); | ||
case 8: | ||
if (!(!opts.responseType || opts.responseType === 'json')) return [3 /*break*/, 13]; | ||
_d.label = 9; | ||
case 9: | ||
_d.trys.push([9, 11, , 12]); | ||
return [4 /*yield*/, response.json()]; | ||
case 10: return [2 /*return*/, _d.sent()]; | ||
case 11: | ||
e_2 = _d.sent(); | ||
return [2 /*return*/, {}]; | ||
case 12: return [3 /*break*/, 17]; | ||
case 13: | ||
if (!(opts.responseType === 'blob')) return [3 /*break*/, 15]; | ||
return [4 /*yield*/, response.blob()]; | ||
case 14: return [2 /*return*/, _d.sent()]; | ||
case 15: return [4 /*yield*/, response.text()]; | ||
case 16: return [2 /*return*/, _d.sent()]; | ||
case 17: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
var Get = function (url, params, opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(tslib.__assign({ url: url, method: 'GET', contentType: 'application/json', params: params }, opts))]; | ||
}); | ||
}); }; | ||
var Post = function (url, body, opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(tslib.__assign({ url: url, method: 'POST', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var Patch = function (url, body, opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(tslib.__assign({ url: url, method: 'PATCH', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var Put = function (url, body, opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(tslib.__assign({ url: url, method: 'PUT', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var Delete = function (url, body, opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(tslib.__assign({ url: url, method: 'DELETE', contentType: 'application/json', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var PostDownload = function (url, body, opts) { return tslib.__awaiter(void 0, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, sendRequest(tslib.__assign({ url: url, method: 'GET', contentType: 'application/json', responseType: 'blob', body: body }, opts))]; | ||
}); | ||
}); }; | ||
var fetch$1 = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
Get: Get, | ||
Post: Post, | ||
Patch: Patch, | ||
Put: Put, | ||
Delete: Delete, | ||
PostDownload: PostDownload | ||
}); | ||
var AUTH_SERVICE_URL_V1 = '/identity/resources/auth/v1'; | ||
var USERS_SERVICE_URL_V1 = '/identity/resources/users/v1'; | ||
var USERS_SERVICE_URL_V2 = '/identity/resources/users/v2'; | ||
var SSO_SERVICE_URL_V1 = '/team/resources/sso/v1'; | ||
var TEAMS_USERS_SERVICE_URL_V1 = '/team/resources/members/v1'; | ||
var TEAMS_ROLES_SERVICE_URL_V1 = '/team/resources/roles/v1'; | ||
var TEAMS_STATS_SERVICE_URL_V1 = '/team/resources/stats/v1/members'; | ||
var NOTIFICATIONS_SERVICE_URL = '/notification'; | ||
/* tslint:disable:no-console */ | ||
/***************************************** | ||
* Authentication | ||
*****************************************/ | ||
function generateLoginResponse(loginResponse) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var me, decodedContent, user; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (!loginResponse.accessToken) { | ||
return [2 /*return*/, loginResponse]; | ||
} | ||
ContextHolder.setAccessToken(loginResponse.accessToken); | ||
return [4 /*yield*/, Get(USERS_SERVICE_URL_V2 + "/me")]; | ||
case 1: | ||
me = _a.sent(); | ||
decodedContent = loginResponse.accessToken ? jwtDecode(loginResponse.accessToken) : {}; | ||
user = tslib.__assign(tslib.__assign(tslib.__assign({}, loginResponse), decodedContent), me); | ||
ContextHolder.setUser(user); | ||
return [2 /*return*/, user]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* Check if requested email address has sso configuration | ||
* If true, this function will return the sso address to navigate to | ||
* else, return null | ||
*/ | ||
function preLogin(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var address, e_1; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.info('preLogin()', body); | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/saml/prelogin", body)]; | ||
case 2: | ||
address = (_a.sent()).address; | ||
return [2 /*return*/, address]; | ||
case 3: | ||
e_1 = _a.sent(); | ||
console.error('preLogin()', e_1); | ||
return [2 /*return*/, null]; | ||
case 4: return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* After calling preLogin and navigated to SSO url, the IDP will redirect you to the ACS Url | ||
* Which configured in the SSO configuraation | ||
* else, return null | ||
*/ | ||
function postLogin(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('postLogin()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/saml/postlogin", body)]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* login with username and password. | ||
* if the user has two factor authentication | ||
* the server will return mfaToken with mfaRequired: true, | ||
* and then ``loginWithMfa`` should be called with the mfaToken and and generated code | ||
* else, the server will accessToken and refreshToken. | ||
* the refresh should be used to renew your access token by calling ``refreshToken`` | ||
* | ||
* @throw exception if login failed | ||
*/ | ||
function login(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('login()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user", body)]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* after login succeeded with mfaRequired token response, this function should be called | ||
* with the mfaToken and the generated code from your authenticator app. | ||
* | ||
* @throw exception if generated code or mfaToken are incorrect | ||
*/ | ||
function loginWithMfa(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('loginWithMfa()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/mfa/verify", body)]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* activating account should be called after registering new user of deactivate account | ||
* ``activateAccount`` should contains userId and the token that has been sent to the user after activation requested. | ||
* | ||
* @throws exception if activation failed | ||
*/ | ||
function activateAccount(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('activateAccount()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/activate", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* activating account should be called after registering new user of deactivate account | ||
* ``activateAccount`` should contains userId and the token that has been sent to the user after activation requested. | ||
* | ||
* @throws exception if activation failed | ||
*/ | ||
function acceptInvitation(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('acceptInvitation()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/invitation/accept", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* refresh token called as authenticated use, access and refresh tokens resolved by the cookies. | ||
* the server will return ILoginResponse with new access Token and refresh token and store it in the browser cookies. | ||
*/ | ||
function refreshToken() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return tslib.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
console.debug('refreshToken()'); | ||
return [4 /*yield*/, Post(AUTH_SERVICE_URL_V1 + "/user/token/refresh")]; | ||
case 1: | ||
data = _a.sent(); | ||
return [2 /*return*/, generateLoginResponse(data)]; | ||
} | ||
}); | ||
}); | ||
} | ||
/** | ||
* logout from server, invalidate access and refresh token, remove it from cookies. | ||
*/ | ||
function logout() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('logout()'); | ||
return [2 /*return*/, Post(AUTH_SERVICE_URL_V1 + "/logout")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* calling forgot password request will send email with link to reset user's password. | ||
* | ||
* @throws exception if the user not found | ||
*/ | ||
function forgotPassword(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('forgotPassword()', body); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/passwords/reset", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* reset password should be called after forget password request. | ||
* userId, new password and the token has been sent to the user email. | ||
* | ||
* @throws exception if the user not found, password validation failed or invalid token. | ||
*/ | ||
function resetPassword(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('resetPassword()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/passwords/reset/verify", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* recover Multi-Factor authentication by providing the recoveryCode | ||
* that has been received when activated it | ||
* | ||
* @throws exception if recovery code is not valid | ||
*/ | ||
function recoverMfaToken(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('recoverMfaToken()', body); | ||
return [2 /*return*/, Post(AUTH_SERVICE_URL_V1 + "/user/mfa/recover", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Multi-Factor Settings | ||
*/ | ||
/** | ||
* enroll Multi-Factor Auth Code to use in 3rd party apps like Google Authenticator. | ||
* the server returns qrCode as png image in base64 format | ||
* | ||
* @throws exception if the user already have mfa-enabled | ||
* ``authorized user`` | ||
*/ | ||
function enrollMfa() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('enrollMfa()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/mfa/enroll")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* after enrolling MFA QR code, verify function should be called | ||
* with the generated code in the Authenticator App | ||
* | ||
* @return recoveryCode to use to disable mfa if your device is lost, this code won't show it again. | ||
* @throws exception if the generated token is invalid | ||
* ``authorized user`` | ||
*/ | ||
function verifyMfa(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('verifyMfa()', body); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/mfa/enroll/verify", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* disable MFA by either passing the recoveryCode or the generated code from the Authenticator App. | ||
* | ||
* @throws exception if the generated token or the recoveryCode are incorrect. | ||
* * ``authorized user`` | ||
*/ | ||
function disableMfa(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('disableMfa()', body); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/mfa/disable", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* SSO Configurations | ||
*/ | ||
/** | ||
* Retrieve SAML configurations from server by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function getSamlConfiguration() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('getSamlConfiguration()'); | ||
return [2 /*return*/, Get(SSO_SERVICE_URL_V1 + "/saml/configurations")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Update SAML configuration by logged in user (tenantId, vendorId) | ||
* * ``authorized user`` | ||
*/ | ||
function updateSamlConfiguration(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('updateSamlConfiguration()', body); | ||
return [2 /*return*/, Post(SSO_SERVICE_URL_V1 + "/saml/configurations", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Retrieve 'Vendor' Saml config from server by logged in user (tenantId, vendorId) | ||
* | ||
* @throws exception 'ACS url information not found' if no saml vendor configuration found. | ||
* * ``authorized user`` | ||
*/ | ||
function getSamlVendorConfiguration() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('getSamlVendorConfiguration()'); | ||
return [2 /*return*/, Get(SSO_SERVICE_URL_V1 + "/saml/configurations/vendor-config")]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Update Vendor Saml metadata by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function updateSamlVendorMetadata(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('updateSamlVendorMetadata()', body); | ||
return [2 /*return*/, Put(SSO_SERVICE_URL_V1 + "/saml/configurations/metadata", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* Validate Saml configuration's domain by logged in user (tenantId, vendorId) | ||
* | ||
* @return {enabled: false} if no saml configuration found. else ISamlConfiguration | ||
* * ``authorized user`` | ||
*/ | ||
function validateSamlDomain() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('validateSamlDomain()'); | ||
return [2 /*return*/, Put(SSO_SERVICE_URL_V1 + "/saml/validations/domain")]; | ||
}); | ||
}); | ||
} | ||
var auth = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
preLogin: preLogin, | ||
postLogin: postLogin, | ||
login: login, | ||
loginWithMfa: loginWithMfa, | ||
activateAccount: activateAccount, | ||
acceptInvitation: acceptInvitation, | ||
refreshToken: refreshToken, | ||
logout: logout, | ||
forgotPassword: forgotPassword, | ||
resetPassword: resetPassword, | ||
recoverMfaToken: recoverMfaToken, | ||
enrollMfa: enrollMfa, | ||
verifyMfa: verifyMfa, | ||
disableMfa: disableMfa, | ||
getSamlConfiguration: getSamlConfiguration, | ||
updateSamlConfiguration: updateSamlConfiguration, | ||
getSamlVendorConfiguration: getSamlVendorConfiguration, | ||
updateSamlVendorMetadata: updateSamlVendorMetadata, | ||
validateSamlDomain: validateSamlDomain | ||
}); | ||
/***************************************** | ||
* Profile Api | ||
*****************************************/ | ||
/** | ||
* change user password by providing current password and the new password. | ||
* | ||
* @throws exception if the current password incorrect or new password validation failed. | ||
* ``authorized user`` | ||
*/ | ||
function getProfile(params) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('getProfile()'); | ||
return [2 /*return*/, Get(USERS_SERVICE_URL_V2 + "/me", params !== null && params !== void 0 ? params : { addRoles: true })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* update user profile by providing updated fields. | ||
* ``authorized user`` | ||
*/ | ||
function updateProfile(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('updateProfile()', body); | ||
return [2 /*return*/, Put(USERS_SERVICE_URL_V2 + "/me", body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* change user password by providing current password and the new password. | ||
* | ||
* @throws exception if the current password incorrect or new password validation failed. | ||
* ``authorized user`` | ||
*/ | ||
function changePassword(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('changePassword()'); | ||
return [2 /*return*/, Post(USERS_SERVICE_URL_V1 + "/passwords/change", body)]; | ||
}); | ||
}); | ||
} | ||
/***************************************** | ||
* Team Api | ||
*****************************************/ | ||
/** | ||
* load users from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadUsers(params) { | ||
var _a, _b, _c, _d, _e, _f; | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var filters, sorts; | ||
return tslib.__generator(this, function (_g) { | ||
console.debug('loadUsers()', params); | ||
filters = (params.filter || []).reduce(function (p, n) { | ||
var _a; | ||
return (tslib.__assign(tslib.__assign({}, p), (_a = {}, _a[n.id] = n.value, _a))); | ||
}, {}); | ||
sorts = ((_a = params.sort) === null || _a === void 0 ? void 0 : _a.length) ? { | ||
sortBy: (_d = (_c = (_b = params.sort) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.id) !== null && _d !== void 0 ? _d : 'name', | ||
sortDirection: ((_f = (_e = params.sort) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.desc) ? 'desc' : 'asc', | ||
} | ||
: null; | ||
return [2 /*return*/, Get(TEAMS_USERS_SERVICE_URL_V1, tslib.__assign(tslib.__assign({ pageOffset: params.pageOffset, pageSize: params.pageSize }, filters), sorts))]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* add user to team, invitation link will be sent to the added user. | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function addUser(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('addUser()', body); | ||
return [2 /*return*/, Post(TEAMS_USERS_SERVICE_URL_V1, body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* delete user from team, user will be deleted. | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function deleteUser(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('deleteUser()', body); | ||
return [2 /*return*/, Delete(TEAMS_USERS_SERVICE_URL_V1 + "/" + body.userId)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* edit user roles in team | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function updateUser(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('editUser()', body); | ||
return [2 /*return*/, Put(TEAMS_USERS_SERVICE_URL_V1, body)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* load available roles that can be assigned to team users from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadAvailableRoles() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('loadAvailableRoles()'); | ||
return [2 /*return*/, Get(TEAMS_ROLES_SERVICE_URL_V1)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* load users stats from team-management-service | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function loadStats() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('loadStats()'); | ||
return [2 /*return*/, Get(TEAMS_STATS_SERVICE_URL_V1)]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* resend activation link to specific user | ||
* | ||
* ``authorized user`` | ||
*/ | ||
function resendActivationLink(body) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('resendActivationLink()', body); | ||
return [2 /*return*/, Post(TEAMS_USERS_SERVICE_URL_V1 + "/" + body.userId + "/resendActivationEmail", {})]; | ||
}); | ||
}); | ||
} | ||
var teams = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getProfile: getProfile, | ||
updateProfile: updateProfile, | ||
changePassword: changePassword, | ||
loadUsers: loadUsers, | ||
addUser: addUser, | ||
deleteUser: deleteUser, | ||
updateUser: updateUser, | ||
loadAvailableRoles: loadAvailableRoles, | ||
loadStats: loadStats, | ||
resendActivationLink: resendActivationLink | ||
}); | ||
function getMetadata(body) { | ||
var _a, _b; | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var data; | ||
return tslib.__generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: return [4 /*yield*/, Get('/metadata', body)]; | ||
case 1: | ||
data = _c.sent(); | ||
if ((_a = data === null || data === void 0 ? void 0 : data.rows) === null || _a === void 0 ? void 0 : _a[0]) | ||
return [2 /*return*/, (_b = data === null || data === void 0 ? void 0 : data.rows) === null || _b === void 0 ? void 0 : _b[0]]; | ||
throw new Error("metadata not found: " + body.entityName); | ||
} | ||
}); | ||
}); | ||
} | ||
var getNotificationsMetadata = function () { return tslib.__awaiter(void 0, void 0, void 0, function () { return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, getMetadata({ entityName: 'notifications' })]; | ||
}); }); }; | ||
var getSamlMetadata = function () { return tslib.__awaiter(void 0, void 0, void 0, function () { return tslib.__generator(this, function (_a) { | ||
return [2 /*return*/, getMetadata({ entityName: 'saml' })]; | ||
}); }); }; | ||
var metadata = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getNotificationsMetadata: getNotificationsMetadata, | ||
getSamlMetadata: getSamlMetadata | ||
}); | ||
var reports = /*#__PURE__*/Object.freeze({ | ||
__proto__: null | ||
}); | ||
/***************************************** | ||
* Notifications Api | ||
*****************************************/ | ||
/** | ||
* get Notifications | ||
*/ | ||
function getNotifications(params) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Get("" + NOTIFICATIONS_SERVICE_URL, { params: params })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* update Notification status | ||
*/ | ||
function updateNotificationStatus(params) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Put(NOTIFICATIONS_SERVICE_URL + "/status", { params: params })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* update Notification pin status | ||
*/ | ||
function updateNotificationIsPinned(params) { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
var notificationId, pinStatus; | ||
return tslib.__generator(this, function (_a) { | ||
notificationId = params.notificationId, pinStatus = params.pinStatus; | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Put(NOTIFICATIONS_SERVICE_URL + "/" + pinStatus, { notificationId: notificationId })]; | ||
}); | ||
}); | ||
} | ||
/** | ||
* mark all notifications as read | ||
*/ | ||
function markAllAsRead() { | ||
return tslib.__awaiter(this, void 0, void 0, function () { | ||
return tslib.__generator(this, function (_a) { | ||
console.debug('getNotifications()'); | ||
return [2 /*return*/, Post(NOTIFICATIONS_SERVICE_URL + "/status/mark-all-read")]; | ||
}); | ||
}); | ||
} | ||
var notifications = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
getNotifications: getNotifications, | ||
updateNotificationStatus: updateNotificationStatus, | ||
updateNotificationIsPinned: updateNotificationIsPinned, | ||
markAllAsRead: markAllAsRead | ||
}); | ||
var api = { | ||
auth: auth, | ||
teams: teams, | ||
metadata: metadata, | ||
reports: reports, | ||
notifications: notifications, | ||
}; | ||
exports.ContextHolder = ContextHolder; | ||
exports.FronteggContext = FronteggContext; | ||
exports.api = api; | ||
exports.fetch = fetch$1; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@frontegg/rest-api", | ||
"libName": "FronteggRestApi", | ||
"version": "1.3.1-alpha.0", | ||
"version": "1.4.0", | ||
"author": "Frontegg LTD", | ||
@@ -12,5 +12,5 @@ "module": "index.esm.js", | ||
}, | ||
"gitHead": "03fc68409fe28fc998494f66b074bbc78b6949ad", | ||
"gitHead": "5c37e746e754e8a190fa3894d4bec6878a10c8fa", | ||
"main": "index.js", | ||
"peerDependencies": {} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
235793
23
3237
1
4