annotate src/goodjava/webserver/handlers/BasicAuthHandler.java @ 1708:8d257d56dc42

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 04 Jul 2022 14:06:59 -0600
parents 557bb90b70d7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1608
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.webserver.handlers;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import goodjava.util.GoodUtils;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 import goodjava.webserver.Handler;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import goodjava.webserver.Request;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import goodjava.webserver.Response;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import goodjava.webserver.Status;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
1609
268b2a26e8d7 minor - cors
Franklin Schmidt <fschmidt@gmail.com>
parents: 1608
diff changeset
10 public final class BasicAuthHandler implements Handler {
1608
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 private final Handler handler;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 private final String realm;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 private final String match;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 public BasicAuthHandler(Handler handler,String realm,String username,String password) {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16 this.handler = handler;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 this.realm = realm;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 this.match = GoodUtils.base64Encode(username+":"+password);
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 }
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 private Response unauthorized() {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 Response response = new Response();
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 response.status = Status.UNAUTHORIZED;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 response.headers.put("WWW-Authenticate","Basic realm=\""+realm+"\"");
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 return response;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 }
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 public Response handle(Request request) {
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 String auth = (String)request.headers.get("Authorization");
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 if( auth==null )
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 return unauthorized();
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 String[] a = auth.split(" ");
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 if( a.length!=2 || !a[0].equals("Basic") || !a[1].equals(match) )
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 return unauthorized();
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 Response response = handler.handle(request);
1614
557bb90b70d7 fix BasicAuthHandler bug
Franklin Schmidt <fschmidt@gmail.com>
parents: 1609
diff changeset
36 if( response != null )
557bb90b70d7 fix BasicAuthHandler bug
Franklin Schmidt <fschmidt@gmail.com>
parents: 1609
diff changeset
37 response.headers.put("X-Accel-Expires","0");
1608
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 return response;
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 }
f7e3adae4907 add BasicAuthHandler
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 }