Path: blob/master/test/jdk/sun/security/krb5/auto/LoginModuleOptions.java
51710 views
/*1* Copyright (c) 2008, 2018, 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*/2223/*24* @test25* @bug 6765491 819448626* @summary Krb5LoginModule a little too restrictive, and the doc is not clear.27* @library /test/lib28* @run main jdk.test.lib.FileInstaller TestHosts TestHosts29* @run main/othervm -Djdk.net.hosts.file=TestHosts LoginModuleOptions30*/31import com.sun.security.auth.module.Krb5LoginModule;32import java.util.HashMap;33import java.util.Map;34import javax.security.auth.Subject;35import javax.security.auth.callback.Callback;36import javax.security.auth.callback.CallbackHandler;37import javax.security.auth.callback.NameCallback;38import javax.security.auth.callback.PasswordCallback;3940public class LoginModuleOptions {4142private static final String NAME = "javax.security.auth.login.name";43private static final String PWD = "javax.security.auth.login.password";4445public static void main(String[] args) throws Exception {46OneKDC kdc = new OneKDC(null);47kdc.addPrincipal("foo", "bar".toCharArray());48kdc.writeKtab(OneKDC.KTAB); // rewrite to add foo4950// All 4 works: keytab, shared state, callback, cache51login(null, "useKeyTab", "true", "principal", "dummy");52login(null, "tryFirstPass", "true", NAME, OneKDC.USER,53PWD, OneKDC.PASS);54System.setProperty("test.kdc.save.ccache", "krbcc");55login(new MyCallback(OneKDC.USER, OneKDC.PASS)); // save the cache56System.clearProperty("test.kdc.save.ccache");57login(null, "useTicketCache", "true", "ticketCache", "krbcc");5859// Fallbacks60// 1. ccache -> keytab61login(null, "useTicketCache", "true", "ticketCache", "krbcc_non_exists",62"useKeyTab", "true", "principal", "dummy");6364// 2. keytab -> shared65login(null, "useKeyTab", "true", "principal", "dummy",66"keyTab", "ktab_non_exist",67"tryFirstPass", "true", NAME, OneKDC.USER, PWD, OneKDC.PASS);6869// 3. shared -> callback70// 3.1. useFirstPass, no callback71boolean failed = false;72try {73login(new MyCallback(OneKDC.USER, OneKDC.PASS),74"useFirstPass", "true",75NAME, OneKDC.USER, PWD, "haha".toCharArray());76} catch (Exception e) {77failed = true;78}79if (!failed) {80throw new Exception("useFirstPass should not fallback to callback");81}82// 3.2. tryFirstPass, has callback83login(new MyCallback(OneKDC.USER, OneKDC.PASS),84"tryFirstPass", "true",85NAME, OneKDC.USER, PWD, "haha".toCharArray());8687// Preferences of type88// 1. ccache preferred to keytab89login(new MyCallback("foo", null),90"useTicketCache", "true", "ticketCache", "krbcc",91"useKeyTab", "true");92// 2. keytab preferred to shared. This test case is not exactly correct,93// because principal=dummy would shadow the PWD setting in the shared94// state. So by only looking at the final authentication user name95// (which is how this program does), there's no way to tell if keyTab96// is picked first, or shared is tried first but fallback to keytab.97login(null, "useKeyTab", "true", "principal", "dummy",98"tryFirstPass", "true", NAME, "foo", PWD, "bar".toCharArray());99// 3. shared preferred to callback100login(new MyCallback("foo", "bar".toCharArray()),101"tryFirstPass", "true", NAME, OneKDC.USER, PWD, OneKDC.PASS);102103// Preferences of username104// 1. principal preferred to NAME (NAME can be wrong or missing)105login(null, "principal", OneKDC.USER,106"tryFirstPass", "true", NAME, "someone_else", PWD, OneKDC.PASS);107login(null, "principal", OneKDC.USER,108"tryFirstPass", "true", PWD, OneKDC.PASS);109// 2. NAME preferred to callback110login(new MyCallback("someone_else", OneKDC.PASS),111"principal", OneKDC.USER);112// 3. With tryFirstPass, NAME preferred to callback113login(new MyCallback("someone_else", null),114"tryFirstPass", "true", NAME, OneKDC.USER, PWD, OneKDC.PASS);115// 3.1. you must provide a NAME (when there's no principal)116failed = false;117try {118login(new MyCallback(OneKDC.USER, null),119"tryFirstPass", "true", PWD, OneKDC.PASS);120} catch (Exception e) {121failed = true;122}123if (!failed) {124throw new Exception("useFirstPass must provide a NAME");125}126// 3.2 Hybrid, you can use NAME as "", and provide it using callback.127// I don't think this is designed.128login(new MyCallback(OneKDC.USER, null),129"tryFirstPass", "true", NAME, "", PWD, OneKDC.PASS);130131// Test for the bug fix: doNotPrompt can be true if tryFirstPass=true132login(null, "doNotPrompt", "true", "storeKey", "true",133"tryFirstPass", "true", NAME, OneKDC.USER, PWD, OneKDC.PASS);134}135136static void login(CallbackHandler callback, Object... options)137throws Exception {138Krb5LoginModule krb5 = new Krb5LoginModule();139Subject subject = new Subject();140Map<String, String> map = new HashMap<>();141Map<String, Object> shared = new HashMap<>();142143int count = options.length / 2;144for (int i = 0; i < count; i++) {145String key = (String) options[2 * i];146Object value = options[2 * i + 1];147if (key.startsWith("javax")) {148shared.put(key, value);149} else {150map.put(key, (String) value);151}152}153krb5.initialize(subject, callback, shared, map);154krb5.login();155krb5.commit();156if (!subject.getPrincipals().iterator().next()157.getName().startsWith(OneKDC.USER)) {158throw new Exception("The authenticated is not " + OneKDC.USER);159}160}161162static class MyCallback implements CallbackHandler {163164private String name;165private char[] password;166167public MyCallback(String name, char[] password) {168this.name = name;169this.password = password;170}171172public void handle(Callback[] callbacks) {173for (Callback callback : callbacks) {174System.err.println(callback);175if (callback instanceof NameCallback) {176System.err.println("name is " + name);177((NameCallback) callback).setName(name);178}179if (callback instanceof PasswordCallback) {180System.err.println("pass is " + new String(password));181((PasswordCallback) callback).setPassword(password);182}183}184}185}186}187188189