Path: blob/master/test/jdk/javax/security/auth/login/modules/JaasClient.java
51705 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 client;2324import java.io.IOException;25import java.security.Principal;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.callback.UnsupportedCallbackException;31import javax.security.auth.login.LoginException;32import javax.security.auth.login.LoginContext;33import com.sun.security.auth.UserPrincipal;3435/**36* JAAS client which will try to authenticate a user through a custom JAAS LOGIN37* Module.38*/39public class JaasClient {4041private static final String USER_NAME = "testUser";42private static final String PASSWORD = "testPassword";43private static final String LOGIN_CONTEXT = "ModularLoginConf";4445public static void main(String[] args) {46try {47LoginContext lc = new LoginContext(LOGIN_CONTEXT,48new MyCallbackHandler());49lc.login();50checkPrincipal(lc, true);51lc.logout();52checkPrincipal(lc, false);53} catch (LoginException le) {54throw new RuntimeException(le);55}56System.out.println("Test passed.");5758}5960/*61* Check context for principal of the test user.62*/63private static void checkPrincipal(LoginContext loginContext,64boolean principalShouldExist) {65if (!principalShouldExist) {66if (loginContext.getSubject().getPrincipals().size() != 0) {67throw new RuntimeException("Test failed. Principal was not "68+ "cleared.");69}70return;71}72for (Principal p : loginContext.getSubject().getPrincipals()) {73if (p instanceof UserPrincipal74&& USER_NAME.equals(p.getName())) {75//Proper principal was found, return.76return;77}78}79throw new RuntimeException("Test failed. UserPrincipal "80+ USER_NAME + " expected.");8182}8384private static class MyCallbackHandler implements CallbackHandler {8586@Override87public void handle(Callback[] callbacks) throws IOException,88UnsupportedCallbackException {89for (Callback callback : callbacks) {90if (callback instanceof NameCallback) {91((NameCallback) callback).setName(USER_NAME);92} else if (callback instanceof PasswordCallback) {93((PasswordCallback) callback).setPassword(94PASSWORD.toCharArray());95} else {96throw new UnsupportedCallbackException(callback);97}98}99}100}101102}103104105