UNPKG

@flexbase/http-client-middleware

Version:
137 lines (136 loc) 4.62 kB
var k = Object.defineProperty; var m = (o, e, r) => e in o ? k(o, e, { enumerable: !0, configurable: !0, writable: !0, value: r }) : o[e] = r; var s = (o, e, r) => (m(o, typeof e != "symbol" ? e + "" : e, r), r); import { DateTime as T } from "luxon"; const w = (o) => (e) => async (r, n) => { let t = null, c = n.authContext; if (c || (c = n.authContext = {}), c.isAnonymousRoute) return e(r, n); t = c.token, t || (t = o.tokenStore.retrieveToken()); const h = await o.tokenAccessor.validateToken(t), i = (a) => { o.tokenStore.storeToken(a), c.token = a; const d = ((a == null ? void 0 : a.tokenType) || "") + " " + ((a == null ? void 0 : a.token) || ""); n = { ...n, headers: { ...n.headers || {}, Authorization: d } }; }, u = async () => { const a = o.credentialProvider(); t = await o.tokenAccessor.requestToken(a, t == null ? void 0 : t.refreshToken), i(t); }; h ? i(t) : await u(); let p = 0; const y = async (a) => { if (++p, a.status === 401) { if (p <= 1) return await u(), e(r, n).then(y); o.tokenStore.storeToken(null); } return a; }; return e(r, n).then(y); }, A = () => ({ tokenType: "", token: "", expiration: 0, refreshToken: "", scope: "" }); class f { validateToken(e) { if (!e) return Promise.resolve(!1); const r = T.fromSeconds(e.expiration); return Promise.resolve(r > T.utc()); } // eslint-disable-next-line @typescript-eslint/no-explicit-any coerceResponse(e) { return { token: e.access_token, tokenType: e.token_type || "Bearer", expiration: e.expires_in, refreshToken: e.refresh_token, scope: e.scope || "", challengePhoneNumberLastFour: e.challenge_phone_number_last_four }; } async requestToken(e, r) { const { url: n, body: t } = this.generateBody(e, r), c = Object.keys(t).map((i) => encodeURIComponent(i) + "=" + encodeURIComponent(t[i])).join("&"), l = await fetch(n, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8" }, body: c }); if (l.status < 200 || l.status > 299) return null; const h = await l.json(); return this.coerceResponse(h); } } class U extends f { // eslint-disable-next-line @typescript-eslint/no-explicit-any generateBody(e, r) { let n = ""; const t = {}; return r && r.trim().length !== 0 ? (t.grant_type = e.refreshGrantType, t.refresh_token = r, n = e.refreshTokenUrl || e.tokenUrl) : (t.grant_type = e.grantType, t.client_secret = e.clientSecret, n = e.tokenUrl), t.client_id = e.clientId, t.scope = e.scope, { url: n, body: t }; } } class b { constructor(e) { /** The url used to request a token */ s(this, "tokenUrl", ""); /** The optional url used to send a refresh token */ s(this, "refreshTokenUrl"); /** The client id */ s(this, "clientId", ""); /** The client secret */ s(this, "clientSecret", ""); /** The grant type, defaults to *client_credentials* */ s(this, "grantType", "client_credentials"); /** The refresh grant type, defaults to *refresh_token* */ s(this, "refreshGrantType", "refresh_token"); /** The requested scopes */ s(this, "scope", ""); Object.assign(this, e); } } class S extends f { // eslint-disable-next-line @typescript-eslint/no-explicit-any generateBody(e, r) { let n = ""; const t = {}; return r && r.trim().length !== 0 ? (t.grant_type = e.refreshGrantType, t.refresh_token = r, n = e.refreshTokenUrl || e.tokenUrl) : (t.grant_type = e.grantType, t.password = e.password, n = e.tokenUrl), t.username = e.username, t.scope = e.scope, { url: n, body: t }; } } class v { constructor(e) { /** The url used to request a token */ s(this, "tokenUrl", ""); /** The optional url used to send a refresh token */ s(this, "refreshTokenUrl"); /** The user name */ s(this, "username", ""); /** The password */ s(this, "password", ""); /** The grant type, defaults to *password* */ s(this, "grantType", "password"); /** The refresh grant type, defaults to *refresh_token* */ s(this, "refreshGrantType", "refresh_token"); /** The requested scopes */ s(this, "scope", ""); Object.assign(this, e); } } export { f as AuthenticationTokenAccessorBase, b as ClientCredentials, U as ClientCredentialsAuthenticationTokenAccessor, S as PasswordAuthenticationTokenAccessor, v as PasswordCredentials, w as authenticationTokenMiddleware, A as createEmptyAuthenticationToken };