1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 package org.melati.login;
47
48 import java.io.IOException;
49 import javax.servlet.http.HttpServletResponse;
50 import org.melati.poem.AccessPoemException;
51 import org.melati.poem.PoemThread;
52 import org.melati.poem.User;
53 import org.melati.Melati;
54 import org.melati.util.MelatiRuntimeException;
55 import org.melati.util.StringUtils;
56 import org.melati.util.UnexpectedExceptionException;
57
58 /**
59 * Flags up when something was illegal or not supported about an incoming HTTP
60 * authorization.
61 */
62 class HttpAuthorizationMelatiException extends MelatiRuntimeException {
63
64 private static final long serialVersionUID = 1L;
65
66 /**
67 * Constructor.
68 *
69 * @param message the message to pass up to super constructor
70 */
71 public HttpAuthorizationMelatiException(String message) {
72 super(message, null);
73 }
74 }
75
76
77 /**
78 * An {@link AccessHandler} which uses the HTTP Basic Authentication scheme to
79 * elicit and maintain the user's login and password.
80 *
81 * This implementation doesn't use the servlet session at all,
82 * so it doesn't try to send cookies or
83 * do URL rewriting.
84 *
85 */
86 public class HttpBasicAuthenticationAccessHandler implements AccessHandler {
87 private static final String className =
88 new HttpBasicAuthenticationAccessHandler().getClass().getName();
89
90 final String REALM = className + ".realm";
91 final String USER = className + ".user";
92
93 /**
94 * Change here to use session, if that makes sense.
95 * @return false
96 */
97 protected boolean useSession() {
98 return false;
99 }
100
101 /**
102 * Force a login by sending a 401 error back to the browser.
103 *
104 * HACK Apache/Netscape appear not to do anything with message, which is
105 * why it's just left as a String.
106 */
107 protected void forceLogin(HttpServletResponse resp,
108 String realm, String message) {
109 String desc = realm == null ? "<unknown>"
110 : StringUtils.tr(realm, '"', ' ');
111 resp.setHeader("WWW-Authenticate", "Basic realm=\"" + desc + "\"");
112
113
114 try {
115 resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, message);
116 } catch (IOException e) {
117 e.printStackTrace(System.err);
118 throw new UnexpectedExceptionException(e);
119 }
120 }
121
122 /**
123 * Called when an AccessPoemException is trapped.
124 *
125 * @param melati the Melati
126 * @param accessException the particular access exception to handle
127 * {@inheritDoc}
128 * @see org.melati.login.AccessHandler#
129 * handleAccessException(org.melati.Melati,
130 * org.melati.poem.AccessPoemException)
131 */
132 public void handleAccessException(Melati melati,
133 AccessPoemException accessException)
134 throws Exception {
135 String capName = "melati";
136 if (useSession())
137 melati.getSession().setAttribute(REALM, capName);
138 forceLogin(melati.getResponse(), capName, accessException.getMessage());
139 }
140
141 /**
142 * Get the users details.
143 *
144 * {@inheritDoc}
145 * @see org.melati.login.AccessHandler#establishUser(org.melati.Melati)
146 */
147 public Melati establishUser(Melati melati) {
148
149 HttpAuthorization auth = HttpAuthorization.from(melati.getRequest());
150
151 if (auth == null) {
152
153
154 PoemThread.setAccessToken(melati.getDatabase().guestAccessToken());
155 return melati;
156 }
157 else {
158
159
160
161
162
163 User sessionUser =
164 useSession() ? (User)melati.getSession().getAttribute(USER) : null;
165 User user = null;
166
167 if (sessionUser == null ||
168 !sessionUser.getLogin().equals(auth.username))
169 user = (User)melati.getDatabase().getUserTable().getLoginColumn().
170 firstWhereEq(auth.username);
171 else
172 user = sessionUser;
173
174 if (user == null || !user.getPassword_unsafe().equals(auth.password)) {
175
176
177
178
179
180 String storedRealm;
181 if (useSession() &&
182 (storedRealm = (String)melati.getSession().getAttribute(REALM))
183 != null) {
184
185
186
187 forceLogin(melati.getResponse(), storedRealm,
188 "Login/password not recognised");
189 return null;
190 }
191 else {
192
193
194
195
196
197
198 PoemThread.setAccessToken(melati.getDatabase().guestAccessToken());
199 return melati;
200 }
201 }
202 else {
203
204
205
206 PoemThread.setAccessToken(user);
207
208 if (useSession() && user != sessionUser)
209 melati.getSession().setAttribute(USER, user);
210
211 return melati;
212 }
213 }
214 }
215
216 /**
217 * If we are allowed in then no need to change request.
218 *
219 * {@inheritDoc}
220 * @see org.melati.login.AccessHandler#buildRequest(org.melati.Melati)
221 */
222 public void buildRequest(Melati melati)
223 throws IOException {
224 }
225 }