@authup/core-http-kit
Version:
Package containing global constants, types & interfaces.
792 lines (791 loc) • 27.1 kB
JavaScript
import { Client as Client$1, HeaderName, HookName, createClient, getHeader, isClient, isClientError as isClientError$1, setHeader, setHeader as setHeader$1, stringifyAuthorizationHeader, unsetHeader, unsetHeader as unsetHeader$1 } from "hapic";
import { buildQuery } from "rapiq";
import { buildIdentityProviderAuthorizePath } from "@authup/core-kit";
import { AuthorizeAPI, TokenAPI, UserInfoAPI } from "@hapic/oauth2";
import { isObject } from "@authup/kit";
import { isJWKErrorCode, isJWTErrorCode } from "@authup/specs";
import { EventEmitter } from "@posva/event-emitter";
//#region src/cookies.ts
let CookieName = /* @__PURE__ */ function(CookieName) {
CookieName["ACCESS_TOKEN"] = "access_token";
CookieName["ACCESS_TOKEN_EXPIRE_DATE"] = "access_token_expire_date";
CookieName["REFRESH_TOKEN"] = "refresh_token";
CookieName["USER"] = "user";
CookieName["REALM"] = "realm";
CookieName["REALM_MANAGEMENT"] = "realm_management";
return CookieName;
}({});
//#endregion
//#region src/client/helper.ts
function isClientError(input) {
return isClientError$1(input);
}
//#endregion
//#region src/utils/duplicate-slashes.ts
function cleanDoubleSlashes(input) {
if (input.includes("://")) return input.split("://").map((str) => cleanDoubleSlashes(str)).join("://");
return input.replace(/\/+/g, "/");
}
//#endregion
//#region src/utils/object-property.ts
function nullifyEmptyObjectProperties(data) {
const keys = Object.keys(data);
for (const key of keys) if (data[key] === "") data[key] = null;
return data;
}
//#endregion
//#region src/domains/base.ts
var BaseAPI = class {
client;
constructor(context) {
context = context || {};
this.setClient(context.client);
}
setClient(input) {
this.client = isClient(input) ? input : createClient(input);
}
};
//#endregion
//#region src/domains/entities/client/module.ts
var ClientAPI = class extends BaseAPI {
async getMany(options) {
return (await this.client.get(`clients${buildQuery(options)}`)).data;
}
async getOne(id, options) {
return (await this.client.get(`clients/${id}${buildQuery(options)}`)).data;
}
async delete(id) {
return (await this.client.delete(`clients/${id}`)).data;
}
async create(data) {
return (await this.client.post("clients", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`clients/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`clients/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/client-permission/module.ts
var ClientPermissionAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`client-permissions${buildQuery(data)}`)).data;
}
async getOne(id, data) {
return (await this.client.get(`client-permissions/${id}${buildQuery(data)}`)).data;
}
async delete(id) {
return (await this.client.delete(`client-permissions/${id}`)).data;
}
async create(data) {
return (await this.client.post("client-permissions", data)).data;
}
async update(id, data) {
return (await this.client.post(`client-permissions/${id}`, data)).data;
}
};
//#endregion
//#region src/domains/entities/client-role/module.ts
var ClientRoleAPI = class extends BaseAPI {
async getMany(data = {}) {
return (await this.client.get(`client-roles${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`client-roles/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`client-roles/${id}`)).data;
}
async create(data) {
return (await this.client.post("client-roles", data)).data;
}
};
//#endregion
//#region src/domains/entities/client-scope/module.ts
var ClientScopeAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`client-scopes${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`client-scopes/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`client-scopes/${id}`)).data;
}
async create(data) {
return (await this.client.post("client-scopes", data)).data;
}
};
//#endregion
//#region src/domains/entities/identity-provider/module.ts
var IdentityProviderAPI = class extends BaseAPI {
getAuthorizeUri(id) {
return cleanDoubleSlashes(`${this.client.defaults.baseURL}/${buildIdentityProviderAuthorizePath(id)}`);
}
async getMany(record) {
return (await this.client.get(`identity-providers${buildQuery(record)}`)).data;
}
async getOne(id, record) {
return (await this.client.get(`identity-providers/${id}${buildQuery(record)}`)).data;
}
async delete(id) {
return (await this.client.delete(`identity-providers/${id}`)).data;
}
async create(data) {
return (await this.client.post("identity-providers", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`identity-providers/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`identity-providers/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/identity-provider-role-mapping/module.ts
var IdentityProviderRoleMappingAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`identity-provider-role-mappings${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`identity-provider-role-mappings/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`identity-provider-role-mappings/${id}`)).data;
}
async create(data) {
return (await this.client.post("identity-provider-role-mappings", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`identity-provider-role-mappings/${id}`, data)).data;
}
};
//#endregion
//#region src/domains/entities/policy/module.ts
var PolicyAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`policies${buildQuery(data)}`)).data;
}
async delete(id) {
return (await this.client.delete(`policies/${id}`)).data;
}
async getOne(id, record) {
return (await this.client.get(`policies/${id}${buildQuery(record)}`)).data;
}
async getOneExpanded(id, record) {
return (await this.client.get(`policies/${id}/expanded${buildQuery(record)}`)).data;
}
async create(data) {
return (await this.client.post("policies", nullifyEmptyObjectProperties(data))).data;
}
async createBuiltIn(data) {
return this.create(data);
}
async update(id, data) {
return (await this.client.post(`policies/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async updateBuiltIn(id, data) {
return this.update(id, data);
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`policies/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdateBuiltin(idOrName, data) {
return this.createOrUpdate(idOrName, data);
}
async check(idOrName, data = {}) {
return (await this.client.post(`policies/${idOrName}/check`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/permission/module.ts
var PermissionAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`permissions${buildQuery(data)}`)).data;
}
async delete(id) {
return (await this.client.delete(`permissions/${id}`)).data;
}
async getOne(id, record) {
return (await this.client.get(`permissions/${id}${buildQuery(record)}`)).data;
}
async create(data) {
return (await this.client.post("permissions", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`permissions/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`permissions/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
async check(idOrName, data = {}) {
return (await this.client.post(`permissions/${idOrName}/check`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/permission-policy/module.ts
var PermissionPolicyAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`permission-policies${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`permission-policies/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`permission-policies/${id}`)).data;
}
async create(data) {
return (await this.client.post("permission-policies", data)).data;
}
};
//#endregion
//#region src/domains/entities/realm/module.ts
var RealmAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`realms${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`realms/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`realms/${id}`)).data;
}
async create(data) {
return (await this.client.post("realms", nullifyEmptyObjectProperties(data))).data;
}
async update(realmId, data) {
return (await this.client.post(`realms/${realmId}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`realms/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/robot/module.ts
var RobotAPI = class extends BaseAPI {
async getMany(options) {
return (await this.client.get(`robots${buildQuery(options)}`)).data;
}
async getOne(id, options) {
return (await this.client.get(`robots/${id}${buildQuery(options)}`)).data;
}
async delete(id) {
return (await this.client.delete(`robots/${id}`)).data;
}
async create(data) {
return (await this.client.post("robots", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`robots/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`robots/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
async integrity(id) {
const { data: response } = await this.client.get(`robots/${id}/integrity`);
return response;
}
};
//#endregion
//#region src/domains/entities/robot-permission/module.ts
var RobotPermissionAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`robot-permissions${buildQuery(data)}`)).data;
}
async getOne(id, data) {
return (await this.client.get(`robot-permissions/${id}${buildQuery(data)}`)).data;
}
async delete(id) {
return (await this.client.delete(`robot-permissions/${id}`)).data;
}
async create(data) {
return (await this.client.post("robot-permissions", data)).data;
}
async update(id, data) {
return (await this.client.post(`robot-permissions/${id}`, data)).data;
}
};
//#endregion
//#region src/domains/entities/robot-role/module.ts
var RobotRoleAPI = class extends BaseAPI {
async getMany(data = {}) {
return (await this.client.get(`robot-roles${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`robot-roles/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`robot-roles/${id}`)).data;
}
async create(data) {
return (await this.client.post("robot-roles", data)).data;
}
};
//#endregion
//#region src/domains/entities/role/module.ts
var RoleAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`roles${buildQuery(data)}`)).data;
}
async getOne(roleId) {
return (await this.client.get(`roles/${roleId}`)).data;
}
async delete(roleId) {
return (await this.client.delete(`roles/${roleId}`)).data;
}
async create(data) {
return (await this.client.post("roles", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`roles/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`roles/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/role-attribute/module.ts
var RoleAttributeAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`role-attributes${buildQuery(data)}`)).data;
}
async getOne(roleId) {
return (await this.client.get(`role-attributes/${roleId}`)).data;
}
async delete(roleId) {
return (await this.client.delete(`role-attributes/${roleId}`)).data;
}
async create(data) {
return (await this.client.post("role-attributes", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`role-attributes/${id}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/role-permission/module.ts
var RolePermissionAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`role-permissions${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`role-permissions/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`role-permissions/${id}`)).data;
}
async create(data) {
return (await this.client.post("role-permissions", data)).data;
}
async update(id, data) {
return (await this.client.post(`role-permissions/${id}`, data)).data;
}
};
//#endregion
//#region src/domains/entities/scope/module.ts
var ScopeAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`scopes${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`scopes/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`scopes/${id}`)).data;
}
async create(data) {
return (await this.client.post("scopes", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`scopes/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`scopes/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/user/module.ts
var UserAPI = class extends BaseAPI {
async getMany(options) {
return (await this.client.get(`users${buildQuery(options)}`)).data;
}
async getOne(id, options) {
return (await this.client.get(`users/${id}${buildQuery(options)}`)).data;
}
async delete(id) {
return (await this.client.delete(`users/${id}`)).data;
}
async create(data) {
return (await this.client.post("users", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`users/${id}`, nullifyEmptyObjectProperties(data))).data;
}
async createOrUpdate(idOrName, data) {
return (await this.client.put(`users/${idOrName}`, nullifyEmptyObjectProperties(data))).data;
}
async activate(token) {
return (await this.client.post("activate", { token })).data;
}
async register(data) {
return (await this.client.post("register", nullifyEmptyObjectProperties(data))).data;
}
async passwordForgot(data) {
return (await this.client.post("password-forgot", nullifyEmptyObjectProperties(data))).data;
}
async passwordReset(data) {
return (await this.client.post("password-reset", nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/user-attribute/module.ts
var UserAttributeAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`user-attributes${buildQuery(data)}`)).data;
}
async getOne(roleId) {
return (await this.client.get(`user-attributes/${roleId}`)).data;
}
async delete(roleId) {
return (await this.client.delete(`user-attributes/${roleId}`)).data;
}
async create(data) {
return (await this.client.post("user-attributes", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`user-attributes/${id}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/user-permission/module.ts
var UserPermissionAPI = class extends BaseAPI {
async getMany(data) {
return (await this.client.get(`user-permissions${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`user-permissions/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`user-permissions/${id}`)).data;
}
async create(data) {
return (await this.client.post("user-permissions", nullifyEmptyObjectProperties(data))).data;
}
async update(id, data) {
return (await this.client.post(`user-permissions/${id}`, nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/entities/user-role/module.ts
var UserRoleAPI = class extends BaseAPI {
async getMany(data = {}) {
return (await this.client.get(`user-roles${buildQuery(data)}`)).data;
}
async getOne(id) {
return (await this.client.get(`user-roles/${id}`)).data;
}
async delete(id) {
return (await this.client.delete(`user-roles/${id}`)).data;
}
async create(data) {
return (await this.client.post("user-roles", data)).data;
}
};
//#endregion
//#region src/domains/workflows/oauth2/authorize/module.ts
var OAuth2AuthorizeAPI = class extends AuthorizeAPI {
async confirm(data) {
return (await this.client.post("authorize", nullifyEmptyObjectProperties(data))).data;
}
};
//#endregion
//#region src/domains/workflows/oauth2/token/module.ts
var OAuth2TokenAPI = class extends TokenAPI {};
//#endregion
//#region src/domains/workflows/oauth2/user-info/module.ts
var OAuth2UserInfoAPI = class extends UserInfoAPI {};
//#endregion
//#region src/client/module.ts
var Client = class extends Client$1 {
token;
authorize;
client;
clientPermission;
clientRole;
clientScope;
identityProvider;
identityProviderRoleMapping;
policy;
permission;
permissionPolicy;
realm;
robot;
robotPermission;
robotRole;
role;
roleAttribute;
rolePermission;
scope;
user;
userInfo;
userAttribute;
userPermission;
userRole;
constructor(config = {}) {
super(config);
const options = {
authorizationEndpoint: "authorize",
introspectionEndpoint: "token/introspect",
tokenEndpoint: "token",
userinfoEndpoint: "users/@me"
};
const baseURL = this.getBaseURL();
if (typeof baseURL === "string") {
const keys = Object.keys(options);
for (const key_ of keys) {
const key = key_;
if (typeof options[key] === "string") options[key] = new URL(options[key], baseURL).href;
}
}
this.authorize = new OAuth2AuthorizeAPI({
client: this,
options
});
this.token = new OAuth2TokenAPI({
client: this,
options
});
this.client = new ClientAPI({ client: this });
this.clientPermission = new ClientPermissionAPI({ client: this });
this.clientRole = new ClientRoleAPI({ client: this });
this.clientScope = new ClientScopeAPI({ client: this });
this.identityProvider = new IdentityProviderAPI({ client: this });
this.identityProviderRoleMapping = new IdentityProviderRoleMappingAPI({ client: this });
this.policy = new PolicyAPI({ client: this });
this.permission = new PermissionAPI({ client: this });
this.permissionPolicy = new PermissionPolicyAPI({ client: this });
this.realm = new RealmAPI({ client: this });
this.robot = new RobotAPI({ client: this });
this.robotPermission = new RobotPermissionAPI({ client: this });
this.robotRole = new RobotRoleAPI({ client: this });
this.role = new RoleAPI({ client: this });
this.roleAttribute = new RoleAttributeAPI({ client: this });
this.rolePermission = new RolePermissionAPI({ client: this });
this.scope = new ScopeAPI({ client: this });
this.user = new UserAPI({ client: this });
this.userInfo = new OAuth2UserInfoAPI({
client: this,
options
});
this.userAttribute = new UserAttributeAPI({ client: this });
this.userPermission = new UserPermissionAPI({ client: this });
this.userRole = new UserRoleAPI({ client: this });
this.on(HookName.RESPONSE_ERROR, ((error) => {
if (isClientError$1(error) && error.response && error.response.data && typeof error.response.data.message === "string") error.message = error.response.data.message;
throw error;
}));
}
async getJwks() {
return (await this.get("jwks")).data;
}
async getJwk(id) {
return (await this.get(`jwks/${id}`)).data;
}
async getWellKnownOpenIDConfiguration() {
return (await this.get("/.well-known/openid-configuration")).data;
}
};
//#endregion
//#region src/helpers/error-code.ts
function getClientErrorCode(err) {
if (!isObject(err) || !isObject(err.response)) return null;
/* istanbul ignore next */
if (!isObject(err.response.data) || typeof err.response.data.code !== "string") return null;
return err.response.data.code;
}
//#endregion
//#region src/hook/constants.ts
let ClientAuthenticationHookEventName = /* @__PURE__ */ function(ClientAuthenticationHookEventName) {
ClientAuthenticationHookEventName["REFRESH_FINISHED"] = "refreshFinished";
ClientAuthenticationHookEventName["REFRESH_FAILED"] = "refreshFailed";
ClientAuthenticationHookEventName["HEADER_SET"] = "headerSet";
ClientAuthenticationHookEventName["HEADER_UNSET"] = "headerRemoved";
return ClientAuthenticationHookEventName;
}({});
//#endregion
//#region src/hook/utils.ts
function getClientRequestRetryState(config) {
const currentState = config.retry || {};
currentState.retryCount = currentState.retryCount || 0;
config.retry = currentState;
return currentState;
}
//#endregion
//#region src/hook/module.ts
const HOOK_SYMBOL = Symbol.for("ClientResponseHook");
var ClientAuthenticationHook = class extends EventEmitter {
isActive;
authorizationHeader;
clients;
creator;
options;
timer;
refreshPromise;
constructor(options) {
super();
this.isActive = true;
this.authorizationHeader = void 0;
this.clients = [];
options.timer ??= true;
this.options = options;
this.creator = options.tokenCreator;
}
enable() {
this.isActive = true;
}
disable() {
this.isActive = false;
}
setAuthorizationHeader(value) {
this.authorizationHeader = value;
for (let i = 0; i < this.clients.length; i++) this.clients[i].setAuthorizationHeader(value);
this.emit("headerSet");
}
unsetAuthorizationHeader() {
this.authorizationHeader = void 0;
for (let i = 0; i < this.clients.length; i++) this.clients[i].unsetAuthorizationHeader();
this.emit("headerRemoved");
}
isAttached(client) {
return HOOK_SYMBOL in client;
}
attach(client) {
if (this.authorizationHeader) client.setAuthorizationHeader(this.authorizationHeader);
else client.unsetAuthorizationHeader();
if (this.clients.indexOf(client) === -1) this.clients.push(client);
if (!this.isAttached(client)) Object.assign(client, { [HOOK_SYMBOL]: client.on(HookName.RESPONSE_ERROR, (err) => {
if (!this.isActive) return Promise.reject(err);
const { request } = err;
const currentState = getClientRequestRetryState(request);
if (currentState.retryCount > 0) return Promise.reject(err);
currentState.retryCount += 1;
const code = getClientErrorCode(err);
if (isJWKErrorCode(code)) {
this.unsetAuthorizationHeader();
if (request.headers) unsetHeader$1(request.headers, HeaderName.AUTHORIZATION);
return Promise.reject(err);
}
const refresh = () => this.refresh().then((response) => {
if (request.headers) setHeader$1(request.headers, HeaderName.AUTHORIZATION, stringifyAuthorizationHeader({
type: "Bearer",
token: response.access_token
}));
return client.request(request);
}).catch((err) => {
if (request.headers) unsetHeader$1(request.headers, HeaderName.AUTHORIZATION);
return Promise.reject(err);
});
if (isJWTErrorCode(code)) return refresh();
if (isObject(err.response)) {
if (err.response.status === 401) return refresh();
if (err.response.status === 403 && request.headers && !getHeader(request.headers, HeaderName.AUTHORIZATION)) return refresh();
}
return Promise.reject(err);
}) });
}
detach(client) {
client.unsetAuthorizationHeader();
const index = this.clients.indexOf(client);
if (index !== -1) this.clients.splice(index, 1);
if (this.isAttached(client)) {
client.off(HookName.RESPONSE_ERROR, client[HOOK_SYMBOL]);
delete client[HOOK_SYMBOL];
}
}
setTimer(expiresIn) {
if (!this.options.timer) return;
this.clearTimer();
const refreshInMs = (expiresIn - 60) * 1e3;
if (refreshInMs > 0) this.timer = setTimeout(async () => this.refresh(), refreshInMs);
}
clearTimer() {
if (this.timer) clearTimeout(this.timer);
}
/**
* Refresh token
*
* @throws ClientError
*/
async refresh() {
if (this.refreshPromise) return this.refreshPromise;
this.refreshPromise = this.creator();
return this.refreshPromise.then((response) => {
this.setTimer(response.expires_in);
this.emit("refreshFinished", response);
this.refreshPromise = void 0;
this.setAuthorizationHeader({
type: "Bearer",
token: response.access_token
});
return response;
}).catch((e) => {
if (isClientError$1(e)) this.emit("refreshFailed", e);
else this.emit("refreshFailed", null);
this.refreshPromise = void 0;
this.unsetAuthorizationHeader();
return Promise.reject(e);
});
}
};
//#endregion
//#region src/token-creator/presets/client.ts
/**
* Create token creator based on client credentials flow.
*
* @param credentials
* @param options
*/
function createClientTokenCreator(credentials, options = {}) {
const client = new Client(options.client);
return async () => client.token.createWithClientCredentials({
client_id: credentials.id,
client_secret: credentials.secret
});
}
//#endregion
//#region src/token-creator/presets/robot.ts
/**
* Create token creator based on robot credentials flow.
*
* @param credentials
* @param options
*/
function createRobotTokenCreator(credentials, options = {}) {
const client = new Client(options.client);
return async () => client.token.createWithRobotCredentials({
id: credentials.id,
secret: credentials.secret
});
}
//#endregion
//#region src/token-creator/presets/user.ts
/**
* Create token creator based on password flow.
*
* @param credentials
* @param options
*/
function createUserTokenCreator(credentials, options = {}) {
const client = new Client(options.client);
return async () => client.token.createWithPassword({
username: credentials.name,
password: credentials.password,
...credentials.realmId ? { realm_id: credentials.realmId } : {},
...credentials.realmName ? { realm_name: credentials.realmName } : {}
});
}
//#endregion
export { BaseAPI, Client, ClientAPI, ClientAuthenticationHook, ClientAuthenticationHookEventName, ClientPermissionAPI, ClientRoleAPI, ClientScopeAPI, CookieName, IdentityProviderAPI, IdentityProviderRoleMappingAPI, OAuth2AuthorizeAPI, OAuth2TokenAPI, OAuth2UserInfoAPI, PermissionAPI, PermissionPolicyAPI, PolicyAPI, RealmAPI, RobotAPI, RobotPermissionAPI, RobotRoleAPI, RoleAPI, RoleAttributeAPI, RolePermissionAPI, ScopeAPI, UserAPI, UserAttributeAPI, UserPermissionAPI, UserRoleAPI, createClientTokenCreator, createRobotTokenCreator, createUserTokenCreator, getClientErrorCode, getClientRequestRetryState, isClientError, setHeader, unsetHeader };
//# sourceMappingURL=index.mjs.map