11package com .twilio .jwt .validation ;
22
33import 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 ;
48import mockit .Mocked ;
59import org .apache .http .Header ;
610import org .apache .http .HttpEntity ;
711import org .apache .http .HttpEntityEnclosingRequest ;
812import org .apache .http .message .BasicHeader ;
13+ import org .junit .Assert ;
914import 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 ;
1124import java .util .List ;
1225
1326public 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