Path: blob/master/test/jdk/javax/security/auth/login/modules/JaasClientWithDefaultHandler.java
51695 views
/*1* Copyright (c) 2016, 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.security.Principal;25import javax.security.auth.login.LoginContext;26import javax.security.auth.login.LoginException;27import com.sun.security.auth.UserPrincipal;2829public class JaasClientWithDefaultHandler {3031private static final String USER_NAME = "testUser";32private static final String LOGIN_CONTEXT = "ModularLoginConf";33private static final String CBH_PROP = "auth.login.defaultCallbackHandler";3435public static void main(String[] args) {36try {37java.security.Security.setProperty(CBH_PROP, args[0]);38LoginContext lc = new LoginContext(LOGIN_CONTEXT);39lc.login();40checkPrincipal(lc, true);41lc.logout();42checkPrincipal(lc, false);43} catch (LoginException le) {44throw new RuntimeException(le);45}46System.out.println("Test passed.");4748}4950/*51* Verify principal for the test user.52*/53private static void checkPrincipal(LoginContext loginContext,54boolean principalShouldExist) {55if (!principalShouldExist) {56if (loginContext.getSubject().getPrincipals().size() != 0) {57throw new RuntimeException("Test failed. Principal was not "58+ "cleared.");59}60return;61}62for (Principal p : loginContext.getSubject().getPrincipals()) {63if (p instanceof UserPrincipal64&& USER_NAME.equals(p.getName())) {65//Proper principal was found, return.66return;67}68}69throw new RuntimeException("Test failed. UserPrincipal "70+ USER_NAME + " expected.");71}7273}747576