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