Search in sources :

Example 1 with TextCallbackHandler

use of com.sun.security.auth.callback.TextCallbackHandler in project jdk8u_jdk by JetBrains.

the class PrincipalSystemPropTest method runTest.

private static void runTest(boolean expected, String jaasConfigEntry, String expectedLoginUser, String loginUserBySysProp) {
    if (loginUserBySysProp != null) {
        System.setProperty("sun.security.krb5.principal", loginUserBySysProp);
    } else {
        System.clearProperty("sun.security.krb5.principal");
    }
    try {
        LoginContext lc = new LoginContext(jaasConfigEntry, new TextCallbackHandler());
        lc.login();
        System.out.println(String.format("Authentication completed with Subject '%s' ", lc.getSubject()));
        if (!expected) {
            throw new RuntimeException("TEST FAILED - JAAS login success isn't expected");
        }
        if (expectedLoginUser != null && !lc.getSubject().getPrincipals().stream().map(p -> p.getName()).filter(expectedLoginUser::equals).findFirst().isPresent()) {
            throw new RuntimeException(String.format("TEST FAILED - Login principal is not matched " + "to expected principal '%s'.", expectedLoginUser));
        }
        System.out.println("TEST PASSED - JAAS login success is expected.");
    } catch (LoginException ie) {
        System.out.println(String.format("Authentication failed with exception: %s", ie.getMessage()));
        if (expected) {
            System.out.println("TEST FAILED - JAAS login failure isn't expected");
            throw new RuntimeException(ie);
        }
        System.out.println("TEST PASSED - JAAS login failure is expected.");
    }
}
Also used : LoginException(javax.security.auth.login.LoginException) List(java.util.List) TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler) Map(java.util.Map) HashMap(java.util.HashMap) File(java.io.File) LoginContext(javax.security.auth.login.LoginContext) ArrayList(java.util.ArrayList) LoginContext(javax.security.auth.login.LoginContext) LoginException(javax.security.auth.login.LoginException) TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler)

Example 2 with TextCallbackHandler

use of com.sun.security.auth.callback.TextCallbackHandler in project jdk8u_jdk by JetBrains.

the class Default method main.

public static void main(String[] args) throws Exception {
    TextCallbackHandler h = new TextCallbackHandler();
    NameCallback nc = new NameCallback("Name: ", "charlie");
    ConfirmationCallback cc = new ConfirmationCallback("Correct?", ConfirmationCallback.INFORMATION, ConfirmationCallback.YES_NO_OPTION, ConfirmationCallback.NO);
    Callback[] callbacks = { nc, cc };
    h.handle(callbacks);
    if (cc.getSelectedIndex() == ConfirmationCallback.YES) {
        System.out.println("yes");
    } else {
        System.out.println("no");
    }
}
Also used : TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler)

Example 3 with TextCallbackHandler

use of com.sun.security.auth.callback.TextCallbackHandler in project jdk8u_jdk by JetBrains.

the class GSSUtil method login.

/**
     * Authenticate using the login module from the specified
     * configuration entry.
     *
     * @param caller the caller of JAAS Login
     * @param mech the mech to be used
     * @return the authenticated subject
     */
public static Subject login(GSSCaller caller, Oid mech) throws LoginException {
    CallbackHandler cb = null;
    if (caller instanceof HttpCaller) {
        cb = new sun.net.www.protocol.http.spnego.NegotiateCallbackHandler(((HttpCaller) caller).info());
    } else {
        String defaultHandler = java.security.Security.getProperty(DEFAULT_HANDLER);
        // get the default callback handler
        if ((defaultHandler != null) && (defaultHandler.length() != 0)) {
            cb = null;
        } else {
            cb = new TextCallbackHandler();
        }
    }
    // New instance of LoginConfigImpl must be created for each login,
    // since the entry name is not passed as the first argument, but
    // generated with caller and mech inside LoginConfigImpl
    LoginContext lc = new LoginContext("", null, cb, new LoginConfigImpl(caller, mech));
    lc.login();
    return lc.getSubject();
}
Also used : TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler) CallbackHandler(javax.security.auth.callback.CallbackHandler) LoginContext(javax.security.auth.login.LoginContext) TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler)

Example 4 with TextCallbackHandler

use of com.sun.security.auth.callback.TextCallbackHandler in project jdk8u_jdk by JetBrains.

the class Password method main.

public static void main(String[] args) throws Exception {
    TextCallbackHandler h = new TextCallbackHandler();
    PasswordCallback nc = new PasswordCallback("Invisible: ", false);
    PasswordCallback nc2 = new PasswordCallback("Visible: ", true);
    System.out.println("Two passwords will be prompted for. The first one " + "should have echo off, the second one on. Otherwise, this test fails");
    Callback[] callbacks = { nc, nc2 };
    h.handle(callbacks);
    System.out.println("You input " + new String(nc.getPassword()) + " and " + new String(nc2.getPassword()));
}
Also used : TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler)

Example 5 with TextCallbackHandler

use of com.sun.security.auth.callback.TextCallbackHandler in project jdk8u_jdk by JetBrains.

the class Confirm method main.

public static void main(String[] args) throws Exception {
    // Provide answer in an individual stream so that the program
    // does not block.
    System.setIn(new ByteArrayInputStream("1\n".getBytes()));
    new TextCallbackHandler().handle(new Callback[] { new ConfirmationCallback("Prince", ConfirmationCallback.INFORMATION, new String[] { "To be", "Not to be" }, 0) });
}
Also used : ConfirmationCallback(javax.security.auth.callback.ConfirmationCallback) ByteArrayInputStream(java.io.ByteArrayInputStream) TextCallbackHandler(com.sun.security.auth.callback.TextCallbackHandler)

Aggregations

TextCallbackHandler (com.sun.security.auth.callback.TextCallbackHandler)5 LoginContext (javax.security.auth.login.LoginContext)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1 ConfirmationCallback (javax.security.auth.callback.ConfirmationCallback)1 LoginException (javax.security.auth.login.LoginException)1