Skip to content

Commit

Permalink
fix login
Browse files Browse the repository at this point in the history
  • Loading branch information
abirembaut committed Feb 20, 2025
1 parent 6c007d5 commit 927e4cb
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 44 deletions.
1 change: 0 additions & 1 deletion .openapi-generator-ignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#!docs/README.md

# Ignore below APIs to keep client iso to legacy Bonita APIs
**/org/bonitasoft/web/client/api/AuthenticationApi.java
**/org/bonitasoft/web/client/api/LicenseApi.java
**/org/bonitasoft/web/client/api/SessionApi.java

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ public interface AuthenticationApi extends ApiClient.Api {
* @param redirect \\\"true\\\" or \\\"false\\\". \\\"false\\\" indicates that the service should not redirect to Bonita Portal
* (after a successful login) or to the login page (after a login failure). (optional, default to false)
* @param redirectURL the URL of the page to be displayed after login (optional, default to )
* @return Response
*/
@RequestLine("POST /loginservice")
@Headers({
"Content-Type: application/x-www-form-urlencoded",
"Accept: application/json",
})
Response login(@Param("username") String username, @Param("password") String password,
void login(@Param("username") String username, @Param("password") String password,
@Param("redirect") String redirect, @Param("redirectURL") String redirectURL);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bonitasoft.web.client.exception.ClientException;
import org.bonitasoft.web.client.exception.UnauthorizedException;
import org.bonitasoft.web.client.feign.ApiProvider;
import org.bonitasoft.web.client.model.ApiResponse;
import org.bonitasoft.web.client.model.Session;
import org.bonitasoft.web.client.services.LoginService;

Expand All @@ -51,26 +52,17 @@ public Session login(String username, String password) {

boolean loginSucceeded;
int loginStatus;
String loginReason = "";

final AuthenticationApi authenticationApi = apiProvider.get(AuthenticationApi.class);
try (Response loginResponse = authenticationApi.login(username, password, "false", "")) {
loginStatus = loginResponse.status();
loginSucceeded = (loginStatus == 200 || loginStatus == 204);
if (loginSucceeded) {
bonitaCookieAuth.initFrom(loginResponse.headers());
} else {
loginReason = loginResponse.reason();
}
} catch (Exception e) {
throw new ClientException("Login failed", e);
}

if (!loginSucceeded) {
ApiResponse<Void> apiResponse = authenticationApi.loginWithHttpInfo(username, password, "false", "");
loginStatus = apiResponse.getStatusCode();
loginSucceeded = (loginStatus == 200 || loginStatus == 204);
if (loginSucceeded) {
bonitaCookieAuth.initFrom(apiResponse.getHeaders());
} else {
throw new UnauthorizedException(
format("Login failed, status: %s %s", loginStatus, loginReason));
format("Login failed, status: %s", loginStatus));
}

// check the session is ok + it will trigger the loading of servlets
Session session = getSession();
log.debug("Login completed. Session: {}", session);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.bonitasoft.web.client.api.AuthenticationApi;
import org.bonitasoft.web.client.api.SessionApi;
import org.bonitasoft.web.client.feign.ApiProvider;
import org.bonitasoft.web.client.model.ApiResponse;
import org.bonitasoft.web.client.model.Session;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -74,31 +75,8 @@ void login_should_work_with_204_response() throws Exception {
final String username = "someone";

final Response loginResponse = mockResponseBuilder().status(204).build();
when(authenticationApi.login(anyString(), anyString(), anyString(), anyString()))
.thenReturn(loginResponse);

final Response sessionResponse = mockResponseBuilder()
.status(200)
.body(objectMapper.writeValueAsBytes(new Session().userName(username)))
.build();
when(sessionApi.getSession()).thenReturn(sessionResponse);

// When
final Session session = loginService.login(username, "myPass");

// Then
assertThat(session).isNotNull();
assertThat(session.getUserName()).isEqualTo(username);
}

@Test
void login_should_work_with_200_response() throws Exception {
// Given
final String username = "someone";

final Response loginResponse = mockResponseBuilder().status(200).build();
when(authenticationApi.login(anyString(), anyString(), anyString(), anyString()))
.thenReturn(loginResponse);
when(authenticationApi.loginWithHttpInfo(anyString(), anyString(), anyString(), anyString()))
.thenReturn(new ApiResponse<>(loginResponse.status(), loginResponse.headers()));

final Response sessionResponse = mockResponseBuilder()
.status(200)
Expand Down

0 comments on commit 927e4cb

Please sign in to comment.