Path: blob/master/test/jdk/javax/security/auth/login/modules/TestLoginModule.java
51695 views
/*1* Copyright (c) 2015, 2017, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package login;2324import java.io.IOException;25import java.util.Map;26import javax.security.auth.Subject;27import javax.security.auth.callback.Callback;28import javax.security.auth.callback.CallbackHandler;29import javax.security.auth.callback.NameCallback;30import javax.security.auth.callback.PasswordCallback;31import javax.security.auth.callback.UnsupportedCallbackException;32import javax.security.auth.login.LoginException;33import javax.security.auth.spi.LoginModule;34import com.sun.security.auth.UserPrincipal;3536/**37* Custom JAAS login module which will be loaded through Java LoginContext when38* it is bundled by Strict/Auto/Unnamed modules.39*/40public class TestLoginModule implements LoginModule {4142private static final String USER_NAME = "testUser";43private static final String PASSWORD = "testPassword";44private Subject subject;45private CallbackHandler callbackHandler;46private UserPrincipal userPrincipal;47private String username;48private String password;49private boolean succeeded = false;50private boolean commitSucceeded = false;5152@Override53public void initialize(Subject subject, CallbackHandler callbackHandler,54Map<String, ?> sharedState, Map<String, ?> options) {5556this.subject = subject;57this.callbackHandler = callbackHandler;58System.out.println(String.format(59"'%s' login module initialized", this.getClass()));60}6162/*63* Authenticate the user by prompting for a username and password.64*/65@Override66public boolean login() throws LoginException {67if (callbackHandler == null) {68throw new LoginException("No CallbackHandler available");69}7071Callback[] callbacks = new Callback[2];72callbacks[0] = new NameCallback("Username: ");73callbacks[1] = new PasswordCallback("Password: ", false);7475try {76callbackHandler.handle(callbacks);77username = ((NameCallback) callbacks[0]).getName();78password = new String(((PasswordCallback) callbacks[1])79.getPassword());80System.out.println(String.format("'%s' login module found username"81+ " as '%s' and password as '%s'", this.getClass(),82username, password));83if (username.equals(USER_NAME)84&& password.equals(PASSWORD)) {85System.out.println(String.format("'%s' login module "86+ "authentication done successfully", this.getClass()));87succeeded = true;88return true;89}90throw new IllegalArgumentException("Incorrect username/password!");91} catch (IOException | UnsupportedCallbackException e) {92throw new LoginException("Login failed: " + e.getMessage());93}94}9596@Override97public boolean commit() throws LoginException {98if (succeeded == false) {99return false;100}101userPrincipal = new UserPrincipal(username);102if (!subject.getPrincipals().contains(userPrincipal)) {103subject.getPrincipals().add(userPrincipal);104}105System.out.println(String.format("'%s' login module authentication "106+ "committed", this.getClass()));107password = null;108commitSucceeded = true;109return true;110}111112@Override113public boolean abort() throws LoginException {114if (succeeded == false) {115return false;116}117System.out.println(String.format(118"'%s' login module aborted", this.getClass()));119clearState();120return true;121}122123@Override124public boolean logout() throws LoginException {125clearState();126System.out.println(String.format(127"'%s' login module logout completed", this.getClass()));128return true;129}130131private void clearState() {132if (commitSucceeded) {133subject.getPrincipals().remove(userPrincipal);134}135username = null;136password = null;137userPrincipal = null;138}139}140141142