@parra/parra-js-sdk 0.0.31 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var assert_1 = __importDefault(require("assert"));
7
+ var jwt = require('jsonwebtoken');
8
+ var AccessToken = /** @class */ (function () {
9
+ /**
10
+ * @constructor
11
+ * @param {string} tenantId - The account's unique ID to which access is scoped
12
+ * @param {string} apiKeyId - The signing key's unique ID
13
+ * @param {string} apiKeySecret - The apiKeySecret to sign the token with
14
+ * @param {object} options - ...
15
+ * @param {number} [options.ttl=3600] - Time to live in seconds
16
+ * @param {string} [options.identity] - The identity of the first person
17
+ */
18
+ function AccessToken(options) {
19
+ assert_1.default(options.tenantId, 'tenantId is required');
20
+ assert_1.default(options.apiKeyId, 'apiKeyId is required');
21
+ assert_1.default(options.apiKeySecret, 'apiKeySecret is required');
22
+ this.tenantId = options.tenantId;
23
+ this.apiKeyId = options.apiKeyId;
24
+ this.apiKeySecret = options.apiKeySecret;
25
+ this.ttl = options.ttl || 3600;
26
+ this.identity = String(options.identity);
27
+ this.nbf = options.nbf;
28
+ this.grants = [];
29
+ }
30
+ AccessToken.prototype.addGrant = function (grant) {
31
+ this.grants.push(grant);
32
+ };
33
+ AccessToken.prototype.toJwt = function (algorithm) {
34
+ algorithm = algorithm || AccessToken.DEFAULT_ALGORITHM;
35
+ if (!AccessToken.ALGORITHMS.includes(algorithm)) {
36
+ throw new Error('Algorithm not supported. Allowed values are ' + AccessToken.ALGORITHMS.join(', '));
37
+ }
38
+ var grants = this.grants.reduce(function (acc, grant) {
39
+ acc[grant.key] = grant.toPayload();
40
+ }, { identity: this.identity });
41
+ var now = Math.floor(Date.now() / 1000);
42
+ var payload = {
43
+ jti: this.apiKeyId + '-' + now,
44
+ grants: grants,
45
+ nbf: this.nbf,
46
+ };
47
+ var header = {
48
+ cty: 'parra-fpa;v=1',
49
+ typ: 'JWT',
50
+ };
51
+ return jwt.sign(payload, this.apiKeySecret, {
52
+ header: header,
53
+ algorithm: algorithm,
54
+ issuer: this.apiKeyId,
55
+ subject: this.tenantId,
56
+ expiresIn: this.ttl,
57
+ });
58
+ };
59
+ AccessToken.DEFAULT_ALGORITHM = 'HS256';
60
+ AccessToken.ALGORITHMS = ['HS256', 'HS384', 'HS512'];
61
+ return AccessToken;
62
+ }());
63
+ module.exports = AccessToken;