livelists-node-js
Version:
node.js server sdk for livelists
85 lines • 2.94 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.AccessToken = void 0;
const jwt = __importStar(require("jsonwebtoken"));
// 6 hours
const defaultTTL = 6 * 60 * 60;
class AccessToken {
/**
* Creates a new AccessToken
* @param apiKey API Key, can be set in env LIVEKIT_API_KEY
* @param apiSecret Secret, can be set in env LIVEKIT_API_SECRET
*/
constructor(apiKey, apiSecret, options) {
if (!apiKey) {
apiKey = process.env.LIVEKIT_API_KEY;
}
if (!apiSecret) {
apiSecret = process.env.LIVEKIT_API_SECRET;
}
if (!apiKey || !apiSecret) {
throw Error('api-key and api-secret must be set');
}
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.grants = {};
this.identity = options === null || options === void 0 ? void 0 : options.identity;
this.ttl = (options === null || options === void 0 ? void 0 : options.ttl) || defaultTTL;
if (options === null || options === void 0 ? void 0 : options.metadata) {
this.metadata = options.metadata;
}
}
/**
* Adds a video grant to this token.
* @param grant
*/
addGrant(grant) {
this.grants = grant;
}
/**
* Set metadata to be passed to the ParticipantClient, used only when joining the room
*/
set metadata(md) {
this.grants.metadata = md;
}
/**
* @returns JWT encoded token
*/
toJwt() {
const opts = {
issuer: this.apiKey,
expiresIn: this.ttl,
notBefore: 0,
};
if (this.identity) {
opts.subject = this.identity;
opts.jwtid = this.identity;
}
return jwt.sign(this.grants, this.apiSecret, opts);
}
}
exports.AccessToken = AccessToken;
//# sourceMappingURL=AccessToken.js.map
;