Skip to content

Commit

Permalink
fix(webapp): Handle null authentication as unauthorized
Browse files Browse the repository at this point in the history
Related to #3739
  • Loading branch information
joaquinfelici committed Jan 30, 2025
1 parent 166278c commit 2e219cd
Showing 1 changed file with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.camunda.bpm.webapp.impl.security.auth;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mockStatic;

import java.util.Date;
import javax.ws.rs.core.Response;
Expand All @@ -37,6 +39,7 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.springframework.mock.web.MockHttpServletRequest;

/**
Expand Down Expand Up @@ -202,6 +205,28 @@ public void shouldSetAuthCacheValidationTime() {
.isEqualTo(new Date(ClockUtil.getCurrentTime().getTime() + 1000 * 60 * 5));
}

@Test
public void assertNullAuthenticationThrowsNPE() {
// given
User jonny = identityService.newUser("jonny");
jonny.setPassword("jonnyspassword");
identityService.saveUser(jonny);

try (MockedStatic<AuthenticationUtil> authenticationUtilMock = mockStatic(AuthenticationUtil.class)) {
authenticationUtilMock.when(() -> AuthenticationUtil.createAuthentication("webapps-test-engine", "jonny")).thenReturn(null);

// when
UserAuthenticationResource authResource = new UserAuthenticationResource();
authResource.request = new MockHttpServletRequest();
assertThatThrownBy(() -> authResource.doLogin("webapps-test-engine", "tasklist", "jonny", "jonnyspassword"))
// then
.isInstanceOf(NullPointerException.class);

// we should get a forbidden return code
// Assert.assertEquals(Status.FORBIDDEN.getStatusCode(), response.getStatus());
}
}

protected void setAuthentication(String user, String engineName) {
Authentications authentications = new Authentications();
authentications.addOrReplace(new UserAuthentication(user, engineName));
Expand Down

0 comments on commit 2e219cd

Please sign in to comment.