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