@@ -136,25 +136,24 @@ private void filter(final HttpServletRequest request,
136
136
chain .doFilter (request , response );
137
137
} else if (isStaticResource (fullPath , servletPath , servletName )) {
138
138
chain .doFilter (request , response );
139
+ } else if (shouldBypassAuthentication (request , fullPath , servletPath , servletName )) {
140
+ LOGGER .debug ("Running as proc user for unauthenticated resource, servletName: {}, " +
141
+ "fullPath: {}, servletPath: {}" , servletName , fullPath , servletPath );
142
+ // Some paths don't need authentication. If that is the case then proceed as proc user.
143
+ securityContext .asProcessingUser (() ->
144
+ process (request , response , chain ));
139
145
} else {
140
146
// Api requests that are not from the front-end should have a token.
141
147
// Also request from an AWS ALB will have an ALB signed token containing the claims
142
148
// Need to do this first, so we get a fresh token from AWS ALB rather than using a stale
143
149
// one from session.
144
150
Optional <UserIdentity > optUserIdentity = openIdManager .loginWithRequestToken (request );
145
-
146
- // Log current user.
147
- if (LOGGER .isDebugEnabled ()) {
148
- logUserIdentityToDebug (
149
- optUserIdentity , fullPath , "after trying to login with request token" );
150
- }
151
+ logUserIdentityToDebug (optUserIdentity , fullPath , servletPath , "from request token" );
151
152
152
153
// If no user from header token, see if we have one in session already.
153
154
if (optUserIdentity .isEmpty ()) {
154
155
optUserIdentity = UserIdentitySessionUtil .get (SessionUtil .getExistingSession (request ));
155
- if (LOGGER .isDebugEnabled ()) {
156
- logUserIdentityToDebug (optUserIdentity , fullPath , "from session" );
157
- }
156
+ logUserIdentityToDebug (optUserIdentity , fullPath , servletPath , "from session" );
158
157
}
159
158
160
159
if (optUserIdentity .isPresent ()) {
@@ -163,19 +162,20 @@ private void filter(final HttpServletRequest request,
163
162
// Now we have the session make note of the user-agent for logging and sessionListServlet duties
164
163
UserAgentSessionUtil .setUserAgentInSession (request );
165
164
165
+ // If OIDC code flow has been handled by the AWS ALB then the session won't have been
166
+ // created by our code flow code. Thus, ensure we have a session with the user in it
167
+ if (isStroomUIServlet (servletName )) {
168
+ SessionUtil .getOrCreateSession (request , session -> {
169
+ LOGGER .info ("Creating session {} for user {}, fullPath: {}, servlet: {}" ,
170
+ session .getId (), userIdentity , fullPath , servletName );
171
+ UserIdentitySessionUtil .set (session , userIdentity );
172
+ });
173
+ }
174
+
166
175
// Now handle the request as this user
167
176
securityContext .asUser (userIdentity , () ->
168
177
process (request , response , chain ));
169
-
170
- } else if (shouldBypassAuthentication (request , fullPath , servletPath , servletName )) {
171
- LOGGER .debug ("Running as proc user for unauthenticated servletName: {}, " +
172
- "fullPath: {}, servletPath: {}" , servletName , fullPath , servletPath );
173
- // Some paths don't need authentication. If that is the case then proceed as proc user.
174
- securityContext .asProcessingUser (() ->
175
- process (request , response , chain ));
176
-
177
- // } else if (isApiRequest(servletPath)) {
178
- } else if (Objects .equals (ResourcePaths .STROOM_SERVLET_NAME , servletName )) {
178
+ } else if (isStroomUIServlet (servletName )) {
179
179
doOpenIdFlow (request , response , fullPath );
180
180
} else {
181
181
// If we couldn't log in with a token or couldn't get a token then error as this is an API call
@@ -187,6 +187,10 @@ private void filter(final HttpServletRequest request,
187
187
}
188
188
}
189
189
190
+ private boolean isStroomUIServlet (final String servletName ) {
191
+ return Objects .equals (ResourcePaths .STROOM_SERVLET_NAME , servletName );
192
+ }
193
+
190
194
private void doOpenIdFlow (final HttpServletRequest request ,
191
195
final HttpServletResponse response ,
192
196
final String fullPath ) throws IOException {
@@ -245,8 +249,9 @@ private void doOpenIdFlow(final HttpServletRequest request,
245
249
@ SuppressWarnings ("OptionalUsedAsFieldOrParameterType" )
246
250
private void logUserIdentityToDebug (final Optional <UserIdentity > optUserIdentity ,
247
251
final String fullPath ,
252
+ final String servletName ,
248
253
final String msg ) {
249
- LOGGER .debug ("User identity ({}): {} path : {}" ,
254
+ LOGGER .debug (() -> LogUtil . message ( "User identity ({}): {}, fullPath: {}, servletName : {}" ,
250
255
msg ,
251
256
optUserIdentity .map (
252
257
identity -> {
@@ -258,7 +263,8 @@ private void logUserIdentityToDebug(final Optional<UserIdentity> optUserIdentity
258
263
identity .getClass ().getSimpleName ());
259
264
})
260
265
.orElse ("<empty>" ),
261
- fullPath );
266
+ fullPath ,
267
+ servletName ));
262
268
}
263
269
264
270
private String getPostAuthRedirectUri (final HttpServletRequest request ) {
@@ -298,17 +304,6 @@ private boolean shouldBypassAuthentication(final HttpServletRequest servletReque
298
304
} else {
299
305
shouldBypass = authenticationBypassChecker .isUnauthenticated (servletName , servletPath , fullPath );
300
306
}
301
-
302
- if (LOGGER .isDebugEnabled ()) {
303
- if (shouldBypass ) {
304
- LOGGER .debug ("Bypassing authentication for servletName: {}, fullPath: {}, servletPath: {}" ,
305
- NullSafe .get (
306
- servletRequest .getHttpServletMapping (),
307
- HttpServletMapping ::getServletName ),
308
- fullPath ,
309
- servletPath );
310
- }
311
- }
312
307
return shouldBypass ;
313
308
}
314
309
0 commit comments