Skip to content

Commit 8054e18

Browse files
committed
DXE-3345 Added some unit tests
1 parent a6bd8ab commit 8054e18

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# EdgeAuth-Token-Java: Akamai Edge Authorization Token for Java
22

33
[![Maven Central](https://img.shields.io/badge/maven%20central-0.2.0-brightgreen.svg)](http://search.maven.org/#artifactdetails%7Ccom.akamai%7Cedgeauth%7C0.2.0%7C)
4-
[![Build Status](https://travis-ci.org/akamai/EdgeAuth-Token-Java.svg?branch=master)](https://github.com/akamai/EdgeAuth-Token-Java/actions/workflows/check.yml/badge.svg)
4+
[![Build Status](https://github.com/akamai/EdgeAuth-Token-Java/actions/workflows/check.yml/badge.svg)
55
[![License](http://img.shields.io/:license-apache-blue.svg)](https://github.com/akamai/EdgeAuth-Token-Java/blob/master/LICENSE)
66

77
EdgeAuth-Token-Java is Akamai Edge Authorization Token in the HTTP Cookie, Query String and Header for a client.
88
You can configure it in the Property Manager at https://control.akamai.com.
99
It's a behavior which is Auth Token 2.0 Verification.
1010

11-
EdgeAuth-Token-Java supports Java 1.6+.
11+
EdgeAuth-Token-Java supports Java 8+.
1212

1313
<div style="text-align:center"><img src=https://github.com/AstinCHOI/akamai-asset/blob/master/edgeauth/edgeauth.png?raw=true /></div>
1414

src/main/java/com/akamai/edgeauth/EdgeAuth.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ private String generateToken(String path, boolean isUrl) throws EdgeAuthExceptio
303303
hmac.init(secretKey);
304304

305305
byte[] hmacBytes = hmac.doFinal(hashSource.toString().getBytes());
306-
return newToken.toString() + "hmac=" +
306+
return newToken + "hmac=" +
307307
String.format("%0" + (2*hmac.getMacLength()) + "x", new BigInteger(1, hmacBytes));
308308
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
309309
throw new EdgeAuthException(e.toString());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.akamai.edgeauth;
2+
3+
import org.junit.Assert;
4+
import org.junit.Rule;
5+
import org.junit.Test;
6+
import org.junit.rules.ExpectedException;
7+
8+
import java.time.Instant;
9+
import java.util.regex.Pattern;
10+
11+
// Test content taken from https://github.com/mobilerider/EdgeAuth-Token-Golang/blob/master/edgeauth_test.go
12+
public class EdgeAuthTest {
13+
14+
private static final String sampleKey = "52a152a152a152a152a152a152a1";
15+
private static final String samplePath = "/this/is/a/test";
16+
17+
@Test
18+
public void testGenerateAclToken() throws EdgeAuthException {
19+
EdgeAuth ea = new EdgeAuthBuilder().algorithm("SHA256").key(sampleKey).startTime(Instant.now().toEpochMilli()/1000).windowSeconds(300).build();
20+
String token = ea.generateACLToken(samplePath);
21+
22+
String[] fields = token.split(String.valueOf(ea.getFieldDelimiter()));
23+
Assert.assertEquals("ACL token should consists of 4 parts: " + token, 4, fields.length);
24+
25+
String expectedStart = "st=" + ea.getStartTime();
26+
Assert.assertEquals("Start field does not match", expectedStart, fields[0]);
27+
28+
String expectedExpire = "exp=" + (ea.getStartTime() + ea.getWindowSeconds());
29+
Assert.assertEquals("Expire field does not match", expectedExpire, fields[1]);
30+
31+
String expectedAcl = "acl=.+";
32+
Assert.assertTrue("ACL field does not match: " + fields[2], Pattern.matches(expectedAcl, fields[2]));
33+
34+
String expectedHmac = "hmac=[a-f0-9]{64}";
35+
Assert.assertTrue("Hmac field does not match: " + fields[3], Pattern.matches(expectedHmac, fields[3]));
36+
}
37+
38+
@Test
39+
public void testGenerateUrlToken() throws EdgeAuthException {
40+
EdgeAuth ea = new EdgeAuthBuilder().algorithm("SHA256").key(sampleKey).startTime(Instant.now().toEpochMilli()/1000).windowSeconds(300).build();
41+
String token = ea.generateURLToken(samplePath);
42+
43+
String[] fields = token.split(String.valueOf(ea.getFieldDelimiter()));
44+
Assert.assertEquals("URL token should consists of 3 parts: " + token, 3, fields.length);
45+
46+
String expectedStart = "st=" + ea.getStartTime();
47+
Assert.assertEquals("Start field does not match", expectedStart, fields[0]);
48+
49+
String expectedExpire = "exp=" + (ea.getStartTime() + ea.getWindowSeconds());
50+
Assert.assertEquals("Expire field does not match", expectedExpire, fields[1]);
51+
52+
String expectedHmac = "hmac=[a-f0-9]{64}";
53+
Assert.assertTrue("Hmac field does not match: " + fields[2], Pattern.matches(expectedHmac, fields[2]));
54+
}
55+
56+
@Rule
57+
public ExpectedException exceptionRule = ExpectedException.none();
58+
59+
@Test
60+
public void testGenerateTokenWithInvalidStartAndEndDate() throws EdgeAuthException {
61+
exceptionRule.expect(EdgeAuthException.class);
62+
exceptionRule.expectMessage("Token will have already expired.");
63+
64+
long startTimeSeconds = Instant.now().toEpochMilli() / 1000;
65+
EdgeAuth ea = new EdgeAuthBuilder().algorithm("SHA256").key(sampleKey)
66+
.startTime(startTimeSeconds + 300)
67+
.endTime(startTimeSeconds).build();
68+
ea.generateURLToken(samplePath);
69+
70+
}
71+
72+
}

0 commit comments

Comments
 (0)