@picahq/authkit-node
Version:
Secure token generation for Pica AuthKit
60 lines • 4.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createEventLinkTokenApi = exports.generateId = void 0;
const tslib_1 = require("tslib");
const axios_1 = tslib_1.__importDefault(require("axios"));
const generateId = (prefix) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _a;
const apiBaseUrl = process.env.PICA_API_BASE_URL || "https://api.picaos.com/v1";
try {
const response = yield axios_1.default.get(`${apiBaseUrl}/public/generate-id/${prefix}`);
const id = (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a.id;
return id;
}
catch (error) {
throw new Error('Failed to generate id from api');
}
});
exports.generateId = generateId;
const createEventLinkTokenApi = (headers, url, secret, payload) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
var _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
try {
const settings = yield axios_1.default.post(`${url}/v1/settings/get`, payload, { headers });
const link = yield axios_1.default.post(`${url}/v1/event-links/create`, Object.assign(Object.assign({}, payload), { environment: secret.startsWith('sk_test') ? 'test' : 'live', usageSource: 'sdk' }), { headers });
const apiBaseUrl = process.env.PICA_API_BASE_URL || "https://api.picaos.com/v1";
const connectionDefinitionUrl = `${apiBaseUrl}/public/connection-definitions`;
const connectionDefinitions = yield axios_1.default.get(`${connectionDefinitionUrl}?limit=1000&skip=0`);
const isLiveSecret = secret.includes('sk_live');
const activeConnectionDefinitionsData = (_c = (_b = connectionDefinitions === null || connectionDefinitions === void 0 ? void 0 : connectionDefinitions.data) === null || _b === void 0 ? void 0 : _b.rows) === null || _c === void 0 ? void 0 : _c.filter((definition) => definition === null || definition === void 0 ? void 0 : definition.active);
const connectedPlatforms = (_e = (_d = settings === null || settings === void 0 ? void 0 : settings.data) === null || _d === void 0 ? void 0 : _d.connectedPlatforms) === null || _e === void 0 ? void 0 : _e.filter((platform) => {
return ((activeConnectionDefinitionsData === null || activeConnectionDefinitionsData === void 0 ? void 0 : activeConnectionDefinitionsData.find((definition) => (definition === null || definition === void 0 ? void 0 : definition._id) === (platform === null || platform === void 0 ? void 0 : platform.connectionDefinitionId))) && (platform === null || platform === void 0 ? void 0 : platform.active));
});
const connectedPlatformsFiltered = connectedPlatforms === null || connectedPlatforms === void 0 ? void 0 : connectedPlatforms.filter((platform) => isLiveSecret
? (platform === null || platform === void 0 ? void 0 : platform.environment) === 'live'
: (platform === null || platform === void 0 ? void 0 : platform.environment) === 'test' || !(platform === null || platform === void 0 ? void 0 : platform.environment));
const sessionId = yield (0, exports.generateId)('session_id');
const tokenPayload = {
linkSettings: {
connectedPlatforms: connectedPlatformsFiltered !== null && connectedPlatformsFiltered !== void 0 ? connectedPlatformsFiltered : [],
eventIncToken: (_f = link === null || link === void 0 ? void 0 : link.data) === null || _f === void 0 ? void 0 : _f.token,
},
identity: (_g = link === null || link === void 0 ? void 0 : link.data) === null || _g === void 0 ? void 0 : _g.identity,
identityType: (_h = link === null || link === void 0 ? void 0 : link.data) === null || _h === void 0 ? void 0 : _h.identityType,
group: (_j = link === null || link === void 0 ? void 0 : link.data) === null || _j === void 0 ? void 0 : _j.group,
label: (_k = link === null || link === void 0 ? void 0 : link.data) === null || _k === void 0 ? void 0 : _k.label,
environment: secret.startsWith('sk_test') ? 'test' : 'live',
expiresAt: new Date().getTime() + 5 * 1000 * 60,
sessionId,
features: (_l = settings === null || settings === void 0 ? void 0 : settings.data) === null || _l === void 0 ? void 0 : _l.features,
};
const token = yield axios_1.default.post(`${url}/v1/embed-tokens/create`, tokenPayload, { headers });
return token.data;
}
catch (error) {
if (axios_1.default.isAxiosError(error)) {
return (_m = error.response) === null || _m === void 0 ? void 0 : _m.data;
}
}
});
exports.createEventLinkTokenApi = createEventLinkTokenApi;
//# sourceMappingURL=link.api.js.map