-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsigner.js
95 lines (80 loc) · 2.64 KB
/
signer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
'use strict';
const ref = require('ref');
const ffi = require('ffi');
const { CString, int } = ref.types;
const GO_SIGNER = ffi.Library('./build/escher', {
SignURL: [CString, [CString, CString, CString, CString, int]],
SignRequest: [CString, [CString, CString, CString, CString]]
});
class EscherSignerError extends Error {}
class EscherSigner {
constructor(
apiKey,
apiSecret,
credentialScope,
hashAlgo = 'SHA256',
algoPrefix = 'EMS',
vendorKey = 'EMS',
authHeaderName = 'X-EMS-Auth',
dateHeaderName = 'X-EMS-Date'
) {
this._config = {
accessKeyId: apiKey,
apiSecret: apiSecret,
hashAlgo: hashAlgo,
algoPrefix: algoPrefix,
vendorKey: vendorKey,
authHeaderName: authHeaderName,
dateHeaderName: dateHeaderName,
credentialScope: credentialScope
};
}
signRequest(method, url, body, headers, headers_to_sign = [], date = new Date().toISOString(), expires = 600) {
const request = this._createRequest(method, url, headers, body);
const signResult = this._signRequest(request, headers_to_sign, date, expires);
return this._unwrapRequestSignResult(signResult);
}
_createRequest(method, url, headers, body) {
const headersTransformed = Object.keys(headers).map(h => [h, headers[h]]);
return {
method,
url,
headers: headersTransformed,
body
};
}
_signRequest(request, headers_to_sign, date, expires) {
const config = this._create_config(date, expires);
const c_result = GO_SIGNER.SignRequest(
JSON.stringify(config),
JSON.stringify(request),
JSON.stringify(headers_to_sign),
date
);
return JSON.parse(c_result);
}
_unwrapRequestSignResult(signResult) {
if (signResult.SignError != '') throw new EscherSignerError(signResult.SignError);
const signedHeaders = {};
for (let header of signResult.RequestHeaders) signedHeaders[header[0]] = header[1];
return signedHeaders;
}
signURL(method, url, date = new Date().toISOString(), expires = 600) {
const signResult = this._sign_url(method, url, date, expires);
return this._unwrapUrlSignResult(signResult);
}
_sign_url(method, url, date, expires) {
const config = this._create_config(date, expires);
const c_result = GO_SIGNER.SignURL(JSON.stringify(config), method, url, date, expires);
return JSON.parse(c_result);
}
_create_config(date, expires) {
const config = { ...this._config, date, expires };
return config;
}
_unwrapUrlSignResult(result) {
if (result.SignError !== '') throw new EscherSignerError(result.SignError);
return result.SignedURL;
}
}
module.exports = EscherSigner;