Path: blob/master/test/jdk/javax/management/security/TestSampleLoginModule.java
51504 views
/*1* Copyright (c) 2006, 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.util.Map;2425import javax.security.auth.Subject;26import javax.security.auth.callback.Callback;27import javax.security.auth.callback.CallbackHandler;28import javax.security.auth.callback.NameCallback;29import javax.security.auth.callback.PasswordCallback;30import javax.security.auth.login.LoginException;31import javax.security.auth.spi.LoginModule;323334public final class TestSampleLoginModule implements LoginModule {3536private Subject subject;37private CallbackHandler callbackHandler;38private Map<String, ?> sharedState;39private Map<String, ?> options;4041public TestSampleLoginModule() {42}4344public void initialize(Subject subject,45CallbackHandler callbackHandler,46Map<String,?> sharedState,47Map<String,?> options) {4849this.subject = subject;50this.callbackHandler = callbackHandler;51this.sharedState = sharedState;52this.options = options;53}5455/*56* Authenticate the user by comparing the values of the java properties57* (username and password) against the values of the credentials.58* */59public boolean login() throws LoginException {6061String credentials_username = null;62String credentials_password = null;63String authenticated_username = System.getProperty("susername");64String authenticated_password = System.getProperty("spassword");6566System.out.println("TestSampleLoginModule::login: Start");6768// First retreive the credentials {username, password} from69// the callback handler70Callback[] callbacks = new Callback[2];71callbacks[0] = new NameCallback("username");72callbacks[1] = new PasswordCallback("password", false);73try {74callbackHandler.handle(callbacks);75credentials_username = ((NameCallback)callbacks[0]).getName();76credentials_password = new String(((PasswordCallback)callbacks[1]).77getPassword());78} catch (Exception e) {79throw new LoginException(e.toString());80}8182System.out.println("TestSampleLoginModule::login: credentials username = " +83credentials_username);84System.out.println("TestSampleLoginModule::login: credentials password = " +85credentials_password);86System.out.println("TestSampleLoginModule::login: authenticated username = " +87authenticated_username);88System.out.println("TestSampleLoginModule::login: authenticated password = " +89authenticated_password);9091if (credentials_username.equals(authenticated_username) &&92credentials_password.equals(authenticated_password)) {93System.out.println("TestSampleLoginModule::login: " +94"Authentication should succeed");95return true;96} else {97System.out.println("TestSampleLoginModule::login: " +98"Authentication should reject");99throw new LoginException("TestSampleLoginModule throws EXCEPTION");100}101}102103public boolean commit() throws LoginException {104return true;105}106107public boolean abort() throws LoginException {108return true;109}110111public boolean logout() throws LoginException {112return true;113}114}115116117