@denodeio/seshat 0.0.32 → 0.0.34

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
- /// <reference types="node" />
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
- type Keychain = {
14
- algorithm: string;
15
- value: Buffer;
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,28 @@
1
+ import { Key, Keychain } from "./types";
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
+ type JwtPayload<T> = {
14
+ iss: string;
15
+ exp: number;
16
+ jti: string;
17
+ event: {
18
+ name: string;
19
+ record: T;
20
+ };
21
+ iat: number;
22
+ };
23
+ export declare const validateJws: <T>(key: Key, input: JwsPayload) => JwtPayload<T> | undefined;
24
+ export declare const validateJwsMultisig: <T>(keychain: Keychain, input: JwsPayload) => {
25
+ verified: string[];
26
+ unverified: string[];
27
+ };
28
+ export {};
@@ -1,4 +1,14 @@
1
- /// <reference types="node" />
1
+ import { 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;
11
+
2
12
  type SignJwsResponse = {
3
13
  payload: string;
4
14
  signatures: {
@@ -13,6 +23,12 @@ declare function signJws(payload: string, options: any): SignJwsResponse;
13
23
  declare function signPayload(payload: any, options: any): string;
14
24
  declare function signData(payload: object, options: any): SignJwsResponse;
15
25
 
26
+ type Key = {
27
+ algorithm: string;
28
+ value: Secret | PublicKey;
29
+ };
30
+ type Keychain = Map<string, Key>;
31
+
16
32
  type JwsSignature = {
17
33
  protected: string;
18
34
  header: {
@@ -24,10 +40,6 @@ type JwsPayload = {
24
40
  payload: string;
25
41
  signatures: JwsSignature[];
26
42
  };
27
- type Keychain = {
28
- algorithm: string;
29
- value: Buffer;
30
- };
31
43
  type JwtPayload<T> = {
32
44
  iss: string;
33
45
  exp: number;
@@ -38,15 +50,10 @@ type JwtPayload<T> = {
38
50
  };
39
51
  iat: number;
40
52
  };
41
-
42
- type OptionsInput = {
43
- fieldName?: string;
44
- barongJwtPublicKey?: string;
45
- jwtPublicKey?: string;
46
- issuer?: string;
53
+ declare const validateJws: <T>(key: Key, input: JwsPayload) => JwtPayload<T> | undefined;
54
+ declare const validateJwsMultisig: <T>(keychain: Keychain, input: JwsPayload) => {
55
+ verified: string[];
56
+ unverified: string[];
47
57
  };
48
- declare const sessionVerifier: (options: OptionsInput) => (req: any, res: any, next: any) => void;
49
- declare const managementSigner: (options: any) => (req: any, res: any, next: any) => void;
50
- declare const validateJws: <T>(key: Keychain, input: JwsPayload) => JwtPayload<T> | undefined;
51
58
 
52
- export { managementSigner, sessionVerifier, signData, signJws, signPayload, validateJws };
59
+ 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,119 +6522,198 @@ 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
- try {
6568
- req[fieldName] = jsonwebtoken_1.default.verify(authHeader, jwtPublicKey, verificationOptions);
6569
- }
6570
- catch (error) {
6571
- res.status(403);
6572
- res.send(`Signature verification raised: ${error}`);
6573
- return;
6574
- }
6575
- next();
6576
- };
6577
- return middleware;
6578
- };
6579
- exports.sessionVerifier = sessionVerifier;
6580
- const managementSigner = function (options) {
6581
- if (!options.privateKey)
6582
- throw new Error("Application's private key should be set");
6583
- const middleware = function (req, res, next) {
6584
- if (!req.management.payload)
6585
- console.error("No payload to be signed");
6586
- const payload = req.management.payload;
6587
- let signedPayload;
6588
- try {
6589
- signedPayload = (0, signer_1.signPayload)(payload, options);
6590
- }
6591
- catch (error) {
6592
- res.status(403);
6593
- res.send(`Unable to sign payload: ${error}`);
6594
- return;
6595
- }
6596
- try {
6597
- req.body = (0, signer_1.signJws)(signedPayload, options);
6598
- }
6599
- catch (error) {
6600
- res.status(403);
6601
- res.send(`Unable to correctly format signed payload: ${error}`);
6602
- }
6603
- next();
6604
- };
6605
- return middleware;
6606
- };
6607
- exports.managementSigner = managementSigner;
6604
+ exports.parseProtectedHeader = exports.base64Decode = void 0;
6608
6605
  const base64Decode = (base64) => {
6609
6606
  return Buffer.from(base64, "base64").toString("utf8");
6610
6607
  };
6608
+ exports.base64Decode = base64Decode;
6611
6609
  const parseProtectedHeader = (protectedHeader) => {
6612
- return JSON.parse(base64Decode(protectedHeader));
6610
+ return JSON.parse((0, exports.base64Decode)(protectedHeader));
6613
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;
6614
6623
  const validateJws = (key, input) => {
6615
6624
  for (const signature of input.signatures) {
6616
- const decodedProtectedHeader = parseProtectedHeader(signature.protected);
6625
+ const decodedProtectedHeader = (0, utils_1.parseProtectedHeader)(signature.protected);
6617
6626
  if (key === undefined) {
6618
6627
  throw new Error("Invalid key");
6619
6628
  }
6620
6629
  if (key.algorithm !== decodedProtectedHeader.alg) {
6621
6630
  throw new Error("Algorithm mismatch");
6622
6631
  }
6623
- try {
6624
- const verified = jsonwebtoken_1.default.verify(`${signature.protected}.${input.payload}.${signature.signature}`, key.value,
6625
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
6626
- { algorithms: [key.algorithm] });
6627
- 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
+ for (const signature of input.signatures) {
6676
+ const key = keychain.get(signature.header.kid);
6677
+ if (key) {
6678
+ (0, exports.validateJws)(key, input);
6679
+ verified.push(signature.header.kid);
6628
6680
  }
6629
- catch (error) {
6630
- console.error(error);
6631
- return undefined;
6681
+ else {
6682
+ unverified.push(signature.header.kid);
6632
6683
  }
6633
6684
  }
6685
+ return {
6686
+ verified,
6687
+ unverified
6688
+ };
6689
+ };
6690
+ exports.validateJwsMultisig = validateJwsMultisig;
6691
+ } (validate));
6692
+
6693
+ var types = {};
6694
+
6695
+ Object.defineProperty(types, "__esModule", { value: true });
6696
+
6697
+ (function (exports) {
6698
+ var __createBinding = (commonjsGlobal && commonjsGlobal.__createBinding) || (Object.create ? (function(o, m, k, k2) {
6699
+ if (k2 === undefined) k2 = k;
6700
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6701
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6702
+ desc = { enumerable: true, get: function() { return m[k]; } };
6703
+ }
6704
+ Object.defineProperty(o, k2, desc);
6705
+ }) : (function(o, m, k, k2) {
6706
+ if (k2 === undefined) k2 = k;
6707
+ o[k2] = m[k];
6708
+ }));
6709
+ var __exportStar = (commonjsGlobal && commonjsGlobal.__exportStar) || function(m, exports) {
6710
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
6634
6711
  };
6635
- exports.validateJws = validateJws;
6712
+ Object.defineProperty(exports, "__esModule", { value: true });
6713
+ __exportStar(middleware, exports);
6714
+ __exportStar(signer, exports);
6715
+ __exportStar(validate, exports);
6716
+ __exportStar(types, exports);
6636
6717
  } (src));
6637
6718
 
6638
6719
  var index = /*@__PURE__*/getDefaultExportFromCjs(src);