-
Notifications
You must be signed in to change notification settings - Fork 0
Auth: core building blocks — token generation, repositories, clock #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RandyJDean
wants to merge
1
commit into
07-09-auth_add_magic-link_session_migrations_and_runtime_datasource
Choose a base branch
from
07-09-auth_core_building_blocks_token_generation_repositories_clock
base: 07-09-auth_add_magic-link_session_migrations_and_runtime_datasource
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−39
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
39 changes: 0 additions & 39 deletions
39
src/main/java/org/patinanetwork/patchats/api/auth/security/SecurityConfig.java
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
src/main/java/org/patinanetwork/patchats/auth/AuthProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package org.patinanetwork.patchats.auth; | ||
|
|
||
| import java.time.Duration; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
| import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
|
||
| /** Auth configuration, bound from {@code app.auth.*}. */ | ||
| @ConfigurationProperties(prefix = "app.auth") | ||
| @Getter | ||
| @Setter | ||
| public class AuthProperties { | ||
|
|
||
| /** Public origin of the SPA; magic links point at {@code <baseUrl>/auth/verify?token=...}. */ | ||
| private String baseUrl; | ||
|
|
||
| /** Whether the session cookie carries the {@code Secure} flag. Off only for plain-HTTP dev. */ | ||
| private boolean cookieSecure = true; | ||
|
|
||
| /** How long an emailed magic link stays valid. */ | ||
| private Duration magicLinkTtl = Duration.ofMinutes(15); | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/org/patinanetwork/patchats/auth/TokenGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package org.patinanetwork.patchats.auth; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.security.MessageDigest; | ||
| import java.security.NoSuchAlgorithmException; | ||
| import java.security.SecureRandom; | ||
| import java.util.Base64; | ||
| import java.util.HexFormat; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * Mints the opaque magic-link tokens. The raw token goes into the emailed link and is never persisted; only its SHA-256 | ||
| * hex digest is stored, so a database leak cannot be replayed as a login. | ||
| */ | ||
| @Component | ||
| public class TokenGenerator { | ||
|
|
||
| private static final int TOKEN_BYTES = 32; | ||
|
|
||
| private final SecureRandom secureRandom = new SecureRandom(); | ||
|
|
||
| /** A freshly minted token: {@code raw} for the email link, {@code hash} for the database. */ | ||
| public record GeneratedToken(String raw, String hash) {} | ||
|
|
||
| public GeneratedToken generate() { | ||
| final byte[] bytes = new byte[TOKEN_BYTES]; | ||
| secureRandom.nextBytes(bytes); | ||
| final String raw = Base64.getUrlEncoder().withoutPadding().encodeToString(bytes); | ||
| return new GeneratedToken(raw, hash(raw)); | ||
| } | ||
|
|
||
| /** SHA-256 hex digest of a raw token, used to look up what the client presents. */ | ||
| public static String hash(final String rawToken) { | ||
| try { | ||
| final MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
| return HexFormat.of().formatHex(digest.digest(rawToken.getBytes(StandardCharsets.UTF_8))); | ||
| } catch (final NoSuchAlgorithmException ex) { | ||
| throw new IllegalStateException("SHA-256 is unavailable", ex); | ||
| } | ||
| } | ||
| } |
52 changes: 52 additions & 0 deletions
52
src/main/java/org/patinanetwork/patchats/auth/repo/MagicLinkTokenRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package org.patinanetwork.patchats.auth.repo; | ||
|
|
||
| import java.time.Instant; | ||
| import java.time.ZoneOffset; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.springframework.jdbc.core.simple.JdbcClient; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| /** Plain-SQL access to {@code magic_link_tokens}. Only token hashes ever touch this table. */ | ||
| @Repository | ||
| @RequiredArgsConstructor | ||
| public class MagicLinkTokenRepository { | ||
|
|
||
| private final JdbcClient jdbc; | ||
|
|
||
| /** Invalidates every outstanding link for an email; called before issuing a new one. */ | ||
| public void deleteByEmail(final String email) { | ||
| jdbc.sql("DELETE FROM magic_link_tokens WHERE email = :email") | ||
| .param("email", email) | ||
| .update(); | ||
| } | ||
|
|
||
| public void insertToken(final UUID id, final String email, final String tokenHash, final Instant expiresAt) { | ||
| jdbc.sql("INSERT INTO magic_link_tokens (id, email, token_hash, expires_at)" | ||
| + " VALUES (:id, :email, :tokenHash, :expiresAt)") | ||
| .param("id", id) | ||
| .param("email", email) | ||
| .param("tokenHash", tokenHash) | ||
| .param("expiresAt", expiresAt.atOffset(ZoneOffset.UTC)) | ||
| .update(); | ||
| } | ||
|
|
||
| /** | ||
| * Atomically consumes an unexpired, unused token and returns the email it was issued to. The single UPDATE | ||
| * guarantees a token can only ever log in one caller, even under concurrent requests. | ||
| * | ||
| * <p>Returns {@link Optional#empty()} for unknown, already-consumed, <em>and</em> expired tokens alike — the | ||
| * uniformity is deliberate: callers surface one generic "invalid or expired" outcome, so presenting tokens never | ||
| * becomes an oracle for which failure occurred or whether an email exists in the system. | ||
| */ | ||
| public Optional<String> consumeAndReturnEmail(final String tokenHash, final Instant now) { | ||
| return jdbc.sql("UPDATE magic_link_tokens SET consumed_at = :now" | ||
| + " WHERE token_hash = :tokenHash AND consumed_at IS NULL AND expires_at > :now" | ||
| + " RETURNING email") | ||
| .param("tokenHash", tokenHash) | ||
| .param("now", now.atOffset(ZoneOffset.UTC)) | ||
| .query(String.class) | ||
| .optional(); | ||
|
RandyJDean marked this conversation as resolved.
|
||
| } | ||
| } | ||
15 changes: 15 additions & 0 deletions
15
src/main/java/org/patinanetwork/patchats/common/config/ClockConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package org.patinanetwork.patchats.common.config; | ||
|
|
||
| import java.time.Clock; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| /** Provides the single injectable {@link Clock} so services never call {@code Instant.now()} directly. */ | ||
|
RandyJDean marked this conversation as resolved.
|
||
| @Configuration | ||
| public class ClockConfig { | ||
|
|
||
| @Bean | ||
| public Clock clock() { | ||
| return Clock.systemUTC(); | ||
| } | ||
| } | ||
42 changes: 42 additions & 0 deletions
42
src/test/java/org/patinanetwork/patchats/auth/TokenGeneratorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package org.patinanetwork.patchats.auth; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.patinanetwork.patchats.auth.TokenGenerator.GeneratedToken; | ||
|
|
||
| class TokenGeneratorTest { | ||
|
|
||
| private final TokenGenerator generator = new TokenGenerator(); | ||
|
|
||
| @Test | ||
| void rawTokenIsUrlSafeAnd256Bits() { | ||
| final GeneratedToken token = generator.generate(); | ||
|
|
||
| // 32 bytes base64url without padding -> 43 chars, no characters needing URL encoding. | ||
| assertEquals(43, token.raw().length()); | ||
| assertTrue(token.raw().matches("[A-Za-z0-9_-]+")); | ||
| } | ||
|
|
||
| @Test | ||
| void hashMatchesSha256HexOfRaw() { | ||
| final GeneratedToken token = generator.generate(); | ||
|
|
||
| assertEquals(TokenGenerator.hash(token.raw()), token.hash()); | ||
| assertEquals(64, token.hash().length()); | ||
| assertTrue(token.hash().matches("[0-9a-f]+")); | ||
| } | ||
|
|
||
| @Test | ||
| void generatedTokensAreUnique() { | ||
| assertNotEquals(generator.generate().raw(), generator.generate().raw()); | ||
| } | ||
|
|
||
| @Test | ||
| void hashIsDeterministic() { | ||
| assertEquals(TokenGenerator.hash("abc"), TokenGenerator.hash("abc")); | ||
| assertNotEquals(TokenGenerator.hash("abc"), TokenGenerator.hash("abd")); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.