hbsig 0.0.1
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.
- package/cjs/bin_to_str.js +44 -0
- package/cjs/collect-body-keys.js +470 -0
- package/cjs/encode-array-item.js +110 -0
- package/cjs/encode-utils.js +236 -0
- package/cjs/encode.js +1318 -0
- package/cjs/erl_json.js +317 -0
- package/cjs/erl_str.js +1037 -0
- package/cjs/flat.js +222 -0
- package/cjs/http-message-signatures/httpbis.js +489 -0
- package/cjs/http-message-signatures/index.js +25 -0
- package/cjs/http-message-signatures/structured-header.js +129 -0
- package/cjs/httpsig.js +716 -0
- package/cjs/httpsig2.js +1160 -0
- package/cjs/id.js +470 -0
- package/cjs/index.js +63 -0
- package/cjs/send.js +194 -0
- package/cjs/signer-utils.js +617 -0
- package/cjs/signer.js +606 -0
- package/cjs/structured.js +296 -0
- package/cjs/test.js +27 -0
- package/cjs/utils.js +42 -0
- package/esm/bin_to_str.js +46 -0
- package/esm/collect-body-keys.js +436 -0
- package/esm/encode-array-item.js +112 -0
- package/esm/encode-utils.js +185 -0
- package/esm/encode.js +1219 -0
- package/esm/erl_json.js +289 -0
- package/esm/erl_str.js +1139 -0
- package/esm/flat.js +196 -0
- package/esm/http-message-signatures/httpbis.js +438 -0
- package/esm/http-message-signatures/index.js +4 -0
- package/esm/http-message-signatures/structured-header.js +105 -0
- package/esm/httpsig.js +658 -0
- package/esm/httpsig2.js +1097 -0
- package/esm/id.js +459 -0
- package/esm/index.js +4 -0
- package/esm/package.json +3 -0
- package/esm/send.js +124 -0
- package/esm/signer-utils.js +494 -0
- package/esm/signer.js +452 -0
- package/esm/structured.js +269 -0
- package/esm/test.js +6 -0
- package/esm/utils.js +28 -0
- package/package.json +28 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
exports.augmentHeaders = augmentHeaders;
|
|
8
|
+
exports.createSignatureBase = createSignatureBase;
|
|
9
|
+
exports.createSigningParameters = createSigningParameters;
|
|
10
|
+
exports.deriveComponent = deriveComponent;
|
|
11
|
+
exports.extractHeader = extractHeader;
|
|
12
|
+
exports.formatSignatureBase = formatSignatureBase;
|
|
13
|
+
exports.signMessage = signMessage;
|
|
14
|
+
exports.verifyMessage = verifyMessage;
|
|
15
|
+
var _structuredHeaders = require("structured-headers");
|
|
16
|
+
var _structuredHeader = require("./structured-header.js");
|
|
17
|
+
function _regenerator() { /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/babel/babel/blob/main/packages/babel-helpers/LICENSE */ var e, t, r = "function" == typeof Symbol ? Symbol : {}, n = r.iterator || "@@iterator", o = r.toStringTag || "@@toStringTag"; function i(r, n, o, i) { var c = n && n.prototype instanceof Generator ? n : Generator, u = Object.create(c.prototype); return _regeneratorDefine2(u, "_invoke", function (r, n, o) { var i, c, u, f = 0, p = o || [], y = !1, G = { p: 0, n: 0, v: e, a: d, f: d.bind(e, 4), d: function d(t, r) { return i = t, c = 0, u = e, G.n = r, a; } }; function d(r, n) { for (c = r, u = n, t = 0; !y && f && !o && t < p.length; t++) { var o, i = p[t], d = G.p, l = i[2]; r > 3 ? (o = l === n) && (u = i[(c = i[4]) ? 5 : (c = 3, 3)], i[4] = i[5] = e) : i[0] <= d && ((o = r < 2 && d < i[1]) ? (c = 0, G.v = n, G.n = i[1]) : d < l && (o = r < 3 || i[0] > n || n > l) && (i[4] = r, i[5] = n, G.n = l, c = 0)); } if (o || r > 1) return a; throw y = !0, n; } return function (o, p, l) { if (f > 1) throw TypeError("Generator is already running"); for (y && 1 === p && d(p, l), c = p, u = l; (t = c < 2 ? e : u) || !y;) { i || (c ? c < 3 ? (c > 1 && (G.n = -1), d(c, u)) : G.n = u : G.v = u); try { if (f = 2, i) { if (c || (o = "next"), t = i[o]) { if (!(t = t.call(i, u))) throw TypeError("iterator result is not an object"); if (!t.done) return t; u = t.value, c < 2 && (c = 0); } else 1 === c && (t = i["return"]) && t.call(i), c < 2 && (u = TypeError("The iterator does not provide a '" + o + "' method"), c = 1); i = e; } else if ((t = (y = G.n < 0) ? u : r.call(n, G)) !== a) break; } catch (t) { i = e, c = 1, u = t; } finally { f = 1; } } return { value: t, done: y }; }; }(r, o, i), !0), u; } var a = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} t = Object.getPrototypeOf; var c = [][n] ? t(t([][n]())) : (_regeneratorDefine2(t = {}, n, function () { return this; }), t), u = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(c); function f(e) { return Object.setPrototypeOf ? Object.setPrototypeOf(e, GeneratorFunctionPrototype) : (e.__proto__ = GeneratorFunctionPrototype, _regeneratorDefine2(e, o, "GeneratorFunction")), e.prototype = Object.create(u), e; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, _regeneratorDefine2(u, "constructor", GeneratorFunctionPrototype), _regeneratorDefine2(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = "GeneratorFunction", _regeneratorDefine2(GeneratorFunctionPrototype, o, "GeneratorFunction"), _regeneratorDefine2(u), _regeneratorDefine2(u, o, "Generator"), _regeneratorDefine2(u, n, function () { return this; }), _regeneratorDefine2(u, "toString", function () { return "[object Generator]"; }), (_regenerator = function _regenerator() { return { w: i, m: f }; })(); }
|
|
18
|
+
function _regeneratorDefine2(e, r, n, t) { var i = Object.defineProperty; try { i({}, "", {}); } catch (e) { i = 0; } _regeneratorDefine2 = function _regeneratorDefine(e, r, n, t) { function o(r, n) { _regeneratorDefine2(e, r, function (e) { return this._invoke(r, n, e); }); } r ? i ? i(e, r, { value: n, enumerable: !t, configurable: !t, writable: !t }) : e[r] = n : (o("next", 0), o("throw", 1), o("return", 2)); }, _regeneratorDefine2(e, r, n, t); }
|
|
19
|
+
function asyncGeneratorStep(n, t, e, r, o, a, c) { try { var i = n[a](c), u = i.value; } catch (n) { return void e(n); } i.done ? t(u) : Promise.resolve(u).then(r, o); }
|
|
20
|
+
function _asyncToGenerator(n) { return function () { var t = this, e = arguments; return new Promise(function (r, o) { var a = n.apply(t, e); function _next(n) { asyncGeneratorStep(a, r, o, _next, _throw, "next", n); } function _throw(n) { asyncGeneratorStep(a, r, o, _next, _throw, "throw", n); } _next(void 0); }); }; }
|
|
21
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
22
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
23
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
24
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
25
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
26
|
+
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
|
|
27
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
28
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
29
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
30
|
+
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
31
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
32
|
+
// Default params if not provided
|
|
33
|
+
var defaultParams = ["created", "keyid", "alg", "expires"];
|
|
34
|
+
|
|
35
|
+
// Helper to check if message is a request
|
|
36
|
+
var isRequest = function isRequest(message) {
|
|
37
|
+
return message.method !== undefined;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Components can be derived from requests or responses (which can also be bound to their request).
|
|
42
|
+
* The signature is essentially (component, params, signingSubject, supplementaryData)
|
|
43
|
+
*
|
|
44
|
+
* MODIFIED: This implementation matches Erlang behavior where @ components are treated as field lookups
|
|
45
|
+
*/
|
|
46
|
+
function deriveComponent(component, params, message, req) {
|
|
47
|
+
// MODIFIED: Match Erlang behavior - all @ components become field lookups
|
|
48
|
+
// The Erlang strips @ and calls extract_field for ALL derived components
|
|
49
|
+
|
|
50
|
+
// Remove @ prefix to match Erlang behavior
|
|
51
|
+
var fieldName = component.startsWith("@") ? component.slice(1) : component;
|
|
52
|
+
|
|
53
|
+
// For all @ components, do a field lookup instead of deriving
|
|
54
|
+
// This matches the Erlang identifier_to_component behavior
|
|
55
|
+
if (component.startsWith("@")) {
|
|
56
|
+
try {
|
|
57
|
+
if (params.has("req") && req) {
|
|
58
|
+
return extractHeader(fieldName, params, req, undefined);
|
|
59
|
+
}
|
|
60
|
+
return extractHeader(fieldName, params, message, req);
|
|
61
|
+
} catch (e) {
|
|
62
|
+
// If field not found, return empty or throw based on component type
|
|
63
|
+
throw new Error("No field \"".concat(fieldName, "\" found for component \"").concat(component, "\""));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// This code path should not be reached for @ components anymore
|
|
68
|
+
// but keeping it for non-@ components
|
|
69
|
+
throw new Error("Unsupported component \"".concat(component, "\""));
|
|
70
|
+
}
|
|
71
|
+
function extractHeader(header, params, messageOrHeaders, req) {
|
|
72
|
+
var headers = messageOrHeaders.headers || messageOrHeaders;
|
|
73
|
+
var context = params.has("req") ? req === null || req === void 0 ? void 0 : req.headers : headers;
|
|
74
|
+
if (!context) {
|
|
75
|
+
throw new Error("Missing request in request-response bound component");
|
|
76
|
+
}
|
|
77
|
+
var headerTuple = Object.entries(context).find(function (_ref) {
|
|
78
|
+
var _ref2 = _slicedToArray(_ref, 1),
|
|
79
|
+
name = _ref2[0];
|
|
80
|
+
return name.toLowerCase() === header;
|
|
81
|
+
});
|
|
82
|
+
if (!headerTuple) {
|
|
83
|
+
throw new Error("No header \"".concat(header, "\" found in headers"));
|
|
84
|
+
}
|
|
85
|
+
var values = Array.isArray(headerTuple[1]) ? headerTuple[1] : [headerTuple[1]];
|
|
86
|
+
if (params.has("bs") && (params.has("sf") || params.has("key"))) {
|
|
87
|
+
throw new Error("Cannot have both `bs` and (implicit) `sf` parameters");
|
|
88
|
+
}
|
|
89
|
+
if (params.has("sf") || params.has("key")) {
|
|
90
|
+
// strict encoding of field
|
|
91
|
+
var value = values.join(", ");
|
|
92
|
+
var parsed = (0, _structuredHeader.parseHeader)(value);
|
|
93
|
+
if (params.has("key") && !(parsed instanceof _structuredHeader.Dictionary)) {
|
|
94
|
+
throw new Error("Unable to parse header as dictionary");
|
|
95
|
+
}
|
|
96
|
+
if (params.has("key")) {
|
|
97
|
+
var key = params.get("key").toString();
|
|
98
|
+
if (!parsed.has(key)) {
|
|
99
|
+
throw new Error("Unable to find key \"".concat(key, "\" in structured field"));
|
|
100
|
+
}
|
|
101
|
+
return [parsed.get(key)];
|
|
102
|
+
}
|
|
103
|
+
return [parsed.toString()];
|
|
104
|
+
}
|
|
105
|
+
if (params.has("bs")) {
|
|
106
|
+
return [values.map(function (val) {
|
|
107
|
+
var encoded = Buffer.from(val.trim().replace(/\n\s*/gm, " "));
|
|
108
|
+
return ":".concat(encoded.toString("base64"), ":");
|
|
109
|
+
}).join(", ")];
|
|
110
|
+
}
|
|
111
|
+
// raw encoding
|
|
112
|
+
return [values.map(function (val) {
|
|
113
|
+
return val.trim().replace(/\n\s*/gm, " ");
|
|
114
|
+
}).join(", ")];
|
|
115
|
+
}
|
|
116
|
+
function normaliseParams(params) {
|
|
117
|
+
var map = new Map();
|
|
118
|
+
params.forEach(function (value, key) {
|
|
119
|
+
if (value instanceof _structuredHeaders.ByteSequence) {
|
|
120
|
+
map.set(key, value.toBase64());
|
|
121
|
+
} else if (value instanceof _structuredHeaders.Token) {
|
|
122
|
+
map.set(key, value.toString());
|
|
123
|
+
} else {
|
|
124
|
+
map.set(key, value);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return map;
|
|
128
|
+
}
|
|
129
|
+
function createSignatureBase(config, res, req) {
|
|
130
|
+
return config.fields.reduce(function (base, fieldName) {
|
|
131
|
+
var _parseItem = (0, _structuredHeaders.parseItem)((0, _structuredHeader.quoteString)(fieldName)),
|
|
132
|
+
_parseItem2 = _slicedToArray(_parseItem, 2),
|
|
133
|
+
field = _parseItem2[0],
|
|
134
|
+
params = _parseItem2[1];
|
|
135
|
+
var fieldParams = normaliseParams(params);
|
|
136
|
+
var lcFieldName = field.toLowerCase();
|
|
137
|
+
if (lcFieldName !== "@signature-params") {
|
|
138
|
+
var value = null;
|
|
139
|
+
if (config.componentParser) {
|
|
140
|
+
var _config$componentPars;
|
|
141
|
+
value = (_config$componentPars = config.componentParser(lcFieldName, fieldParams, res, req)) !== null && _config$componentPars !== void 0 ? _config$componentPars : null;
|
|
142
|
+
}
|
|
143
|
+
if (value === null) {
|
|
144
|
+
value = field.startsWith("@") ? deriveComponent(lcFieldName, fieldParams, res, req) : extractHeader(lcFieldName, fieldParams, res, req);
|
|
145
|
+
}
|
|
146
|
+
base.push([(0, _structuredHeaders.serializeItem)([field, params]), value]);
|
|
147
|
+
}
|
|
148
|
+
return base;
|
|
149
|
+
}, []);
|
|
150
|
+
}
|
|
151
|
+
function formatSignatureBase(base) {
|
|
152
|
+
return base.map(function (_ref3) {
|
|
153
|
+
var _ref4 = _slicedToArray(_ref3, 2),
|
|
154
|
+
key = _ref4[0],
|
|
155
|
+
value = _ref4[1];
|
|
156
|
+
var quotedKey = (0, _structuredHeaders.serializeItem)((0, _structuredHeaders.parseItem)((0, _structuredHeader.quoteString)(key)));
|
|
157
|
+
|
|
158
|
+
// MODIFIED: Special handling to match Erlang behavior
|
|
159
|
+
// If the key is "@path", format it as "path" (without @) in the signature base
|
|
160
|
+
var formattedKey = quotedKey;
|
|
161
|
+
if (quotedKey === '"@path"') {
|
|
162
|
+
formattedKey = '"path"';
|
|
163
|
+
}
|
|
164
|
+
return value.map(function (val) {
|
|
165
|
+
return "".concat(formattedKey, ": ").concat(val);
|
|
166
|
+
}).join("\n");
|
|
167
|
+
}).join("\n");
|
|
168
|
+
}
|
|
169
|
+
function createSigningParameters(config) {
|
|
170
|
+
var _config$params;
|
|
171
|
+
var now = new Date();
|
|
172
|
+
return ((_config$params = config.params) !== null && _config$params !== void 0 ? _config$params : defaultParams).reduce(function (params, paramName) {
|
|
173
|
+
var _config$paramValues, _config$paramValues3, _config$paramValues4, _config$paramValues9, _config$paramValues0;
|
|
174
|
+
var value = "";
|
|
175
|
+
switch (paramName.toLowerCase()) {
|
|
176
|
+
case "created":
|
|
177
|
+
// created is optional but recommended. If created is supplied but is null, that's an explicit
|
|
178
|
+
// instruction to *not* include the created parameter
|
|
179
|
+
if (((_config$paramValues = config.paramValues) === null || _config$paramValues === void 0 ? void 0 : _config$paramValues.created) !== null) {
|
|
180
|
+
var _config$paramValues$c, _config$paramValues2;
|
|
181
|
+
var created = (_config$paramValues$c = (_config$paramValues2 = config.paramValues) === null || _config$paramValues2 === void 0 ? void 0 : _config$paramValues2.created) !== null && _config$paramValues$c !== void 0 ? _config$paramValues$c : now;
|
|
182
|
+
value = Math.floor(created.getTime() / 1000);
|
|
183
|
+
}
|
|
184
|
+
break;
|
|
185
|
+
case "expires":
|
|
186
|
+
// attempt to obtain an explicit expires time, otherwise create one that is 300 seconds after
|
|
187
|
+
// creation. Don't add an expires time if there is no created time
|
|
188
|
+
if ((_config$paramValues3 = config.paramValues) !== null && _config$paramValues3 !== void 0 && _config$paramValues3.expires || ((_config$paramValues4 = config.paramValues) === null || _config$paramValues4 === void 0 ? void 0 : _config$paramValues4.created) !== null) {
|
|
189
|
+
var _config$paramValues$e, _config$paramValues5, _config$paramValues$c2, _config$paramValues6;
|
|
190
|
+
var expires = (_config$paramValues$e = (_config$paramValues5 = config.paramValues) === null || _config$paramValues5 === void 0 ? void 0 : _config$paramValues5.expires) !== null && _config$paramValues$e !== void 0 ? _config$paramValues$e : new Date(((_config$paramValues$c2 = (_config$paramValues6 = config.paramValues) === null || _config$paramValues6 === void 0 ? void 0 : _config$paramValues6.created) !== null && _config$paramValues$c2 !== void 0 ? _config$paramValues$c2 : now).getTime() + 300000);
|
|
191
|
+
value = Math.floor(expires.getTime() / 1000);
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
case "keyid":
|
|
195
|
+
{
|
|
196
|
+
var _ref5, _config$paramValues$k, _config$paramValues7;
|
|
197
|
+
// attempt to obtain the keyid omit if missing
|
|
198
|
+
var kid = (_ref5 = (_config$paramValues$k = (_config$paramValues7 = config.paramValues) === null || _config$paramValues7 === void 0 ? void 0 : _config$paramValues7.keyid) !== null && _config$paramValues$k !== void 0 ? _config$paramValues$k : config.key.id) !== null && _ref5 !== void 0 ? _ref5 : null;
|
|
199
|
+
if (kid) {
|
|
200
|
+
value = kid.toString();
|
|
201
|
+
}
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
case "alg":
|
|
205
|
+
{
|
|
206
|
+
var _ref6, _config$paramValues$a, _config$paramValues8;
|
|
207
|
+
// if there is no alg, but it's listed as a required parameter, we should probably
|
|
208
|
+
// throw an error - the problem is that if it's in the default set of params, do we
|
|
209
|
+
// really want to throw if there's no keyid?
|
|
210
|
+
var alg = (_ref6 = (_config$paramValues$a = (_config$paramValues8 = config.paramValues) === null || _config$paramValues8 === void 0 ? void 0 : _config$paramValues8.alg) !== null && _config$paramValues$a !== void 0 ? _config$paramValues$a : config.key.alg) !== null && _ref6 !== void 0 ? _ref6 : null;
|
|
211
|
+
if (alg) {
|
|
212
|
+
value = alg.toString();
|
|
213
|
+
}
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
default:
|
|
217
|
+
if (((_config$paramValues9 = config.paramValues) === null || _config$paramValues9 === void 0 ? void 0 : _config$paramValues9[paramName]) instanceof Date) {
|
|
218
|
+
value = Math.floor(config.paramValues[paramName].getTime() / 1000);
|
|
219
|
+
} else if ((_config$paramValues0 = config.paramValues) !== null && _config$paramValues0 !== void 0 && _config$paramValues0[paramName]) {
|
|
220
|
+
value = config.paramValues[paramName];
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (value) {
|
|
224
|
+
params.set(paramName, value);
|
|
225
|
+
}
|
|
226
|
+
return params;
|
|
227
|
+
}, new Map());
|
|
228
|
+
}
|
|
229
|
+
function augmentHeaders(headers, signature, signatureInput, name) {
|
|
230
|
+
var signatureHeaderName = "Signature";
|
|
231
|
+
var signatureInputHeaderName = "Signature-Input";
|
|
232
|
+
var signatureHeader = new Map();
|
|
233
|
+
var inputHeader = new Map();
|
|
234
|
+
// check to see if there are already signature/signature-input headers
|
|
235
|
+
// if there are we want to store the current (case-sensitive) name of the header
|
|
236
|
+
// and we want to parse out the current values so we can append our new signature
|
|
237
|
+
for (var header in headers) {
|
|
238
|
+
switch (header.toLowerCase()) {
|
|
239
|
+
case "signature":
|
|
240
|
+
{
|
|
241
|
+
signatureHeaderName = header;
|
|
242
|
+
signatureHeader = (0, _structuredHeaders.parseDictionary)(Array.isArray(headers[header]) ? headers[header].join(", ") : headers[header]);
|
|
243
|
+
break;
|
|
244
|
+
}
|
|
245
|
+
case "signature-input":
|
|
246
|
+
signatureInputHeaderName = header;
|
|
247
|
+
inputHeader = (0, _structuredHeaders.parseDictionary)(Array.isArray(headers[header]) ? headers[header].join(", ") : headers[header]);
|
|
248
|
+
break;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
// find a unique signature name for the header. Check if any existing headers already use
|
|
252
|
+
// the name we intend to use, if there are, add incrementing numbers to the signature name
|
|
253
|
+
// until we have a unique name to use
|
|
254
|
+
var signatureName = name !== null && name !== void 0 ? name : "sig";
|
|
255
|
+
if (signatureHeader.has(signatureName) || inputHeader.has(signatureName)) {
|
|
256
|
+
var count = 0;
|
|
257
|
+
while (signatureHeader.has("".concat(signatureName).concat(count)) || inputHeader.has("".concat(signatureName).concat(count))) {
|
|
258
|
+
count++;
|
|
259
|
+
}
|
|
260
|
+
signatureName += count.toString();
|
|
261
|
+
}
|
|
262
|
+
// append our signature and signature-inputs to the headers and return
|
|
263
|
+
signatureHeader.set(signatureName, [new _structuredHeaders.ByteSequence(signature.toString("base64")), new Map()]);
|
|
264
|
+
inputHeader.set(signatureName, (0, _structuredHeaders.parseList)(signatureInput)[0]);
|
|
265
|
+
return _objectSpread(_objectSpread({}, headers), {}, _defineProperty(_defineProperty({}, signatureHeaderName, (0, _structuredHeaders.serializeDictionary)(signatureHeader)), signatureInputHeaderName, (0, _structuredHeaders.serializeDictionary)(inputHeader)));
|
|
266
|
+
}
|
|
267
|
+
function signMessage(_x, _x2, _x3) {
|
|
268
|
+
return _signMessage.apply(this, arguments);
|
|
269
|
+
}
|
|
270
|
+
function _signMessage() {
|
|
271
|
+
_signMessage = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee(config, message, req) {
|
|
272
|
+
var _config$fields;
|
|
273
|
+
var signingParameters, signatureBase, signatureInput, base, signature;
|
|
274
|
+
return _regenerator().w(function (_context) {
|
|
275
|
+
while (1) switch (_context.n) {
|
|
276
|
+
case 0:
|
|
277
|
+
signingParameters = createSigningParameters(config);
|
|
278
|
+
signatureBase = createSignatureBase({
|
|
279
|
+
fields: (_config$fields = config.fields) !== null && _config$fields !== void 0 ? _config$fields : [],
|
|
280
|
+
componentParser: config.componentParser
|
|
281
|
+
}, message, req);
|
|
282
|
+
signatureInput = (0, _structuredHeaders.serializeList)([[signatureBase.map(function (_ref7) {
|
|
283
|
+
var _ref8 = _slicedToArray(_ref7, 1),
|
|
284
|
+
item = _ref8[0];
|
|
285
|
+
return (0, _structuredHeaders.parseItem)(item);
|
|
286
|
+
}), signingParameters]]);
|
|
287
|
+
signatureBase.push(['"@signature-params"', [signatureInput]]);
|
|
288
|
+
base = formatSignatureBase(signatureBase); // call sign
|
|
289
|
+
_context.n = 1;
|
|
290
|
+
return config.key.sign(Buffer.from(base));
|
|
291
|
+
case 1:
|
|
292
|
+
signature = _context.v;
|
|
293
|
+
return _context.a(2, _objectSpread(_objectSpread({}, message), {}, {
|
|
294
|
+
headers: augmentHeaders(_objectSpread({}, message.headers), signature, signatureInput, config.name)
|
|
295
|
+
}));
|
|
296
|
+
}
|
|
297
|
+
}, _callee);
|
|
298
|
+
}));
|
|
299
|
+
return _signMessage.apply(this, arguments);
|
|
300
|
+
}
|
|
301
|
+
function verifyMessage(_x4, _x5, _x6) {
|
|
302
|
+
return _verifyMessage.apply(this, arguments);
|
|
303
|
+
}
|
|
304
|
+
function _verifyMessage() {
|
|
305
|
+
_verifyMessage = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee3(config, message, req) {
|
|
306
|
+
var _config$tolerance, _config$notAfter, _config$maxAge, _config$requiredParam, _config$requiredField;
|
|
307
|
+
var _Object$entries$reduc, signatures, signatureInputs, now, tolerance, notAfter, maxAge, requiredParams, requiredFields;
|
|
308
|
+
return _regenerator().w(function (_context3) {
|
|
309
|
+
while (1) switch (_context3.n) {
|
|
310
|
+
case 0:
|
|
311
|
+
_Object$entries$reduc = Object.entries(message.headers).reduce(function (accum, _ref9) {
|
|
312
|
+
var _ref0 = _slicedToArray(_ref9, 2),
|
|
313
|
+
name = _ref0[0],
|
|
314
|
+
value = _ref0[1];
|
|
315
|
+
switch (name.toLowerCase()) {
|
|
316
|
+
case "signature":
|
|
317
|
+
return Object.assign(accum, {
|
|
318
|
+
signatures: (0, _structuredHeaders.parseDictionary)(Array.isArray(value) ? value.join(", ") : value)
|
|
319
|
+
});
|
|
320
|
+
case "signature-input":
|
|
321
|
+
return Object.assign(accum, {
|
|
322
|
+
signatureInputs: (0, _structuredHeaders.parseDictionary)(Array.isArray(value) ? value.join(", ") : value)
|
|
323
|
+
});
|
|
324
|
+
default:
|
|
325
|
+
return accum;
|
|
326
|
+
}
|
|
327
|
+
}, {}), signatures = _Object$entries$reduc.signatures, signatureInputs = _Object$entries$reduc.signatureInputs; // no signatures means an indeterminate result
|
|
328
|
+
if (!(!(signatures !== null && signatures !== void 0 && signatures.size) && !(signatureInputs !== null && signatureInputs !== void 0 && signatureInputs.size))) {
|
|
329
|
+
_context3.n = 1;
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
return _context3.a(2, null);
|
|
333
|
+
case 1:
|
|
334
|
+
if (!(!(signatures !== null && signatures !== void 0 && signatures.size) || !(signatureInputs !== null && signatureInputs !== void 0 && signatureInputs.size))) {
|
|
335
|
+
_context3.n = 2;
|
|
336
|
+
break;
|
|
337
|
+
}
|
|
338
|
+
throw new Error("Incomplete signature headers");
|
|
339
|
+
case 2:
|
|
340
|
+
now = Math.floor(Date.now() / 1000);
|
|
341
|
+
tolerance = (_config$tolerance = config.tolerance) !== null && _config$tolerance !== void 0 ? _config$tolerance : 0;
|
|
342
|
+
notAfter = config.notAfter instanceof Date ? Math.floor(config.notAfter.getTime() / 1000) : (_config$notAfter = config.notAfter) !== null && _config$notAfter !== void 0 ? _config$notAfter : now;
|
|
343
|
+
maxAge = (_config$maxAge = config.maxAge) !== null && _config$maxAge !== void 0 ? _config$maxAge : null;
|
|
344
|
+
requiredParams = (_config$requiredParam = config.requiredParams) !== null && _config$requiredParam !== void 0 ? _config$requiredParam : [];
|
|
345
|
+
requiredFields = (_config$requiredField = config.requiredFields) !== null && _config$requiredField !== void 0 ? _config$requiredField : [];
|
|
346
|
+
return _context3.a(2, Array.from(signatureInputs.entries()).reduce(/*#__PURE__*/function () {
|
|
347
|
+
var _ref10 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee2(prev, _ref1) {
|
|
348
|
+
var _key$algs;
|
|
349
|
+
var _ref11, name, input, signatureParams, _yield$Promise$all, _yield$Promise$all2, result, key, hasRequiredParams, hasRequiredFields, created, expires, fields, signingBase, base, signature;
|
|
350
|
+
return _regenerator().w(function (_context2) {
|
|
351
|
+
while (1) switch (_context2.n) {
|
|
352
|
+
case 0:
|
|
353
|
+
_ref11 = _slicedToArray(_ref1, 2), name = _ref11[0], input = _ref11[1];
|
|
354
|
+
signatureParams = Array.from(input[1].entries()).reduce(function (params, _ref12) {
|
|
355
|
+
var _ref13 = _slicedToArray(_ref12, 2),
|
|
356
|
+
key = _ref13[0],
|
|
357
|
+
value = _ref13[1];
|
|
358
|
+
if (value instanceof _structuredHeaders.ByteSequence) {
|
|
359
|
+
Object.assign(params, _defineProperty({}, key, value.toBase64()));
|
|
360
|
+
} else if (value instanceof _structuredHeaders.Token) {
|
|
361
|
+
Object.assign(params, _defineProperty({}, key, value.toString()));
|
|
362
|
+
} else if (key === "created" || key === "expired") {
|
|
363
|
+
Object.assign(params, _defineProperty({}, key, new Date(value * 1000)));
|
|
364
|
+
} else {
|
|
365
|
+
Object.assign(params, _defineProperty({}, key, value));
|
|
366
|
+
}
|
|
367
|
+
return params;
|
|
368
|
+
}, {});
|
|
369
|
+
_context2.n = 1;
|
|
370
|
+
return Promise.all([prev["catch"](function (e) {
|
|
371
|
+
return e;
|
|
372
|
+
}), config.keyLookup(signatureParams)]);
|
|
373
|
+
case 1:
|
|
374
|
+
_yield$Promise$all = _context2.v;
|
|
375
|
+
_yield$Promise$all2 = _slicedToArray(_yield$Promise$all, 2);
|
|
376
|
+
result = _yield$Promise$all2[0];
|
|
377
|
+
key = _yield$Promise$all2[1];
|
|
378
|
+
if (!(config.all && !key)) {
|
|
379
|
+
_context2.n = 2;
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
throw new Error("Unknown key");
|
|
383
|
+
case 2:
|
|
384
|
+
if (key) {
|
|
385
|
+
_context2.n = 4;
|
|
386
|
+
break;
|
|
387
|
+
}
|
|
388
|
+
if (!(result instanceof Error)) {
|
|
389
|
+
_context2.n = 3;
|
|
390
|
+
break;
|
|
391
|
+
}
|
|
392
|
+
throw result;
|
|
393
|
+
case 3:
|
|
394
|
+
return _context2.a(2, result);
|
|
395
|
+
case 4:
|
|
396
|
+
if (!(input[1].has("alg") && ((_key$algs = key.algs) === null || _key$algs === void 0 ? void 0 : _key$algs.includes(input[1].get("alg"))) === false)) {
|
|
397
|
+
_context2.n = 5;
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
throw new Error("Unsupported key algorithm");
|
|
401
|
+
case 5:
|
|
402
|
+
if ((0, _structuredHeaders.isInnerList)(input)) {
|
|
403
|
+
_context2.n = 6;
|
|
404
|
+
break;
|
|
405
|
+
}
|
|
406
|
+
throw new Error("Malformed signature input");
|
|
407
|
+
case 6:
|
|
408
|
+
hasRequiredParams = requiredParams.every(function (param) {
|
|
409
|
+
return input[1].has(param);
|
|
410
|
+
});
|
|
411
|
+
if (hasRequiredParams) {
|
|
412
|
+
_context2.n = 7;
|
|
413
|
+
break;
|
|
414
|
+
}
|
|
415
|
+
throw new Error("Missing required signature parameters");
|
|
416
|
+
case 7:
|
|
417
|
+
// this could be tricky, what if we say "@method" but there is "@method;req"
|
|
418
|
+
hasRequiredFields = requiredFields.every(function (field) {
|
|
419
|
+
return input[0].some(function (_ref14) {
|
|
420
|
+
var _ref15 = _slicedToArray(_ref14, 1),
|
|
421
|
+
fieldName = _ref15[0];
|
|
422
|
+
return fieldName === field;
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
if (hasRequiredFields) {
|
|
426
|
+
_context2.n = 8;
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
throw new Error("Missing required signed fields");
|
|
430
|
+
case 8:
|
|
431
|
+
if (!input[1].has("created")) {
|
|
432
|
+
_context2.n = 9;
|
|
433
|
+
break;
|
|
434
|
+
}
|
|
435
|
+
created = input[1].get("created") - tolerance; // maxAge overrides expires.
|
|
436
|
+
// signature is older than maxAge
|
|
437
|
+
if (!(maxAge && now - created > maxAge || created > notAfter)) {
|
|
438
|
+
_context2.n = 9;
|
|
439
|
+
break;
|
|
440
|
+
}
|
|
441
|
+
throw new Error("Signature is too old");
|
|
442
|
+
case 9:
|
|
443
|
+
if (!input[1].has("expires")) {
|
|
444
|
+
_context2.n = 10;
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
expires = input[1].get("expires") + tolerance; // expired signature
|
|
448
|
+
if (!(now > expires)) {
|
|
449
|
+
_context2.n = 10;
|
|
450
|
+
break;
|
|
451
|
+
}
|
|
452
|
+
throw new Error("Signature has expired");
|
|
453
|
+
case 10:
|
|
454
|
+
// now look to verify the signature! Build the expected "signing base" and verify it!
|
|
455
|
+
fields = input[0].map(function (item) {
|
|
456
|
+
return (0, _structuredHeaders.serializeItem)(item);
|
|
457
|
+
});
|
|
458
|
+
signingBase = createSignatureBase({
|
|
459
|
+
fields: fields,
|
|
460
|
+
componentParser: config.componentParser
|
|
461
|
+
}, message, req);
|
|
462
|
+
signingBase.push(['"@signature-params"', [(0, _structuredHeaders.serializeList)([input])]]);
|
|
463
|
+
base = formatSignatureBase(signingBase);
|
|
464
|
+
signature = signatures.get(name);
|
|
465
|
+
if (signature) {
|
|
466
|
+
_context2.n = 11;
|
|
467
|
+
break;
|
|
468
|
+
}
|
|
469
|
+
throw new Error("No corresponding signature for input");
|
|
470
|
+
case 11:
|
|
471
|
+
if ((0, _structuredHeaders.isByteSequence)(signature[0])) {
|
|
472
|
+
_context2.n = 12;
|
|
473
|
+
break;
|
|
474
|
+
}
|
|
475
|
+
throw new Error("Malformed signature");
|
|
476
|
+
case 12:
|
|
477
|
+
return _context2.a(2, key.verify(Buffer.from(base), Buffer.from(signature[0].toBase64(), "base64"), signatureParams));
|
|
478
|
+
}
|
|
479
|
+
}, _callee2);
|
|
480
|
+
}));
|
|
481
|
+
return function (_x7, _x8) {
|
|
482
|
+
return _ref10.apply(this, arguments);
|
|
483
|
+
};
|
|
484
|
+
}(), Promise.resolve(null)));
|
|
485
|
+
}
|
|
486
|
+
}, _callee3);
|
|
487
|
+
}));
|
|
488
|
+
return _verifyMessage.apply(this, arguments);
|
|
489
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
var _exportNames = {
|
|
8
|
+
httpbis: true
|
|
9
|
+
};
|
|
10
|
+
exports.httpbis = exports["default"] = void 0;
|
|
11
|
+
var httpbis = _interopRequireWildcard(require("./httpbis.js"));
|
|
12
|
+
exports.httpbis = httpbis;
|
|
13
|
+
exports["default"] = httpbis;
|
|
14
|
+
Object.keys(httpbis).forEach(function (key) {
|
|
15
|
+
if (key === "default" || key === "__esModule") return;
|
|
16
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
17
|
+
if (key in exports && exports[key] === httpbis[key]) return;
|
|
18
|
+
Object.defineProperty(exports, key, {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function get() {
|
|
21
|
+
return httpbis[key];
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.List = exports.Item = exports.Dictionary = void 0;
|
|
7
|
+
exports.parseHeader = parseHeader;
|
|
8
|
+
exports.quoteString = quoteString;
|
|
9
|
+
var _structuredHeaders = require("structured-headers");
|
|
10
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
11
|
+
function _toArray(r) { return _arrayWithHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableRest(); }
|
|
12
|
+
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
13
|
+
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
|
|
14
|
+
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
|
|
15
|
+
function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); }
|
|
16
|
+
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
|
|
17
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
18
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
19
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
20
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
|
|
21
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
22
|
+
var Dictionary = exports.Dictionary = /*#__PURE__*/function () {
|
|
23
|
+
function Dictionary(input) {
|
|
24
|
+
_classCallCheck(this, Dictionary);
|
|
25
|
+
this.raw = input;
|
|
26
|
+
this.parsed = (0, _structuredHeaders.parseDictionary)(input);
|
|
27
|
+
}
|
|
28
|
+
return _createClass(Dictionary, [{
|
|
29
|
+
key: "toString",
|
|
30
|
+
value: function toString() {
|
|
31
|
+
return this.serialize();
|
|
32
|
+
}
|
|
33
|
+
}, {
|
|
34
|
+
key: "serialize",
|
|
35
|
+
value: function serialize() {
|
|
36
|
+
return (0, _structuredHeaders.serializeDictionary)(this.parsed);
|
|
37
|
+
}
|
|
38
|
+
}, {
|
|
39
|
+
key: "has",
|
|
40
|
+
value: function has(key) {
|
|
41
|
+
return this.parsed.has(key);
|
|
42
|
+
}
|
|
43
|
+
}, {
|
|
44
|
+
key: "get",
|
|
45
|
+
value: function get(key) {
|
|
46
|
+
var value = this.parsed.get(key);
|
|
47
|
+
if (!value) {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
if ((0, _structuredHeaders.isInnerList)(value)) {
|
|
51
|
+
return (0, _structuredHeaders.serializeInnerList)(value);
|
|
52
|
+
}
|
|
53
|
+
return (0, _structuredHeaders.serializeItem)(value);
|
|
54
|
+
}
|
|
55
|
+
}]);
|
|
56
|
+
}();
|
|
57
|
+
var List = exports.List = /*#__PURE__*/function () {
|
|
58
|
+
function List(input) {
|
|
59
|
+
_classCallCheck(this, List);
|
|
60
|
+
this.raw = input;
|
|
61
|
+
this.parsed = (0, _structuredHeaders.parseList)(input);
|
|
62
|
+
}
|
|
63
|
+
return _createClass(List, [{
|
|
64
|
+
key: "toString",
|
|
65
|
+
value: function toString() {
|
|
66
|
+
return this.serialize();
|
|
67
|
+
}
|
|
68
|
+
}, {
|
|
69
|
+
key: "serialize",
|
|
70
|
+
value: function serialize() {
|
|
71
|
+
return (0, _structuredHeaders.serializeList)(this.parsed);
|
|
72
|
+
}
|
|
73
|
+
}]);
|
|
74
|
+
}();
|
|
75
|
+
var Item = exports.Item = /*#__PURE__*/function () {
|
|
76
|
+
function Item(input) {
|
|
77
|
+
_classCallCheck(this, Item);
|
|
78
|
+
this.raw = input;
|
|
79
|
+
this.parsed = (0, _structuredHeaders.parseItem)(input);
|
|
80
|
+
}
|
|
81
|
+
return _createClass(Item, [{
|
|
82
|
+
key: "toString",
|
|
83
|
+
value: function toString() {
|
|
84
|
+
return this.serialize();
|
|
85
|
+
}
|
|
86
|
+
}, {
|
|
87
|
+
key: "serialize",
|
|
88
|
+
value: function serialize() {
|
|
89
|
+
return (0, _structuredHeaders.serializeItem)(this.parsed);
|
|
90
|
+
}
|
|
91
|
+
}]);
|
|
92
|
+
}();
|
|
93
|
+
function parseHeader(header) {
|
|
94
|
+
var classes = [List, Dictionary, Item];
|
|
95
|
+
for (var i = 0; i < classes.length; i++) {
|
|
96
|
+
try {
|
|
97
|
+
return new classes[i](header);
|
|
98
|
+
} catch (e) {
|
|
99
|
+
// noop
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
throw new Error("Unable to parse header as structured field");
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* This allows consumers of the library to supply field specifications that aren't
|
|
107
|
+
* strictly "structured fields". Really a string must start with a `"` but that won't
|
|
108
|
+
* tend to happen in our configs.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} input
|
|
111
|
+
* @returns {string}
|
|
112
|
+
*/
|
|
113
|
+
function quoteString(input) {
|
|
114
|
+
// if it's not quoted, attempt to quote
|
|
115
|
+
if (!input.startsWith('"')) {
|
|
116
|
+
// try to split the structured field
|
|
117
|
+
var _input$split = input.split(";"),
|
|
118
|
+
_input$split2 = _toArray(_input$split),
|
|
119
|
+
name = _input$split2[0],
|
|
120
|
+
rest = _input$split2.slice(1);
|
|
121
|
+
// no params, just quote the whole thing
|
|
122
|
+
if (!rest.length) {
|
|
123
|
+
return "\"".concat(name, "\"");
|
|
124
|
+
}
|
|
125
|
+
// quote the first part and put the rest back as it was
|
|
126
|
+
return "\"".concat(name, "\";").concat(rest.join(";"));
|
|
127
|
+
}
|
|
128
|
+
return input;
|
|
129
|
+
}
|