UNPKG

@intuitionrobotics/user-account

Version:
116 lines 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JWTBuilder = exports.TYP_DEFAULT = exports.AUDIENCE = exports.JWT_ID = exports.ISSUED_AT = exports.NOT_BEFORE = exports.EXPIRES_AT = exports.SUBJECT = exports.ISSUER = exports.KEY_ID = exports.TYPE = exports.CONTENT_TYPE = exports.ALGORITHM = void 0; const ts_common_1 = require("@intuitionrobotics/ts-common"); const jws_1 = require("jws"); const SecretsModule_1 = require("./SecretsModule"); //Header exports.ALGORITHM = "alg"; exports.CONTENT_TYPE = "cty"; exports.TYPE = "typ"; exports.KEY_ID = "kid"; //Payload exports.ISSUER = "iss"; exports.SUBJECT = "sub"; exports.EXPIRES_AT = "exp"; exports.NOT_BEFORE = "nbf"; exports.ISSUED_AT = "iat"; exports.JWT_ID = "jti"; exports.AUDIENCE = "aud"; exports.TYP_DEFAULT = "JWT"; class JWTBuilder { constructor(alg) { this.payload = {}; // End Generic this.setContentType = (cty) => { this.header[exports.CONTENT_TYPE] = cty; return this; }; this.setType = (typ) => { this.header[exports.TYPE] = typ; return this; }; this.setKeyID = (kid) => { this.header[exports.KEY_ID] = kid; return this; }; this.assertAlg(alg); this.header = { [exports.ALGORITHM]: alg }; } // Generic addClaims(claims) { Object.keys(claims).forEach(k => this.addClaim(k, claims[k])); return this; } addClaim(key, value) { this.payload[key] = value; return this; } addHeader(key, value) { this.header[key] = value; return this; } // Payload setIssuer(iss) { this.payload[exports.ISSUER] = iss; return this; } setSub(iss) { this.payload[exports.SUBJECT] = iss; return this; } setExpiration(exp) { this.payload[exports.EXPIRES_AT] = exp; return this; } setNotBefore(nbf) { this.payload[exports.NOT_BEFORE] = nbf; return this; } setIssuedAt() { this.payload[exports.ISSUED_AT] = Math.floor((0, ts_common_1.currentTimeMillies)() / 1000); } setJWTID(jti) { this.payload[exports.JWT_ID] = jti; return this; } setAudience(aud) { this.payload[exports.AUDIENCE] = aud; return this; } // End Payload getIssuer() { return this.payload[exports.ISSUER]; } getAlgorithm() { return this.header[exports.ALGORITHM]; } getExpiration() { return this.payload[exports.EXPIRES_AT]; } getType() { return this.header[exports.TYPE]; } assertAlg(alg) { const foundAlg = jws_1.ALGORITHMS.find(a => a === alg); if (!foundAlg) throw new ts_common_1.BadImplementationException(`Algorithm with name ${alg} is not valid`); } build(secret) { this.setIssuedAt(); if (!this.getType()) this.setType(exports.TYP_DEFAULT); if (!this.getIssuer()) // TODO move the config to the module which I need to create this.setIssuer(SecretsModule_1.SecretsModule.getIss()); if (!this.getExpiration()) throw new ts_common_1.BadImplementationException("Missing expiration, cannot build a valid JWT without this value"); if (!this.getAlgorithm()) throw new ts_common_1.BadImplementationException("Missing algorithm, cannot build a valid JWT without this value"); return (0, jws_1.sign)({ secret, payload: this.payload, header: this.header }); } } exports.JWTBuilder = JWTBuilder; //# sourceMappingURL=JWTBuilder.js.map