Path: blob/master/test/jdk/javax/security/auth/login/LoginContext/StandardCallbacks.java
51706 views
/*1* Copyright (c) 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.Locale;26import javax.security.auth.Subject;27import javax.security.auth.callback.Callback;28import javax.security.auth.callback.CallbackHandler;29import javax.security.auth.callback.ChoiceCallback;30import javax.security.auth.callback.ConfirmationCallback;31import javax.security.auth.callback.LanguageCallback;32import javax.security.auth.callback.NameCallback;33import javax.security.auth.callback.PasswordCallback;34import javax.security.auth.callback.TextInputCallback;35import javax.security.auth.callback.TextOutputCallback;36import javax.security.auth.callback.UnsupportedCallbackException;37import javax.security.auth.login.LoginContext;38import javax.security.auth.login.LoginException;3940/*41* @test42* @bug 804813843* @summary Checks if JAAS login works fine with standard callbacks44* @compile DefaultHandlerModule.java45* @run main/othervm StandardCallbacks46*/47public class StandardCallbacks {4849private static final String USERNAME = "username";50private static final char[] PASSWORD = "password".toCharArray();5152public static void main(String[] args) throws LoginException {53System.setProperty("java.security.auth.login.config",54System.getProperty("test.src")55+ System.getProperty("file.separator")56+ "custom.config");5758CustomCallbackHandler handler = new CustomCallbackHandler(USERNAME);59LoginContext context = new LoginContext("StandardCallbacks", handler);6061handler.setPassword(PASSWORD);62System.out.println("Try to login with correct password, "63+ "successful authentication is expected");64context.login();65System.out.println("Authentication succeeded!");6667Subject subject = context.getSubject();68System.out.println("Authenticated user has the following principals ["69+ subject.getPrincipals().size() + " ]:");70boolean found = true;71for (Principal principal : subject.getPrincipals()) {72System.out.println("principal: " + principal);73if (principal instanceof CustomLoginModule.TestPrincipal) {74CustomLoginModule.TestPrincipal testPrincipal =75(CustomLoginModule.TestPrincipal) principal;76if (USERNAME.equals(testPrincipal.getName())) {77System.out.println("Found test principal: "78+ testPrincipal);79found = true;80break;81}82}83}8485if (!found) {86throw new RuntimeException("TestPrincipal not found");87}8889// check if all expected text output callbacks have been called90if (!handler.info) {91throw new RuntimeException("TextOutputCallback.INFO not called");92}9394if (!handler.warning) {95throw new RuntimeException("TextOutputCallback.WARNING not called");96}9798if (!handler.error) {99throw new RuntimeException("TextOutputCallback.ERROR not called");100}101102System.out.println("Authenticated user has the following public "103+ "credentials [" + subject.getPublicCredentials().size()104+ "]:");105subject.getPublicCredentials().stream().106forEach((o) -> {107System.out.println("public credential: " + o);108});109110context.logout();111112System.out.println("Test passed");113}114115private static class CustomCallbackHandler implements CallbackHandler {116117private final String username;118private char[] password;119private boolean info = false;120private boolean warning = false;121private boolean error = false;122123CustomCallbackHandler(String username) {124this.username = username;125}126127void setPassword(char[] password) {128this.password = password;129}130131@Override132public void handle(Callback[] callbacks)133throws UnsupportedCallbackException {134for (Callback callback : callbacks) {135if (callback instanceof TextOutputCallback) {136TextOutputCallback toc = (TextOutputCallback) callback;137switch (toc.getMessageType()) {138case TextOutputCallback.INFORMATION:139System.out.println("INFO: " + toc.getMessage());140info = true;141break;142case TextOutputCallback.ERROR:143System.out.println("ERROR: " + toc.getMessage());144error = true;145break;146case TextOutputCallback.WARNING:147System.out.println("WARNING: " + toc.getMessage());148warning = true;149break;150default:151throw new UnsupportedCallbackException(toc,152"Unsupported message type: "153+ toc.getMessageType());154}155} else if (callback instanceof TextInputCallback) {156TextInputCallback tic = (TextInputCallback) callback;157System.out.println(tic.getPrompt());158tic.setText(CustomLoginModule.HELLO);159} else if (callback instanceof LanguageCallback) {160LanguageCallback lc = (LanguageCallback) callback;161lc.setLocale(Locale.GERMANY);162} else if (callback instanceof ConfirmationCallback) {163ConfirmationCallback cc = (ConfirmationCallback) callback;164System.out.println(cc.getPrompt());165cc.setSelectedIndex(ConfirmationCallback.YES);166} else if (callback instanceof ChoiceCallback) {167ChoiceCallback cc = (ChoiceCallback) callback;168System.out.println(cc.getPrompt()169+ Arrays.toString(cc.getChoices()));170cc.setSelectedIndex(0);171} else if (callback instanceof NameCallback) {172NameCallback nc = (NameCallback) callback;173System.out.println(nc.getPrompt());174nc.setName(username);175} else if (callback instanceof PasswordCallback) {176PasswordCallback pc = (PasswordCallback) callback;177System.out.println(pc.getPrompt());178pc.setPassword(password);179} else {180throw new UnsupportedCallbackException(callback,181"Unknown callback");182}183}184}185186}187188}189190191