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
Refs: #170
  • Loading branch information
grigoriev committed Sep 2, 2024
1 parent 77a76d4 commit e3935fd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>ch.sbb.polarion.extensions</groupId>
<artifactId>ch.sbb.polarion.extension.generic</artifactId>
<version>7.0.0</version>
<version>7.0.1</version>
</parent>

<artifactId>ch.sbb.polarion.extension.pdf-exporter</artifactId>
Expand Down
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 e3935fd

Please sign in to comment.