bc-node-sdk
Version:
BetterCommerce's NodeJS SDK encapsulates the base framework for all the Next.js applications.
112 lines (111 loc) • 6.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const request_util_1 = __importDefault(require("../../utils/request-util"));
const constants_1 = require("../../domain/constants");
const cipher_util_1 = __importDefault(require("../../utils/cipher-util"));
const token_util_1 = __importDefault(require("../../utils/token-util"));
const ApiService_1 = __importDefault(require("./ApiService"));
class BaseApiController {
/**
* Constructor
* @param {Contracts.Caching.ICacheManager} cacheManager - The cache manager to be used for caching data.
*/
constructor(cacheManager) {
this.cacheManager = cacheManager;
}
/**
* Retrieves the SSO user ID from the provided request cookies.
*
* @param cookies - The request cookies containing the SSO user ID.
* @returns An object with the SSO user ID extracted from the cookies.
*/
requestCookies(cookies) {
return request_util_1.default.getRequestCookies(cookies);
}
/**
* Retrieves the authorization header from the given cookies and returns it as a string.
*
* @param cookies - The cookies containing the SSO user id.
* @returns The authorization header string.
*/
/*protected async authorizationHeader(cookies: Object): Promise<any> {
const authHeaders = await RequestUtil.getAuthorizationHeader(cookies)
return authHeaders
}*/
/**
* Extracts the authorization header and additional user information from the provided cookies and headers.
*
* This function retrieves the SSO user ID from the cookies or headers, decrypts it, and fetches the
* authentication parameters from the cache. It constructs an authorization header and additional user
* details if the token is available. If cookies contain language and domain ID, they are also extracted.
*
* @param cookies - The cookies containing potential user information such as SSO user ID and language.
* @param headers - The headers containing potential user information, used if cookies do not provide it.
* @returns A promise that resolves to an object with the authorization header and additional user details
* such as DomainId, OrgId, UserName, UserId, Email, and OrgCode. Returns default values if no
* valid token is found.
*/
async authorizationHeaderFromRequestCookies(cookies, headers, cacheManager) {
var _a, _b, _c;
let token = constants_1.Defaults.String.Value;
let domainId = constants_1.Defaults.String.Value;
let language = constants_1.Defaults.String.Value;
//const cacheManager = CacheManagerUtil.getCacheManager(ParseUtil.stringToBoolean(process.env.REDIS_CACHE_ENABLED!))
if (cookies.size > 0) {
if (cookies.get(constants_1.Key.Cookie.USER_ID)) {
const ssoUserId = (_a = cookies.get(constants_1.Key.Cookie.USER_ID)) === null || _a === void 0 ? void 0 : _a.value;
const userId = cipher_util_1.default.decrypt(ssoUserId);
if (userId) {
const authParams = await cacheManager.get(userId);
token = (authParams === null || authParams === void 0 ? void 0 : authParams.apitoken) || constants_1.Defaults.String.Value;
}
}
if (cookies.get(constants_1.Key.Cookie.LANGUAGE)) {
language = (_b = cookies.get(constants_1.Key.Cookie.LANGUAGE)) === null || _b === void 0 ? void 0 : _b.value;
}
if (cookies.get(constants_1.Key.Cookie.DOMAIN_ID)) {
domainId = (_c = cookies.get(constants_1.Key.Cookie.DOMAIN_ID)) === null || _c === void 0 ? void 0 : _c.value;
}
}
if (!token && headers) {
const ssoUserId = headers.get(constants_1.Key.Cookie.USER_ID);
const userId = cipher_util_1.default.decrypt(ssoUserId);
if (userId) {
const authParams = await cacheManager.get(userId);
token = (authParams === null || authParams === void 0 ? void 0 : authParams.apitoken) || constants_1.Defaults.String.Value;
}
}
if (headers) {
if (!language) {
language = headers.get(constants_1.Key.Cookie.LANGUAGE);
}
if (!domainId && (headers === null || headers === void 0 ? void 0 : headers.has(constants_1.Key.Cookie.DOMAIN_ID))) {
domainId = headers.get(constants_1.Key.Cookie.DOMAIN_ID);
}
}
if (token) {
const tokenValue = token_util_1.default.decodeToken(token);
if (tokenValue) {
return { Authorization: `Bearer ${token}`, DomainId: (tokenValue === null || tokenValue === void 0 ? void 0 : tokenValue.DomainId) || constants_1.Defaults.String.Value, OrgId: (tokenValue === null || tokenValue === void 0 ? void 0 : tokenValue.OrgId) || constants_1.Defaults.String.Value, UserName: (tokenValue === null || tokenValue === void 0 ? void 0 : tokenValue.Email) || constants_1.Defaults.String.Value, UserId: (tokenValue === null || tokenValue === void 0 ? void 0 : tokenValue.UserId) || constants_1.Defaults.Guid.Value, Email: (tokenValue === null || tokenValue === void 0 ? void 0 : tokenValue.Email) || constants_1.Defaults.String.Value, OrgCode: (tokenValue === null || tokenValue === void 0 ? void 0 : tokenValue.OrgCode) || constants_1.Defaults.Guid.Value, };
}
else {
return { Authorization: `Bearer ${token}` };
}
}
return constants_1.Defaults.Object.Value;
}
/**
* Returns an instance of the ApiService configured with the specified base URL.
*
* @param baseUrl - The base URL to be used for the API service instance.
* @returns An instance of the ApiService class.
*/
serviceInstance(baseUrl) {
const instance = ApiService_1.default.getInstance(process.env.CLIENT_ID, process.env.SHARED_SECRET, baseUrl, process.env.AUTH_BASE_URL);
return instance;
}
}
exports.default = BaseApiController;