77import javax .inject .Provider ;
88import javax .servlet .ServletContext ;
99import javax .servlet .http .HttpServletRequest ;
10+ import javax .ws .rs .ForbiddenException ;
1011import javax .ws .rs .NotAuthorizedException ;
1112import javax .ws .rs .container .ContainerRequestContext ;
1213import javax .ws .rs .container .ContainerRequestFilter ;
@@ -57,6 +58,26 @@ public class CheckLoginFilter implements ContainerRequestFilter, ContainerRespon
5758 @ Inject
5859 protected Provider <Request > _grizzlyRequest ;
5960
61+ /*************** The following methods control the default behavior for WDK endpoints ************/
62+
63+ // override and add paths to this list if no authentication is required AND'
64+ // no guest user should be created for this request
65+ protected boolean isPathToSkip (String path ) {
66+ // skip user check for prometheus metrics requests
67+ return SystemService .PROMETHEUS_ENDPOINT_PATH .equals (path );
68+ }
69+
70+ // override and add paths to this list if valid token is required (no guest will be created)
71+ protected boolean isValidTokenRequired (String path ) {
72+ return false ;
73+ }
74+
75+ // authentication is required AND
76+ // if token is absent or expired, create new guest to use for this request
77+ protected boolean isGuestUserAllowed (String path ) {
78+ return true ;
79+ }
80+
6081 @ Override
6182 public void filter (ContainerRequestContext requestContext ) throws IOException {
6283 // skip endpoints which do not require a user; prevents guests from being unnecessarily created
@@ -72,16 +93,32 @@ public void filter(ContainerRequestContext requestContext) throws IOException {
7293
7394 try {
7495 if (rawToken == null ) {
75- // no credentials submitted; automatically create a guest to use on this request
76- useNewGuest (factory , request , requestContext , requestPath );
96+ // no credentials submitted; check requirements of this path
97+ if (isValidTokenRequired (requestPath )) {
98+ LOG .warn ("Did not received bearer token as required for path:" + requestPath );
99+ throw new NotAuthorizedException (Response .status (Status .UNAUTHORIZED ).build ());
100+ }
101+ // if allowed, automatically create a guest to use on this request
102+ if (isGuestUserAllowed (requestPath )) {
103+ useNewGuest (factory , request , requestContext , requestPath );
104+ return ;
105+ }
106+ // no authentication provided, and guests are disallowed
107+ throw new NotAuthorizedException (Response .status (Status .UNAUTHORIZED ).build ());
77108 }
78109 else {
79110 try {
80111 // validate submitted token
81112 ValidatedToken token = factory .validateBearerToken (rawToken );
82113 User user = factory .convertToUser (token );
83- setRequestAttributes (request , token , user );
84- LOG .info ("Validated successfully. Request will be processed for user " + user .getUserId ());
114+ if (isGuestUserAllowed (requestPath )) {
115+ setRequestAttributes (request , token , user );
116+ LOG .info ("Validated successfully. Request will be processed for user " + user .getUserId ());
117+ }
118+ else {
119+ // valid guest token submitted, but guests disallowed for this path
120+ throw new ForbiddenException ();
121+ }
85122 }
86123 catch (ExpiredTokenException e ) {
87124 // token is expired; use guest token for now which should inspire them to log back in
@@ -131,11 +168,6 @@ private String findRawBearerToken(RequestData request, ContainerRequestContext r
131168 return cookie == null ? null : cookie .getValue ();
132169 }
133170
134- protected boolean isPathToSkip (String path ) {
135- // skip user check for prometheus metrics requests
136- return SystemService .PROMETHEUS_ENDPOINT_PATH .equals (path );
137- }
138-
139171 @ Override
140172 public void filter (ContainerRequestContext requestContext , ContainerResponseContext responseContext )
141173 throws IOException {
0 commit comments