@talentsoft-opensource/widget-display-tool
Version:
Widget Simulator
48 lines (41 loc) • 1.44 kB
text/typescript
import { SecurityModeImplementation } from "./securityMode";
const sha1 = require("sha1");
function padleft(nbr: number) {
return nbr.toString().padStart(2, "0");
}
function formatDate(date: Date) {
const yyyy = date.getUTCFullYear();
const MM = padleft(date.getUTCMonth() + 1); //January is 0!
const dd = padleft(date.getUTCDate());
const HH = padleft(date.getUTCHours());
const mm = padleft(date.getUTCMinutes());
return `${yyyy}${MM}${dd}${HH}${mm}`;
}
function getDirectConnectHash(secretKey: string, login: string, date: Date) {
const tokenBase = secretKey + formatDate(date) + login;
return sha1(tokenBase).toUpperCase();
}
export class DirectConnectSecurityMode implements SecurityModeImplementation {
getSecurityHeaderParams(
secretKey: string,
login: string,
date: Date
): Record<string, string> {
const directConnectHash = getDirectConnectHash(secretKey, login, date);
return {
"x-login": login,
"x-api-token": directConnectHash
};
}
getSecurityQueryParams(
secretKey: string,
login: string,
date: Date
): Record<string, string> {
const directConnectHash = getDirectConnectHash(secretKey, login, date);
return {
"x-login": login,
"x-api-token": directConnectHash
};
}
}