Repository navigation
Record signed-in operators and show the linked SCM login #428
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0d286c1
Record the signed-in operator on the session probe
artyomsv 5bdf05b
Show the SCM login a sign-in observed beside the id
artyomsv b253ea3
Skip repeat operator writes and split the handle tests
artyomsv ff7c92c
Move NameWithId into its own file
artyomsv 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
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
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
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
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
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
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
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
55 changes: 55 additions & 0 deletions
55
spire-orchestrator/src/main/java/dev/codespire/orchestrator/security/OperatorNames.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,55 @@ | ||
| package dev.codespire.orchestrator.security; | ||
|
|
||
| import io.quarkus.security.identity.SecurityIdentity; | ||
| import org.eclipse.microprofile.jwt.JsonWebToken; | ||
|
|
||
| /** | ||
| * The names a signed-in operator is shown by, with every email address taken out. | ||
| * | ||
| * <p>Email is never persisted (CLAUDE.md), and an identity provider does not promise that its name | ||
| * claims are not emails: on the first production realm {@code preferred_username} IS the email | ||
| * address, and the principal name follows it. So any value with an {@code @} is dropped, and the | ||
| * caller gets an empty string instead. The dashboard then shows the display name, or the subject. | ||
| */ | ||
| public final class OperatorNames { | ||
|
|
||
| private static final String NAME_CLAIM = "name"; | ||
| private static final String USERNAME_CLAIM = "preferred_username"; | ||
|
|
||
| private OperatorNames() { | ||
| } | ||
|
|
||
| /** The login, or empty when the provider's login is an email address or there is none. */ | ||
| public static String username(SecurityIdentity identity) { | ||
| if (identity == null || identity.isAnonymous()) { | ||
| return ""; | ||
| } | ||
| String claimed = claim(identity, USERNAME_CLAIM); | ||
| return withoutEmail(claimed.isEmpty() ? identity.getPrincipal().getName() : claimed); | ||
| } | ||
|
|
||
| /** The {@code name} claim, else the login, else empty. Never an email address. */ | ||
| public static String displayName(SecurityIdentity identity) { | ||
| if (identity == null || identity.isAnonymous()) { | ||
| return ""; | ||
| } | ||
| String name = withoutEmail(claim(identity, NAME_CLAIM)); | ||
| return name.isEmpty() ? username(identity) : name; | ||
| } | ||
|
|
||
| /** {@code value} trimmed, or empty when it is blank or looks like an email address. */ | ||
| public static String withoutEmail(String value) { | ||
| if (value == null || value.isBlank() || value.contains("@")) { | ||
| return ""; | ||
| } | ||
| return value.trim(); | ||
| } | ||
|
|
||
| private static String claim(SecurityIdentity identity, String claimName) { | ||
| if (identity.getPrincipal() instanceof JsonWebToken jwt | ||
| && jwt.getClaim(claimName) instanceof String text) { | ||
| return text.trim(); | ||
| } | ||
| return ""; | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
...chestrator/src/main/java/dev/codespire/orchestrator/security/RecentOperatorSightings.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,45 @@ | ||
| package dev.codespire.orchestrator.security; | ||
|
|
||
| import jakarta.enterprise.context.ApplicationScoped; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Which operators this process recorded a moment ago, so {@code /api/me} does not write the same row | ||
| * again. | ||
| * | ||
| * <p>The session probe is not only asked once per dashboard load: the live socket's close handler | ||
| * ({@code useLiveSocket}) asks it on every close before it reconnects, so a flapping socket would turn | ||
| * {@code operator_seen} into a write stream. An identical sighting within {@link #WINDOW} is skipped; | ||
| * a changed username or display name is written at once. Per process and in memory: a second | ||
| * instance or a restart writes once more, which is harmless. | ||
| */ | ||
| @ApplicationScoped | ||
| public class RecentOperatorSightings { | ||
|
|
||
| static final Duration WINDOW = Duration.ofSeconds(60); | ||
|
|
||
| private record Sighting(String username, String displayName, Instant at) { | ||
| } | ||
|
|
||
| private final Map<String, Sighting> recent = new ConcurrentHashMap<>(); | ||
|
|
||
| Supplier<Instant> clock = Instant::now; | ||
|
|
||
| /** Whether this sighting differs from the last one recorded, or that one is older than the window. */ | ||
| public boolean isNew(String subject, String username, String displayName) { | ||
| Sighting last = recent.get(subject); | ||
| return last == null | ||
| || !last.username().equals(username) | ||
| || !last.displayName().equals(displayName) | ||
| || last.at().plus(WINDOW).isBefore(clock.get()); | ||
| } | ||
|
|
||
| public void remember(String subject, String username, String displayName) { | ||
| recent.put(subject, new Sighting(username, displayName, clock.get())); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
spire-orchestrator/src/main/resources/db/migration/V116__operator_identity_author_handle.sql
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,10 @@ | ||
| -- The SCM login an operator proved by signing in, shown beside the stable id (FR-11). | ||
| -- | ||
| -- A fresh install showed a linked account as its numeric id twice ("3218389 / 3218389"): the only | ||
| -- handle the dashboard knew came from authors observed in reviews, and there were none yet. The connect | ||
| -- callback has just been told the login by the forge itself, so it is kept with the link. | ||
| -- | ||
| -- Display data only, never a key: author_id stays the identity. NULL for a link an admin made by hand | ||
| -- (no login was observed); the dashboard then falls back to the observed authors, then to the id. | ||
| -- Never an email address: the callback refuses to store one. | ||
| ALTER TABLE operator_identity ADD COLUMN author_handle TEXT; |
Oops, something went wrong.
Oops, something went wrong.
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.