Skip to content

Commit 8702ba3

Browse files
Refactor auth to use keycloak.js instead of homegrown system (#580)
* Mid-work with Spencer Co-authored-by: Spencer Stolworthy <[email protected]> * keycloa and dagger * Add realm to config.json * Fix merge conflict and update snapshots * Make lots of things optional for the sake of tests * Remove auth tests which were for things that keycloak.js abstracts. Coerce 'undefined' auth to false. * Add config for realm, remove hardcoded realm * Switch to silent SSO check, disable iframe, update deployment dir and README * role mappings * map the roles object, not the groups objec Co-authored-by: Spencer Stolworthy <[email protected]> Co-authored-by: Spencer Stolworthy <[email protected]>
1 parent 9f5f178 commit 8702ba3

File tree

25 files changed

+165
-401
lines changed

25 files changed

+165
-401
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,8 @@ Depending on the type of deployment, the way of setting these variables may vary
298298
| Variable | Type | Description | Required | Default |
299299
| :------------------------ | :------ | :------------------------------------------------------------------------------------- | :------- | :------- |
300300
| **baseUrl** | string | Target URL for the deployment of **this** Frontend App | Yes | N/A |
301-
| **authBaseUrl** | string | URI for SSO integration | Yes | N/A |
301+
| **authBaseUrl** | string | URI for SSO integration (ending in /auth for Keycloak/RHSSO) | Yes | N/A |
302+
| **realm** | string | Realm for SSO integration | Yes | N/A |
302303
| **clientId** | string | Identification of the client application for SSO integration | Yes | N/A |
303304
| **backendUrl** | string | URI for [Backend](https://github.com/rht-labs/lodestar-backend.git) APIs | Yes | N/A |
304305
| **disableLaunch** | boolean | Flag to toggle launch functionality on/off | No | false |

deployment/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ helm template . \
3434
--set baseUrl=<your-base-url> \
3535
--set clientId=<your-sso-client-id> \
3636
--set authBaseUrl=<your-sso-openid-connect-url> \
37+
--set realm=<your-realm> \
3738
--set backendUrl=<your-backend-url> \
3839
--set 'access.groups[0].name=group-name','access.groups[0].roles={access_type,axxess_type}'
3940
| oc apply -f -
@@ -47,7 +48,8 @@ It accepts the following variables
4748
| `git.ref` | The branch name to build |
4849
| `baseUrl` | The FQDN at which this route will be exposed - depends on your environment |
4950
| `clientId` | The client ID that the SSO server is configured to accept auth requests using |
50-
| `authBaseUrl` | The url that your SSO server accepts OpenID Connect requests on - for Keycloak, something like `https://<keycloak-base-url>.com/auth/realms/<realm-id>/protocol/openid-connect` |
51+
| `realm` | Realm for SSO integration
52+
| `authBaseUrl` | The url that your SSO server accepts OpenID Connect requests on - for Keycloak, something like `https://<keycloak-base-url>.com/auth` |
5153
| `backendUrl` | The url that the LodeStar backend accepts requests on |
5254
| `access.groups` | A list of groups to receive access to LodeStar |
5355
| `access.groups[i].roles` | A list of roles to map access |

deployment/templates/configmap.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ data:
99
{
1010
"baseUrl": "{{ .Values.baseUrl }}",
1111
"clientId": "{{ .Values.clientId }}",
12+
"realm": "{{ .Values.realm }}",
1213
"authBaseUrl": "{{ .Values.authBaseUrl }}",
1314
"backendUrl": "{{ .Values.backendUrl }}",
1415
"disableLaunch": "{{ .Values.disableLaunch }}",

package-lock.json

Lines changed: 46 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"@patternfly/react-core": "^4.192.15",
88
"@patternfly/react-icons": "^4.43.15",
99
"@patternfly/react-table": "^4.61.15",
10+
"@react-keycloak/web": "^3.4.0",
1011
"@testing-library/jest-dom": "^5.11.9",
1112
"@testing-library/react": "^11.2.5",
1213
"@testing-library/user-event": "^7.2.1",
@@ -27,6 +28,7 @@
2728
"date-fns": "^2.14.0",
2829
"date-fns-tz": "^1.0.12",
2930
"faker": "^4.1.0",
31+
"keycloak-js": "^10.0.2",
3032
"mockdate": "^3.0.2",
3133
"prettier": "^1.19.1",
3234
"query-string": "^6.13.8",

public/config/config.example.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"baseUrl": "http://my-base.url",
33
"clientId": "lodestar",
4-
"authBaseUrl": "https://my-sso-endpoint.url",
4+
"realm": "realm",
5+
"authBaseUrl": "https://my-sso-endpoint.url/auth",
56
"backendUrl": "http://my-backend.url/api",
67
"disableLaunch": true,
78
"loggerType": "console",

public/silent-check-sso.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<body>
3+
<script>
4+
parent.postMessage(location.href, location.origin)
5+
</script>
6+
</body>
7+
</html>

src/app.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
getFeaturesFromVersion,
3434
} from './common/version_feature_factory';
3535
import { CategoryProvider } from './context/category_context/category_context';
36+
import { useKeycloak } from '@react-keycloak/web';
3637

3738
export const App = ({ config }: { config: Config }) => {
3839
const serviceProviders =
@@ -41,12 +42,12 @@ export const App = ({ config }: { config: Config }) => {
4142
: createApiV1Services(config);
4243

4344
const { appConfig } = useConfig();
45+
const { keycloak } = useKeycloak();
4446
return (
4547
<ErrorBoundary>
4648
<ServiceProvider serviceFactory={serviceProviders}>
4749
<ServiceProviderContext.Consumer>
4850
{({
49-
authService,
5051
notificationService,
5152
versionService,
5253
categoryService,
@@ -69,8 +70,9 @@ export const App = ({ config }: { config: Config }) => {
6970
{analyticsContext => (
7071
<FeedbackProvider>
7172
<AuthProvider
72-
authService={authService}
73-
analyticsContext={analyticsContext}
73+
keycloak={keycloak}
74+
publicUrl={appConfig.baseUrl}
75+
roleMapping={appConfig.roles}
7476
>
7577
<NotificationProvider
7678
notificationService={notificationService}
Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,16 @@
1-
import React, { useState, useEffect } from 'react';
1+
import React from 'react';
22
import { Route, RouteProps } from 'react-router-dom';
3-
import { SendToSSO } from './send_to_sso';
4-
import { useSession } from '../../context/auth_context/auth_context';
5-
import { AuthenticationError } from '../../services/auth_service/auth_errors';
6-
import { Logger } from '../../utilities/logger';
3+
import { useKeycloak } from '@react-keycloak/web';
74

85
export const PrivateRoute = (props: RouteProps) => {
9-
const { checkIsAuthenticated, authError } = useSession();
10-
const [authStatusChecked, setAuthStatusChecked] = useState<boolean>(false);
11-
const [isAuthed, setIsAuthed] = useState<boolean>(null);
12-
useEffect(() => {
13-
Logger.instance.debug('private_route:authError useEffect', authError);
14-
if (authError instanceof AuthenticationError) {
15-
setIsAuthed(false);
16-
}
17-
}, [authError]);
18-
useEffect(() => {
19-
if (!authStatusChecked) {
20-
checkIsAuthenticated().then(isAuthenticated =>
21-
setIsAuthed(isAuthenticated)
22-
);
23-
}
24-
setAuthStatusChecked(true);
25-
}, [checkIsAuthenticated, authStatusChecked]);
26-
27-
if (isAuthed) {
6+
const { keycloak, initialized } = useKeycloak();
7+
if (!initialized) {
8+
return <div />;
9+
}
10+
if (keycloak.authenticated) {
2811
return <Route {...props} />;
29-
} else if (isAuthed === false) {
30-
return <SendToSSO />;
3112
} else {
32-
return <div />;
13+
keycloak.login();
14+
return <div />
3315
}
3416
};

src/components/drawer_and_header/user_dropdown.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
AnalyticsCategory,
99
} from '../../context/analytics_context/analytics_context';
1010

11-
export interface UserDropdownProps {}
11+
export interface UserDropdownProps { }
1212

1313
export function UserDropdown(props: UserDropdownProps) {
1414
const authContext = useSession();

0 commit comments

Comments
 (0)