< prev index next >

src/share/classes/com/sun/net/httpserver/BasicAuthenticator.java

Print this page
rev 1564 : 7090158: Networking Libraries don't build with javac -Werror
7125055: ContentHandler.getContent API changed in error
Summary: Minor changes to networking java files to remove warnings
Reviewed-by: chegar, weijun, hawtin, alanb
Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
   1 /*
   2  * Copyright (c) 2006, 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


  41     /**
  42      * Creates a BasicAuthenticator for the given HTTP realm
  43      * @param realm The HTTP Basic authentication realm
  44      * @throws NullPointerException if the realm is an empty string
  45      */
  46     public BasicAuthenticator (String realm) {
  47         this.realm = realm;
  48     }
  49 
  50     /**
  51      * returns the realm this BasicAuthenticator was created with
  52      * @return the authenticator's realm string.
  53      */
  54     public String getRealm () {
  55         return realm;
  56     }
  57 
  58     public Result authenticate (HttpExchange t)
  59     {
  60         HttpContext context = t.getHttpContext();
  61         Headers rmap = (Headers) t.getRequestHeaders();
  62         /*
  63          * look for auth token
  64          */
  65         String auth = rmap.getFirst ("Authorization");
  66         if (auth == null) {
  67             Headers map = (Headers) t.getResponseHeaders();
  68             map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
  69             return new Authenticator.Retry (401);
  70         }
  71         int sp = auth.indexOf (' ');
  72         if (sp == -1 || !auth.substring(0, sp).equals ("Basic")) {
  73             return new Authenticator.Failure (401);
  74         }
  75         byte[] b = Base64.base64ToByteArray (auth.substring(sp+1));
  76         String userpass = new String (b);
  77         int colon = userpass.indexOf (':');
  78         String uname = userpass.substring (0, colon);
  79         String pass = userpass.substring (colon+1);
  80 
  81         if (checkCredentials (uname, pass)) {
  82             return new Authenticator.Success (
  83                 new HttpPrincipal (
  84                     uname, realm
  85                 )
  86             );
  87         } else {
  88             /* reject the request again with 401 */
  89 
  90             Headers map = (Headers) t.getResponseHeaders();
  91             map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
  92             return new Authenticator.Failure(401);
  93         }
  94     }
  95 
  96     /**
  97      * called for each incoming request to verify the
  98      * given name and password in the context of this
  99      * Authenticator's realm. Any caching of credentials
 100      * must be done by the implementation of this method
 101      * @param username the username from the request
 102      * @param password the password from the request
 103      * @return <code>true</code> if the credentials are valid,
 104      *    <code>false</code> otherwise.
 105      */
 106     public abstract boolean checkCredentials (String username, String password);
 107 }
 108 
 109 class Base64 {
 110 


   1 /*
   2  * Copyright (c) 2006, 2011, 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


  41     /**
  42      * Creates a BasicAuthenticator for the given HTTP realm
  43      * @param realm The HTTP Basic authentication realm
  44      * @throws NullPointerException if the realm is an empty string
  45      */
  46     public BasicAuthenticator (String realm) {
  47         this.realm = realm;
  48     }
  49 
  50     /**
  51      * returns the realm this BasicAuthenticator was created with
  52      * @return the authenticator's realm string.
  53      */
  54     public String getRealm () {
  55         return realm;
  56     }
  57 
  58     public Result authenticate (HttpExchange t)
  59     {
  60         HttpContext context = t.getHttpContext();
  61         Headers rmap = t.getRequestHeaders();
  62         /*
  63          * look for auth token
  64          */
  65         String auth = rmap.getFirst ("Authorization");
  66         if (auth == null) {
  67             Headers map = t.getResponseHeaders();
  68             map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
  69             return new Authenticator.Retry (401);
  70         }
  71         int sp = auth.indexOf (' ');
  72         if (sp == -1 || !auth.substring(0, sp).equals ("Basic")) {
  73             return new Authenticator.Failure (401);
  74         }
  75         byte[] b = Base64.base64ToByteArray (auth.substring(sp+1));
  76         String userpass = new String (b);
  77         int colon = userpass.indexOf (':');
  78         String uname = userpass.substring (0, colon);
  79         String pass = userpass.substring (colon+1);
  80 
  81         if (checkCredentials (uname, pass)) {
  82             return new Authenticator.Success (
  83                 new HttpPrincipal (
  84                     uname, realm
  85                 )
  86             );
  87         } else {
  88             /* reject the request again with 401 */
  89 
  90             Headers map = t.getResponseHeaders();
  91             map.set ("WWW-Authenticate", "Basic realm=" + "\""+realm+"\"");
  92             return new Authenticator.Failure(401);
  93         }
  94     }
  95 
  96     /**
  97      * called for each incoming request to verify the
  98      * given name and password in the context of this
  99      * Authenticator's realm. Any caching of credentials
 100      * must be done by the implementation of this method
 101      * @param username the username from the request
 102      * @param password the password from the request
 103      * @return <code>true</code> if the credentials are valid,
 104      *    <code>false</code> otherwise.
 105      */
 106     public abstract boolean checkCredentials (String username, String password);
 107 }
 108 
 109 class Base64 {
 110 


< prev index next >