Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/security/auth/spi/LoginModule.java
38918 views
/*1* Copyright (c) 1998, 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 javax.security.auth.spi;2627import javax.security.auth.Subject;28import javax.security.auth.AuthPermission;29import javax.security.auth.callback.*;30import javax.security.auth.login.*;31import java.util.Map;3233/**34* <p> {@code LoginModule} describes the interface35* implemented by authentication technology providers. LoginModules36* are plugged in under applications to provide a particular type of37* authentication.38*39* <p> While applications write to the {@code LoginContext} API,40* authentication technology providers implement the41* {@code LoginModule} interface.42* A {@code Configuration} specifies the LoginModule(s)43* to be used with a particular login application. Therefore different44* LoginModules can be plugged in under the application without45* requiring any modifications to the application itself.46*47* <p> The {@code LoginContext} is responsible for reading the48* {@code Configuration} and instantiating the appropriate49* LoginModules. Each {@code LoginModule} is initialized with50* a {@code Subject}, a {@code CallbackHandler}, shared51* {@code LoginModule} state, and LoginModule-specific options.52*53* The {@code Subject} represents the54* {@code Subject} currently being authenticated and is updated55* with relevant Credentials if authentication succeeds.56* LoginModules use the {@code CallbackHandler} to57* communicate with users. The {@code CallbackHandler} may be58* used to prompt for usernames and passwords, for example.59* Note that the {@code CallbackHandler} may be null. LoginModules60* which absolutely require a {@code CallbackHandler} to authenticate61* the {@code Subject} may throw a {@code LoginException}.62* LoginModules optionally use the shared state to share information63* or data among themselves.64*65* <p> The LoginModule-specific options represent the options66* configured for this {@code LoginModule} by an administrator or user67* in the login {@code Configuration}.68* The options are defined by the {@code LoginModule} itself69* and control the behavior within it. For example, a70* {@code LoginModule} may define options to support debugging/testing71* capabilities. Options are defined using a key-value syntax,72* such as <i>debug=true</i>. The {@code LoginModule}73* stores the options as a {@code Map} so that the values may74* be retrieved using the key. Note that there is no limit to the number75* of options a {@code LoginModule} chooses to define.76*77* <p> The calling application sees the authentication process as a single78* operation. However, the authentication process within the79* {@code LoginModule} proceeds in two distinct phases.80* In the first phase, the LoginModule's81* {@code login} method gets invoked by the LoginContext's82* {@code login} method. The {@code login}83* method for the {@code LoginModule} then performs84* the actual authentication (prompt for and verify a password for example)85* and saves its authentication status as private state86* information. Once finished, the LoginModule's {@code login}87* method either returns {@code true} (if it succeeded) or88* {@code false} (if it should be ignored), or throws a89* {@code LoginException} to specify a failure.90* In the failure case, the {@code LoginModule} must not retry the91* authentication or introduce delays. The responsibility of such tasks92* belongs to the application. If the application attempts to retry93* the authentication, the LoginModule's {@code login} method will be94* called again.95*96* <p> In the second phase, if the LoginContext's overall authentication97* succeeded (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL98* LoginModules succeeded), then the {@code commit}99* method for the {@code LoginModule} gets invoked.100* The {@code commit} method for a {@code LoginModule} checks its101* privately saved state to see if its own authentication succeeded.102* If the overall {@code LoginContext} authentication succeeded103* and the LoginModule's own authentication succeeded, then the104* {@code commit} method associates the relevant105* Principals (authenticated identities) and Credentials (authentication data106* such as cryptographic keys) with the {@code Subject}107* located within the {@code LoginModule}.108*109* <p> If the LoginContext's overall authentication failed (the relevant110* REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules did not succeed),111* then the {@code abort} method for each {@code LoginModule}112* gets invoked. In this case, the {@code LoginModule} removes/destroys113* any authentication state originally saved.114*115* <p> Logging out a {@code Subject} involves only one phase.116* The {@code LoginContext} invokes the LoginModule's {@code logout}117* method. The {@code logout} method for the {@code LoginModule}118* then performs the logout procedures, such as removing Principals or119* Credentials from the {@code Subject} or logging session information.120*121* <p> A {@code LoginModule} implementation must have a constructor with122* no arguments. This allows classes which load the {@code LoginModule}123* to instantiate it.124*125* @see javax.security.auth.login.LoginContext126* @see javax.security.auth.login.Configuration127*/128public interface LoginModule {129130/**131* Initialize this LoginModule.132*133* <p> This method is called by the {@code LoginContext}134* after this {@code LoginModule} has been instantiated.135* The purpose of this method is to initialize this136* {@code LoginModule} with the relevant information.137* If this {@code LoginModule} does not understand138* any of the data stored in {@code sharedState} or139* {@code options} parameters, they can be ignored.140*141* <p>142*143* @param subject the {@code Subject} to be authenticated. <p>144*145* @param callbackHandler a {@code CallbackHandler} for communicating146* with the end user (prompting for usernames and147* passwords, for example). <p>148*149* @param sharedState state shared with other configured LoginModules. <p>150*151* @param options options specified in the login152* {@code Configuration} for this particular153* {@code LoginModule}.154*/155void initialize(Subject subject, CallbackHandler callbackHandler,156Map<String,?> sharedState,157Map<String,?> options);158159/**160* Method to authenticate a {@code Subject} (phase 1).161*162* <p> The implementation of this method authenticates163* a {@code Subject}. For example, it may prompt for164* {@code Subject} information such165* as a username and password and then attempt to verify the password.166* This method saves the result of the authentication attempt167* as private state within the LoginModule.168*169* <p>170*171* @exception LoginException if the authentication fails172*173* @return true if the authentication succeeded, or false if this174* {@code LoginModule} should be ignored.175*/176boolean login() throws LoginException;177178/**179* Method to commit the authentication process (phase 2).180*181* <p> This method is called if the LoginContext's182* overall authentication succeeded183* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules184* succeeded).185*186* <p> If this LoginModule's own authentication attempt187* succeeded (checked by retrieving the private state saved by the188* {@code login} method), then this method associates relevant189* Principals and Credentials with the {@code Subject} located in the190* {@code LoginModule}. If this LoginModule's own191* authentication attempted failed, then this method removes/destroys192* any state that was originally saved.193*194* <p>195*196* @exception LoginException if the commit fails197*198* @return true if this method succeeded, or false if this199* {@code LoginModule} should be ignored.200*/201boolean commit() throws LoginException;202203/**204* Method to abort the authentication process (phase 2).205*206* <p> This method is called if the LoginContext's207* overall authentication failed.208* (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules209* did not succeed).210*211* <p> If this LoginModule's own authentication attempt212* succeeded (checked by retrieving the private state saved by the213* {@code login} method), then this method cleans up any state214* that was originally saved.215*216* <p>217*218* @exception LoginException if the abort fails219*220* @return true if this method succeeded, or false if this221* {@code LoginModule} should be ignored.222*/223boolean abort() throws LoginException;224225/**226* Method which logs out a {@code Subject}.227*228* <p>An implementation of this method might remove/destroy a Subject's229* Principals and Credentials.230*231* <p>232*233* @exception LoginException if the logout fails234*235* @return true if this method succeeded, or false if this236* {@code LoginModule} should be ignored.237*/238boolean logout() throws LoginException;239}240241242