2 * Copyright [2006] [University Corporation for Advanced Internet Development, Inc.]
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package edu.internet2.middleware.shibboleth.idp.authn.impl;
19 import java.net.InetAddress;
20 import java.net.UnknownHostException;
21 import java.util.List;
23 import java.util.concurrent.CopyOnWriteArrayList;
25 import javax.servlet.RequestDispatcher;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28 import javax.servlet.http.HttpSession;
29 import javax.servlet.ServletRequest;
31 import edu.internet2.middleware.shibboleth.idp.authn.AuthenticationHandler;
32 import edu.internet2.middleware.shibboleth.idp.authn.LoginContext;
34 import org.apache.log4j.Logger;
36 import org.joda.time.DateTime;
39 * IP Address authentication handler.
41 * This "authenticates" a user based on their IP address. It operates in either
42 * default deny or default allow mode, and evaluates a given request against a
43 * list of blocked or permitted IPs. It supports both IPv4 and IPv6.
45 public class IPAddressHandler implements AuthenticationHandler {
47 private static final Logger log = Logger.getLogger(IPAddressHandler.class
50 /** the URI of the AuthnContextDeclRef or the AuthnContextClass */
51 private String authnMethodURI;
53 /** The return location */
54 private String returnLocation;
56 /** Are the IPs in ipList a permitted list or a deny list */
57 private boolean defaultDeny;
59 /** The list of denied or permitted IPs */
60 private List<InetAddress> ipList;
62 /** Creates a new instance of IPAddressHandler */
63 public IPAddressHandler() {
67 * Set the permitted IP addresses.
69 * If <code>defaultDeny</code> is <code>true</code> then only the IP
70 * addresses in <code>ipList</code> will be "authenticated." If
71 * <code>defaultDeny</code> is <code>false</code>, then all IP
72 * addresses except those in <code>ipList</code> will be authenticated.
75 * A list of {@link InetAddress}es.
77 * Does <code>ipList</code> contain a deny or permit list.
79 public void setIpList(final List<InetAddress> ipList, boolean defaultDeny) {
81 this.ipList = new CopyOnWriteArrayList(ipList);
82 this.defaultDeny = defaultDeny;
86 public void setReturnLocation(String location) {
87 this.returnLocation = location;
91 public boolean supportsPassive() {
96 public boolean supportsForceAuthentication() {
101 public void logout(HttpServletRequest request,
102 HttpServletResponse response, String principal) {
104 RequestDispatcher dispatcher = request
105 .getRequestDispatcher(returnLocation);
106 // dispatcher.forward(request, response);
110 public void login(HttpServletRequest request, HttpServletResponse response,
111 LoginContext loginCtx) {
113 loginContext.setAuthenticationAttempted();
114 loginContext.setAuthenticationInstant(new DateTime());
117 handleDefaultDeny(request, response, loginContext);
119 handleDefaultAllow(request, response, loginContext);
123 protected void handleDefaultDeny(HttpServletRequest request,
124 HttpServletResponse response, LoginContext loginCtx) {
126 boolean ipAllowed = searchIpList(request);
129 loginCtx.setAuthenticationOK(true);
131 loginCtx.setAuthenticationOK(false);
133 .setAuthenticationFailureMessage("User's IP is not in the permitted list.");
137 protected void handleDefaultAllow(HttpServletRequest request,
138 HttpServletResponse response, LoginContext loginCtx) {
140 boolean ipDenied = searchIpList(request);
143 loginCtx.setAuthenticationOK(false);
145 .setAuthenticationFailureMessage("Users's IP is in the deny list.");
147 loginCtx.setAuthenticationOK(true);
152 * Search the list of InetAddresses for the client's address.
157 * @return <code>true</code> if the client's address is in
158 * <code>this.ipList</code>
160 private boolean searchIpList(final ServletRequest request) {
162 boolean found = false;
165 InetAddress[] addrs = InetAddress.getAllByName(request
167 for (InetAddress a : addrs) {
168 if (ipList.contains(a)) {
173 } catch (UnknownHostException ex) {
174 log.error("Error resolving hostname: ", ex);