@denodeio/seshat 0.0.33 → 0.0.35

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.
@@ -1,36 +1,4 @@
1
- import { PublicKey, Secret } from "jsonwebtoken";
2
- type JwsSignature = {
3
- protected: string;
4
- header: {
5
- kid: string;
6
- };
7
- signature: string;
8
- };
9
- type JwsPayload = {
10
- payload: string;
11
- signatures: JwsSignature[];
12
- };
13
- export type Keychain = {
14
- algorithm: string;
15
- value: Secret | PublicKey;
16
- };
17
- type JwtPayload<T> = {
18
- iss: string;
19
- exp: number;
20
- jti: string;
21
- event: {
22
- name: string;
23
- record: T;
24
- };
25
- iat: number;
26
- };
1
+ export * from "./middleware";
27
2
  export * from "./signer";
28
- type OptionsInput = {
29
- fieldName?: string;
30
- barongJwtPublicKey?: string;
31
- jwtPublicKey?: string;
32
- issuer?: string;
33
- };
34
- export declare const sessionVerifier: (options: OptionsInput) => (req: any, res: any, next: any) => void;
35
- export declare const managementSigner: (options: any) => (req: any, res: any, next: any) => void;
36
- export declare const validateJws: <T>(key: Keychain, input: JwsPayload) => JwtPayload<T> | undefined;
3
+ export * from "./validate";
4
+ export * from "./types";
@@ -0,0 +1,9 @@
1
+ export declare const managementSigner: (options: any) => (req: any, res: any, next: any) => void;
2
+ type OptionsInput = {
3
+ fieldName?: string;
4
+ barongJwtPublicKey?: string;
5
+ jwtPublicKey?: string;
6
+ issuer?: string;
7
+ };
8
+ export declare const sessionVerifier: (options: OptionsInput) => (req: any, res: any, next: any) => void;
9
+ export {};
@@ -0,0 +1,6 @@
1
+ import { PublicKey, Secret } from "jsonwebtoken";
2
+ export type Key = {
3
+ algorithm: string;
4
+ value: Secret | PublicKey;
5
+ };
6
+ export type Keychain = Map<string, Key>;
@@ -0,0 +1,7 @@
1
+ type ProtectedHeader = {
2
+ alg: string;
3
+ typ: string;
4
+ };
5
+ export declare const base64Decode: (base64: string) => string;
6
+ export declare const parseProtectedHeader: (protectedHeader: string) => ProtectedHeader;
7
+ export {};
@@ -0,0 +1,30 @@
1
+ import jwt from "jsonwebtoken";
2
+ import { Key, Keychain } from "./types";
3
+ type JwsSignature = {
4
+ protected: string;
5
+ header: {
6
+ kid: string;
7
+ };
8
+ signature: string;
9
+ };
10
+ type JwsPayload = {
11
+ payload: string;
12
+ signatures: JwsSignature[];
13
+ };
14
+ type JwtPayload<T> = {
15
+ iss: string;
16
+ exp: number;
17
+ jti: string;
18
+ event: {
19
+ name: string;
20
+ record: T;
21
+ };
22
+ iat: number;
23
+ };
24
+ export declare const validateJws: <T>(key: Key, input: JwsPayload) => JwtPayload<T> | undefined;
25
+ export declare const validateJwsMultisig: (keychain: Keychain, input: JwsPayload) => {
26
+ payload: string | jwt.JwtPayload | null;
27
+ verified: string[];
28
+ unverified: string[];
29
+ };
30
+ export {};
@@ -1,4 +1,13 @@
1
- import { Secret, PublicKey } from 'jsonwebtoken';
1
+ import jwt, { Secret, PublicKey } from 'jsonwebtoken';
2
+
3
+ declare const managementSigner: (options: any) => (req: any, res: any, next: any) => void;
4
+ type OptionsInput = {
5
+ fieldName?: string;
6
+ barongJwtPublicKey?: string;
7
+ jwtPublicKey?: string;
8
+ issuer?: string;
9
+ };
10
+ declare const sessionVerifier: (options: OptionsInput) => (req: any, res: any, next: any) => void;
2
11
 
3
12
  type SignJwsResponse = {
4
13
  payload: string;
@@ -14,6 +23,12 @@ declare function signJws(payload: string, options: any): SignJwsResponse;
14
23
  declare function signPayload(payload: any, options: any): string;
15
24
  declare function signData(payload: object, options: any): SignJwsResponse;
16
25
 
26
+ type Key = {
27
+ algorithm: string;
28
+ value: Secret | PublicKey;
29
+ };
30
+ type Keychain = Map<string, Key>;
31
+
17
32
  type JwsSignature = {
18
33
  protected: string;
19
34
  header: {
@@ -25,10 +40,6 @@ type JwsPayload = {
25
40
  payload: string;
26
41
  signatures: JwsSignature[];
27
42
  };
28
- type Keychain = {
29
- algorithm: string;
30
- value: Secret | PublicKey;
31
- };
32
43
  type JwtPayload<T> = {
33
44
  iss: string;
34
45
  exp: number;
@@ -39,15 +50,11 @@ type JwtPayload<T> = {
39
50
  };
40
51
  iat: number;
41
52
  };
42
-
43
- type OptionsInput = {
44
- fieldName?: string;
45
- barongJwtPublicKey?: string;
46
- jwtPublicKey?: string;
47
- issuer?: string;
53
+ declare const validateJws: <T>(key: Key, input: JwsPayload) => JwtPayload<T> | undefined;
54
+ declare const validateJwsMultisig: (keychain: Keychain, input: JwsPayload) => {
55
+ payload: string | jwt.JwtPayload | null;
56
+ verified: string[];
57
+ unverified: string[];
48
58
  };
49
- declare const sessionVerifier: (options: OptionsInput) => (req: any, res: any, next: any) => void;
50
- declare const managementSigner: (options: any) => (req: any, res: any, next: any) => void;
51
- declare const validateJws: <T>(key: Keychain, input: JwsPayload) => JwtPayload<T> | undefined;
52
59
 
53
- export { type Keychain, managementSigner, sessionVerifier, signData, signJws, signPayload, validateJws };
60
+ export { type Key, type Keychain, managementSigner, sessionVerifier, signData, signJws, signPayload, validateJws, validateJwsMultisig };
@@ -13,6 +13,8 @@ function getDefaultExportFromCjs (x) {
13
13
 
14
14
  var src = {};
15
15
 
16
+ var middleware = {};
17
+
16
18
  var jws$3 = {};
17
19
 
18
20
  var safeBuffer = {exports: {}};
@@ -6241,7 +6243,7 @@ const registered_claims_schema = {
6241
6243
  nbf: { isValid: isNumber, message: '"nbf" should be a number of seconds' }
6242
6244
  };
6243
6245
 
6244
- function validate(schema, allowUnknown, object, parameterName) {
6246
+ function validate$1(schema, allowUnknown, object, parameterName) {
6245
6247
  if (!isPlainObject(object)) {
6246
6248
  throw new Error('Expected "' + parameterName + '" to be a plain object.');
6247
6249
  }
@@ -6261,11 +6263,11 @@ function validate(schema, allowUnknown, object, parameterName) {
6261
6263
  }
6262
6264
 
6263
6265
  function validateOptions(options) {
6264
- return validate(sign_options_schema, false, options, 'options');
6266
+ return validate$1(sign_options_schema, false, options, 'options');
6265
6267
  }
6266
6268
 
6267
6269
  function validatePayload(payload) {
6268
- return validate(registered_claims_schema, true, payload, 'payload');
6270
+ return validate$1(registered_claims_schema, true, payload, 'payload');
6269
6271
  }
6270
6272
 
6271
6273
  const options_to_payload = {
@@ -6467,7 +6469,7 @@ var signer = {};
6467
6469
 
6468
6470
  Object.defineProperty(signer, "__esModule", { value: true });
6469
6471
  signer.signData = signer.signPayload = signer.signJws = void 0;
6470
- const jsonwebtoken_1 = jsonwebtoken;
6472
+ const jsonwebtoken_1$1 = jsonwebtoken;
6471
6473
  const crypto_1 = require$$2;
6472
6474
  // export type CSignOptions = {
6473
6475
  // privateKey: Secret
@@ -6503,7 +6505,7 @@ function signPayload(payload, options) {
6503
6505
  ...defaultOptions,
6504
6506
  ...options
6505
6507
  };
6506
- const token = (0, jsonwebtoken_1.sign)({
6508
+ const token = (0, jsonwebtoken_1$1.sign)({
6507
6509
  iss: options.issuer,
6508
6510
  exp: Math.round(Date.now() / 1000) + mergedOptions.jwtExpireSeconds,
6509
6511
  jti: (0, crypto_1.randomUUID)(),
@@ -6520,122 +6522,200 @@ function signData(payload, options) {
6520
6522
  }
6521
6523
  signer.signData = signData;
6522
6524
 
6525
+ var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
6526
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6527
+ };
6528
+ Object.defineProperty(middleware, "__esModule", { value: true });
6529
+ middleware.sessionVerifier = middleware.managementSigner = void 0;
6530
+ const jsonwebtoken_1 = __importDefault(jsonwebtoken);
6531
+ const signer_1 = signer;
6532
+ const managementSigner = function (options) {
6533
+ if (!options.privateKey)
6534
+ throw new Error("Application's private key should be set");
6535
+ const middleware = function (req, res, next) {
6536
+ if (!req.management.payload)
6537
+ console.error("No payload to be signed");
6538
+ const payload = req.management.payload;
6539
+ let signedPayload;
6540
+ try {
6541
+ signedPayload = (0, signer_1.signPayload)(payload, options);
6542
+ }
6543
+ catch (error) {
6544
+ res.status(403);
6545
+ res.send(`Unable to sign payload: ${error}`);
6546
+ return;
6547
+ }
6548
+ try {
6549
+ req.body = (0, signer_1.signJws)(signedPayload, options);
6550
+ }
6551
+ catch (error) {
6552
+ res.status(403);
6553
+ res.send(`Unable to correctly format signed payload: ${error}`);
6554
+ }
6555
+ next();
6556
+ };
6557
+ return middleware;
6558
+ };
6559
+ middleware.managementSigner = managementSigner;
6560
+ const sessionVerifier = function (options) {
6561
+ const { fieldName = "session", ...actualOptions } = options;
6562
+ if (!options || (!options.barongJwtPublicKey && !options.jwtPublicKey)) {
6563
+ throw new Error("JWT Public key should be set");
6564
+ }
6565
+ const jwtPublicKey = options.barongJwtPublicKey || options.jwtPublicKey;
6566
+ const defaultOptions = {
6567
+ algorithms: ["RS256"],
6568
+ issuer: "auth"
6569
+ };
6570
+ const verificationOptions = { ...defaultOptions, ...actualOptions };
6571
+ const middleware = function (req, res, next) {
6572
+ let authHeader;
6573
+ try {
6574
+ authHeader = req.headers.authorization.split("Bearer ")[1];
6575
+ }
6576
+ catch (error) {
6577
+ res.status(401);
6578
+ res.send("Signature verification raised: Authorization header is missing or malformed");
6579
+ return;
6580
+ }
6581
+ if (!jwtPublicKey) {
6582
+ throw new Error("JWT Public key should be set");
6583
+ }
6584
+ try {
6585
+ req[fieldName] = jsonwebtoken_1.default.verify(authHeader, jwtPublicKey, verificationOptions);
6586
+ }
6587
+ catch (error) {
6588
+ res.status(403);
6589
+ res.send(`Signature verification raised: ${error}`);
6590
+ return;
6591
+ }
6592
+ next();
6593
+ };
6594
+ return middleware;
6595
+ };
6596
+ middleware.sessionVerifier = sessionVerifier;
6597
+
6598
+ var validate = {};
6599
+
6600
+ var utils = {};
6601
+
6523
6602
  (function (exports) {
6524
- var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6525
- if (k2 === undefined) k2 = k;
6526
- var desc = Object.getOwnPropertyDescriptor(m, k);
6527
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6528
- desc = { enumerable: true, get: function() { return m[k]; } };
6529
- }
6530
- Object.defineProperty(o, k2, desc);
6531
- }) : (function(o, m, k, k2) {
6532
- if (k2 === undefined) k2 = k;
6533
- o[k2] = m[k];
6534
- }));
6535
- var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
6536
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
6537
- };
6538
- var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
6539
- return (mod && mod.__esModule) ? mod : { "default": mod };
6540
- };
6541
6603
  Object.defineProperty(exports, "__esModule", { value: true });
6542
- exports.validateJws = exports.managementSigner = exports.sessionVerifier = void 0;
6543
- const jsonwebtoken_1 = __importDefault(jsonwebtoken);
6544
- const signer_1 = signer;
6545
- __exportStar(signer, exports);
6546
- const sessionVerifier = function (options) {
6547
- const { fieldName = "session", ...actualOptions } = options;
6548
- if (!options || (!options.barongJwtPublicKey && !options.jwtPublicKey)) {
6549
- throw new Error("JWT Public key should be set");
6550
- }
6551
- const jwtPublicKey = options.barongJwtPublicKey || options.jwtPublicKey;
6552
- const defaultOptions = {
6553
- algorithms: ["RS256"],
6554
- issuer: "auth"
6555
- };
6556
- const verificationOptions = { ...defaultOptions, ...actualOptions };
6557
- const middleware = function (req, res, next) {
6558
- let authHeader;
6559
- try {
6560
- authHeader = req.headers.authorization.split("Bearer ")[1];
6561
- }
6562
- catch (error) {
6563
- res.status(401);
6564
- res.send("Signature verification raised: Authorization header is missing or malformed");
6565
- return;
6566
- }
6567
- if (!jwtPublicKey) {
6568
- throw new Error("JWT Public key should be set");
6569
- }
6570
- try {
6571
- req[fieldName] = jsonwebtoken_1.default.verify(authHeader, jwtPublicKey, verificationOptions);
6572
- }
6573
- catch (error) {
6574
- res.status(403);
6575
- res.send(`Signature verification raised: ${error}`);
6576
- return;
6577
- }
6578
- next();
6579
- };
6580
- return middleware;
6581
- };
6582
- exports.sessionVerifier = sessionVerifier;
6583
- const managementSigner = function (options) {
6584
- if (!options.privateKey)
6585
- throw new Error("Application's private key should be set");
6586
- const middleware = function (req, res, next) {
6587
- if (!req.management.payload)
6588
- console.error("No payload to be signed");
6589
- const payload = req.management.payload;
6590
- let signedPayload;
6591
- try {
6592
- signedPayload = (0, signer_1.signPayload)(payload, options);
6593
- }
6594
- catch (error) {
6595
- res.status(403);
6596
- res.send(`Unable to sign payload: ${error}`);
6597
- return;
6598
- }
6599
- try {
6600
- req.body = (0, signer_1.signJws)(signedPayload, options);
6601
- }
6602
- catch (error) {
6603
- res.status(403);
6604
- res.send(`Unable to correctly format signed payload: ${error}`);
6605
- }
6606
- next();
6607
- };
6608
- return middleware;
6609
- };
6610
- exports.managementSigner = managementSigner;
6604
+ exports.parseProtectedHeader = exports.base64Decode = void 0;
6611
6605
  const base64Decode = (base64) => {
6612
6606
  return Buffer.from(base64, "base64").toString("utf8");
6613
6607
  };
6608
+ exports.base64Decode = base64Decode;
6614
6609
  const parseProtectedHeader = (protectedHeader) => {
6615
- return JSON.parse(base64Decode(protectedHeader));
6610
+ return JSON.parse((0, exports.base64Decode)(protectedHeader));
6616
6611
  };
6612
+ exports.parseProtectedHeader = parseProtectedHeader;
6613
+ } (utils));
6614
+
6615
+ (function (exports) {
6616
+ var __importDefault = (commonjsGlobal && commonjsGlobal.__importDefault) || function (mod) {
6617
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6618
+ };
6619
+ Object.defineProperty(exports, "__esModule", { value: true });
6620
+ exports.validateJwsMultisig = exports.validateJws = void 0;
6621
+ const jsonwebtoken_1 = __importDefault(jsonwebtoken);
6622
+ const utils_1 = utils;
6617
6623
  const validateJws = (key, input) => {
6618
6624
  for (const signature of input.signatures) {
6619
- const decodedProtectedHeader = parseProtectedHeader(signature.protected);
6625
+ const decodedProtectedHeader = (0, utils_1.parseProtectedHeader)(signature.protected);
6620
6626
  if (key === undefined) {
6621
6627
  throw new Error("Invalid key");
6622
6628
  }
6623
6629
  if (key.algorithm !== decodedProtectedHeader.alg) {
6624
6630
  throw new Error("Algorithm mismatch");
6625
6631
  }
6626
- try {
6627
- const verified = jsonwebtoken_1.default.verify(`${signature.protected}.${input.payload}.${signature.signature}`, key.value,
6628
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
6629
- { algorithms: [key.algorithm] });
6630
- return verified;
6632
+ const verified = jsonwebtoken_1.default.verify(`${signature.protected}.${input.payload}.${signature.signature}`, key.value,
6633
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
6634
+ { algorithms: [key.algorithm] });
6635
+ return verified;
6636
+ }
6637
+ };
6638
+ exports.validateJws = validateJws;
6639
+ /*
6640
+ * Verifies JWT.
6641
+ *
6642
+ * @param jwt [Hash]
6643
+ * The JWT in the format as defined in RFC 7515.
6644
+ * Example:
6645
+ * { "payload" => "eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFtcGxlLmNvbS9pc19yb290Ijp0cnVlfQ",
6646
+ * "signatures" => [
6647
+ * { "protected" => "eyJhbGciOiJSUzI1NiJ9",
6648
+ * "header" => { "kid" => "2010-12-29" },
6649
+ * "signature" => "cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqvhJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrBp0igcN_IoypGlUPQGe77Rw"
6650
+ * },
6651
+ * { "protected" => "eyJhbGciOiJFUzI1NiJ9",
6652
+ * "header" => { "kid" => "e9bc097a-ce51-4036-9562-d2ade882db0d" },
6653
+ * "signature" => "DtEhU3ljbEg8L38VWAfUAqOyKAM6-Xx-F4GawxaepmXFCgfTjDxw5djxLa8ISlSApmWQxfKTUJqPP3-Kg6NU1Q"
6654
+ * }
6655
+ * ]
6656
+ * }
6657
+ * @param public_keychain [Hash]
6658
+ * The hash which consists of pairs: key ID => public key.
6659
+ * The key may be presented as string in PEM format or as instance of {OpenSSL::PKey::PKey}.
6660
+ * The implementation only verifies signatures for which public key exists in keychain.
6661
+ * @param options [Hash]
6662
+ * The rules for verifying JWT. The variable «algorithms» is always overwritten by the value from JWS header.
6663
+ * @return [Hash]
6664
+ * The returning value contains payload, list of verified, and unverified signatures (key ID).
6665
+ * Example:
6666
+ * { payload: { sub: "session", profile: { email: "username@mailbox.example" },
6667
+ * verified: [:"backend-1.mycompany.example", :"backend-3.mycompany.example"],
6668
+ * unverified: [:"backend-2.mycompany.example"] }
6669
+ * }
6670
+ * @raise [JWT::DecodeError]
6671
+ */
6672
+ const validateJwsMultisig = (keychain, input) => {
6673
+ const verified = [];
6674
+ const unverified = [];
6675
+ const payload = jsonwebtoken_1.default.decode(input.payload);
6676
+ for (const signature of input.signatures) {
6677
+ const key = keychain.get(signature.header.kid);
6678
+ if (key) {
6679
+ (0, exports.validateJws)(key, input);
6680
+ verified.push(signature.header.kid);
6631
6681
  }
6632
- catch (error) {
6633
- console.error(error);
6634
- return undefined;
6682
+ else {
6683
+ unverified.push(signature.header.kid);
6635
6684
  }
6636
6685
  }
6686
+ return {
6687
+ payload,
6688
+ verified,
6689
+ unverified
6690
+ };
6691
+ };
6692
+ exports.validateJwsMultisig = validateJwsMultisig;
6693
+ } (validate));
6694
+
6695
+ var types = {};
6696
+
6697
+ Object.defineProperty(types, "__esModule", { value: true });
6698
+
6699
+ (function (exports) {
6700
+ var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6701
+ if (k2 === undefined) k2 = k;
6702
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6703
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6704
+ desc = { enumerable: true, get: function() { return m[k]; } };
6705
+ }
6706
+ Object.defineProperty(o, k2, desc);
6707
+ }) : (function(o, m, k, k2) {
6708
+ if (k2 === undefined) k2 = k;
6709
+ o[k2] = m[k];
6710
+ }));
6711
+ var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
6712
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
6637
6713
  };
6638
- exports.validateJws = validateJws;
6714
+ Object.defineProperty(exports, "__esModule", { value: true });
6715
+ __exportStar(middleware, exports);
6716
+ __exportStar(signer, exports);
6717
+ __exportStar(validate, exports);
6718
+ __exportStar(types, exports);
6639
6719
  } (src));
6640
6720
 
6641
6721
  var index = /*@__PURE__*/getDefaultExportFromCjs(src);