2323import com .auth0 .jwt .PemUtils ;
2424import com .auth0 .jwt .TokenUtils ;
2525import com .auth0 .jwt .algorithms .Algorithm ;
26- import com .auth0 .jwt .creators .JWTCreator ;
2726import com .auth0 .jwt .interfaces .DecodedJWT ;
2827import com .auth0 .jwt .interfaces .ECDSAKeyProvider ;
2928import com .auth0 .jwt .interfaces .RSAKeyProvider ;
3029import com .auth0 .jwt .jwts .JWT ;
3130import org .apache .commons .codec .binary .Base64 ;
32- import org .bouncycastle .asn1 .eac .RSAPublicKey ;
3331import org .junit .Rule ;
3432import org .junit .Test ;
3533import org .junit .rules .ExpectedException ;
3634
3735import java .nio .charset .StandardCharsets ;
38- import java .security .*;
39- import java .security .SecureRandom ;
4036import java .security .interfaces .ECPrivateKey ;
4137import java .security .interfaces .RSAPrivateKey ;
4238import java .util .Date ;
4642import static org .hamcrest .Matchers .is ;
4743import static org .hamcrest .Matchers .notNullValue ;
4844import static org .junit .Assert .assertThat ;
49- import org .mockito .Mockito ;
5045import static org .mockito .Mockito .mock ;
5146import static org .mockito .Mockito .when ;
5247
53- import javax .crypto .Cipher ;
5448
5549public class JWTCreatorTest {
50+
5651 private static final String PRIVATE_KEY_FILE_RSA = "src/test/resources/rsa-private-from-Roland.pem" ;
5752 private static final String PRIVATE_KEY_FILE_EC_256 = "src/test/resources/ec256-key-private.pem" ;
5853
59-
6054 @ Rule
6155 public ExpectedException exception = ExpectedException .none ();
6256
@@ -112,7 +106,7 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithms() throws Exception {
112106 }
113107
114108 @ Test
115- public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEE16 () throws Exception {
109+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase16 () throws Exception {
116110 RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
117111 RSAKeyProvider provider = mock (RSAKeyProvider .class );
118112 when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
@@ -127,38 +121,10 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEE16() throws Exce
127121 JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
128122 DecodedJWT decoded = jwt .decode16Bytes (signed );
129123 algorithm .verify (decoded , EncodeType .Base16 );
130-
131- /*assertThat(signed, is(notNullValue()));
132- String[] parts = signed.split("\\.");
133- String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
134- assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));*/
135- }
136-
137- @ Test
138- public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEE32 () throws Exception {
139- RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
140- RSAKeyProvider provider = mock (RSAKeyProvider .class );
141- when (provider .getPrivateKeyId ()).thenReturn ("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg" );
142- when (provider .getPrivateKey ()).thenReturn (privateKey );
143- Algorithm algorithm = Algorithm .RSA256 (provider );
144-
145- String signed = JWTCreator .init ()
146- .withKeyId ("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg" )
147- .withIssuer ("auth0" )
148- .sign (algorithm , EncodeType .Base32 );
149-
150- JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
151- DecodedJWT decoded = jwt .decode32Bytes (signed );
152- algorithm .verify (decoded , EncodeType .Base32 );
153-
154- /*assertThat(signed, is(notNullValue()));
155- String[] parts = signed.split("\\.");
156- String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
157- assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));*/
158124 }
159125
160126 @ Test
161- public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEE32Roland () throws Exception {
127+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase32 () throws Exception {
162128 RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
163129 RSAKeyProvider provider = mock (RSAKeyProvider .class );
164130 when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
@@ -173,60 +139,24 @@ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEE32Roland() throw
173139 JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
174140 DecodedJWT decoded = jwt .decode32Bytes (signed );
175141 algorithm .verify (decoded , EncodeType .Base32 );
176-
177- /*assertThat(signed, is(notNullValue()));
178- String[] parts = signed.split("\\.");
179- String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
180- assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));*/
181142 }
182143
183144 @ Test
184- public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEE () throws Exception {
185- RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
186- RSAKeyProvider provider = mock (RSAKeyProvider .class );
187- when (provider .getPrivateKeyId ()).thenReturn ("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg" );
188- when (provider .getPrivateKey ()).thenReturn (privateKey );
189- Algorithm algorithm = Algorithm .RSA256 (provider );
190-
191- String signed = JWTCreator .init ()
192- .withKeyId ("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg" )
193- .withIssuer ("auth0" )
194- .sign (algorithm );
195-
196- JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("auth0" ).build ();
197- DecodedJWT decoded = jwt .decode (signed );
198- algorithm .verify (decoded , EncodeType .Base64 );
199-
200- /*assertThat(signed, is(notNullValue()));
201- String[] parts = signed.split("\\.");
202- String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
203- assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));*/
204- }
205-
206- @ Test
207- public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsDELETEEEEERoland () throws Exception {
145+ public void shouldAddKeyIdIfAvailableFromRSAAlgorithmsForBase64 () throws Exception {
208146 RSAPrivateKey privateKey = (RSAPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_RSA , "RSA" );
209147 RSAKeyProvider provider = mock (RSAKeyProvider .class );
210148 when (provider .getPrivateKeyId ()).thenReturn ("8RGoVdVjD8fItyR3FFo0hVNaZYtPGwoP6xKi9e_V7bI" );
211149 when (provider .getPrivateKey ()).thenReturn (privateKey );
212150 Algorithm algorithm = Algorithm .RSA256 (provider );
213151
214- String signed = /*JWTCreator.init()
215- .withKeyId("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg")
216- .withIssuer("auth0")
217- .sign(algorithm);*/
152+ String signed =
218153 "eyJhbGciOiJSUzI1NiIsImtpZCI6IjhSR29WZFZqRDhmSXR5UjNGRm8waFZOYVpZdFBHd29QNnhLaTllX1Y3YkkifQ.eyJpc3MiOiAiaHR0cHM6Ly9hZ2F0b24tc2F4LmNvbS8iLCAiaWF0IjogMTUxMzYyODc3OSwgImV4cCI6IDE1MTM3MTUxNzksICJraWQiOiAiOFJHb1ZkVmpEOGZJdHlSM0ZGbzBoVk5hWll0UEd3b1A2eEtpOWVfVjdiSSIsICJmb28iOiAiYmFyIiwgImtpdCI6ICJrYXQifQ.OMSHRJRW3J2MHFvPZhRaxxJhHJ5WFBaRzdPb3KpxWsF1Y3Or4BH-2pL8HE1CAoUTTqGYvNSShi2O-NFupGmaY5SRehOma_6XHcL2OrKKwFkG21M57T13_qagG7VUF7n7yhaLXMKWNli9JZ9iwHqLfA__j2X4XqxqPRxr5LxLXz-eynRp1jax2-eqiAMVYdSnH02e_bmnO89nIys6VUPoOAQFJjoPNtUo0urG8vTsiFPHFCgWUljDUFIu-TiRVTu5gJea-cigUJeG7i_4cp0qkWHo7POrS4Dq-gyzUSbkqBNdg-4LAAU40staTA236MPekQkzeTQlHa9418davEvfboYnCYeAhhX9Pnn7YotZSsD6S9HnTh5OjJ5E3O_Y-5MSK-eIYHV79FLQbaG1Xmcuv7WsRyhPadAwmNYLuWBtgaQDVV58ZmZPB9EaBDczqJDymjLkz2NpfLVI1kKFFDdLDC6ZBnKSsksDKvyrCS3JT3nHLR9LTIBN6mPii5xKy9Ysa1AQ1lK9ytcHCbG2iJJMZ57zpcjEevncvfGkB8RALe6GAthrNclA3mosB9b_z1TBPAzUZVh7VZLCsST7RIO1olDKEtZDvG-qGBEdjNS885nxoYe6ASCcEiUZ16Rixo8xw6lBb9l6qUkw3KST29W7_tcSGC3GnWQCN6q_SS-SD90" ;
219154
220155 JWT jwt = JWT .require (Algorithm .RSA256 (provider )).withIssuer ("https://agaton-sax.com/" )
221156 .withNonStandardClaim ("foo" ,"bar" )
222157 .withNonStandardClaim ("kit" , "kat" ).build ();
223158 DecodedJWT decoded = jwt .decode (signed );
224159 algorithm .verify (decoded , EncodeType .Base64 );
225-
226- /*assertThat(signed, is(notNullValue()));
227- String[] parts = signed.split("\\.");
228- String headerJson = new String(Base64.decodeBase64(parts[0]), StandardCharsets.UTF_8);
229- assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));*/
230160 }
231161
232162 @ Test
@@ -262,28 +192,6 @@ public void shouldAddKeyIdIfAvailableFromECDSAAlgorithms() throws Exception {
262192 assertThat (headerJson , JsonMatcher .hasEntry ("kid" , "my-key-id" ));
263193 }
264194
265- @ Test
266- public void shouldAddKeyIdIfAvailableFromECDSAAlgorithmsDELETEEEEEE () throws Exception {
267- ECPrivateKey privateKey = (ECPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_EC_256 , "EC" );
268- ECDSAKeyProvider provider = mock (ECDSAKeyProvider .class );
269- when (provider .getPrivateKeyId ()).thenReturn ("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg" );
270- when (provider .getPrivateKey ()).thenReturn (privateKey );
271-
272- String signed = JWTCreator .init ()
273- .withKeyId ("RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg" )
274- .withIssuer ("auth0" )
275- .sign (Algorithm .ECDSA256 (provider ));
276-
277- JWT jwt = JWT .require (Algorithm .ECDSA256 (provider )).withIssuer ("auth0" ).build ();
278- DecodedJWT decoded = jwt .decode (signed );
279- Algorithm .ECDSA256 (provider ).verify (decoded , EncodeType .Base64 );
280-
281- assertThat (signed , is (notNullValue ()));
282- String [] parts = signed .split ("\\ ." );
283- String headerJson = new String (Base64 .decodeBase64 (parts [0 ]), StandardCharsets .UTF_8 );
284- //assertThat(headerJson, JsonMatcher.hasEntry("kid", "my-key-id"));
285- }
286-
287195 @ Test
288196 public void shouldNotOverwriteKeyIdIfAddedFromECDSAAlgorithms () throws Exception {
289197 ECPrivateKey privateKey = (ECPrivateKey ) PemUtils .readPrivateKeyFromFile (PRIVATE_KEY_FILE_EC_256 , "EC" );
0 commit comments