-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChannelClient.js
153 lines (140 loc) · 3.56 KB
/
ChannelClient.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const axios = require('axios').default;
const crypto = require('crypto');
class ChannelClient {
/**
*
* @param {string} zwermBaseUrl
* @param {string} botTeam
* @param {string} botId
* @param {string} channelId
* @param {string} authToken
*/
constructor(zwermBaseUrl, botTeam, botId, channelId, authToken) {
/**
*
* @type {string}
* @private
*/
this._zwermBaseUrl = zwermBaseUrl;
/**
*
* @type {string}
* @private
*/
this._botTeam = botTeam;
/**
*
* @type {string}
* @private
*/
this._botId = botId;
/**
*
* @type {string}
* @private
*/
this._channelId = channelId;
/**
*
* @type {string}
* @private
*/
this._authToken = authToken;
}
// region getters & setters
/**
*
* @return {'sha256'}
* @static
* @constant
*/
static get HMAC_HEADER_SIGNATURE_ALGORITHM() {
return 'sha256';
}
// region authToken (get & set)
/**
*
* @return {string}
*/
get authToken() {
return this._authToken;
};
/**
*
* @param {string} authToken
*/
set authToken(authToken) {
this._authToken = authToken;
};
// endregion
/**
*
* @return {string}
*/
get fullZwermUrl() {
return `${this._zwermBaseUrl}/bots/${this._botTeam}/${this._botId}/channels/${this._channelId}`;
}
// endregion
// noinspection JSMethodCanBeStatic
/**
* Computes the HMAC-X signature for the given raw request body, using the given string.
*
* 'X' in this case is the algorithm, usually sha1 or sha256.
*
* The signature should be stored on the request as 'x-stamp-signature'
*
* @param {'sha1'|'sha256'|string} algorithm
* @param {string|Buffer} key
* @param {string} rawRequestBody
*
* @return {string} the computed signature of the raw request body, in uppercase.
* @protected
* @instance
*/
_computeRequestSignature(algorithm, key, rawRequestBody) {
return crypto.createHmac(algorithm, key)
.update(new Buffer(rawRequestBody))
.digest('hex')
.toUpperCase();
}
/**
* Builds the value for the x-stamp-signature header.
*
* @param {Object} requestBody
*
* @return {string}
* @private
*/
_buildSignatureHeaderForRequest(requestBody) {
return this._computeRequestSignature(this.constructor.HMAC_HEADER_SIGNATURE_ALGORITHM, this._authToken, JSON.stringify(requestBody));
}
/**
*
* @param {Object} requestBody
*
* @return {{'X-StaMP-Signature': string, 'X-Clacks-Overhead': 'GNU Terry Pratchett'}}
* @private
*/
_buildHeadersForRequest(requestBody) {
return {
'X-StaMP-Signature': this._buildSignatureHeaderForRequest(requestBody),
'X-Clacks-Overhead': 'GNU Terry Pratchett'
};
}
/**
* @param {string} senderId
* @param {StaMP.Protocol.Letter} letter
*
* @return {AxiosPromise}
*/
sendLetter(senderId, letter) {
const body = {
token: this._authToken,
senderId,
letter
};
const headers = this._buildHeadersForRequest(body);
return axios.post(this.fullZwermUrl, body, { headers });
}
}
module.exports = ChannelClient;