Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/net/Authenticator.java
38829 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.net;2627/**28* The class Authenticator represents an object that knows how to obtain29* authentication for a network connection. Usually, it will do this30* by prompting the user for information.31* <p>32* Applications use this class by overriding {@link33* #getPasswordAuthentication()} in a sub-class. This method will34* typically use the various getXXX() accessor methods to get information35* about the entity requesting authentication. It must then acquire a36* username and password either by interacting with the user or through37* some other non-interactive means. The credentials are then returned38* as a {@link PasswordAuthentication} return value.39* <p>40* An instance of this concrete sub-class is then registered41* with the system by calling {@link #setDefault(Authenticator)}.42* When authentication is required, the system will invoke one of the43* requestPasswordAuthentication() methods which in turn will call the44* getPasswordAuthentication() method of the registered object.45* <p>46* All methods that request authentication have a default implementation47* that fails.48*49* @see java.net.Authenticator#setDefault(java.net.Authenticator)50* @see java.net.Authenticator#getPasswordAuthentication()51*52* @author Bill Foote53* @since 1.254*/5556// There are no abstract methods, but to be useful the user must57// subclass.58public abstract59class Authenticator {6061// The system-wide authenticator object. See setDefault().62private static Authenticator theAuthenticator;6364private String requestingHost;65private InetAddress requestingSite;66private int requestingPort;67private String requestingProtocol;68private String requestingPrompt;69private String requestingScheme;70private URL requestingURL;71private RequestorType requestingAuthType;7273/**74* The type of the entity requesting authentication.75*76* @since 1.577*/78public enum RequestorType {79/**80* Entity requesting authentication is a HTTP proxy server.81*/82PROXY,83/**84* Entity requesting authentication is a HTTP origin server.85*/86SERVER87}8889private void reset() {90requestingHost = null;91requestingSite = null;92requestingPort = -1;93requestingProtocol = null;94requestingPrompt = null;95requestingScheme = null;96requestingURL = null;97requestingAuthType = RequestorType.SERVER;98}99100101/**102* Sets the authenticator that will be used by the networking code103* when a proxy or an HTTP server asks for authentication.104* <p>105* First, if there is a security manager, its {@code checkPermission}106* method is called with a107* {@code NetPermission("setDefaultAuthenticator")} permission.108* This may result in a java.lang.SecurityException.109*110* @param a The authenticator to be set. If a is {@code null} then111* any previously set authenticator is removed.112*113* @throws SecurityException114* if a security manager exists and its115* {@code checkPermission} method doesn't allow116* setting the default authenticator.117*118* @see SecurityManager#checkPermission119* @see java.net.NetPermission120*/121public synchronized static void setDefault(Authenticator a) {122SecurityManager sm = System.getSecurityManager();123if (sm != null) {124NetPermission setDefaultPermission125= new NetPermission("setDefaultAuthenticator");126sm.checkPermission(setDefaultPermission);127}128129theAuthenticator = a;130}131132/**133* Ask the authenticator that has been registered with the system134* for a password.135* <p>136* First, if there is a security manager, its {@code checkPermission}137* method is called with a138* {@code NetPermission("requestPasswordAuthentication")} permission.139* This may result in a java.lang.SecurityException.140*141* @param addr The InetAddress of the site requesting authorization,142* or null if not known.143* @param port the port for the requested connection144* @param protocol The protocol that's requesting the connection145* ({@link java.net.Authenticator#getRequestingProtocol()})146* @param prompt A prompt string for the user147* @param scheme The authentication scheme148*149* @return The username/password, or null if one can't be gotten.150*151* @throws SecurityException152* if a security manager exists and its153* {@code checkPermission} method doesn't allow154* the password authentication request.155*156* @see SecurityManager#checkPermission157* @see java.net.NetPermission158*/159public static PasswordAuthentication requestPasswordAuthentication(160InetAddress addr,161int port,162String protocol,163String prompt,164String scheme) {165166SecurityManager sm = System.getSecurityManager();167if (sm != null) {168NetPermission requestPermission169= new NetPermission("requestPasswordAuthentication");170sm.checkPermission(requestPermission);171}172173Authenticator a = theAuthenticator;174if (a == null) {175return null;176} else {177synchronized(a) {178a.reset();179a.requestingSite = addr;180a.requestingPort = port;181a.requestingProtocol = protocol;182a.requestingPrompt = prompt;183a.requestingScheme = scheme;184return a.getPasswordAuthentication();185}186}187}188189/**190* Ask the authenticator that has been registered with the system191* for a password. This is the preferred method for requesting a password192* because the hostname can be provided in cases where the InetAddress193* is not available.194* <p>195* First, if there is a security manager, its {@code checkPermission}196* method is called with a197* {@code NetPermission("requestPasswordAuthentication")} permission.198* This may result in a java.lang.SecurityException.199*200* @param host The hostname of the site requesting authentication.201* @param addr The InetAddress of the site requesting authentication,202* or null if not known.203* @param port the port for the requested connection.204* @param protocol The protocol that's requesting the connection205* ({@link java.net.Authenticator#getRequestingProtocol()})206* @param prompt A prompt string for the user which identifies the authentication realm.207* @param scheme The authentication scheme208*209* @return The username/password, or null if one can't be gotten.210*211* @throws SecurityException212* if a security manager exists and its213* {@code checkPermission} method doesn't allow214* the password authentication request.215*216* @see SecurityManager#checkPermission217* @see java.net.NetPermission218* @since 1.4219*/220public static PasswordAuthentication requestPasswordAuthentication(221String host,222InetAddress addr,223int port,224String protocol,225String prompt,226String scheme) {227228SecurityManager sm = System.getSecurityManager();229if (sm != null) {230NetPermission requestPermission231= new NetPermission("requestPasswordAuthentication");232sm.checkPermission(requestPermission);233}234235Authenticator a = theAuthenticator;236if (a == null) {237return null;238} else {239synchronized(a) {240a.reset();241a.requestingHost = host;242a.requestingSite = addr;243a.requestingPort = port;244a.requestingProtocol = protocol;245a.requestingPrompt = prompt;246a.requestingScheme = scheme;247return a.getPasswordAuthentication();248}249}250}251252/**253* Ask the authenticator that has been registered with the system254* for a password.255* <p>256* First, if there is a security manager, its {@code checkPermission}257* method is called with a258* {@code NetPermission("requestPasswordAuthentication")} permission.259* This may result in a java.lang.SecurityException.260*261* @param host The hostname of the site requesting authentication.262* @param addr The InetAddress of the site requesting authorization,263* or null if not known.264* @param port the port for the requested connection265* @param protocol The protocol that's requesting the connection266* ({@link java.net.Authenticator#getRequestingProtocol()})267* @param prompt A prompt string for the user268* @param scheme The authentication scheme269* @param url The requesting URL that caused the authentication270* @param reqType The type (server or proxy) of the entity requesting271* authentication.272*273* @return The username/password, or null if one can't be gotten.274*275* @throws SecurityException276* if a security manager exists and its277* {@code checkPermission} method doesn't allow278* the password authentication request.279*280* @see SecurityManager#checkPermission281* @see java.net.NetPermission282*283* @since 1.5284*/285public static PasswordAuthentication requestPasswordAuthentication(286String host,287InetAddress addr,288int port,289String protocol,290String prompt,291String scheme,292URL url,293RequestorType reqType) {294295SecurityManager sm = System.getSecurityManager();296if (sm != null) {297NetPermission requestPermission298= new NetPermission("requestPasswordAuthentication");299sm.checkPermission(requestPermission);300}301302Authenticator a = theAuthenticator;303if (a == null) {304return null;305} else {306synchronized(a) {307a.reset();308a.requestingHost = host;309a.requestingSite = addr;310a.requestingPort = port;311a.requestingProtocol = protocol;312a.requestingPrompt = prompt;313a.requestingScheme = scheme;314a.requestingURL = url;315a.requestingAuthType = reqType;316return a.getPasswordAuthentication();317}318}319}320321/**322* Gets the {@code hostname} of the323* site or proxy requesting authentication, or {@code null}324* if not available.325*326* @return the hostname of the connection requiring authentication, or null327* if it's not available.328* @since 1.4329*/330protected final String getRequestingHost() {331return requestingHost;332}333334/**335* Gets the {@code InetAddress} of the336* site requesting authorization, or {@code null}337* if not available.338*339* @return the InetAddress of the site requesting authorization, or null340* if it's not available.341*/342protected final InetAddress getRequestingSite() {343return requestingSite;344}345346/**347* Gets the port number for the requested connection.348* @return an {@code int} indicating the349* port for the requested connection.350*/351protected final int getRequestingPort() {352return requestingPort;353}354355/**356* Give the protocol that's requesting the connection. Often this357* will be based on a URL, but in a future JDK it could be, for358* example, "SOCKS" for a password-protected SOCKS5 firewall.359*360* @return the protocol, optionally followed by "/version", where361* version is a version number.362*363* @see java.net.URL#getProtocol()364*/365protected final String getRequestingProtocol() {366return requestingProtocol;367}368369/**370* Gets the prompt string given by the requestor.371*372* @return the prompt string given by the requestor (realm for373* http requests)374*/375protected final String getRequestingPrompt() {376return requestingPrompt;377}378379/**380* Gets the scheme of the requestor (the HTTP scheme381* for an HTTP firewall, for example).382*383* @return the scheme of the requestor384*385*/386protected final String getRequestingScheme() {387return requestingScheme;388}389390/**391* Called when password authorization is needed. Subclasses should392* override the default implementation, which returns null.393* @return The PasswordAuthentication collected from the394* user, or null if none is provided.395*/396protected PasswordAuthentication getPasswordAuthentication() {397return null;398}399400/**401* Returns the URL that resulted in this402* request for authentication.403*404* @since 1.5405*406* @return the requesting URL407*408*/409protected URL getRequestingURL () {410return requestingURL;411}412413/**414* Returns whether the requestor is a Proxy or a Server.415*416* @since 1.5417*418* @return the authentication type of the requestor419*420*/421protected RequestorType getRequestorType () {422return requestingAuthType;423}424}425426427