Skip to content

Commit c6925c1

Browse files
committed
Fix the tests
1 parent 77d89d0 commit c6925c1

File tree

4 files changed

+134
-109
lines changed

4 files changed

+134
-109
lines changed

src/main/java/com/twilio/example/ValidationExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ public static void main(String[] args) throws Exception {
6666
System.out.println(m.getSid());
6767

6868
}
69-
}
69+
}

src/main/java/com/twilio/http/ValidationInterceptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ public void process(HttpRequest request, HttpContext context) throws HttpExcepti
3333
Jwt jwt = ValidationToken.fromHttpRequest(accountSid, credentialSid, signingKeySid, privateKey, request, HEADERS);
3434
request.addHeader("Twilio-Client-Validation", jwt.toJwt());
3535
}
36-
}
36+
}

src/main/java/com/twilio/jwt/Jwt.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import io.jsonwebtoken.Jwts;
55
import io.jsonwebtoken.SignatureAlgorithm;
66

7-
import java.nio.charset.StandardCharsets;
8-
import java.security.PrivateKey;
7+
import javax.crypto.spec.SecretKeySpec;
8+
import java.security.Key;
99
import java.util.Date;
1010
import java.util.HashMap;
1111
import java.util.Map;
@@ -16,8 +16,7 @@
1616
public abstract class Jwt {
1717

1818
private final SignatureAlgorithm algorithm;
19-
private final PrivateKey secretKey;
20-
private final String secret;
19+
private final Key secretKey;
2120
private final String issuer;
2221
private final Date expiration;
2322

@@ -35,21 +34,29 @@ public Jwt(
3534
String issuer,
3635
Date expiration
3736
) {
38-
this.algorithm = algorithm;
39-
this.secret = secret;
40-
this.secretKey = null;
41-
this.issuer = issuer;
42-
this.expiration = expiration;
37+
this(
38+
algorithm,
39+
new SecretKeySpec(secret.getBytes(), algorithm.getJcaName()),
40+
issuer,
41+
expiration
42+
);
4343
}
4444

45+
/**
46+
* Create a new JWT.
47+
*
48+
* @param algorithm algorithm to use
49+
* @param secretKey secret key
50+
* @param issuer JWT issuer
51+
* @param expiration expiration Date
52+
*/
4553
public Jwt(
4654
SignatureAlgorithm algorithm,
47-
PrivateKey secretKey,
55+
Key secretKey,
4856
String issuer,
4957
Date expiration
5058
) {
5159
this.algorithm = algorithm;
52-
this.secret = null;
5360
this.secretKey = secretKey;
5461
this.issuer = issuer;
5562
this.expiration = expiration;
@@ -67,16 +74,11 @@ public String toJwt() {
6774

6875
JwtBuilder builder =
6976
Jwts.builder()
77+
.signWith(this.algorithm, this.secretKey)
7078
.setHeaderParams(headers)
7179
.setIssuer(this.issuer)
7280
.setExpiration(expiration);
7381

74-
if (this.secret != null) {
75-
builder.signWith(this.algorithm, this.secret.getBytes(StandardCharsets.UTF_8));
76-
} else if (this.secretKey != null) {
77-
builder.signWith(this.algorithm, this.secretKey);
78-
}
79-
8082
if (this.getClaims() != null) {
8183
for (Map.Entry<String, Object> entry : this.getClaims().entrySet()) {
8284
builder.claim(entry.getKey(), entry.getValue());
Lines changed: 113 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
package com.twilio.jwt.validation;
22

33
import com.google.common.collect.Lists;
4+
import com.twilio.jwt.Jwt;
5+
import io.jsonwebtoken.Claims;
6+
import io.jsonwebtoken.Jwts;
7+
import mockit.Expectations;
48
import mockit.Mocked;
59
import org.apache.http.Header;
610
import org.apache.http.HttpEntity;
711
import org.apache.http.HttpEntityEnclosingRequest;
812
import org.apache.http.message.BasicHeader;
13+
import org.junit.Assert;
914
import org.junit.Before;
15+
import org.junit.Test;
1016

17+
import java.io.ByteArrayInputStream;
18+
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
20+
import java.security.KeyPair;
21+
import java.security.KeyPairGenerator;
22+
import java.security.PrivateKey;
23+
import java.util.Date;
1124
import java.util.List;
1225

1326
public class ValidationTokenTest {
1427

1528
private static final List<String> SIGNED_HEADERS = Lists.newArrayList("host", "authorization");
16-
// private static final String CREDENTIAL_SID = "CR123";
29+
private static final String ACCOUNT_SID = "AC123";
1730
private static final String CREDENTIAL_SID = "CR123";
18-
private static final String SECRET = "secret";
31+
private static final String SIGNING_KEY_SID = "SK123";
1932

2033
private Header[] headers;
34+
private PrivateKey privateKey;
2135

2236
@Mocked
2337
private HttpEntityEnclosingRequest request;
@@ -26,98 +40,107 @@ public class ValidationTokenTest {
2640
private HttpEntity entity;
2741

2842
@Before
29-
public void setup() {
43+
public void setup() throws Exception {
3044
headers = new Header[2];
3145
headers[0] = new BasicHeader("host", "api.twilio.com");
3246
headers[1] = new BasicHeader("authorization", "foobar");
47+
48+
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
49+
keyGen.initialize(2048);
50+
KeyPair pair = keyGen.generateKeyPair();
51+
privateKey = pair.getPrivate();
52+
}
53+
54+
@Test
55+
public void testTokenBuilder() {
56+
Jwt jwt = new ValidationToken.Builder(ACCOUNT_SID, CREDENTIAL_SID, SIGNING_KEY_SID, privateKey)
57+
.method("GET")
58+
.uri("/Messages")
59+
.queryString("PageSize=5&Limit=10")
60+
.headers(headers)
61+
.signedHeaders(SIGNED_HEADERS)
62+
.requestBody("foobar")
63+
.build();
64+
65+
Claims claims =
66+
Jwts.parser()
67+
.setSigningKey(privateKey)
68+
.parseClaimsJws(jwt.toJwt())
69+
.getBody();
70+
71+
72+
this.validateToken(claims);
73+
Assert.assertEquals("authorization;host", claims.get("hrh"));
74+
Assert.assertEquals("4dc9b67bed579647914587b0e22a1c65c1641d8674797cd82de65e766cce5f80", claims.get("rqh"));
75+
}
76+
77+
@Test
78+
public void testTokenFromHttpRequest() throws IOException {
79+
new Expectations() {{
80+
request.getRequestLine().getMethod();
81+
result = "GET";
82+
83+
request.getRequestLine().getUri();
84+
result = "/Messages?PageSize=5&Limit=10";
85+
86+
request.getAllHeaders();
87+
result = headers;
88+
}};
89+
90+
Jwt jwt = ValidationToken.fromHttpRequest(ACCOUNT_SID, CREDENTIAL_SID, SIGNING_KEY_SID, privateKey, request, SIGNED_HEADERS);
91+
Claims claims =
92+
Jwts.parser()
93+
.setSigningKey(privateKey)
94+
.parseClaimsJws(jwt.toJwt())
95+
.getBody();
96+
97+
98+
this.validateToken(claims);
99+
Assert.assertEquals("authorization;host", claims.get("hrh"));
100+
Assert.assertEquals("4b3d2666845a38f00259a5231a08765bb2d12564bc4469fd5b2816204c588967", claims.get("rqh"));
33101
}
34102

35-
// @Test
36-
// public void testTokenBuilder() {
37-
// Jwt jwt = new ValidationToken.Builder(CREDENTIAL_SID, SECRET)
38-
// .method("GET")
39-
// .uri("/Messages")
40-
// .queryString("PageSize=5&Limit=10")
41-
// .headers(headers)
42-
// .signedHeaders(SIGNED_HEADERS)
43-
// .requestBody("foobar")
44-
// .build();
45-
//
46-
// Claims claims =
47-
// Jwts.parser()
48-
// .setSigningKey(SECRET.getBytes())
49-
// .parseClaimsJws(jwt.toJwt())
50-
// .getBody();
51-
//
52-
//
53-
// Assert.assertEquals("authorization;host", claims.get("hrh"));
54-
// Assert.assertEquals("4dc9b67bed579647914587b0e22a1c65c1641d8674797cd82de65e766cce5f80", claims.get("rqh"));
55-
// }
56-
//
57-
// @Test
58-
// public void testTokenFromHttpRequest() throws IOException {
59-
// new Expectations() {{
60-
// request.getRequestLine().getMethod();
61-
// result = "GET";
62-
//
63-
// request.getRequestLine().getUri();
64-
// result = "/Messages?PageSize=5&Limit=10";
65-
//
66-
// request.getAllHeaders();
67-
// result = headers;
68-
// }};
69-
//
70-
// Jwt jwt = ValidationToken.fromHttpRequest(CREDENTIAL_SID, SECRET, request, SIGNED_HEADERS);
71-
// Claims claims =
72-
// Jwts.parser()
73-
// .setSigningKey(SECRET.getBytes())
74-
// .parseClaimsJws(jwt.toJwt())
75-
// .getBody();
76-
//
77-
//
78-
// Assert.assertEquals("authorization;host", claims.get("hrh"));
79-
// Assert.assertEquals("4b3d2666845a38f00259a5231a08765bb2d12564bc4469fd5b2816204c588967", claims.get("rqh"));
80-
// }
81-
//
82-
// @Test
83-
// public void testTokenFromPostRequest() throws IOException {
84-
// new Expectations() {{
85-
// request.getRequestLine().getMethod();
86-
// result = "POST";
87-
//
88-
// request.getRequestLine().getUri();
89-
// result = "/Messages";
90-
//
91-
// request.getAllHeaders();
92-
// result = headers;
93-
//
94-
// request.getEntity();
95-
// result = entity;
96-
//
97-
// entity.getContent();
98-
// result = new ByteArrayInputStream("testbody".getBytes(StandardCharsets.UTF_8));
99-
// }};
100-
//
101-
// Jwt jwt = ValidationToken.fromHttpRequest(CREDENTIAL_SID, SECRET, request, SIGNED_HEADERS);
102-
// Claims claims =
103-
// Jwts.parser()
104-
// .setSigningKey(SECRET.getBytes())
105-
// .parseClaimsJws(jwt.toJwt())
106-
// .getBody();
107-
//
108-
//
109-
// Assert.assertEquals("authorization;host", claims.get("hrh"));
110-
// Assert.assertEquals("bd792c967c20d546c738b94068f5f72758a10d26c12979677501e1eefe58c65a", claims.get("rqh"));
111-
// }
112-
//
113-
// private void validateToken(Claims claims) {
114-
// Assert.assertEquals(CREDENTIAL_SID, claims.getIssuer());
115-
//
116-
// Assert.assertNotNull(claims.getExpiration());
117-
// Assert.assertNotNull(claims.get("hrh"));
118-
// Assert.assertNotNull(claims.get("rqh"));
119-
//
120-
// Assert.assertTrue(claims.getExpiration().getTime() > new Date().getTime());
121-
// }
103+
@Test
104+
public void testTokenFromPostRequest() throws IOException {
105+
new Expectations() {{
106+
request.getRequestLine().getMethod();
107+
result = "POST";
108+
109+
request.getRequestLine().getUri();
110+
result = "/Messages";
111+
112+
request.getAllHeaders();
113+
result = headers;
114+
115+
request.getEntity();
116+
result = entity;
117+
118+
entity.getContent();
119+
result = new ByteArrayInputStream("testbody".getBytes(StandardCharsets.UTF_8));
120+
}};
121+
122+
Jwt jwt = ValidationToken.fromHttpRequest(ACCOUNT_SID, CREDENTIAL_SID, SIGNING_KEY_SID, privateKey, request, SIGNED_HEADERS);
123+
Claims claims =
124+
Jwts.parser()
125+
.setSigningKey(privateKey)
126+
.parseClaimsJws(jwt.toJwt())
127+
.getBody();
128+
129+
130+
this.validateToken(claims);
131+
Assert.assertEquals("authorization;host", claims.get("hrh"));
132+
Assert.assertEquals("bd792c967c20d546c738b94068f5f72758a10d26c12979677501e1eefe58c65a", claims.get("rqh"));
133+
}
134+
135+
private void validateToken(Claims claims) {
136+
Assert.assertEquals(SIGNING_KEY_SID, claims.getIssuer());
137+
Assert.assertEquals(ACCOUNT_SID, claims.getSubject());
138+
139+
Assert.assertNotNull(claims.getExpiration());
140+
Assert.assertNotNull(claims.get("hrh"));
141+
Assert.assertNotNull(claims.get("rqh"));
142+
143+
Assert.assertTrue(claims.getExpiration().getTime() > new Date().getTime());
144+
}
122145

123146
}

0 commit comments

Comments
 (0)