16
16
import org .apache .commons .lang .builder .ToStringBuilder ;
17
17
import org .apache .http .annotation .Contract ;
18
18
import org .apache .http .annotation .ThreadingBehavior ;
19
+ import org .apache .solr .common .SolrException ;
20
+ import org .apache .solr .common .SolrException .ErrorCode ;
19
21
import org .apache .solr .security .AuthenticationPlugin ;
22
+ import org .apache .solr .security .BasicAuthPlugin ;
20
23
21
24
public class ForwardAuthPlugin extends AuthenticationPlugin {
22
25
23
26
public static final String ARG_USER_HEADER = "httpUserHeader" ;
24
27
public static final String HTTP_HEADER_USER_DEFAULT = "X-Forwarded-User" ;
28
+ public static final String X_REQUESTED_WITH_HEADER = "X-Requested-With" ;
25
29
26
30
private String httpUserHeader ;
31
+ private boolean blockUnknown = false ;
27
32
28
33
@ Override
29
34
public void init (Map <String , Object > args ) {
30
35
this .httpUserHeader = (String ) args .getOrDefault (ARG_USER_HEADER , HTTP_HEADER_USER_DEFAULT );
36
+
37
+ Object o = args .get (BasicAuthPlugin .PROPERTY_BLOCK_UNKNOWN );
38
+ if (o != null ) {
39
+ try {
40
+ blockUnknown = Boolean .parseBoolean (o .toString ());
41
+ } catch (Exception e ) {
42
+ throw new SolrException (ErrorCode .BAD_REQUEST ,
43
+ "Invalid value for parameter " + BasicAuthPlugin .PROPERTY_BLOCK_UNKNOWN );
44
+ }
45
+ }
31
46
}
32
47
33
48
@ Override
34
- public boolean doAuthenticate (ServletRequest servletRequest , ServletResponse servletResponse , FilterChain filterChain )
49
+ public boolean doAuthenticate (ServletRequest servletRequest , ServletResponse servletResponse ,
50
+ FilterChain filterChain )
35
51
throws Exception {
36
52
HttpServletRequest request = (HttpServletRequest ) servletRequest ;
37
53
HttpServletResponse response = (HttpServletResponse ) servletResponse ;
@@ -47,9 +63,14 @@ public Principal getUserPrincipal() {
47
63
numAuthenticated .inc ();
48
64
filterChain .doFilter (wrapper , response );
49
65
return true ;
66
+ } else if (blockUnknown ) {
67
+ numMissingCredentials .inc ();
68
+ return false ;
69
+ } else {
70
+ numPassThrough .inc ();
71
+ filterChain .doFilter (request , response );
72
+ return true ;
50
73
}
51
-
52
- return false ;
53
74
}
54
75
55
76
@ Contract (threading = ThreadingBehavior .IMMUTABLE )
@@ -76,7 +97,7 @@ public boolean equals(Object o) {
76
97
return true ;
77
98
if (o == null || getClass () != o .getClass ())
78
99
return false ;
79
- ForwardAuthUserPrincipal that = (ForwardAuthUserPrincipal ) o ;
100
+ ForwardAuthUserPrincipal that = (ForwardAuthUserPrincipal ) o ;
80
101
return Objects .equals (username , that .username );
81
102
}
82
103
0 commit comments