Skip to content

Commit 7b076e2

Browse files
Added missing logs for OIDC login (geonetwork#8995)
1 parent c44a66c commit 7b076e2

File tree

1 file changed

+45
-33
lines changed

1 file changed

+45
-33
lines changed

core/src/main/java/org/fao/geonet/kernel/security/openidconnect/GeonetworkOAuth2LoginAuthenticationFilter.java

Lines changed: 45 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected void successfulAuthentication(HttpServletRequest request,
8484
FilterChain chain,
8585
Authentication authResult)
8686
throws IOException, ServletException {
87-
87+
String username = "UNIDENTIFIED";
8888
if (authResult == null) {
8989
throw new IOException("authresult is null!"); // this shouldn't happen
9090
}
@@ -104,54 +104,66 @@ protected void successfulAuthentication(HttpServletRequest request,
104104
//save user
105105
try {
106106
UserDetails userDetails = oAuth2SecurityProviderUtil.getUserDetails(authResult, true);
107+
if(userDetails != null) {
108+
username = userDetails.getUsername();
109+
Log.info(Geonet.SECURITY, "User '" + username
110+
+ "' authenticated via OIDC");
111+
}
112+
107113
} catch (Exception e) {
108114
throw new IOException("OIDC: couldnt save user details",e);
109115
}
110116

111-
SecurityContextHolder.getContext().setAuthentication(authResult);
117+
try{
118+
SecurityContextHolder.getContext().setAuthentication(authResult);
112119

113120

114-
//cf GN keycloak
115-
String redirectURL = findQueryParameter(request, "redirectUrl");
116-
if (redirectURL != null) {
117-
try {
118-
URI redirectUri = new URI(redirectURL);
119-
if (redirectUri != null && !redirectUri.isAbsolute()) {
120-
response.sendRedirect(redirectUri.toString());
121-
} else {
122-
// If the redirect url ends up being null or absolute url then lets redirect back to the context home.
123-
Log.warning(Geonet.SECURITY, "Failed to perform login redirect to '" + redirectURL + "'. Redirected to context home");
121+
//cf GN keycloak
122+
String redirectURL = findQueryParameter(request, "redirectUrl");
123+
if (redirectURL != null) {
124+
try {
125+
URI redirectUri = new URI(redirectURL);
126+
if (redirectUri != null && !redirectUri.isAbsolute()) {
127+
response.sendRedirect(redirectUri.toString());
128+
} else {
129+
// If the redirect url ends up being null or absolute url then lets redirect back to the context home.
130+
Log.warning(Geonet.SECURITY, "Failed to perform login redirect to '" + redirectURL + "'. Redirected to context home");
131+
response.sendRedirect(request.getContextPath());
132+
}
133+
} catch (URISyntaxException e) {
124134
response.sendRedirect(request.getContextPath());
125135
}
126-
} catch (URISyntaxException e) {
136+
} else {
127137
response.sendRedirect(request.getContextPath());
128138
}
129-
} else {
130-
response.sendRedirect(request.getContextPath());
131-
}
132139

133-
// Set users preferred locale if it exists. - cf. keycloak
134-
String localeString = oidcUser.getLocale();
135-
if (!StringUtils.isEmpty(localeString)) {
136-
try {
140+
// Set users preferred locale if it exists. - cf. keycloak
141+
String localeString = oidcUser.getLocale();
142+
if (!StringUtils.isEmpty(localeString)) {
137143
try {
138-
//Try to parse the locale as a languageTag i.e. en-CA
139-
response.setLocale(new Locale.Builder().setLanguageTag(localeString).build());
140-
} catch (IllformedLocaleException e) {
141-
// If there are any exceptions try a different approach as it may be in the format of en_CA or simply en
142-
response.setLocale(LocaleUtils.toLocale(localeString));
144+
try {
145+
//Try to parse the locale as a languageTag i.e. en-CA
146+
response.setLocale(new Locale.Builder().setLanguageTag(localeString).build());
147+
} catch (IllformedLocaleException e) {
148+
// If there are any exceptions try a different approach as it may be in the format of en_CA or simply en
149+
response.setLocale(LocaleUtils.toLocale(localeString));
150+
}
151+
} catch (IllegalArgumentException e) {
152+
Log.warning(Geonet.SECURITY, "Unable to parse oidc locale " + oidcUser.getLocale() + ": " + e.getMessage());
143153
}
144-
} catch (IllegalArgumentException e) {
145-
Log.warning(Geonet.SECURITY, "Unable to parse oidc locale " + oidcUser.getLocale() + ": " + e.getMessage());
146154
}
147-
}
148155

149156

150-
// Fire event so that updateTimestampListener can be trigger.
151-
// It may have been triggered at the beginning of the authentication when the user information was not available for new users.
152-
// Firing the event again as the user information now exists.
153-
if (this.eventPublisher != null) {
154-
eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
157+
// Fire event so that updateTimestampListener can be trigger.
158+
// It may have been triggered at the beginning of the authentication when the user information was not available for new users.
159+
// Firing the event again as the user information now exists.
160+
if (this.eventPublisher != null) {
161+
eventPublisher.publishEvent(new InteractiveAuthenticationSuccessEvent(authResult, this.getClass()));
162+
}
163+
164+
} catch (Exception ex) {
165+
Log.warning(Geonet.SECURITY, "Error during OIDC login for user "
166+
+ username + ": " + ex.getMessage(), ex);
155167
}
156168

157169

0 commit comments

Comments
 (0)