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
|
|
23 import javax.servlet.http.HttpServletRequest;
|
|
24 import java.util.*;
|
|
25 import java.io.*;
|
|
26
|
|
27 import digilib.*;
|
|
28 import digilib.io.*;
|
73
|
29 import digilib.servlet.DigilibRequest;
|
1
|
30
|
73
|
31 /** Implementation of AuthOps using XML files.
|
|
32 *
|
|
33 * The configuration file is read by an XMLListLoader into HashTree objects for
|
|
34 * authentication paths and IP numbers.
|
|
35 */
|
1
|
36 public class XMLAuthOps extends AuthOpsImpl {
|
|
37
|
73
|
38 private String configFile =
|
|
39 "/docuserver/www/digitallibrary/WEB-INF/digilib-auth.xml";
|
|
40 private HashTree authPaths;
|
|
41 private HashTree authIPs;
|
|
42
|
|
43 /** Constructor taking an XML config file name and utils object.
|
|
44 *
|
|
45 * @param u utils object
|
|
46 * @param confFile Configuration file name.
|
|
47 * @throws AuthOpException Exception thrown on error.
|
|
48 */
|
|
49 public XMLAuthOps(Utils u, String confFile) throws AuthOpException {
|
|
50 util = u;
|
|
51 configFile = confFile;
|
|
52 init();
|
|
53 }
|
|
54
|
|
55 /** Set configuration file and utils object.
|
|
56 *
|
|
57 * @param confFile XML config file name.
|
|
58 * @throws AuthOpException Exception thrown on error.
|
|
59 */
|
|
60 public void setConfig(String confFile) throws AuthOpException {
|
|
61 configFile = confFile;
|
|
62 init();
|
|
63 }
|
1
|
64
|
73
|
65 /** Initialize.
|
|
66 *
|
|
67 * Read configuration files and setup authentication arrays.
|
|
68 *
|
|
69 * @throws AuthOpException Exception thrown on error.
|
|
70 */
|
|
71 public void init() throws AuthOpException {
|
|
72 util.dprintln(10, "xmlauthops.init (" + configFile + ")");
|
88
|
73 HashMap pathList = null;
|
|
74 HashMap ipList = null;
|
73
|
75 try {
|
|
76 // create data loader for auth-path file
|
|
77 File confFile = new File(configFile);
|
|
78 // load authPaths
|
|
79 XMLListLoader pathLoader =
|
|
80 new XMLListLoader("digilib-paths", "path", "name", "role");
|
|
81 pathList = pathLoader.loadURL(confFile.toURL().toString());
|
|
82 // load authIPs
|
|
83 XMLListLoader ipLoader =
|
|
84 new XMLListLoader("digilib-addresses", "address", "ip", "role");
|
|
85 ipList = ipLoader.loadURL(confFile.toURL().toString());
|
|
86 } catch (Exception e) {
|
|
87 throw new AuthOpException(
|
|
88 "ERROR loading authorization config file: " + e);
|
|
89 }
|
|
90 if ((pathList == null) || (ipList == null)) {
|
|
91 throw new AuthOpException("ERROR unable to load authorization config file!");
|
|
92 }
|
|
93 // setup path tree
|
|
94 authPaths = new HashTree(pathList, "/", ",");
|
|
95 // setup ip tree
|
|
96 authIPs = new HashTree(ipList, ".", ",");
|
|
97 }
|
1
|
98
|
73
|
99 /** Return authorization roles needed for request.
|
|
100 *
|
|
101 * Returns the list of authorization roles that are needed to access the
|
|
102 * specified path. No list means the path is free.
|
|
103 *
|
|
104 * The location information of the request is also considered.
|
|
105 *
|
|
106 * @param filepath filepath to be accessed.
|
|
107 * @param request ServletRequest with address information.
|
|
108 * @throws AuthOpException Exception thrown on error.
|
|
109 * @return List of Strings with role names.
|
|
110 */
|
|
111 public List rolesForPath(String filepath, HttpServletRequest request)
|
|
112 throws digilib.auth.AuthOpException {
|
|
113 util.dprintln(
|
|
114 4,
|
|
115 "rolesForPath ("
|
|
116 + filepath
|
|
117 + ") by ["
|
|
118 + request.getRemoteAddr()
|
|
119 + "]");
|
1
|
120
|
73
|
121 // check if the requests address provides a role
|
|
122 List provided = authIPs.match(request.getRemoteAddr());
|
|
123 if ((provided != null) && (provided.contains("ALL"))) {
|
|
124 // ALL switches off checking;
|
|
125 return null;
|
|
126 }
|
|
127 // which roles are required?
|
|
128 List required = authPaths.match(filepath);
|
|
129 // do any provided roles match?
|
|
130 if ((provided != null) && (required != null)) {
|
|
131 for (int i = 0; i < provided.size(); i++) {
|
|
132 if (required.contains(provided.get(i))) {
|
|
133 // satisfied
|
|
134 return null;
|
|
135 }
|
|
136 }
|
|
137 }
|
|
138 return required;
|
|
139 }
|
1
|
140
|
73
|
141 /**
|
|
142 * @see digilib.auth.AuthOps#rolesForPath(digilib.servlet.DigilibRequest)
|
|
143 */
|
|
144 public List rolesForPath(DigilibRequest request) throws AuthOpException {
|
|
145 util.dprintln(
|
|
146 4,
|
|
147 "rolesForPath ("
|
|
148 + request.getFilePath()
|
|
149 + ") by ["
|
|
150 + request.getServletRequest().getRemoteAddr()
|
|
151 + "]");
|
|
152
|
|
153 // check if the requests address provides a role
|
|
154 List provided =
|
|
155 authIPs.match(request.getServletRequest().getRemoteAddr());
|
|
156 if ((provided != null) && (provided.contains("ALL"))) {
|
|
157 // ALL switches off checking;
|
|
158 return null;
|
|
159 }
|
|
160 // which roles are required?
|
|
161 List required = authPaths.match(request.getFilePath());
|
|
162 // do any provided roles match?
|
|
163 if ((provided != null) && (required != null)) {
|
|
164 for (int i = 0; i < provided.size(); i++) {
|
|
165 if (required.contains(provided.get(i))) {
|
|
166 // satisfied
|
|
167 return null;
|
|
168 }
|
|
169 }
|
|
170 }
|
|
171 return required;
|
|
172 }
|
1
|
173
|
|
174 }
|