UNPKG

@splitsoftware/splitio-commons

Version:
44 lines (43 loc) 1.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hashUserKey = exports.authenticateFactory = void 0; var objectAssign_1 = require("../../../utils/lang/objectAssign"); var base64_1 = require("../../../utils/base64"); var jwt_1 = require("../../../utils/jwt"); var murmur3_1 = require("../../../utils/murmur3/murmur3"); /** * Factory of authentication function. * * @param fetchAuth - `SplitAPI.fetchAuth` endpoint */ function authenticateFactory(fetchAuth) { /** * Run authentication requests to Auth Server, and returns a promise that resolves with the decoded JTW token. * @param userKeys - set of user Keys to track membership updates. It is undefined for server-side API. */ return function authenticate(userKeys) { return fetchAuth(userKeys) .then(function (resp) { return resp.json(); }) .then(function (json) { if (json.token) { // empty token when `"pushEnabled": false` var decodedToken = (0, jwt_1.decodeJWTtoken)(json.token); if (typeof decodedToken.iat !== 'number' || typeof decodedToken.exp !== 'number') throw new Error('token properties "issuedAt" (iat) or "expiration" (exp) are missing or invalid'); var channels = JSON.parse(decodedToken['x-ably-capability']); return (0, objectAssign_1.objectAssign)({ decodedToken: decodedToken, channels: channels }, json); } return json; }); }; } exports.authenticateFactory = authenticateFactory; /** * Returns the hash of a given user key */ function hashUserKey(userKey) { return (0, base64_1.encodeToBase64)((0, murmur3_1.hash)(userKey, 0).toString()); } exports.hashUserKey = hashUserKey;