1
|
1 /* AuthOps -- Authentication class implementation
|
|
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.util.List;
|
|
24 import java.util.ListIterator;
|
|
25
|
1
|
26 import javax.servlet.http.HttpServletRequest;
|
|
27
|
181
|
28 import org.apache.log4j.Logger;
|
|
29
|
|
30 import digilib.Utils;
|
73
|
31 import digilib.servlet.DigilibRequest;
|
1
|
32
|
73
|
33 /** Basic implementation of AuthOps interface.
|
|
34 *
|
|
35 * Provides basic implementations. Only rolesForPath needs to be implemented
|
|
36 * by specific implementations.
|
|
37 */
|
1
|
38 public abstract class AuthOpsImpl implements AuthOps {
|
|
39
|
181
|
40 /** general logger for this class */
|
|
41 protected Logger logger = Logger.getLogger(this.getClass());
|
|
42
|
73
|
43 /** Default constructor. */
|
1
|
44 public AuthOpsImpl() {
|
|
45 try {
|
|
46 init();
|
|
47 } catch (AuthOpException e) {
|
|
48 }
|
|
49 }
|
|
50
|
73
|
51 /** Constructor taking an utils object.
|
|
52 * @param u utils object.
|
|
53 */
|
1
|
54 public AuthOpsImpl(Utils u) {
|
|
55 try {
|
|
56 init();
|
|
57 } catch (AuthOpException e) {
|
|
58 }
|
|
59 }
|
|
60
|
73
|
61 /** Test if the request is allowed to access filepath.
|
|
62 * @param filepath filepath to be acessed.
|
|
63 * @param request Request with user information.
|
|
64 * @throws AuthOpException Exception thrown on error.
|
|
65 * @return true if the request is allowed.
|
|
66 */
|
1
|
67 public boolean isAuthRequired(String filepath, HttpServletRequest request) throws AuthOpException {
|
|
68 // check permissions
|
|
69 List rolesRequired = rolesForPath(filepath, request);
|
|
70 return (rolesRequired != null);
|
|
71 }
|
|
72
|
73
|
73 /**
|
|
74 * @see digilib.auth.AuthOps#isAuthRequired(digilib.servlet.DigilibRequest)
|
|
75 */
|
|
76 public boolean isAuthRequired(DigilibRequest request)
|
|
77 throws AuthOpException {
|
|
78 // check permissions
|
|
79 List rolesRequired = rolesForPath(request);
|
|
80 return (rolesRequired != null);
|
|
81 }
|
|
82
|
|
83 /** Return authorization roles needed for request.
|
|
84 *
|
|
85 * Returns a list of authorization roles that would be allowed to access the
|
|
86 * specified path. The location information of the request is considered also.
|
|
87 * @param filepath filepath to be accessed.
|
|
88 * @param request ServletRequest with address information.
|
|
89 * @throws AuthOpException Exception thrown on error.
|
|
90 * @return List of Strings with role names.
|
|
91 */
|
1
|
92 public boolean isAuthorized(String filepath, HttpServletRequest request) throws AuthOpException {
|
|
93 List rolesAllowed = rolesForPath(filepath, request);
|
|
94 return isRoleAuthorized(rolesAllowed, request);
|
|
95 }
|
|
96
|
73
|
97 /**
|
|
98 * @see digilib.auth.AuthOps#isAuthorized(digilib.servlet.DigilibRequest)
|
|
99 */
|
|
100 public boolean isAuthorized(DigilibRequest request)
|
|
101 throws AuthOpException {
|
|
102 List rolesAllowed = rolesForPath(request);
|
|
103 return isRoleAuthorized(rolesAllowed, request);
|
|
104 }
|
|
105
|
|
106 /** Test request authorization against a list of roles.
|
|
107 * @param roles List of Strings with role names.
|
|
108 * @param request ServletRequest with address information.
|
|
109 * @return true if the user information in the request authorizes one of the roles.
|
|
110 */
|
1
|
111 public boolean isRoleAuthorized(List roles, HttpServletRequest request) {
|
|
112 ListIterator r = roles.listIterator();
|
|
113 String s = "";
|
|
114 while (r.hasNext()) {
|
|
115 s = (String)r.next();
|
181
|
116 logger.debug("Testing role: "+s);
|
1
|
117 if (request.isUserInRole(s)) {
|
181
|
118 logger.debug("Role Authorized");
|
1
|
119 return true;
|
|
120 }
|
|
121 }
|
|
122 return false;
|
|
123 }
|
|
124
|
73
|
125 /**
|
|
126 * @see digilib.auth.AuthOps#isRoleAuthorized(java.util.List, digilib.servlet.DigilibRequest)
|
|
127 */
|
|
128 public boolean isRoleAuthorized(List roles, DigilibRequest request) {
|
|
129 ListIterator r = roles.listIterator();
|
|
130 String s = "";
|
|
131 while (r.hasNext()) {
|
|
132 s = (String)r.next();
|
181
|
133 logger.debug("Testing role: "+s);
|
73
|
134 if (((HttpServletRequest)request.getServletRequest()).isUserInRole(s)) {
|
181
|
135 logger.debug("Role Authorized");
|
73
|
136 return true;
|
|
137 }
|
|
138 }
|
|
139 return false;
|
|
140 }
|
|
141
|
1
|
142 public abstract void init() throws AuthOpException;
|
|
143
|
|
144 public abstract List rolesForPath(String filepath, HttpServletRequest request) throws AuthOpException;
|
|
145
|
73
|
146 public abstract List rolesForPath(DigilibRequest request) throws AuthOpException;
|
|
147
|
1
|
148 }
|