Skip to content

Commit 024151c

Browse files
Merge pull request #17 from socketlabs/usr/mike.boshuyzen/add-api-key-parser
Added api key parser
2 parents 44d7efb + e6d9892 commit 024151c

File tree

6 files changed

+229
-16
lines changed

6 files changed

+229
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ typings/
6060
# next.js build output
6161
.next
6262
.vscode/launch.json
63+
/.vs

package-lock.json

Lines changed: 68 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@socketlabs/email",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "SocketLabs Email Delivery node.js client library",
55
"main": "./src/socketlabsClient.js",
66
"repository": {
@@ -20,7 +20,8 @@
2020
"contributors": [
2121
"Matt Reibach <matt@socketlabs.com>",
2222
"David Schrenker <david.schrenker@socketlabs.com>",
23-
"Mirriam Ronoh <mirriam.ronoh@socketlabs.com>"
23+
"Mirriam Ronoh <mirriam.ronoh@socketlabs.com>",
24+
"Michael Boshuyzen <mike.boshuyzen@socketlabs.com"
2425
],
2526
"license": "MIT",
2627
"bugs": {

src/apiKeyParseResult.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Enumerated result of the client send
3+
*/
4+
module.exports = Object.freeze(
5+
{
6+
/**
7+
* No result could be produced.
8+
*/
9+
Invalid: {
10+
name: "None",
11+
value: 0
12+
},
13+
/**
14+
* The key length was found to be invalid.
15+
*/
16+
InvalidKeyLength: {
17+
name: "InvalidKeyLength",
18+
value: 1
19+
},
20+
/**
21+
* The key format was found to be invalid.
22+
*/
23+
InvalidKeyFormat: {
24+
name: "InvalidKeyFormat",
25+
value: 2
26+
},
27+
/**
28+
* The key was found to be blank or invalid.
29+
*/
30+
InvalidEmptyOrWhitespace: {
31+
name: "InvalidEmptyOrWhitespace",
32+
value: 3
33+
},
34+
/**
35+
* The public portion of the key was unable to be parsed.
36+
*/
37+
InvalidUnableToExtractPublicPart: {
38+
name: "InvalidUnableToExtractPublicPart",
39+
value: 4
40+
},
41+
/**
42+
* The secret portion of the key was unable to be parsed.
43+
*/
44+
InvalidUnableToExtractSecretPart: {
45+
name: "InvalidUnableToExtractSecretPart",
46+
value: 5
47+
},
48+
/**
49+
* The public portion of the key is the incorrect length.
50+
*/
51+
InvalidPublicPartLength: {
52+
name: "InvalidPublicPartLength",
53+
value: 6
54+
},
55+
/**
56+
* The secret portion of the key is the incorrect length.
57+
*/
58+
InvalidSecretPartLength: {
59+
name: "InvalidSecretPartLength",
60+
value: 7
61+
},
62+
/**
63+
* Key was successfully parsed.
64+
*/
65+
Success: {
66+
name: "Success",
67+
value: 8
68+
}
69+
});

src/core/apiKeyParser.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
const apiKeyParseResult = require('../apiKeyParseResult');
4+
/**
5+
* Used by the SocketLabsClient to parse the provided API Key and determine the authorization method.
6+
*/
7+
class ApiKeyParser {
8+
9+
constructor(){
10+
11+
}
12+
13+
parseApiKey(wholeApiKey) {
14+
15+
if (!wholeApiKey || wholeApiKey === "")
16+
{
17+
return apiKeyParseResult.InvalidEmptyOrWhitespace;
18+
}
19+
20+
if (wholeApiKey.length != 61)
21+
{
22+
return apiKeyParseResult.InvalidKeyLength;
23+
}
24+
25+
let seperator = wholeApiKey.indexOf(".");
26+
27+
if (seperator == -1)
28+
{
29+
return apiKeyParseResult.InvalidKeyFormat;
30+
}
31+
32+
let publicPart = wholeApiKey.substring(0, seperator);
33+
34+
if (publicPart.length != 20)
35+
{
36+
return apiKeyParseResult.InvalidPublicPartLength;
37+
}
38+
39+
let privatePart = wholeApiKey.substring(seperator +1);
40+
41+
if (privatePart.length != 40)
42+
{
43+
return apiKeyParseResult.InvalidSecretPartLength;
44+
}
45+
46+
return apiKeyParseResult.Success;
47+
48+
}
49+
50+
}
51+
52+
module.exports = ApiKeyParser;

src/socketlabsClient.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const { Attachment, BasicMessage, BulkMessage, BulkRecipient, CustomHeader, Emai
88
const sendResultEnum = require('./sendResultEnum');
99
const sendResponse = require('./sendResponse');
1010
const retryHandler = require('./core/retryHandler');
11+
const apiKeyParser = require('./core/apiKeyParser');
12+
const apiKeyParseResult = require('./apiKeyParseResult');
1113

1214
/**
1315
* SocketLabsClient is a wrapper for the SocketLabs Injection API that makes
@@ -99,18 +101,40 @@ class SocketLabsClient {
99101

100102
createRequest(requestJson) {
101103

102-
let request = {
103-
url: this.endpointUrl,
104-
method: "POST",
105-
data: {
106-
apiKey: this.apiKey,
107-
serverId: this.serverId,
108-
messages: [requestJson],
109-
},
110-
headers: { 'User-Agent': this.userAgent },
111-
timeout: this.requestTimeout * 1000,
112-
113-
};
104+
var request;
105+
106+
let parser = new apiKeyParser
107+
108+
if (parser.parseApiKey(this.apiKey) === apiKeyParseResult.Success)
109+
{
110+
request = {
111+
url: this.endpointUrl,
112+
method: "POST",
113+
data: {
114+
serverId: this.serverId,
115+
messages: [requestJson],
116+
},
117+
headers: {
118+
'User-Agent': this.userAgent,
119+
'Authorization': 'Bearer ' + this.apiKey
120+
},
121+
timeout: this.requestTimeout * 1000,
122+
123+
};
124+
} else {
125+
request = {
126+
url: this.endpointUrl,
127+
method: "POST",
128+
data: {
129+
apiKey: this.apiKey,
130+
serverId: this.serverId,
131+
messages: [requestJson],
132+
},
133+
headers: { 'User-Agent': this.userAgent },
134+
timeout: this.requestTimeout * 1000,
135+
136+
};
137+
}
114138

115139
if (this.proxy) {
116140
let proxyUrl = new URL(this.proxy);

0 commit comments

Comments
 (0)