Skip to content

Commit

Permalink
fix: do not logout the user if XSRF token is used in async operation (#…
Browse files Browse the repository at this point in the history
…181)

Refs: #170
  • Loading branch information
grigoriev authored Sep 2, 2024
1 parent 928d126 commit 97ce5f4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ public record JobState(

private boolean isJobLogoutRequired() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
return (requestAttributes != null) &&
(requestAttributes.getAttribute(LogoutFilter.ASYNC_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST) == Boolean.TRUE);
if (requestAttributes != null) {
if (requestAttributes.getAttribute(LogoutFilter.XSRF_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST) == Boolean.TRUE) {
return false;
}
return requestAttributes.getAttribute(LogoutFilter.ASYNC_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST) == Boolean.TRUE;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void tearDown() {
@Test
void shouldStartJobAndGetStatus() {
prepareSecurityServiceSubject(subject);
when(requestAttributes.getAttribute(LogoutFilter.XSRF_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(Boolean.FALSE);
when(requestAttributes.getAttribute(LogoutFilter.ASYNC_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(Boolean.TRUE);
ExportParams exportParams = ExportParams.builder().build();
when(pdfConverter.convertToPdf(exportParams, null)).thenReturn("test pdf".getBytes());
Expand All @@ -86,6 +87,7 @@ void shouldStartJobAndGetStatus() {
@Test
void shouldReturnFailInExceptionalCase() {
prepareSecurityServiceSubject(subject);
when(requestAttributes.getAttribute(LogoutFilter.XSRF_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(Boolean.FALSE);
when(requestAttributes.getAttribute(LogoutFilter.ASYNC_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(Boolean.TRUE);
ExportParams exportParams = ExportParams.builder().build();
when(pdfConverter.convertToPdf(exportParams, null)).thenThrow(new RuntimeException("test error"));
Expand Down Expand Up @@ -131,11 +133,16 @@ void shouldAcceptNullSubject() {
verify(securityService, never()).logout(null);
}

@Test
void shouldNotLogoutWithoutLogoutProperty() {
@ParameterizedTest
@CsvSource({
"true,true",
"true,false",
"false,false"
})
void shouldNotLogoutWithoutAsyncSkipLogoutProperty(boolean xsrfSkipLogout, boolean asyncSkipLogout) {
prepareSecurityServiceSubject(subject);
when(requestAttributes.getAttribute(LogoutFilter.ASYNC_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(null);

lenient().when(requestAttributes.getAttribute(LogoutFilter.XSRF_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(xsrfSkipLogout);
lenient().when(requestAttributes.getAttribute(LogoutFilter.ASYNC_SKIP_LOGOUT, RequestAttributes.SCOPE_REQUEST)).thenReturn(asyncSkipLogout);

ExportParams exportParams = ExportParams.builder().build();
when(pdfConverter.convertToPdf(exportParams, null)).thenReturn("test pdf".getBytes());
Expand All @@ -147,7 +154,7 @@ void shouldNotLogoutWithoutLogoutProperty() {
JobState jobState = pdfConverterJobsService.getJobState(jobId);
assertThat(jobState.isCompletedExceptionally()).isFalse();
assertThat(jobState.isCancelled()).isFalse();
verify(securityService, never()).logout(null);
verify(securityService, never()).logout(subject);
}

@ParameterizedTest
Expand Down

0 comments on commit 97ce5f4

Please sign in to comment.