Skip to content

Commit f70901b

Browse files
Merge pull request #10 from socketlabs/usr/mike.boshuyzen/add-api-key-parser
Usr/mike.boshuyzen/add api key parser
2 parents ec435db + 3abb95e commit f70901b

File tree

8 files changed

+177
-13
lines changed

8 files changed

+177
-13
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,16 @@ proguard/
3232
build/
3333
bin/
3434
gradle.properties
35+
/.vs/VSWorkspaceState.json
36+
/.vs/socketlabs-java/v17/TestStore/0/testlog.manifest
37+
/.vs/socketlabs-java/v17/TestStore/0/000.testlog
38+
/.vs/socketlabs-java/v17/TestStore/0
39+
/.vs/socketlabs-java/v17/.suo
40+
/.vs/socketlabs-java/v17
41+
/.vs/socketlabs-java/FileContentIndex/read.lock
42+
/.vs/socketlabs-java/FileContentIndex/ef65df61-58c3-4809-877e-1a74a50b04c7.vsidx
43+
/.vs/socketlabs-java/FileContentIndex/48b5b17b-b96e-40bb-b2b8-c0ea41589ac7.vsidx
44+
/.vs/socketlabs-java/FileContentIndex/269ac744-ce5b-47d6-93ab-6719ed06796b.vsidx
45+
/.vs/socketlabs-java/FileContentIndex/1393436c-32a5-4a38-a2bb-d05cfa884457.vsidx
46+
/.vs/socketlabs-java/FileContentIndex
47+
/.vs/slnx.sqlite

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,19 @@ repositories {
4646
<dependency>
4747
<groupId>com.socketlabs</groupId>
4848
<artifactId>injectionApi</artifactId>
49-
<version>1.4.0</version>
49+
<version>1.4.4</version>
5050
</dependency>
5151
```
5252

5353
### via jar file
5454

5555
You can just drop the jar file in.
5656

57-
[injectionApi-1.4.0.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.4.0/injectionApi-1.4.0.jar)
57+
[injectionApi-1.4.4.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.4.4/injectionApi-1.4.4.jar)
5858

59-
[injectionApi-1.4.0-sources.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.4.0/injectionApi-1.4.0-sources.jar)
59+
[injectionApi-1.4.4-sources.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.4.4/injectionApi-1.4.4-sources.jar)
6060

61-
[injectionApi-1.4.0-javadoc.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.4.0/injectionApi-1.4.0-javadoc.jar)
61+
[injectionApi-1.4.4-javadoc.jar](https://github.com/socketlabs/socketlabs-java/releases/download/1.4.4/injectionApi-1.4.4-javadoc.jar)
6262

6363
Alternately, you can simply [clone this repository](https://github.com/socketlabs/socketlabs-java.git) directly to include the source code in your project.
6464

@@ -232,6 +232,7 @@ For more information about AMP please see [AMP Project](https://amp.dev/document
232232

233233
<a name="version"></a>
234234
# Version
235+
* 1.4.4 - Added MetadataOrTagsAreTooLarge error messsage
235236
* 1.4.0 - Adding Metadata and Tags
236237
* 1.2.1 - Adding optional retry logic for Http requests. If configured, the request will retry when certain 500 errors occur (500, 502, 503, 504)
237238
* 1.1.1 - Adding request timeout value on the client for Http requests

examples/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ dependencies {
1616
testCompile group: 'junit', name: 'junit', version: '4.9.2'
1717
compile 'com.fasterxml.jackson.core:jackson-core:2.14.0'
1818
compile 'com.fasterxml.jackson.core:jackson-databind:2.14.0'
19-
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.4.0-SNAPSHOT'
19+
compile group: 'com.socketlabs', name: 'injectionApi', version: '1.4.2'
2020
}

injectionApi/build.gradle

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ plugins {
77
def baseGroupId = "com.socketlabs"
88
def baseArtifactId = 'injectionApi'
99
def computeVersion() {
10-
def baseVersion = "1.4.0"
10+
def baseVersion = "1.4.4"
1111
def release = true
1212
if (release)
1313
return "${baseVersion}"
@@ -28,8 +28,9 @@ compileJava {
2828
dependencies {
2929
compile 'com.fasterxml.jackson.core:jackson-core:2.14.0'
3030
compile 'com.fasterxml.jackson.core:jackson-databind:2.14.0'
31-
compile group: 'com.google.guava', name: 'guava', version: '31.1-jre'
31+
compile group: 'com.google.guava', name: 'guava', version: '32.1.3-jre'
3232
compile 'com.squareup.okhttp3:okhttp:4.10.0'
33+
3334
}
3435
jar {
3536
manifest {
@@ -65,7 +66,7 @@ uploadArchives {
6566
MavenDeployment deployment -> signing.signPom(deployment)
6667
}
6768

68-
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") {
69+
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
6970
authentication(userName: sonatypeUsername, password: sonatypePassword)
7071
}
7172
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots") {
@@ -123,6 +124,17 @@ uploadArchives {
123124
role 'Developer'
124125
}
125126
}
127+
developer {
128+
id 'mike.boshuyzen'
129+
name 'Mike Boshuyzen'
130+
organization {
131+
name 'socketlabs'
132+
url 'https://github.com/socketlabs'
133+
}
134+
roles {
135+
role 'Developer'
136+
}
137+
}
126138
}
127139
contributors {
128140
contributor {
@@ -139,4 +151,4 @@ uploadArchives {
139151
}.writeTo("$projectDir/pom.xml")
140152
}
141153
}
142-
}
154+
}

injectionApi/pom.xml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>com.socketlabs</groupId>
66
<artifactId>injectionApi</artifactId>
7-
<version>1.4.0</version>
7+
<version>1.4.2</version>
88
<name>socketlabs-java</name>
99
<description>SocketLabs Email Delivery Java library</description>
1010
<url>https://github.com/socketlabs/socketlabs-java/</url>
@@ -23,15 +23,23 @@
2323
<id>david-schrenker</id>
2424
<name>David Schrenker</name>
2525
<email>[email protected]</email>
26-
<organization>org.apache.maven.model.Organization@5245fb42</organization>
26+
<organization>org.apache.maven.model.Organization@f125de5</organization>
2727
<roles>
2828
<role>Developer</role>
2929
</roles>
3030
</developer>
3131
<developer>
3232
<id>rbrazuk</id>
3333
<name>Ross Brazuk</name>
34-
<organization>org.apache.maven.model.Organization@560210f5</organization>
34+
<organization>org.apache.maven.model.Organization@19eaf41b</organization>
35+
<roles>
36+
<role>Developer</role>
37+
</roles>
38+
</developer>
39+
<developer>
40+
<id>mike.boshuyzen</id>
41+
<name>Mike Boshuyzen</name>
42+
<organization>org.apache.maven.model.Organization@6c023990</organization>
3543
<roles>
3644
<role>Developer</role>
3745
</roles>
@@ -40,7 +48,7 @@
4048
<contributors>
4149
<contributor>
4250
<name>Ryan Lydzinski</name>
43-
<organization>org.apache.maven.model.Organization@166a59d1</organization>
51+
<organization>org.apache.maven.model.Organization@399b8af0</organization>
4452
<roles>
4553
<role>Intern</role>
4654
</roles>

injectionApi/src/main/java/com/socketLabs/injectionApi/SocketLabsClient.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ public SendResponse send(BasicMessage message) throws Exception {
8383
return result;
8484

8585
HttpRequest request = buildHttpRequest(this.proxy);
86+
87+
ApiKeyParser keyParser = new ApiKeyParser();
88+
ApiKeyParseResult parseResult = keyParser.Parse(this.apiKey);
89+
90+
if (parseResult != null && parseResult == ApiKeyParseResult.Success)
91+
{
92+
request.setHeader("Authorization", "Bearer " + this.apiKey);
93+
}
94+
8695
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
8796
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
8897
Response response = retryHandler.send();
@@ -105,6 +114,15 @@ public SendResponse send(BulkMessage message) throws Exception {
105114
return result;
106115

107116
HttpRequest request = buildHttpRequest(this.proxy);
117+
118+
ApiKeyParser keyParser = new ApiKeyParser();
119+
ApiKeyParseResult parseResult = keyParser.Parse(this.apiKey);
120+
121+
if (parseResult != null && parseResult == ApiKeyParseResult.Success)
122+
{
123+
request.setHeader("Authorization", "Bearer " + this.apiKey);
124+
}
125+
108126
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
109127

110128
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
@@ -131,6 +149,15 @@ public void sendAsync(BasicMessage message, final SendAsyncCallback callback) th
131149
}
132150

133151
HttpRequest request = buildHttpRequest(this.proxy);
152+
153+
ApiKeyParser keyParser = new ApiKeyParser();
154+
ApiKeyParseResult parseResult = keyParser.Parse(this.apiKey);
155+
156+
if (parseResult != null && parseResult == ApiKeyParseResult.Success)
157+
{
158+
request.setHeader("Authorization", "Bearer " + this.apiKey);
159+
}
160+
134161
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
135162

136163
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
@@ -164,6 +191,15 @@ public void sendAsync(BulkMessage message, final SendAsyncCallback callback) thr
164191
}
165192

166193
HttpRequest request = buildHttpRequest(this.proxy);
194+
195+
ApiKeyParser keyParser = new ApiKeyParser();
196+
ApiKeyParseResult parseResult = keyParser.Parse(this.apiKey);
197+
198+
if (parseResult != null && parseResult == ApiKeyParseResult.Success)
199+
{
200+
request.setHeader("Authorization", "Bearer " + this.apiKey);
201+
}
202+
167203
request.setBody(new InjectionRequestFactory(this.serverId, this.apiKey).GenerateRequest(message));
168204

169205
RetryHandler retryHandler = new RetryHandler(request, this.endPointUrl, new RetrySettings(this.numberOfRetries));
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.socketLabs.injectionApi.core;
2+
3+
public enum ApiKeyParseResult {
4+
/// <summary>
5+
/// No result could be produced.
6+
/// </summary>
7+
8+
None,
9+
/// <summary>
10+
/// The key length was found.
11+
/// </summary>
12+
InvalidKeyLength,
13+
14+
/// <summary>
15+
/// The key format was found.
16+
/// </summary>
17+
InvalidKeyFormat,
18+
19+
/// <summary>
20+
/// The key was found to be blank or invalid.
21+
/// </summary>
22+
InvalidEmptyOrWhitespace,
23+
24+
/// <summary>
25+
/// The public portion of the key was unable to be parsed.
26+
/// </summary>
27+
InvalidUnableToExtractPublicPart,
28+
29+
/// <summary>
30+
/// The secret portion of the key was unable to be parsed.
31+
/// </summary>
32+
InvalidUnableToExtractSecretPart,
33+
34+
/// <summary>
35+
/// The public portion of the key is the incorrect length.
36+
/// </summary>
37+
InvalidPublicPartLength,
38+
39+
/// <summary>
40+
/// The secret portion of the key is the incorrect length.
41+
/// </summary>
42+
InvalidSecretPartLength,
43+
44+
/// <summary>
45+
/// Key was successfully parsed.
46+
/// </summary>
47+
48+
Success
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.socketLabs.injectionApi.core;
2+
3+
/**
4+
* Contains the method for parsing the Api Key.
5+
*/
6+
public class ApiKeyParser {
7+
8+
/**
9+
* Parse the API key to determine what kind of key was provided.
10+
* @param wholeApiKey String
11+
* @return A ApiKeyParseResult with the parsing results
12+
*/
13+
public ApiKeyParseResult Parse(String wholeApiKey)
14+
{
15+
if (wholeApiKey == null || wholeApiKey.length() == 0)
16+
return ApiKeyParseResult.InvalidEmptyOrWhitespace;
17+
18+
if (wholeApiKey.length() != 61)
19+
return ApiKeyParseResult.InvalidKeyLength;
20+
21+
if (wholeApiKey.indexOf('.') == -1)
22+
return ApiKeyParseResult.InvalidKeyFormat;
23+
24+
//extract public part
25+
int publicPartEnd = wholeApiKey.substring(0, 50).indexOf('.'); //don't check more than 50 chars
26+
if (publicPartEnd == -1)
27+
return ApiKeyParseResult.InvalidUnableToExtractPublicPart;
28+
29+
String publicPart = wholeApiKey.substring(0, publicPartEnd);
30+
if (publicPart.length() != 20)
31+
return ApiKeyParseResult.InvalidPublicPartLength;
32+
33+
//now extract the private part
34+
if (wholeApiKey.length() <= publicPartEnd + 1)
35+
return ApiKeyParseResult.InvalidUnableToExtractSecretPart;
36+
37+
String privatePart = wholeApiKey.substring(publicPartEnd + 1);
38+
if (privatePart.length() != 40)
39+
return ApiKeyParseResult.InvalidSecretPartLength;
40+
41+
//success
42+
return ApiKeyParseResult.Success;
43+
}
44+
45+
}

0 commit comments

Comments
 (0)