55import static org .junit .jupiter .api .Assertions .assertThrows ;
66import static org .junit .jupiter .api .Assertions .assertTrue ;
77import static org .mockito .ArgumentMatchers .eq ;
8- import static org .mockito .Mockito .mock ;
98import static org .mockito .Mockito .when ;
109
1110import com .adorsys .keycloakstatuslist .config .StatusListConfig ;
2423import org .keycloak .crypto .KeyWrapper ;
2524import org .keycloak .jose .jws .JWSInput ;
2625import org .keycloak .util .JsonSerialization ;
26+ import org .mockito .Mockito ;
2727
2828class CryptoIdentityServiceTest extends MockKeycloakTest {
2929
@@ -42,8 +42,52 @@ void getActiveKeyShouldReturnCurrentSigningKey() {
4242 assertNotNull (key .getPublicKey ());
4343 }
4444
45+ @ Test
46+ void getActiveKeyShouldPreferEs256OverRs256 () throws Exception {
47+ KeyPairGenerator ecGen = KeyPairGenerator .getInstance ("EC" );
48+ ecGen .initialize (256 );
49+ KeyPair ecPair = ecGen .generateKeyPair ();
50+
51+ KeyWrapper esKey = new KeyWrapper ();
52+ esKey .setKid ("es-kid" );
53+ esKey .setAlgorithm (Algorithm .ES256 );
54+ esKey .setPublicKey (ecPair .getPublic ()); // must be non-null for the shared resolver
55+
56+ KeyWrapper rsaKey = new KeyWrapper ();
57+ rsaKey .setKid ("rsa-kid" );
58+ rsaKey .setAlgorithm (Algorithm .RS256 );
59+
60+ Mockito .when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .ES256 )))
61+ .thenReturn (esKey );
62+ Mockito .lenient ()
63+ .when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
64+ .thenReturn (rsaKey );
65+
66+ KeyWrapper result = service .getActiveKey (realm );
67+ assertEquals ("es-kid" , result .getKid ());
68+ assertEquals (Algorithm .ES256 , result .getAlgorithm ());
69+ }
70+
71+ @ Test
72+ void getActiveKeyShouldFallbackToRs256WhenEs256Missing () {
73+ KeyWrapper rsaKey = new KeyWrapper ();
74+ rsaKey .setKid ("rsa-kid-fallback" );
75+ rsaKey .setAlgorithm (Algorithm .RS256 );
76+
77+ Mockito .when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .ES256 )))
78+ .thenReturn (null );
79+ Mockito .when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
80+ .thenReturn (rsaKey );
81+
82+ KeyWrapper result = service .getActiveKey (realm );
83+ assertEquals ("rsa-kid-fallback" , result .getKid ());
84+ assertEquals (Algorithm .RS256 , result .getAlgorithm ());
85+ }
86+
4587 @ Test
4688 void getActiveKeyShouldThrowWhenNoActiveSigningKey () {
89+ Mockito .when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .ES256 )))
90+ .thenReturn (null );
4791 when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
4892 .thenReturn (null );
4993
@@ -53,7 +97,7 @@ void getActiveKeyShouldThrowWhenNoActiveSigningKey() {
5397
5498 @ Test
5599 void getJwtTokenShouldContainExpectedIssuerClaim () throws Exception {
56- when (realm .getAttribute (StatusListConfig .STATUS_LIST_TOKEN_ISSUER_PREFIX ))
100+ Mockito . when (realm .getAttribute (StatusListConfig .STATUS_LIST_TOKEN_ISSUER_PREFIX ))
57101 .thenReturn ("issuer-prefix" );
58102 StatusListConfig config = new StatusListConfig (realm );
59103
@@ -72,8 +116,10 @@ void getJwtTokenShouldContainExpectedIssuerClaim() throws Exception {
72116 @ Test
73117 void getRealmKeyDataShouldFallbackToRs256WhenDefaultAlgMissing () throws Exception {
74118 when (realm .getDefaultSignatureAlgorithm ()).thenReturn (null );
119+ // ES256 check fails
75120 when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .ES256 )))
76121 .thenReturn (null );
122+ // Fallback to RS256
77123 when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
78124 .thenReturn (RSATestUtils .getRsaKeyWrapper (testJwkResource ("/keycloak-active-key-rsa.json" )));
79125
@@ -95,8 +141,8 @@ void getRealmKeyDataShouldSupportEcPublicKey() throws Exception {
95141 ecKey .setAlgorithm (Algorithm .ES256 );
96142 ecKey .setPublicKey (ecPair .getPublic ());
97143
98- when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .ES256 );
99- when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .ES256 )))
144+ Mockito . when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .ES256 );
145+ Mockito . when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .ES256 )))
100146 .thenReturn (ecKey );
101147
102148 CryptoIdentityService .KeyData keyData = CryptoIdentityService .getRealmKeyData (session , realm );
@@ -109,8 +155,8 @@ void getRealmKeyDataShouldSupportEcPublicKey() throws Exception {
109155
110156 @ Test
111157 void getRealmKeyDataShouldThrowWhenNoActiveKeyFound () {
112- when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .RS256 );
113- when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
158+ Mockito . when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .RS256 );
159+ Mockito . when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
114160 .thenReturn (null );
115161
116162 StatusListException ex =
@@ -125,8 +171,8 @@ void getRealmKeyDataShouldThrowWhenPublicKeyMissing() {
125171 keyWithoutPublicKey .setAlgorithm (Algorithm .RS256 );
126172 keyWithoutPublicKey .setPublicKey (null );
127173
128- when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .RS256 );
129- when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
174+ Mockito . when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .RS256 );
175+ Mockito . when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
130176 .thenReturn (keyWithoutPublicKey );
131177
132178 StatusListException ex =
@@ -136,15 +182,15 @@ void getRealmKeyDataShouldThrowWhenPublicKeyMissing() {
136182
137183 @ Test
138184 void getRealmKeyDataShouldThrowForUnsupportedPublicKeyType () {
139- PublicKey unsupportedKey = mock (PublicKey .class );
185+ PublicKey unsupportedKey = Mockito . mock (PublicKey .class );
140186
141187 KeyWrapper unsupported = new KeyWrapper ();
142188 unsupported .setKid ("unsupported-kid" );
143189 unsupported .setAlgorithm (Algorithm .RS256 );
144190 unsupported .setPublicKey (unsupportedKey );
145191
146- when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .RS256 );
147- when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
192+ Mockito . when (realm .getDefaultSignatureAlgorithm ()).thenReturn (Algorithm .RS256 );
193+ Mockito . when (keyManager .getActiveKey (eq (realm ), eq (KeyUse .SIG ), eq (Algorithm .RS256 )))
148194 .thenReturn (unsupported );
149195
150196 StatusListException ex =
0 commit comments