99import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
1010import org .springframework .test .util .ReflectionTestUtils ;
1111
12+ import java .security .SecureRandom ;
13+ import java .util .Base64 ;
1214import java .util .UUID ;
1315
16+ import static org .junit .Assert .assertEquals ;
1417import static org .junit .Assert .assertTrue ;
1518
1619public class SecureEndpointTest {
1720
1821 private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder ();
22+ private final SecureRandom secureRandom = new SecureRandom ();
1923
2024 @ Test
2125 public void secretsMatch () {
@@ -31,4 +35,45 @@ public void secretsMatch() {
3135 boolean matches = secureEndpoint .secretsMatch (plainClientSecret , openIDClient );
3236 assertTrue (matches );
3337 }
34- }
38+
39+ @ Test
40+ public void longSecrets () {
41+ int numBytes = 54 ;// Because of base64 encoding, this will become 72 bytes (54 * 4 / 3 = 72);
42+ byte [] randomBytes = new byte [numBytes ];
43+ secureRandom .nextBytes (randomBytes );
44+ String secret = Base64 .getUrlEncoder ().withoutPadding ().encodeToString (randomBytes );
45+ assertEquals (72 , secret .getBytes ().length );
46+ //must not be larger the 72, otherwise the encoding fails
47+ String encoded = passwordEncoder .encode (secret );
48+ OpenIDClient openIDClient = new OpenIDClient ();
49+ ReflectionTestUtils .setField (openIDClient , "secret" , encoded );
50+
51+ SecureEndpoint secureEndpoint = new SecureEndpoint ();
52+ ClientID clientID = new ClientID ("test" );
53+ Secret theSecret = new Secret (secret +
54+ "does_not_matter_anymore_only_the_first_72_bytes_are used" );
55+ PlainClientSecret plainClientSecret = new ClientSecretBasic (clientID , theSecret );
56+
57+ boolean matches = secureEndpoint .secretsMatch (plainClientSecret , openIDClient );
58+ assertTrue (matches );
59+ }
60+
61+ @ Test
62+ public void existingLongSecrets () {
63+ String secret = "AsNZ_H_IvWj_z19o7thTO_S83MOEWWSJ_Hiwt4Ms2qMEvPpvMia7SwbsvCLcLIu9h5rdEMYHFGsJD0eTcH0sRjj4OhghMlgfJQ" ;
64+ assertTrue (secret .getBytes ().length > 72 );
65+
66+ String encoded = "$2a$10$F1eD3M74d3BS5FnI1moxme89R6rE/doNNhDpeIH9NgJWfF6lQRTd6" ;
67+ OpenIDClient openIDClient = new OpenIDClient ();
68+ ReflectionTestUtils .setField (openIDClient , "secret" , encoded );
69+
70+ SecureEndpoint secureEndpoint = new SecureEndpoint ();
71+ ClientID clientID = new ClientID ("test" );
72+ Secret theSecret = new Secret (secret );
73+ PlainClientSecret plainClientSecret = new ClientSecretBasic (clientID , theSecret );
74+
75+ boolean matches = secureEndpoint .secretsMatch (plainClientSecret , openIDClient );
76+ assertTrue (matches );
77+ }
78+
79+ }
0 commit comments