Search in sources :

Example 76 with Configuration

use of javax.security.auth.login.Configuration in project calcite-avatica by apache.

the class KerberosConnectionTest method noPreviousContextOnLogin.

@Test
public void noPreviousContextOnLogin() throws Exception {
    KerberosConnection krbUtil = mock(KerberosConnection.class);
    Subject subject = new Subject();
    Subject loggedInSubject = new Subject();
    Configuration conf = mock(Configuration.class);
    LoginContext context = mock(LoginContext.class);
    // Call the real login(LoginContext, Configuration, Subject) method
    when(krbUtil.login(nullable(LoginContext.class), any(Configuration.class), any(Subject.class))).thenCallRealMethod();
    // Return a fake LoginContext
    when(krbUtil.createLoginContext(conf)).thenReturn(context);
    // Return a fake Subject from that fake LoginContext
    when(context.getSubject()).thenReturn(loggedInSubject);
    Entry<LoginContext, Subject> pair = krbUtil.login(null, conf, subject);
    // Verify we get the fake LoginContext and Subject
    assertEquals(context, pair.getKey());
    assertEquals(loggedInSubject, pair.getValue());
    // login should be called on the LoginContext
    verify(context).login();
}
Also used : LoginContext(javax.security.auth.login.LoginContext) Configuration(javax.security.auth.login.Configuration) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 77 with Configuration

use of javax.security.auth.login.Configuration in project zm-mailbox by Zimbra.

the class Krb5Login method withPassword.

public static LoginContext withPassword(String name, final String password) throws LoginException {
    Krb5Config kc = Krb5Config.getInstance();
    kc.setPrincipal(name);
    kc.setUseTicketCache(false);
    kc.setStoreKey(false);
    Configuration dc = new DynamicConfiguration(S_CONFIG_NAME, new AppConfigurationEntry[] { kc });
    CallbackHandler handler = new CallbackHandler() {

        public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
            for (Callback callback : callbacks) {
                if (callback instanceof PasswordCallback) {
                    PasswordCallback pc = (PasswordCallback) callback;
                    pc.setPassword(password.toCharArray());
                }
            }
        }
    };
    return new LoginContext(S_CONFIG_NAME, null, handler, dc);
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) PasswordCallback(javax.security.auth.callback.PasswordCallback) Callback(javax.security.auth.callback.Callback) LoginContext(javax.security.auth.login.LoginContext) Configuration(javax.security.auth.login.Configuration) PasswordCallback(javax.security.auth.callback.PasswordCallback)

Example 78 with Configuration

use of javax.security.auth.login.Configuration in project zm-mailbox by Zimbra.

the class Krb5Login method withKeyTab.

/**
 * Constructs a new Krb5Config entry with the specified
 * principal and keytab, logs in with that entry, and
 * then removes that entry and returns the new LoginContext.
 * <p>Equivalent to the following calls:
 *<pre>
 * Krb5Config kc = Krb5Config.getInstance();
 * kc.setPrincipal(principal);
 * kc.setKeyTab(keytab);
 * kc.setStoreKey(true);
 * LoginContext lc = Login.login(kc);
 *</pre>
 */
public static LoginContext withKeyTab(String principal, String keytab) throws LoginException {
    /*
         * com.sun.security.auth.module.Krb5LoginModule required 
         * useKeyTab=true 
         * debug=true 
         * keyTab="/apps/workgroup-audit/keytab/keytab.workgroup-audit" 
         * doNotPrompt=true 
         * storeKey=true 
         * principal="service/workgroup-audit@stanford.edu" 
         * useTicketCache=true
         */
    Krb5Config kc = Krb5Config.getInstance();
    // kc.setDebug(true);
    kc.setPrincipal(principal);
    kc.setKeyTab(keytab);
    kc.setStoreKey(true);
    kc.setDoNotPrompt(true);
    kc.setUseTicketCache(true);
    Configuration dc = new DynamicConfiguration(S_CONFIG_NAME, new AppConfigurationEntry[] { kc });
    return new LoginContext(S_CONFIG_NAME, null, null, dc);
}
Also used : LoginContext(javax.security.auth.login.LoginContext) Configuration(javax.security.auth.login.Configuration)

Example 79 with Configuration

use of javax.security.auth.login.Configuration in project apache-kafka-on-k8s by banzaicloud.

the class JaasContextTest method testMultipleLoginModules.

@Test
public void testMultipleLoginModules() throws Exception {
    StringBuilder builder = new StringBuilder();
    int moduleCount = 3;
    Map<Integer, Map<String, Object>> moduleOptions = new HashMap<>();
    for (int i = 0; i < moduleCount; i++) {
        Map<String, Object> options = new HashMap<>();
        options.put("index", "Index" + i);
        options.put("module", "Module" + i);
        moduleOptions.put(i, options);
        String module = jaasConfigProp("test.Module" + i, LoginModuleControlFlag.REQUIRED, options);
        builder.append(' ');
        builder.append(module);
    }
    String jaasConfigProp = builder.toString();
    String clientContextName = "CLIENT";
    Configuration configuration = new JaasConfig(clientContextName, jaasConfigProp);
    AppConfigurationEntry[] dynamicEntries = configuration.getAppConfigurationEntry(clientContextName);
    assertEquals(moduleCount, dynamicEntries.length);
    for (int i = 0; i < moduleCount; i++) {
        AppConfigurationEntry entry = dynamicEntries[i];
        checkEntry(entry, "test.Module" + i, LoginModuleControlFlag.REQUIRED, moduleOptions.get(i));
    }
    String serverContextName = "SERVER";
    writeConfiguration(serverContextName, jaasConfigProp);
    AppConfigurationEntry[] staticEntries = Configuration.getConfiguration().getAppConfigurationEntry(serverContextName);
    for (int i = 0; i < moduleCount; i++) {
        AppConfigurationEntry staticEntry = staticEntries[i];
        checkEntry(staticEntry, dynamicEntries[i].getLoginModuleName(), LoginModuleControlFlag.REQUIRED, dynamicEntries[i].getOptions());
    }
}
Also used : Configuration(javax.security.auth.login.Configuration) HashMap(java.util.HashMap) AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 80 with Configuration

use of javax.security.auth.login.Configuration in project apache-kafka-on-k8s by banzaicloud.

the class JaasContext method defaultContext.

private static JaasContext defaultContext(JaasContext.Type contextType, String listenerContextName, String globalContextName) {
    String jaasConfigFile = System.getProperty(JaasUtils.JAVA_LOGIN_CONFIG_PARAM);
    if (jaasConfigFile == null) {
        if (contextType == Type.CLIENT) {
            LOG.debug("System property '" + JaasUtils.JAVA_LOGIN_CONFIG_PARAM + "' and Kafka SASL property '" + SaslConfigs.SASL_JAAS_CONFIG + "' are not set, using default JAAS configuration.");
        } else {
            LOG.debug("System property '" + JaasUtils.JAVA_LOGIN_CONFIG_PARAM + "' is not set, using default JAAS " + "configuration.");
        }
    }
    Configuration jaasConfig = Configuration.getConfiguration();
    AppConfigurationEntry[] configEntries = null;
    String contextName = globalContextName;
    if (listenerContextName != null) {
        configEntries = jaasConfig.getAppConfigurationEntry(listenerContextName);
        if (configEntries != null)
            contextName = listenerContextName;
    }
    if (configEntries == null)
        configEntries = jaasConfig.getAppConfigurationEntry(globalContextName);
    if (configEntries == null) {
        String listenerNameText = listenerContextName == null ? "" : " or '" + listenerContextName + "'";
        String errorMessage = "Could not find a '" + globalContextName + "'" + listenerNameText + " entry in the JAAS " + "configuration. System property '" + JaasUtils.JAVA_LOGIN_CONFIG_PARAM + "' is " + (jaasConfigFile == null ? "not set" : jaasConfigFile);
        throw new IllegalArgumentException(errorMessage);
    }
    return new JaasContext(contextName, contextType, jaasConfig, null);
}
Also used : AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) Configuration(javax.security.auth.login.Configuration)

Aggregations

Configuration (javax.security.auth.login.Configuration)100 AppConfigurationEntry (javax.security.auth.login.AppConfigurationEntry)47 LoginContext (javax.security.auth.login.LoginContext)30 HashMap (java.util.HashMap)27 Subject (javax.security.auth.Subject)22 Test (org.junit.Test)17 IOException (java.io.IOException)15 LoginException (javax.security.auth.login.LoginException)13 File (java.io.File)8 Principal (java.security.Principal)7 CallbackHandler (javax.security.auth.callback.CallbackHandler)7 URI (java.net.URI)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 ArrayList (java.util.ArrayList)6 Test (org.junit.jupiter.api.Test)5 URIParameter (java.security.URIParameter)4 Map (java.util.Map)4 Callback (javax.security.auth.callback.Callback)4 LoginModuleImpl (org.apache.jackrabbit.oak.security.authentication.user.LoginModuleImpl)4 NoSuchProviderException (java.security.NoSuchProviderException)3