Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ typings/
# next.js build output
.next
.vscode/launch.json
/.vs
70 changes: 68 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@socketlabs/email",
"version": "1.4.0",
"version": "1.4.1",
"description": "SocketLabs Email Delivery node.js client library",
"main": "./src/socketlabsClient.js",
"repository": {
Expand All @@ -20,7 +20,8 @@
"contributors": [
"Matt Reibach <matt@socketlabs.com>",
"David Schrenker <david.schrenker@socketlabs.com>",
"Mirriam Ronoh <mirriam.ronoh@socketlabs.com>"
"Mirriam Ronoh <mirriam.ronoh@socketlabs.com>",
"Michael Boshuyzen <mike.boshuyzen@socketlabs.com"
],
"license": "MIT",
"bugs": {
Expand Down
69 changes: 69 additions & 0 deletions src/apiKeyParseResult.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* Enumerated result of the client send
*/
module.exports = Object.freeze(
{
/**
* No result could be produced.
*/
Invalid: {
name: "None",
value: 0
},
/**
* The key length was found to be invalid.
*/
InvalidKeyLength: {
name: "InvalidKeyLength",
value: 1
},
/**
* The key format was found to be invalid.
*/
InvalidKeyFormat: {
name: "InvalidKeyFormat",
value: 2
},
/**
* The key was found to be blank or invalid.
*/
InvalidEmptyOrWhitespace: {
name: "InvalidEmptyOrWhitespace",
value: 3
},
/**
* The public portion of the key was unable to be parsed.
*/
InvalidUnableToExtractPublicPart: {
name: "InvalidUnableToExtractPublicPart",
value: 4
},
/**
* The secret portion of the key was unable to be parsed.
*/
InvalidUnableToExtractSecretPart: {
name: "InvalidUnableToExtractSecretPart",
value: 5
},
/**
* The public portion of the key is the incorrect length.
*/
InvalidPublicPartLength: {
name: "InvalidPublicPartLength",
value: 6
},
/**
* The secret portion of the key is the incorrect length.
*/
InvalidSecretPartLength: {
name: "InvalidSecretPartLength",
value: 7
},
/**
* Key was successfully parsed.
*/
Success: {
name: "Success",
value: 8
}
});
52 changes: 52 additions & 0 deletions src/core/apiKeyParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict';

const apiKeyParseResult = require('../apiKeyParseResult');
/**
* Used by the SocketLabsClient to parse the provided API Key and determine the authorization method.
*/
class ApiKeyParser {

constructor(){

}

parseApiKey(wholeApiKey) {

if (!wholeApiKey || wholeApiKey === "")
{
return apiKeyParseResult.InvalidEmptyOrWhitespace;
}

if (wholeApiKey.length != 61)
{
return apiKeyParseResult.InvalidKeyLength;
}

let seperator = wholeApiKey.indexOf(".");

if (seperator == -1)
{
return apiKeyParseResult.InvalidKeyFormat;
}

let publicPart = wholeApiKey.substring(0, seperator);

if (publicPart.length != 20)
{
return apiKeyParseResult.InvalidPublicPartLength;
}

let privatePart = wholeApiKey.substring(seperator +1);

if (privatePart.length != 40)
{
return apiKeyParseResult.InvalidSecretPartLength;
}

return apiKeyParseResult.Success;

}

}

module.exports = ApiKeyParser;
48 changes: 36 additions & 12 deletions src/socketlabsClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { Attachment, BasicMessage, BulkMessage, BulkRecipient, CustomHeader, Emai
const sendResultEnum = require('./sendResultEnum');
const sendResponse = require('./sendResponse');
const retryHandler = require('./core/retryHandler');
const apiKeyParser = require('./core/apiKeyParser');
const apiKeyParseResult = require('./apiKeyParseResult');

/**
* SocketLabsClient is a wrapper for the SocketLabs Injection API that makes
Expand Down Expand Up @@ -99,18 +101,40 @@ class SocketLabsClient {

createRequest(requestJson) {

let request = {
url: this.endpointUrl,
method: "POST",
data: {
apiKey: this.apiKey,
serverId: this.serverId,
messages: [requestJson],
},
headers: { 'User-Agent': this.userAgent },
timeout: this.requestTimeout * 1000,

};
var request;

let parser = new apiKeyParser

if (parser.parseApiKey(this.apiKey) === apiKeyParseResult.Success)
{
request = {
url: this.endpointUrl,
method: "POST",
data: {
serverId: this.serverId,
messages: [requestJson],
},
headers: {
'User-Agent': this.userAgent,
'Authorization': 'Bearer ' + this.apiKey
},
timeout: this.requestTimeout * 1000,

};
} else {
request = {
url: this.endpointUrl,
method: "POST",
data: {
apiKey: this.apiKey,
serverId: this.serverId,
messages: [requestJson],
},
headers: { 'User-Agent': this.userAgent },
timeout: this.requestTimeout * 1000,

};
}

if (this.proxy) {
let proxyUrl = new URL(this.proxy);
Expand Down