1515import java .util .Arrays ;
1616import java .util .Optional ;
1717import java .util .logging .Logger ;
18- import org .apache .commons .lang3 .StringUtils ;
1918
20- import jakarta .annotation .Priority ;
2119import jakarta .ejb .EJB ;
2220import jakarta .inject .Inject ;
2321import jakarta .servlet .Filter ;
2624import jakarta .servlet .ServletException ;
2725import jakarta .servlet .ServletRequest ;
2826import jakarta .servlet .ServletResponse ;
29- import jakarta .servlet .annotation .WebFilter ;
3027import jakarta .servlet .http .HttpServletRequest ;
3128import jakarta .servlet .http .HttpServletResponse ;
3229import jakarta .servlet .http .HttpSession ;
3330import jakarta .ws .rs .HttpMethod ;
3431
3532import com .nimbusds .openid .connect .sdk .Prompt ;
33+ import org .apache .commons .lang3 .Strings ;
3634
37- @ WebFilter ("/*" )
38- @ Priority (100 ) // Lower number means higher priority
3935public class AuthFilter implements Filter {
4036
4137 private static final Logger logger = Logger .getLogger (AuthFilter .class .getCanonicalName ());
@@ -54,14 +50,13 @@ public class AuthFilter implements Filter {
5450 Clock clock ;
5551
5652
57- //QDR setting for the Drupal URL
58- private String drupalUrl ;
53+
5954
6055 @ Override
6156 public void init (FilterConfig filterConfig ) throws ServletException {
6257 logger .fine (AuthFilter .class .getName () + "initialized. filterConfig.getServletContext().getServerInfo(): " + filterConfig .getServletContext ().getServerInfo ());
63- drupalUrl = settingsService . getValueForKey ( SettingsServiceBean . Key . QDRDrupalSiteURL );
64- logger .fine ("Setting Drupal URl to : " + drupalUrl );
58+ //QDR setting for the Drupal URL
59+ logger .fine ("Setting Drupal URl to : " + settingsService . getValueForKey ( SettingsServiceBean . Key . QDRDrupalSiteURL ) );
6560 }
6661
6762 @ Override
@@ -72,7 +67,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse response, Fi
7267 String path = httpServletRequest .getRequestURI ();
7368 String uaHeader = httpServletRequest .getHeader ("user-agent" );
7469 //Nagios uses a user-agent starting with check_http and we don't want to do a passive login check in that case.
75- boolean isCheck = (uaHeader != null ) && (uaHeader .contains ("check_http" ) || StringUtils . containsIgnoreCase (uaHeader , "bot" ) || StringUtils . containsIgnoreCase (uaHeader , "google" ));
70+ boolean isCheck = (uaHeader != null ) && (uaHeader .contains ("check_http" ) || Strings . CI . contains (uaHeader , "bot" ) || Strings . CI . contains (uaHeader , "google" ));
7671 //boolean hasAuthToken = httpServletRequest.getParameter("key") != null) || (httpServletRequest.getParameter("token")!= null) || httpServletRequest.getHeader('X-Dataverse-key');
7772 //~QDR specific - a means to reset the passiveChecked flag so the next access will try passive login again
7873 //If the origin were configurable, this might be useful in general
@@ -94,7 +89,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse response, Fi
9489 httpServletResponse .setStatus (204 );
9590 return ;
9691 }
97- if ((httpServletRequest .getMethod () == HttpMethod .GET ) && !isCheck && (path .equals ("/" ) || path .endsWith (".xhtml" ) && !(path .endsWith ("logout.xhtml" )|| path .endsWith ("privateurl.xhtml" ) || path .contains ("jakarta.faces.resource" ) || path .contains ("/oauth2/callback" )))) {
92+ if ((httpServletRequest .getMethod (). equals ( HttpMethod .GET ) ) && !isCheck && (path .equals ("/" ) || path .endsWith (".xhtml" ) && !(path .endsWith ("logout.xhtml" )|| path .endsWith ("privateurl.xhtml" ) || path .contains ("jakarta.faces.resource" ) || path .contains ("/oauth2/callback" )))) {
9893 logger .fine ("Path: " + path );
9994 String sso = httpServletRequest .getParameter ("sso" );
10095 //Going to /
@@ -130,7 +125,7 @@ public void doFilter(ServletRequest servletRequest, ServletResponse response, Fi
130125
131126 StringBuilder sb = new StringBuilder ();
132127 for (String string : Arrays .asList (remoteAddr , requestUri , userAgent )) {
133- sb .append (string + separator );
128+ sb .append (string ). append ( separator );
134129 }
135130
136131 logger .fine (sb .toString ());
@@ -153,9 +148,9 @@ public void destroy() {
153148 * Create a randomized unique state string to be used while crafting the
154149 * authorization request
155150 *
156- * @param idp
157- * @param redirectPage
158- * @return Random state string, composed from system time, random numbers and
151+ * @param idp the Oauth2 provider
152+ * @param redirectPage where redirects should go
153+ * @return Random state string, created from system time, random numbers, and
159154 * redirectPage parameter
160155 */
161156 private String createState (AbstractOAuth2AuthenticationProvider idp , Optional <String > redirectPage ) {
@@ -169,8 +164,7 @@ private String createState(AbstractOAuth2AuthenticationProvider idp, Optional<St
169164 + redirectPage .map (page -> "~" + page ).orElse ("" );
170165
171166 String encrypted = StringUtil .encrypt (base , idp .getClientSecret ());
172- final String state = idp .getId () + "~" + encrypted ;
173- return state ;
167+ return idp .getId () + "~" + encrypted ;
174168 }
175169
176170}
0 commit comments