-
Notifications
You must be signed in to change notification settings - Fork 0
feat(java): dashboard OAuth/OIDC (Google, GitHub, generic) #393
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
2 commits
Select commit
Hold shift + click to select a range
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
137 changes: 137 additions & 0 deletions
137
sdks/java/src/main/java/org/byteveda/taskito/dashboard/auth/oauth/OAuthFlow.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,137 @@ | ||
| package org.byteveda.taskito.dashboard.auth.oauth; | ||
|
|
||
| import java.net.http.HttpClient; | ||
| import java.util.ArrayList; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import org.byteveda.taskito.dashboard.auth.AuthStore; | ||
| import org.byteveda.taskito.dashboard.auth.Session; | ||
| import org.byteveda.taskito.dashboard.auth.User; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.config.OAuthConfig; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.error.IdentityFetchError; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.error.ProviderNotConfigured; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.error.StateValidationError; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.model.OAuthState; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.model.ProviderIdentity; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.provider.OAuthProvider; | ||
| import org.byteveda.taskito.dashboard.auth.oauth.provider.Providers; | ||
|
|
||
| /** | ||
| * The seam between the HTTP handler layer and the provider implementations. It | ||
| * owns the provider registry, the state store, and the {@link AuthStore} | ||
| * integration: handlers call {@link #start} to mint a redirect URL and | ||
| * {@link #handleCallback} to land a session. | ||
| */ | ||
| public final class OAuthFlow { | ||
| private final AuthStore authStore; | ||
| private final OAuthConfig config; | ||
| private final OAuthStateStore stateStore; | ||
| private final Map<String, OAuthProvider> providers; | ||
|
|
||
| public OAuthFlow( | ||
| AuthStore authStore, OAuthConfig config, OAuthStateStore stateStore, Map<String, OAuthProvider> providers) { | ||
| this.authStore = authStore; | ||
| this.config = config; | ||
| this.stateStore = stateStore; | ||
| this.providers = new LinkedHashMap<>(providers); | ||
| } | ||
|
|
||
| /** The landed session plus the sanitised post-login redirect target. */ | ||
| public record CallbackResult(Session session, String nextUrl) {} | ||
|
|
||
| /** Instantiate one provider per configured slot, keyed by slot, in display order. */ | ||
| public static Map<String, OAuthProvider> buildProviders(OAuthConfig config, HttpClient http) { | ||
| return Providers.build(config, http); | ||
| } | ||
|
|
||
| // ---- introspection ----------------------------------------------------- | ||
|
|
||
| public boolean passwordAuthEnabled() { | ||
| return config.passwordAuthEnabled(); | ||
| } | ||
|
|
||
| public boolean hasProvider(String slot) { | ||
| return providers.containsKey(slot); | ||
| } | ||
|
|
||
| /** Compact provider summary for the login UI (no secrets), in display order. */ | ||
| public List<Map<String, Object>> providersListing() { | ||
| List<Map<String, Object>> out = new ArrayList<>(); | ||
| for (OAuthProvider provider : providers.values()) { | ||
| Map<String, Object> entry = new LinkedHashMap<>(); | ||
| entry.put("slot", provider.slot()); | ||
| entry.put("label", provider.label()); | ||
| entry.put("type", provider.type()); | ||
| out.add(entry); | ||
| } | ||
| return out; | ||
| } | ||
|
|
||
| // ---- flow -------------------------------------------------------------- | ||
|
|
||
| /** | ||
| * Mint a state row and return the provider's authorize URL. {@code nextUrl} | ||
| * is sanitised against {@link UrlSafety#isSafeRedirect}, falling back to | ||
| * {@code "/"}. | ||
| */ | ||
| public String start(String slot, String nextUrl) { | ||
| OAuthProvider provider = requireProvider(slot); | ||
| String safeNext = nextUrl != null && UrlSafety.isSafeRedirect(nextUrl) ? nextUrl : "/"; | ||
| OAuthState state = stateStore.create(slot, safeNext); | ||
| return provider.authorizationUrl(state, config.callbackUrl(slot)); | ||
| } | ||
|
|
||
| /** | ||
| * Exchange {@code code} for an identity and create a session. | ||
| * | ||
| * @throws StateValidationError missing/expired/replayed state or a slot mismatch | ||
| * @throws IdentityFetchError any token / userinfo / claim failure | ||
| * @throws org.byteveda.taskito.dashboard.auth.oauth.error.AllowlistDenied the | ||
| * identity is outside a configured allowlist | ||
| * @throws ProviderNotConfigured the slot has no registered provider | ||
| */ | ||
| public CallbackResult handleCallback(String slot, String code, String stateToken, String error) { | ||
| if (error != null && !error.isEmpty()) { | ||
| throw new IdentityFetchError("provider returned error: " + error); | ||
| } | ||
| if (code == null || code.isEmpty() || stateToken == null || stateToken.isEmpty()) { | ||
| throw new StateValidationError("missing code or state parameter"); | ||
| } | ||
| Optional<OAuthState> row = stateStore.consume(stateToken); | ||
| if (row.isEmpty()) { | ||
| throw new StateValidationError("state is invalid, expired, or already used"); | ||
| } | ||
| OAuthState state = row.get(); | ||
| // slot is the non-null callback route param; compare from it so a null | ||
| // slot on a malformed-but-parsed state row is rejected, not an NPE. | ||
| if (!slot.equals(state.slot())) { | ||
| throw new StateValidationError("state slot does not match callback slot"); | ||
| } | ||
|
|
||
| OAuthProvider provider = requireProvider(slot); | ||
| ProviderIdentity identity = | ||
| provider.exchangeCode(code, state.codeVerifier(), config.callbackUrl(slot), state.nonce()); | ||
| provider.checkAllowlist(identity); | ||
|
|
||
| User user = authStore.getOrCreateOauthUser( | ||
| identity.slot(), | ||
| identity.subject(), | ||
| identity.email(), | ||
| identity.name(), | ||
| identity.emailVerified(), | ||
| config.adminEmails()); | ||
| Session session = authStore.createSession(user.username(), user.role()); | ||
| stateStore.pruneExpiredIfDue(); | ||
| return new CallbackResult(session, state.nextUrl()); | ||
| } | ||
|
pratyush618 marked this conversation as resolved.
|
||
|
|
||
| private OAuthProvider requireProvider(String slot) { | ||
| OAuthProvider provider = providers.get(slot); | ||
| if (provider == null) { | ||
| throw new ProviderNotConfigured("OAuth provider '" + slot + "' is not configured"); | ||
| } | ||
| return provider; | ||
| } | ||
| } | ||
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.