1/*
2 * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.net.httpserver;
27
28import java.util.Base64;
29
30/**
31 * BasicAuthenticator provides an implementation of HTTP Basic
32 * authentication. It is an abstract class and must be extended
33 * to provide an implementation of {@link #checkCredentials(String,String)}
34 * which is called to verify each incoming request.
35 */
36public abstract class BasicAuthenticator extends Authenticator {
37
38    protected String realm;
39
40    /**
41     * Creates a BasicAuthenticator for the given HTTP realm
42     * @param realm The HTTP Basic authentication realm
43     * @throws NullPointerException if the realm is an empty string
44     */
45    public BasicAuthenticator (String realm) {
46        this.realm = realm;
47    }
48
49    /**
50     * returns the realm this BasicAuthenticator was created with
51     * @return the authenticator's realm string.
52     */
53    public String getRealm () {
54        return realm;
55    }
56
57    public Result authenticate (HttpExchange t)
58    {
59        Headers rmap = t.getRequestHeaders();
60        /*
61         * look for auth token
62         */
63        String auth = rmap.getFirst ("Authorization");
64        if (auth == null) {
65            Headers map = t.getResponseHeaders();
66            map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
67            return new Authenticator.Retry (401);
68        }
69        int sp = auth.indexOf (' ');
70        if (sp == -1 || !auth.substring(0, sp).equals ("Basic")) {
71            return new Authenticator.Failure (401);
72        }
73        byte[] b = Base64.getDecoder().decode(auth.substring(sp+1));
74        String userpass = new String (b);
75        int colon = userpass.indexOf (':');
76        String uname = userpass.substring (0, colon);
77        String pass = userpass.substring (colon+1);
78
79        if (checkCredentials (uname, pass)) {
80            return new Authenticator.Success (
81                new HttpPrincipal (
82                    uname, realm
83                )
84            );
85        } else {
86            /* reject the request again with 401 */
87
88            Headers map = t.getResponseHeaders();
89            map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
90            return new Authenticator.Failure(401);
91        }
92    }
93
94    /**
95     * called for each incoming request to verify the
96     * given name and password in the context of this
97     * Authenticator's realm. Any caching of credentials
98     * must be done by the implementation of this method
99     * @param username the username from the request
100     * @param password the password from the request
101     * @return <code>true</code> if the credentials are valid,
102     *    <code>false</code> otherwise.
103     */
104    public abstract boolean checkCredentials (String username, String password);
105}
106
107