@@ -103,6 +103,7 @@ public void testSameUserPermission() {
103103 final String action = changePasswordRequest ? ChangePasswordAction .NAME : AuthenticateAction .NAME ;
104104 final Authentication authentication = mock (Authentication .class );
105105 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
106+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
106107 when (authentication .getUser ()).thenReturn (user );
107108 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
108109 when (authenticatedBy .getType ())
@@ -126,9 +127,10 @@ public void testSameUserPermissionDoesNotAllowNonMatchingUsername() {
126127 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
127128 when (authentication .getUser ()).thenReturn (user );
128129 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
129- when (authenticatedBy .getType ())
130- .thenReturn (changePasswordRequest ? randomFrom (ReservedRealm .TYPE , NativeRealmSettings .TYPE ) :
131- randomAlphaOfLengthBetween (4 , 12 ));
130+ final String authenticationType = changePasswordRequest ? randomFrom (ReservedRealm .TYPE , NativeRealmSettings .TYPE ) :
131+ randomAlphaOfLengthBetween (4 , 12 );
132+ when (authenticatedBy .getType ()).thenReturn (authenticationType );
133+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
132134
133135 assertThat (request , instanceOf (UserRequest .class ));
134136 assertFalse (engine .checkSameUserPermissions (action , request , authentication ));
@@ -181,6 +183,7 @@ public void testSameUserPermissionRunAsChecksAuthenticatedBy() {
181183 final Authentication authentication = mock (Authentication .class );
182184 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
183185 final Authentication .RealmRef lookedUpBy = mock (Authentication .RealmRef .class );
186+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
184187 when (authentication .getUser ()).thenReturn (user );
185188 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
186189 when (authentication .getLookedUpBy ()).thenReturn (lookedUpBy );
@@ -199,6 +202,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForOtherRealms() {
199202 final String action = ChangePasswordAction .NAME ;
200203 final Authentication authentication = mock (Authentication .class );
201204 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
205+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
202206 when (authentication .getUser ()).thenReturn (user );
203207 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
204208 when (authenticatedBy .getType ()).thenReturn (randomFrom (LdapRealmSettings .LDAP_TYPE , FileRealmSettings .TYPE ,
@@ -210,6 +214,47 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForOtherRealms() {
210214 verify (authenticatedBy ).getType ();
211215 verify (authentication ).getAuthenticatedBy ();
212216 verify (authentication , times (2 )).getUser ();
217+ verify (authentication ).getAuthenticationType ();
218+ verifyNoMoreInteractions (authenticatedBy , authentication );
219+ }
220+
221+ public void testSameUserPermissionDoesNotAllowChangePasswordForApiKey () {
222+ final User user = new User ("joe" );
223+ final ChangePasswordRequest request = new ChangePasswordRequestBuilder (mock (Client .class )).username (user .principal ()).request ();
224+ final String action = ChangePasswordAction .NAME ;
225+ final Authentication authentication = mock (Authentication .class );
226+ final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
227+ when (authentication .getUser ()).thenReturn (user );
228+ when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
229+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .API_KEY );
230+ when (authenticatedBy .getType ()).thenReturn (ApiKeyService .API_KEY_REALM_TYPE );
231+
232+ assertThat (request , instanceOf (UserRequest .class ));
233+ assertFalse (engine .checkSameUserPermissions (action , request , authentication ));
234+ verify (authenticatedBy ).getType ();
235+ verify (authentication ).getAuthenticatedBy ();
236+ verify (authentication , times (2 )).getUser ();
237+ verify (authentication ).getAuthenticationType ();
238+ verifyNoMoreInteractions (authenticatedBy , authentication );
239+ }
240+
241+ public void testSameUserPermissionDoesNotAllowChangePasswordForAccessToken () {
242+ final User user = new User ("joe" );
243+ final ChangePasswordRequest request = new ChangePasswordRequestBuilder (mock (Client .class )).username (user .principal ()).request ();
244+ final String action = ChangePasswordAction .NAME ;
245+ final Authentication authentication = mock (Authentication .class );
246+ final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
247+ when (authentication .getUser ()).thenReturn (user );
248+ when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
249+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .TOKEN );
250+ when (authenticatedBy .getType ()).thenReturn (NativeRealmSettings .TYPE );
251+
252+ assertThat (request , instanceOf (UserRequest .class ));
253+ assertFalse (engine .checkSameUserPermissions (action , request , authentication ));
254+ verify (authenticatedBy ).getType ();
255+ verify (authentication ).getAuthenticatedBy ();
256+ verify (authentication , times (2 )).getUser ();
257+ verify (authentication ).getAuthenticationType ();
213258 verifyNoMoreInteractions (authenticatedBy , authentication );
214259 }
215260
@@ -221,6 +266,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForLookedUpByOtherRe
221266 final Authentication authentication = mock (Authentication .class );
222267 final Authentication .RealmRef authenticatedBy = mock (Authentication .RealmRef .class );
223268 final Authentication .RealmRef lookedUpBy = mock (Authentication .RealmRef .class );
269+ when (authentication .getAuthenticationType ()).thenReturn (Authentication .AuthenticationType .REALM );
224270 when (authentication .getUser ()).thenReturn (user );
225271 when (authentication .getAuthenticatedBy ()).thenReturn (authenticatedBy );
226272 when (authentication .getLookedUpBy ()).thenReturn (lookedUpBy );
@@ -233,6 +279,7 @@ public void testSameUserPermissionDoesNotAllowChangePasswordForLookedUpByOtherRe
233279 verify (authentication ).getLookedUpBy ();
234280 verify (authentication , times (2 )).getUser ();
235281 verify (lookedUpBy ).getType ();
282+ verify (authentication ).getAuthenticationType ();
236283 verifyNoMoreInteractions (authentication , lookedUpBy , authenticatedBy );
237284 }
238285
0 commit comments