@@ -102,6 +102,7 @@ public void testSameUserPermission() {
102102 final String action = changePasswordRequest ? ChangePasswordAction .NAME : AuthenticateAction .NAME ;
103103 final Authentication authentication = mock (Authentication .class );
104104 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
105+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
105106 when (authentication .getUser ()).thenReturn (user );
106107 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
107108 when (authenticatedBy .getType ())
@@ -125,9 +126,10 @@ public void testSameUserPermissionDoesNotAllowNonMatchingUsername() {
125126 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
126127 when (authentication .getUser ()).thenReturn (user );
127128 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
128- when (authenticatedBy .getType ())
129- .thenReturn (changePasswordRequest ? randomFrom (ReservedRealm .TYPE , NativeRealmSettings .TYPE ) :
130- randomAlphaOfLengthBetween (4 , 12 ));
129+ final String authenticationType = changePasswordRequest ? randomFrom (ReservedRealm .TYPE , NativeRealmSettings .TYPE ) :
130+ randomAlphaOfLengthBetween (4 , 12 );
131+ when (authenticatedBy .getType ()).thenReturn (authenticationType );
132+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
131133
132134 assertThat (request , instanceOf (UserRequest .class ));
133135 assertFalse (engine .checkSameUserPermissions (action , request , authentication ));
@@ -180,6 +182,7 @@ public void testSameUserPermissionRunAsChecksAuthenticatedBy() {
180182 final Authentication authentication = mock (Authentication .class );
181183 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
182184 final Authentication .RealmRef lookedUpBy = mock (Authentication .RealmRef .class );
185+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
183186 when (authentication .getUser ()).thenReturn (user );
184187 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
185188 when (authentication .getLookedUpBy ()).thenReturn (lookedUpBy );
@@ -198,6 +201,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForOtherRealms() {
198201 final String action = ChangePasswordAction .NAME ;
199202 final Authentication authentication = mock (Authentication .class );
200203 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
204+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
201205 when (authentication .getUser ()).thenReturn (user );
202206 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
203207 when (authenticatedBy .getType ()).thenReturn (randomFrom (LdapRealmSettings .LDAP_TYPE , FileRealmSettings .TYPE ,
@@ -209,6 +213,47 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForOtherRealms() {
209213 verify (authenticatedBy ).getType ();
210214 verify (authentication ).getAuthenticatedBy ();
211215 verify (authentication , times (2 )).getUser ();
216+ verify (authentication ).getAuthenticationType ();
217+ verifyNoMoreInteractions (authenticatedBy , authentication );
218+ }
219+
220+ public void testSameUserPermissionDoesNotAllowChangePasswordForApiKey () {
221+ final User user = new User ("joe" );
222+ final ChangePasswordRequest request = new ChangePasswordRequestBuilder (mock (Client .class )).username (user .principal ()).request ();
223+ final String action = ChangePasswordAction .NAME ;
224+ final Authentication authentication = mock (Authentication .class );
225+ final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
226+ when (authentication .getUser ()).thenReturn (user );
227+ when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
228+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .API_KEY );
229+ when (authenticatedBy .getType ()).thenReturn (ApiKeyService .API_KEY_REALM_TYPE );
230+
231+ assertThat (request , instanceOf (UserRequest .class ));
232+ assertFalse (engine .checkSameUserPermissions (action , request , authentication ));
233+ verify (authenticatedBy ).getType ();
234+ verify (authentication ).getAuthenticatedBy ();
235+ verify (authentication , times (2 )).getUser ();
236+ verify (authentication ).getAuthenticationType ();
237+ verifyNoMoreInteractions (authenticatedBy , authentication );
238+ }
239+
240+ public void testSameUserPermissionDoesNotAllowChangePasswordForAccessToken () {
241+ final User user = new User ("joe" );
242+ final ChangePasswordRequest request = new ChangePasswordRequestBuilder (mock (Client .class )).username (user .principal ()).request ();
243+ final String action = ChangePasswordAction .NAME ;
244+ final Authentication authentication = mock (Authentication .class );
245+ final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
246+ when (authentication .getUser ()).thenReturn (user );
247+ when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
248+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .TOKEN );
249+ when (authenticatedBy .getType ()).thenReturn (NativeRealmSettings .TYPE );
250+
251+ assertThat (request , instanceOf (UserRequest .class ));
252+ assertFalse (engine .checkSameUserPermissions (action , request , authentication ));
253+ verify (authenticatedBy ).getType ();
254+ verify (authentication ).getAuthenticatedBy ();
255+ verify (authentication , times (2 )).getUser ();
256+ verify (authentication ).getAuthenticationType ();
212257 verifyNoMoreInteractions (authenticatedBy , authentication );
213258 }
214259
@@ -220,6 +265,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForLookedUpByOtherRe
220265 final Authentication authentication = mock (Authentication .class );
221266 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
222267 final Authentication .RealmRef lookedUpBy = mock (Authentication .RealmRef .class );
268+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
223269 when (authentication .getUser ()).thenReturn (user );
224270 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
225271 when (authentication .getLookedUpBy ()).thenReturn (lookedUpBy );
@@ -232,6 +278,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForLookedUpByOtherRe
232278 verify (authentication ).getLookedUpBy ();
233279 verify (authentication , times (2 )).getUser ();
234280 verify (lookedUpBy ).getType ();
281+ verify (authentication ).getAuthenticationType ();
235282 verifyNoMoreInteractions (authentication , lookedUpBy , authenticatedBy );
236283 }
237284
0 commit comments