-
Notifications
You must be signed in to change notification settings - Fork 25
fix: prevent CDA GUI auth bootstrap crash #1865
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| import { | ||
| AuthProvider, | ||
| createKeycloakAuthMethod, | ||
| } from "@usace-watermanagement/groundwork-water"; | ||
| import PropTypes from "prop-types"; | ||
| import { useEffect, useState } from "react"; | ||
|
|
||
| import { getBasePath } from "../utils/base"; | ||
| import { getKeycloakConfig, normalizeOpenIdConnectUrls } from "../utils/auth-config"; | ||
| import { AuthConfigurationContext } from "./auth-configuration-context"; | ||
|
|
||
| function createLocalAuthMethod() { | ||
| let token; | ||
| return { | ||
| async login() { | ||
| const response = await fetch( | ||
| `${import.meta.env.VITE_AUTH_HOST}/realms/${import.meta.env.VITE_AUTH_REALM}/protocol/openid-connect/token`, | ||
| { | ||
| method: "POST", | ||
| headers: { "Content-Type": "application/x-www-form-urlencoded" }, | ||
| body: new URLSearchParams({ | ||
| grant_type: "password", | ||
| client_id: "cwms", | ||
| username: import.meta.env.VITE_AUTH_USER, | ||
| password: import.meta.env.VITE_AUTH_PASSWORD, | ||
| }), | ||
| }, | ||
| ); | ||
| if (!response.ok) { | ||
| throw new Error(`Local Keycloak login failed (${response.status})`); | ||
| } | ||
| token = (await response.json()).access_token; | ||
| }, | ||
| async logout() { | ||
| token = undefined; | ||
| }, | ||
| async isAuth() { | ||
| return !!token; | ||
| }, | ||
| get token() { | ||
| return token; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| const unavailableAuthMethod = { | ||
| async login() {}, | ||
| async logout() {}, | ||
| async isAuth() { | ||
| return false; | ||
| }, | ||
| get token() { | ||
| return undefined; | ||
| }, | ||
| }; | ||
|
|
||
| async function loadDeployedAuthMethod() { | ||
| const response = await fetch(`${getBasePath()}/swagger-docs`, { | ||
| cache: "no-store", | ||
| headers: { Accept: "application/json" }, | ||
| }); | ||
| if (!response.ok) { | ||
| throw new Error(`OpenAPI request failed with HTTP ${response.status}`); | ||
| } | ||
|
|
||
| const spec = await response.json(); | ||
| normalizeOpenIdConnectUrls(spec, window.location.origin); | ||
| const config = getKeycloakConfig(spec, window.location.href); | ||
| if (!config) { | ||
| throw new Error("The OpenAPI document does not advertise a usable OpenID client."); | ||
| } | ||
| return createKeycloakAuthMethod(config); | ||
| } | ||
|
|
||
| export default function AppAuthProvider({ children }) { | ||
| const [state, setState] = useState(() => | ||
| import.meta.env.MODE === "dev-cda-compose" | ||
| ? { method: createLocalAuthMethod(), error: null } | ||
| : { method: null, error: null }, | ||
| ); | ||
|
|
||
| useEffect(() => { | ||
| if (state.method) return undefined; | ||
|
|
||
| let cancelled = false; | ||
| loadDeployedAuthMethod() | ||
| .then((method) => { | ||
| if (!cancelled) setState({ method, error: null }); | ||
| }) | ||
| .catch((error) => { | ||
| if (!cancelled) { | ||
| setState({ | ||
| method: unavailableAuthMethod, | ||
| error: `Sign-in is unavailable: ${error?.message ?? "invalid authentication configuration"}`, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return () => { | ||
| cancelled = true; | ||
| }; | ||
| }, [state.method]); | ||
|
|
||
| if (!state.method) { | ||
| return ( | ||
| <div className="p-6 text-slate-700" role="status"> | ||
| Loading authentication configuration… | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
| return ( | ||
| <AuthConfigurationContext.Provider value={{ error: state.error }}> | ||
| <AuthProvider method={state.method}>{children}</AuthProvider> | ||
| </AuthConfigurationContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| AppAuthProvider.propTypes = { | ||
| children: PropTypes.node.isRequired, | ||
| }; | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import PropTypes from "prop-types"; | ||
| import { Component } from "react"; | ||
|
|
||
| export default class GlobalErrorBoundary extends Component { | ||
| constructor(props) { | ||
| super(props); | ||
| this.state = { error: null }; | ||
| } | ||
|
|
||
| static getDerivedStateFromError(error) { | ||
| return { error }; | ||
| } | ||
|
|
||
| componentDidCatch(error, errorInfo) { | ||
| console.error("Uncaught CDA GUI error", error, errorInfo); | ||
| } | ||
|
|
||
| render() { | ||
| if (this.state.error) { | ||
| return ( | ||
| <main className="p-6"> | ||
| <h1 className="text-lg font-semibold text-red-700">Something went wrong</h1> | ||
| <p className="mt-2 text-slate-700"> | ||
| {this.state.error?.message ?? "An unexpected error occurred."} | ||
| </p> | ||
| <a className="mt-4 inline-block text-blue-700 underline" href="/cwms-data/"> | ||
| Return to CDA | ||
| </a> | ||
| </main> | ||
| ); | ||
| } | ||
|
|
||
| return this.props.children; | ||
| } | ||
| } | ||
|
|
||
| GlobalErrorBoundary.propTypes = { | ||
| children: PropTypes.node.isRequired, | ||
| }; |
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,7 @@ | ||
| import { createContext, useContext } from "react"; | ||
|
|
||
| export const AuthConfigurationContext = createContext({ error: null }); | ||
|
|
||
| export function useAuthConfiguration() { | ||
| return useContext(AuthConfigurationContext); | ||
| } |
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
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.