Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/security/auth/login/LoginContext/SmartLoginModule.java
38859 views
/*1* Copyright (c) 2004, 2015, 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*/2223import java.security.Principal;24import java.util.Arrays;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.FailedLoginException;33import javax.security.auth.login.LoginException;34import javax.security.auth.spi.LoginModule;3536/**37* This code was based on JAAS demo code, small modification is made for testing38* purpose.39*/40public class SmartLoginModule implements LoginModule {4142// initial state43private Subject subject;44private CallbackHandler callbackHandler;4546// the authentication status47private boolean succeeded = false;48private boolean commitSucceeded = false;4950// username and password51private String username;52private char[] password;5354// Default values for this login module. In real world,55// don't do it in this way!56private String myUsername;57private char[] myPassword;58private String header;5960// testUser's SamplePrincipal61private SamplePrincipal userPrincipal;6263public SmartLoginModule() {64this("testUser",65new char[]{'t', 'e', 's', 't', 'P', 'a', 's', 's',66'w', 'o', 'r', 'd', '1'},67"SmartLoginModule1: ");68}6970public SmartLoginModule(String userName, char[] password, String header) {71myUsername = userName;72myPassword = password;73this.header = header;74}7576@Override77public boolean abort() throws LoginException {78if (!succeeded) {79return false;80} else if (succeeded && !commitSucceeded) {81// login succeeded but overall authentication failed82succeeded = false;83username = null;84password = null;85userPrincipal = null;86} else {87// overall authentication succeeded and commit succeeded,88// but someone else's commit failed89logout();90}91return true;92}9394@Override95public boolean commit() throws LoginException {96if (!succeeded) {97return false;98} else {99// add a Principal (authenticated identity) to the Subject100// assume the user we authenticated is the SamplePrincipal101userPrincipal = new SamplePrincipal(username);102if (!subject.getPrincipals().contains(userPrincipal)) {103subject.getPrincipals().add(userPrincipal);104}105// in any case, clean out state106username = null;107password = null;108commitSucceeded = true;109return true;110}111}112113@Override114public void initialize(Subject subject, CallbackHandler callbackHandler,115Map<String, ?> sharedState, Map<String, ?> options) {116this.subject = subject;117this.callbackHandler = callbackHandler;118}119120@Override121public boolean login() throws LoginException {122if (callbackHandler == null) {123throw new LoginException("Error: no CallbackHandler available to "124+ "garner authentication information from the user");125}126127Callback[] callbacks = new Callback[2];128callbacks[0] = new NameCallback(header + "user name: ");129callbacks[1] = new PasswordCallback(header + "password: ", false);130131try {132callbackHandler.handle(callbacks);133username = ((NameCallback) callbacks[0]).getName();134char[] tmpPassword135= ((PasswordCallback) callbacks[1]).getPassword();136if (tmpPassword == null) {137tmpPassword = new char[0];138}139password = new char[tmpPassword.length];140System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length);141((PasswordCallback) callbacks[1]).clearPassword();142} catch (java.io.IOException ioe) {143throw (LoginException) new LoginException().initCause(ioe);144} catch (UnsupportedCallbackException uce) {145throw new LoginException("Error: " + header146+ uce.getCallback().toString()147+ " not available to garner authentication information "148+ "from the user");149}150151// verify the username/password152if (username.equals(myUsername)153&& Arrays.equals(password, myPassword)) {154System.out.println("\t\t" + header + " authentication succeeded");155succeeded = true;156return true;157} else {158// authentication failed -- clean out state159System.out.println("\t\t" + header + " authentication failed");160printDebugInfo();161succeeded = false;162username = null;163password = null;164throw new FailedLoginException("User Name or Password Incorrect");165}166}167168@Override169public boolean logout() throws LoginException {170subject.getPrincipals().remove(userPrincipal);171succeeded = false;172succeeded = commitSucceeded;173username = null;174password = null;175userPrincipal = null;176return true;177}178179// print debugging information180private void printDebugInfo() {181System.out.println("\t\t" + header + " correct user name: "182+ myUsername);183System.out.println("\t\t" + header + " user entered user name: "184+ username);185System.out.print("\t\t" + header + " correct password: ");186for (char c : myPassword) {187System.out.print(c);188}189System.out.println();190System.out.print("\t\t" + header + " user entered password: ");191for (char c : password) {192System.out.print(c);193}194System.out.println();195}196}197198class SamplePrincipal implements Principal, java.io.Serializable {199200/**201* @serial202*/203private String name;204205/**206* Create a SamplePrincipal with a Sample username.207*208* @param name the Sample username for this user.209* @exception NullPointerException if the <code>name</code> is210* <code>null</code>.211*/212public SamplePrincipal(String name) {213if (name == null) {214throw new NullPointerException("illegal null input");215}216217this.name = name;218}219220@Override221public String getName() {222return name;223}224225@Override226public String toString() {227return "SamplePrincipal: " + name;228}229230@Override231public boolean equals(Object o) {232if (o == null) {233return false;234}235236if (this == o) {237return true;238}239240if (!(o instanceof SamplePrincipal)) {241return false;242}243SamplePrincipal that = (SamplePrincipal) o;244245return this.getName().equals(that.getName());246}247248@Override249public int hashCode() {250return name.hashCode();251}252}253254255