1 | 'use strict';
|
2 |
|
3 | const HEADER_ENCODING_FORMAT = 'base64';
|
4 |
|
5 | const encodingModeEnum = {
|
6 | STRICT: 'strict',
|
7 | LOOSE: 'loose',
|
8 | };
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function useFormURLEncode(value) {
|
21 | return encodeURIComponent(value).replace(/%20/g, '+');
|
22 | }
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | function getCredentialsString(clientID, clientSecret) {
|
32 | return `${clientID}:${clientSecret}`;
|
33 | }
|
34 |
|
35 | class Encoding {
|
36 | constructor(encodingMode) {
|
37 | this.encodingMode = encodingMode;
|
38 | }
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | getAuthorizationHeaderToken(clientID, clientSecret) {
|
47 | let encodedCredentials;
|
48 |
|
49 | if (this.encodingMode === encodingModeEnum.STRICT) {
|
50 | encodedCredentials = getCredentialsString(useFormURLEncode(clientID), useFormURLEncode(clientSecret));
|
51 | } else {
|
52 | encodedCredentials = getCredentialsString(clientID, clientSecret);
|
53 | }
|
54 |
|
55 | return Buffer.from(encodedCredentials).toString(HEADER_ENCODING_FORMAT);
|
56 | }
|
57 | }
|
58 |
|
59 | module.exports = {
|
60 | Encoding,
|
61 | encodingModeEnum,
|
62 | };
|