JwtTest.java revision a393c458f24ae38c8259dea2245aa436abe248f9
/*
* The contents of this file are subject to the terms of the Common Development and
* Distribution License (the License). You may not use this file except in compliance with the
* License.
*
* You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
* specific language governing permission and limitations under the License.
*
* When distributing Covered Software, include this CDDL Header Notice in each file and include
* the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
* Header, with the fields enclosed by brackets [] replaced by your own identifying
* information: "Portions copyright [year] [name of copyright owner]".
*
* Copyright 2013 ForgeRock Inc.
*/
public class JwtTest {
private JwtBuilder jwtBuilder;
public void setUp() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
jwtBuilder = new JwtBuilder();
}
}
}
@Test
public void shouldCreatePlaintextJwt() throws JWTBuilderException {
//Given
//When
.build();
//Then
String expectedContent = "{\"KEY2\":true,\"KEY1\":\"VALUE1\",\"KEY4\":\"Mon Mar 04 11:44:46 GMT 2013\"," +
"\"KEY3\":123,\"KEY6\":45.43,\"KEY5\":1362397486273}";
}
@Test
public void shouldReconstructPlaintextJwt() throws JSONException {
//Given
//When
".eyJLRVkyIjp0cnVlLCJLRVkxIjoiVkFMVUUxIiwiS0VZNCI6Ik1vbiBNYXIgMDQgMTE6NDQ6NDYgR01UIDIwMTMiLCJLRV" +
"kzIjoxMjMsIktFWTYiOjQ1LjQzLCJLRVk1IjoxMzYyMzk3NDg2MjczfQ==" +
".";
//When
//Then
}
@Test
//Given
//When
boolean verified = JwsSignatureUtil.getInstance().verify(JwsAlgorithm.HS256, key, certificate, "123abc",
//Then
}
@Test
//Given
//When
boolean verified = JwsSignatureUtil.getInstance().verify(JwsAlgorithm.RS256, key, certificate, "123abc",
//Then
}
@Test
public void shouldCreateSignedHS256Jwt() throws JWTBuilderException, KeyStoreException, IOException,
//Given
//When
.build();
//Then
String expectedHeader = "{\"alg\":\"HS256\",\"H2\":\"HEADER2\",\"H1\":\"HEADER1\",\"typ\":\"JWT\"}";
String expectedContent = "{\"KEY2\":true,\"KEY1\":\"VALUE1\",\"KEY4\":\"Mon Mar 04 11:44:46 GMT 2013\"," +
"\"KEY3\":123,\"KEY6\":45.43,\"KEY5\":1362397486273}";
}
@Test
public void shouldReconstructSignedHS256Jwt() throws JSONException, CertificateException, NoSuchAlgorithmException, IOException, KeyStoreException, JWTBuilderException, UnrecoverableKeyException {
//Given
//When
.build();
//When
//Then
assertEquals(reconstructedJwt.getJwt().getContent("KEY4").toString(), new Date(1362397486273L).toString());
}
@Test
public void shouldCreateSignedRS256Jwt() throws JWTBuilderException, KeyStoreException, IOException,
//Given
//When
.build();
//Then
String expectedHeader = "{\"alg\":\"RS256\",\"H2\":\"HEADER2\",\"H1\":\"HEADER1\",\"typ\":\"JWT\"}";
String expectedContent = "{\"KEY2\":true,\"KEY1\":\"VALUE1\",\"KEY4\":\"Mon Mar 04 11:44:46 GMT 2013\"," +
"\"KEY3\":123,\"KEY6\":45.43,\"KEY5\":1362397486273}";
String expectedEncodedThirdPart = "WX9ZTJXbDTbwGCFEv7ak//Gmah+PyOl+aJJyXuiYNiGLU2S35qFsfNKOgcT3NxLsu" +
"m2qkOOeJ4KJNusSD6ZSC7d8oGtgGSy4LOMJCEu89YCo8HCPKux+Y8PNMzlf4VrGgnb+CLZOix2R4JflqhDDV2Z1egyy" +
"24ltzoXntU7CYVodHazIwJkkPNrM4UE6oA+xQRkACdISpMSmom/ZVHV7WfHFom/YbiAkunlbkYUuLjrs+BPLTNg0KXIkWDw==";
}
@Test
NoSuchAlgorithmException, IOException, KeyStoreException, JWTBuilderException, UnrecoverableKeyException {
//Given
//When
.build();
//When
//Then
assertEquals(reconstructedJwt.getJwt().getContent("KEY4").toString(), new Date(1362397486273L).toString());
}
public void shouldThrowUnsupportedOperationExceptionWhenEncryptingJwt() {
//Given
//When
//Then
fail();
}
public void shouldThrowUnsupportedOperationExceptionWhenEncryptingSignedJwt() throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableKeyException {
//Given
//When
//Then
fail();
}
}