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