UNPKG

1.94 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var definition_1 = require("@brontosaurus/definition");
4var crypto_1 = require("./crypto");
5var util_1 = require("./util");
6var BrontosaurusSign = (function () {
7 function BrontosaurusSign(key, body, secret) {
8 this._key = key;
9 this._body = body;
10 this._secret = secret;
11 }
12 BrontosaurusSign.create = function (key, body, secret) {
13 return new BrontosaurusSign(key, body, secret);
14 };
15 Object.defineProperty(BrontosaurusSign.prototype, "body", {
16 get: function () {
17 return this._body;
18 },
19 enumerable: true,
20 configurable: true
21 });
22 Object.defineProperty(BrontosaurusSign.prototype, "publicKey", {
23 get: function () {
24 return this._secret.public;
25 },
26 enumerable: true,
27 configurable: true
28 });
29 Object.defineProperty(BrontosaurusSign.prototype, "privateKey", {
30 get: function () {
31 return this._secret.private;
32 },
33 enumerable: true,
34 configurable: true
35 });
36 BrontosaurusSign.prototype.token = function (expireAt, issuedAt) {
37 var _this = this;
38 if (expireAt === void 0) { expireAt = Date.now(); }
39 if (issuedAt === void 0) { issuedAt = Date.now(); }
40 var header = util_1.definition.header({
41 alg: "RS256",
42 algorithm: "RSA-SHA256",
43 expireAt: expireAt,
44 issuedAt: issuedAt,
45 key: this._key,
46 });
47 var body = util_1.definition.body(this.body);
48 var encrypted = definition_1.BrontosaurusDefinition.signWith(header, body, function (content) { return crypto_1.signString(content, _this._secret.private); });
49 return encrypted;
50 };
51 return BrontosaurusSign;
52}());
53exports.BrontosaurusSign = BrontosaurusSign;