diff --git a/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeRepository.java b/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeRepository.java index a045edd2c4..4643001ff2 100644 --- a/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeRepository.java +++ b/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeRepository.java @@ -55,17 +55,23 @@ public interface OneTimeCodeRepository extends JpaRepository= :now """) - void revokeByCode(@Param("code") UUID code); + int consumeByCode(@Param("code") UUID code, @Param("now") Instant now); /** * Delete all codes for a specific login (used when user is deleted/disabled). diff --git a/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeService.java b/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeService.java index 477e94e946..0e2790a932 100644 --- a/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeService.java +++ b/src/main/java/org/ohdsi/webapi/security/authc/OneTimeCodeService.java @@ -83,32 +83,17 @@ public UUID generateCode(String login, UserOrigin origin, String jwtToken) { * @return Optional containing the OTC entity if valid, empty if invalid/expired/already revoked */ public Optional validateAndConsume(UUID code) { - Optional otcOpt = repo.findById(code); - - if (otcOpt.isEmpty()) { - log.debug("OTC: {} not found", code); - return Optional.empty(); - } - - OneTimeCodeEntity otc = otcOpt.get(); - - // Check expiration - if (otc.getExpiresAt().isBefore(Instant.now())) { - log.debug("OTC: {} expired", code); + // Consume first with a single conditional UPDATE. Checking the revoked/expired + // flags in Java and then updating would let two concurrent requests both read + // revoked=false and both receive the JWT, breaking the single-use guarantee. + if (repo.consumeByCode(code, Instant.now()) != 1) { + log.debug("OTC: {} not found, expired, or already consumed", code); return Optional.empty(); } - // Check revocation (already used) - if (otc.isRevoked()) { - log.debug("OTC: {} already revoked (single-use)", code); - return Optional.empty(); - } - - // Mark as revoked (consumed) - single use - repo.revokeByCode(code); - log.debug("OTC: {} consumed for user: {}", code, otc.getLogin()); - - return Optional.of(otc); + Optional otcOpt = repo.findById(code); + otcOpt.ifPresent(otc -> log.debug("OTC: {} consumed for user: {}", code, otc.getLogin())); + return otcOpt; } /** diff --git a/src/test/java/org/ohdsi/webapi/security/authc/OneTimeCodeServiceTest.java b/src/test/java/org/ohdsi/webapi/security/authc/OneTimeCodeServiceTest.java new file mode 100644 index 0000000000..7d1bd6ab6c --- /dev/null +++ b/src/test/java/org/ohdsi/webapi/security/authc/OneTimeCodeServiceTest.java @@ -0,0 +1,57 @@ +package org.ohdsi.webapi.security.authc; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.util.Optional; +import java.util.UUID; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class OneTimeCodeServiceTest { + + private OneTimeCodeRepository repo; + private OneTimeCodeService service; + + @BeforeEach + void setUp() { + repo = mock(OneTimeCodeRepository.class); + service = new OneTimeCodeService(repo, new OneTimeCodeProperties()); + } + + @Test + void returnsCodeWhenThisCallConsumedIt() { + UUID code = UUID.randomUUID(); + OneTimeCodeEntity otc = new OneTimeCodeEntity(); + otc.setCode(code); + otc.setLogin("alice"); + otc.setJwtToken("jwt"); + when(repo.consumeByCode(eq(code), any())).thenReturn(1); + when(repo.findById(code)).thenReturn(Optional.of(otc)); + + Optional result = service.validateAndConsume(code); + + assertTrue(result.isPresent()); + assertEquals("jwt", result.get().getJwtToken()); + } + + @Test + void rejectsCodeAlreadyConsumedByConcurrentRequest() { + UUID code = UUID.randomUUID(); + // The conditional UPDATE matched no row: another request consumed it first, + // or it is expired or unknown. The JWT must not be read or returned. + when(repo.consumeByCode(eq(code), any())).thenReturn(0); + + Optional result = service.validateAndConsume(code); + + assertTrue(result.isEmpty()); + verify(repo, never()).findById(code); + } +}