1
|
1 /* XMLAuthOps -- Authentication class implementation using XML files
|
|
2
|
|
3 Digital Image Library servlet components
|
|
4
|
|
5 Copyright (C) 2001, 2002 Robert Casties (robcast@mail.berlios.de)
|
|
6
|
|
7 This program is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2 of the License, or (at your
|
|
10 option) any later version.
|
|
11
|
|
12 Please read license.txt for the full details. A copy of the GPL
|
|
13 may be found at http://www.gnu.org/copyleft/lgpl.html
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with this program; if not, write to the Free Software
|
|
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
18
|
|
19 */
|
|
20
|
|
21 package digilib.auth;
|
|
22
|
181
|
23 import java.io.File;
|
|
24 import java.util.List;
|
269
|
25 import java.util.Map;
|
181
|
26
|
1
|
27 import javax.servlet.http.HttpServletRequest;
|
|
28
|
181
|
29 import digilib.io.XMLListLoader;
|
73
|
30 import digilib.servlet.DigilibRequest;
|
1
|
31
|
73
|
32 /** Implementation of AuthOps using XML files.
|
|
33 *
|
|
34 * The configuration file is read by an XMLListLoader into HashTree objects for
|
|
35 * authentication paths and IP numbers.
|
|
36 */
|
1
|
37 public class XMLAuthOps extends AuthOpsImpl {
|
|
38
|
259
|
39 private File configFile;
|
73
|
40 private HashTree authPaths;
|
|
41 private HashTree authIPs;
|
|
42
|
259
|
43 /** Constructor taking an XML config file.
|
73
|
44 *
|
|
45 * @param u utils object
|
259
|
46 * @param confFile Configuration file.
|
73
|
47 * @throws AuthOpException Exception thrown on error.
|
|
48 */
|
259
|
49 public XMLAuthOps(File confFile) throws AuthOpException {
|
73
|
50 configFile = confFile;
|
|
51 init();
|
|
52 }
|
|
53
|
259
|
54 /** Set configuration file.
|
73
|
55 *
|
259
|
56 * @param confFile XML config file.
|
73
|
57 * @throws AuthOpException Exception thrown on error.
|
|
58 */
|
259
|
59 public void setConfig(File confFile) throws AuthOpException {
|
73
|
60 configFile = confFile;
|
|
61 init();
|
|
62 }
|
1
|
63
|
73
|
64 /** Initialize.
|
|
65 *
|
|
66 * Read configuration files and setup authentication arrays.
|
|
67 *
|
|
68 * @throws AuthOpException Exception thrown on error.
|
|
69 */
|
|
70 public void init() throws AuthOpException {
|
181
|
71 logger.debug("xmlauthops.init (" + configFile + ")");
|
531
|
72 Map<String, String> pathList = null;
|
|
73 Map<String, String> ipList = null;
|
73
|
74 try {
|
|
75 // load authPaths
|
|
76 XMLListLoader pathLoader =
|
|
77 new XMLListLoader("digilib-paths", "path", "name", "role");
|
259
|
78 pathList = pathLoader.loadURL(configFile.toURL().toString());
|
73
|
79 // load authIPs
|
|
80 XMLListLoader ipLoader =
|
|
81 new XMLListLoader("digilib-addresses", "address", "ip", "role");
|
259
|
82 ipList = ipLoader.loadURL(configFile.toURL().toString());
|
73
|
83 } catch (Exception e) {
|
|
84 throw new AuthOpException(
|
|
85 "ERROR loading authorization config file: " + e);
|
|
86 }
|
|
87 if ((pathList == null) || (ipList == null)) {
|
|
88 throw new AuthOpException("ERROR unable to load authorization config file!");
|
|
89 }
|
|
90 // setup path tree
|
|
91 authPaths = new HashTree(pathList, "/", ",");
|
|
92 // setup ip tree
|
|
93 authIPs = new HashTree(ipList, ".", ",");
|
|
94 }
|
1
|
95
|
73
|
96 /** Return authorization roles needed for request.
|
|
97 *
|
|
98 * Returns the list of authorization roles that are needed to access the
|
|
99 * specified path. No list means the path is free.
|
|
100 *
|
|
101 * The location information of the request is also considered.
|
|
102 *
|
|
103 * @param filepath filepath to be accessed.
|
|
104 * @param request ServletRequest with address information.
|
|
105 * @throws AuthOpException Exception thrown on error.
|
|
106 * @return List of Strings with role names.
|
|
107 */
|
531
|
108 public List<String> rolesForPath(String filepath, HttpServletRequest request)
|
73
|
109 throws digilib.auth.AuthOpException {
|
181
|
110 logger.debug("rolesForPath ("
|
73
|
111 + filepath
|
|
112 + ") by ["
|
|
113 + request.getRemoteAddr()
|
|
114 + "]");
|
1
|
115
|
73
|
116 // check if the requests address provides a role
|
531
|
117 List<String> provided = authIPs.match(request.getRemoteAddr());
|
73
|
118 if ((provided != null) && (provided.contains("ALL"))) {
|
|
119 // ALL switches off checking;
|
|
120 return null;
|
|
121 }
|
|
122 // which roles are required?
|
531
|
123 List<String> required = authPaths.match(filepath);
|
73
|
124 // do any provided roles match?
|
|
125 if ((provided != null) && (required != null)) {
|
|
126 for (int i = 0; i < provided.size(); i++) {
|
|
127 if (required.contains(provided.get(i))) {
|
|
128 // satisfied
|
|
129 return null;
|
|
130 }
|
|
131 }
|
|
132 }
|
|
133 return required;
|
|
134 }
|
1
|
135
|
73
|
136 /**
|
|
137 * @see digilib.auth.AuthOps#rolesForPath(digilib.servlet.DigilibRequest)
|
|
138 */
|
531
|
139 public List<String> rolesForPath(DigilibRequest request) throws AuthOpException {
|
181
|
140 logger.debug("rolesForPath ("
|
73
|
141 + request.getFilePath()
|
|
142 + ") by ["
|
|
143 + request.getServletRequest().getRemoteAddr()
|
|
144 + "]");
|
|
145
|
|
146 // check if the requests address provides a role
|
531
|
147 List<String> provided =
|
73
|
148 authIPs.match(request.getServletRequest().getRemoteAddr());
|
|
149 if ((provided != null) && (provided.contains("ALL"))) {
|
|
150 // ALL switches off checking;
|
|
151 return null;
|
|
152 }
|
|
153 // which roles are required?
|
531
|
154 List<String> required = authPaths.match(request.getFilePath());
|
73
|
155 // do any provided roles match?
|
|
156 if ((provided != null) && (required != null)) {
|
|
157 for (int i = 0; i < provided.size(); i++) {
|
|
158 if (required.contains(provided.get(i))) {
|
|
159 // satisfied
|
|
160 return null;
|
|
161 }
|
|
162 }
|
|
163 }
|
|
164 return required;
|
|
165 }
|
1
|
166
|
|
167 }
|