fix: avoid parsing access token on frontend during login (#1201)
This commit is contained in:
@@ -3,6 +3,7 @@ package org.booklore.config.security.oidc;
|
||||
import com.nimbusds.jwt.JWTClaimsSet;
|
||||
import org.booklore.config.security.service.AuthenticationService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.model.dto.AccessTokenDto;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.dto.settings.OidcAutoProvisionDetails;
|
||||
import org.booklore.model.dto.settings.OidcProviderDetails;
|
||||
@@ -74,7 +75,7 @@ class OidcAuthServiceTest {
|
||||
var claims = new JWTClaimsSet.Builder().subject("sub-123").build();
|
||||
var userClaims = userClaims("jdoe", "sub-123");
|
||||
var user = existingOidcUser("jdoe", "sub-123");
|
||||
var expectedResponse = ResponseEntity.ok(Map.of("token", "jwt"));
|
||||
var expectedResponse = ResponseEntity.ok(AccessTokenDto.builder().accessToken("jwt").build());
|
||||
|
||||
when(appSettingService.getAppSettings()).thenReturn(settings);
|
||||
when(oidcTokenClient.exchangeAuthorizationCode(CODE, CODE_VERIFIER, REDIRECT_URI, settings.getOidcProviderDetails()))
|
||||
@@ -161,7 +162,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -185,7 +186,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn("8");
|
||||
when(authenticationService.loginUser(user, 8 * 3_600_000L)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user, 8 * 3_600_000L)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -210,7 +211,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn("not-a-number");
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -234,7 +235,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -280,7 +281,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -310,7 +311,7 @@ class OidcAuthServiceTest {
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.empty());
|
||||
when(userRepository.findByUsername("jdoe")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -366,7 +367,7 @@ class OidcAuthServiceTest {
|
||||
when(userProvisioningService.provisionOidcUser("newuser", "newuser@example.com", "New User", "sub-new", ISSUER_URI, null, provisionDetails))
|
||||
.thenReturn(newUser);
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(newUser)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(newUser)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -421,7 +422,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -451,7 +452,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -481,7 +482,7 @@ class OidcAuthServiceTest {
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.empty());
|
||||
when(userRepository.findByUsername("jdoe")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, REDIRECT_URI, NONCE, mockRequest());
|
||||
|
||||
@@ -508,7 +509,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
var result = oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, "https://example.com/oauth2-callback", NONCE, mockRequest());
|
||||
|
||||
@@ -531,7 +532,7 @@ class OidcAuthServiceTest {
|
||||
when(oidcClaimExtractor.extractClaims(eq(claims), any(), eq(Map.of()))).thenReturn(userClaims);
|
||||
when(userRepository.findByOidcIssuerAndOidcSubject(ISSUER_URI, "sub-123")).thenReturn(Optional.of(user));
|
||||
when(appSettingService.getSettingValue("oidc_session_duration_hours")).thenReturn(null);
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(Map.of()));
|
||||
when(authenticationService.loginUser(user)).thenReturn(ResponseEntity.ok(AccessTokenDto.builder().build()));
|
||||
|
||||
var result = oidcAuthService.exchangeCodeForTokens(CODE, CODE_VERIFIER, "booklore://oauth2-callback", NONCE, mockRequest());
|
||||
|
||||
|
||||
+15
-12
@@ -3,6 +3,7 @@ package org.booklore.config.security.service;
|
||||
import org.booklore.config.AppProperties;
|
||||
import org.booklore.config.security.JwtUtils;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.model.dto.AccessTokenDto;
|
||||
import org.booklore.model.dto.request.UserLoginRequest;
|
||||
import org.booklore.model.dto.settings.AppSettings;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
@@ -29,9 +30,7 @@ import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -139,11 +138,12 @@ class AuthenticationServiceTest {
|
||||
request.setUsername("admin");
|
||||
request.setPassword("password");
|
||||
|
||||
ResponseEntity<Map<String, String>> response = authenticationService.loginUser(request);
|
||||
ResponseEntity<AccessTokenDto> response = authenticationService.loginUser(request);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThat(response.getBody()).containsEntry("accessToken", "access-token");
|
||||
assertThat(response.getBody()).containsEntry("refreshToken", "refresh-token");
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody().getAccessToken()).isEqualTo("access-token");
|
||||
assertThat(response.getBody().getRefreshToken()).isEqualTo("refresh-token");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -170,10 +170,11 @@ class AuthenticationServiceTest {
|
||||
request.setUsername("normaluser");
|
||||
request.setPassword("password");
|
||||
|
||||
ResponseEntity<Map<String, String>> response = authenticationService.loginUser(request);
|
||||
ResponseEntity<AccessTokenDto> response = authenticationService.loginUser(request);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThat(response.getBody()).containsEntry("accessToken", "access-token");
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody().getAccessToken()).isEqualTo("access-token");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -229,12 +230,14 @@ class AuthenticationServiceTest {
|
||||
when(jwtUtils.generateRefreshToken(user)).thenReturn("my-refresh-token");
|
||||
when(refreshTokenRepository.save(any(RefreshTokenEntity.class))).thenAnswer(inv -> inv.getArgument(0));
|
||||
|
||||
ResponseEntity<Map<String, String>> response = authenticationService.loginUser(user, 7200000L);
|
||||
ResponseEntity<AccessTokenDto> response = authenticationService.loginUser(user, 7200000L);
|
||||
|
||||
assertThat(response.getStatusCode().value()).isEqualTo(200);
|
||||
assertThat(response.getBody())
|
||||
.containsEntry("accessToken", "my-access-token")
|
||||
.containsEntry("refreshToken", "my-refresh-token")
|
||||
.containsEntry("isDefaultPassword", "false");
|
||||
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody().getAccessToken()).isEqualTo("my-access-token");
|
||||
assertThat(response.getBody().getRefreshToken()).isEqualTo("my-refresh-token");
|
||||
assertThat(response.getBody().getExpires()).isEqualTo(7200);
|
||||
assertThat(response.getBody().getIsDefaultPassword()).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.booklore.config.security.oidc.OidcAuthService;
|
||||
import org.booklore.config.security.oidc.OidcCallbackRequest;
|
||||
import org.booklore.config.security.oidc.OidcStateService;
|
||||
import org.booklore.exception.APIException;
|
||||
import org.booklore.model.dto.AccessTokenDto;
|
||||
import org.booklore.service.audit.AuditService;
|
||||
import org.booklore.model.enums.AuditAction;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@@ -59,15 +60,16 @@ class OidcAuthControllerTest {
|
||||
@Test
|
||||
void handleCallback_validatesStateAndReturnsTokens() {
|
||||
var request = new OidcCallbackRequest("code1", "verifier1", "https://redirect", "nonce1", "state1");
|
||||
var tokenResponse = ResponseEntity.ok(Map.of("accessToken", "at", "refreshToken", "rt"));
|
||||
var tokenResponse = ResponseEntity.ok(AccessTokenDto.builder().accessToken("at").refreshToken("rt").build());
|
||||
when(oidcAuthService.exchangeCodeForTokens("code1", "verifier1", "https://redirect", "nonce1", httpRequest))
|
||||
.thenReturn(tokenResponse);
|
||||
|
||||
ResponseEntity<Map<String, String>> response = controller.handleCallback(request, httpRequest);
|
||||
ResponseEntity<AccessTokenDto> response = controller.handleCallback(request, httpRequest);
|
||||
|
||||
verify(oidcStateService).validateAndConsume("state1");
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).containsEntry("accessToken", "at");
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody().getAccessToken()).isEqualTo("at");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -85,7 +87,7 @@ class OidcAuthControllerTest {
|
||||
|
||||
@Test
|
||||
void handleRedirect_returns302WithFragmentContainingTokens() {
|
||||
var tokens = Map.of("accessToken", "at123", "refreshToken", "rt456");
|
||||
var tokens = AccessTokenDto.builder().accessToken("at123").refreshToken("rt456").build();
|
||||
var tokenResponse = ResponseEntity.ok(tokens);
|
||||
when(oidcAuthService.exchangeCodeForTokens("code", "verifier", "https://redir", "nonce", httpRequest))
|
||||
.thenReturn(tokenResponse);
|
||||
@@ -105,7 +107,7 @@ class OidcAuthControllerTest {
|
||||
|
||||
@Test
|
||||
void handleRedirect_includesIsDefaultPasswordInFragmentWhenPresent() {
|
||||
var tokens = Map.of("accessToken", "at", "refreshToken", "rt", "isDefaultPassword", "true");
|
||||
var tokens = AccessTokenDto.builder().accessToken("at").refreshToken("rt").isDefaultPassword(true).build();
|
||||
var tokenResponse = ResponseEntity.ok(tokens);
|
||||
when(oidcAuthService.exchangeCodeForTokens("code", "verifier", "https://redir", "nonce", httpRequest))
|
||||
.thenReturn(tokenResponse);
|
||||
@@ -114,7 +116,7 @@ class OidcAuthControllerTest {
|
||||
"code", "verifier", "https://redir", "nonce", "state", "https://app.example.com", httpRequest);
|
||||
|
||||
String location = response.getHeaders().getLocation().toString();
|
||||
assertThat(location).contains("is_default_password=" + URLEncoder.encode("true", StandardCharsets.UTF_8));
|
||||
assertThat(location).contains("is_default_password=true");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -134,7 +136,7 @@ class OidcAuthControllerTest {
|
||||
|
||||
@Test
|
||||
void handleRedirect_throwsWhenTokenResponseBodyIsNull() {
|
||||
ResponseEntity<Map<String, String>> tokenResponse = ResponseEntity.ok(null);
|
||||
ResponseEntity<AccessTokenDto> tokenResponse = ResponseEntity.ok(null);
|
||||
when(oidcAuthService.exchangeCodeForTokens("code", "verifier", "https://redir", "nonce", httpRequest))
|
||||
.thenReturn(tokenResponse);
|
||||
|
||||
@@ -149,16 +151,17 @@ class OidcAuthControllerTest {
|
||||
|
||||
@Test
|
||||
void handleMobileCallback_validatesStateAndReturnsTokens() {
|
||||
var tokenResponse = ResponseEntity.ok(Map.of("accessToken", "at", "refreshToken", "rt"));
|
||||
var tokenResponse = ResponseEntity.ok(AccessTokenDto.builder().accessToken("at").refreshToken("rt").build());
|
||||
when(oidcAuthService.exchangeCodeForTokens("code", "verifier", "https://redir", "nonce", httpRequest))
|
||||
.thenReturn(tokenResponse);
|
||||
|
||||
ResponseEntity<Map<String, String>> response = controller.handleMobileCallback(
|
||||
ResponseEntity<AccessTokenDto> response = controller.handleMobileCallback(
|
||||
"code", "verifier", "https://redir", "nonce", "state", httpRequest);
|
||||
|
||||
verify(oidcStateService).validateAndConsume("state");
|
||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(response.getBody()).containsEntry("accessToken", "at");
|
||||
assertThat(response.getBody()).isNotNull();
|
||||
assertThat(response.getBody().getAccessToken()).isEqualTo("at");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user