Skip to content

Commit e20cbd1

Browse files
authored
oidc, console2: improve error handling (#979)
1 parent cac6659 commit e20cbd1

File tree

6 files changed

+61
-17
lines changed

6 files changed

+61
-17
lines changed

console2/src/components/pages/UnauthorizedPage/index.tsx

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,11 @@ import * as React from 'react';
2323
import { RedirectButton } from '../../organisms';
2424

2525
import './styles.css';
26-
import { Card, CardContent, CardHeader, Divider, Image } from 'semantic-ui-react';
27-
import { useContext, useEffect } from 'react';
28-
import { UserSessionContext } from '../../../session';
29-
30-
export default () => {
31-
const session = useContext(UserSessionContext);
32-
33-
useEffect(() => {
34-
session.setUserInfo(undefined);
35-
}, [session]);
26+
import {Card, CardContent, CardDescription, CardHeader, Divider, Image} from 'semantic-ui-react';
27+
import {withRouter} from "react-router";
3628

29+
export default withRouter((props) => {
30+
const error = new URLSearchParams(props.location.search).get('error');
3731
return (
3832
<div className="flexbox-container">
3933
<Card centered={true}>
@@ -42,6 +36,8 @@ export default () => {
4236

4337
<CardHeader>You are not authorized.</CardHeader>
4438

39+
{error && <CardDescription>Error: {error}</CardDescription>}
40+
4541
<Divider />
4642

4743
<RedirectButton primary={true} fluid={true} location={'/'}>
@@ -51,4 +47,4 @@ export default () => {
5147
</Card>
5248
</div>
5349
);
54-
};
50+
});

server/dist/src/main/resources/concord-server.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ concord-server {
589589
urlBase = "http://concord.example.com"
590590
afterLoginUrl = "http://concord.example.com"
591591
afterLogoutUrl = "http://concord.example.com/#/logout/done"
592+
onErrorUrl = "http://concord.example.com/#/unauthorized"
592593

593594
scopes = [ "openid", "profile", "email", "groups"]
594595

server/plugins/oidc/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ concord-server {
2323
}
2424
```
2525

26+
For running in development mode (i.e. on `localhost`), callback URLs must be
27+
in the form of
28+
29+
```
30+
http://localhost:8001/api/service/oidc/callback?client_name=oidc
31+
```
32+
33+
Note the `client_name=oidc` query parameter, it is required by the plugin and
34+
must be present in the provider's configuration.
35+
36+
The plugin uses the following scopes: `openid`, `profile`, `email`, `groups`.
37+
Which may or may not be enabled by default in the provider's configuration.
38+
39+
Okta, for example, does not provide the `groups` scope by default. You can
40+
add it in the "Security" -> "API" -> "Authorization Servers" -> your_server ->
41+
"Scope" section.
42+
2643
### Interactive Login
2744

2845
Configure the Concord Console to use custom logout/login URLs:

server/plugins/oidc/src/main/java/com/walmartlabs/concord/server/plugins/oidc/OidcAuthFilter.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,42 @@ public class OidcAuthFilter implements Filter {
3737

3838
public static final String URL = "/api/service/oidc/auth";
3939

40-
private final Config config;
40+
private final PluginConfiguration pluginConfig;
41+
private final Config oidcConfig;
4142
private final OidcClient<?> client;
4243

4344
@Inject
44-
public OidcAuthFilter(@Named("oidc") Config config, OidcClient<?> client) {
45-
this.config = config;
45+
public OidcAuthFilter(PluginConfiguration pluginConfig, @Named("oidc") Config oidcConfig, OidcClient<?> client) {
46+
this.pluginConfig = pluginConfig;
47+
this.oidcConfig = oidcConfig;
4648
this.client = client;
49+
50+
if (pluginConfig.isEnabled() && !client.isInitialized()) {
51+
client.init();
52+
}
4753
}
4854

4955
@Override
5056
@SuppressWarnings("unchecked")
51-
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
57+
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException {
5258
HttpServletRequest req = (HttpServletRequest) request;
5359
HttpServletResponse resp = (HttpServletResponse) response;
5460

61+
if (!pluginConfig.isEnabled()) {
62+
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "OIDC disabled");
63+
return;
64+
}
65+
5566
JEEContext context = new JEEContext(req, resp);
5667

5768
String redirectUrl = req.getParameter("from");
5869
context.getSessionStore().set(context, Pac4jConstants.REQUESTED_URL, redirectUrl);
5970

60-
RedirectionAction action = client.getRedirectionAction(context)
71+
RedirectionAction action = client.getRedirectionActionBuilder()
72+
.getRedirectionAction(context)
6173
.orElseThrow(() -> new IllegalStateException("Can't get a redirection action for the request"));
6274

63-
config.getHttpActionAdapter().adapt(action, context);
75+
oidcConfig.getHttpActionAdapter().adapt(action, context);
6476
}
6577

6678
@Override

server/plugins/oidc/src/main/java/com/walmartlabs/concord/server/plugins/oidc/OidcCallbackFilter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
7272
postLoginUrl = cfg.getAfterLoginUrl();
7373
}
7474

75+
String error = req.getParameter("error");
76+
if (error != null) {
77+
String derivedError = "unknown";
78+
if ("access_denied".equals(error)) {
79+
derivedError = "oidc_access_denied";
80+
}
81+
resp.sendRedirect(resp.encodeRedirectURL(cfg.getOnErrorUrl() + "?from=" + postLoginUrl + "&error=" + derivedError));
82+
return;
83+
}
84+
7585
try {
7686
CallbackLogic<?, JEEContext> callback = pac4jConfig.getCallbackLogic();
7787
callback.perform(context, pac4jConfig, pac4jConfig.getHttpActionAdapter(), postLoginUrl, true, false, true, OidcPluginModule.CLIENT_NAME);

server/plugins/oidc/src/main/java/com/walmartlabs/concord/server/plugins/oidc/PluginConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ public class PluginConfiguration {
5858
@Config("oidc.afterLogoutUrl")
5959
private String afterLogoutUrl;
6060

61+
@Inject
62+
@Config("oidc.onErrorUrl")
63+
private String onErrorUrl;
64+
6165
@Inject
6266
@Nullable
6367
@Config("oidc.scopes")
@@ -102,6 +106,10 @@ public String getAfterLogoutUrl() {
102106
return afterLogoutUrl;
103107
}
104108

109+
public String getOnErrorUrl() {
110+
return onErrorUrl;
111+
}
112+
105113
public List<String> getScopes() {
106114
return scopes;
107115
}

0 commit comments

Comments
 (0)