Search in sources :

Example 96 with Configuration

use of javax.security.auth.login.Configuration in project aries by apache.

the class JAASHelper method doAs.

public static <T> void doAs(final String[] groups, PrivilegedAction<T> action) {
    Configuration config = new Configuration() {

        @Override
        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
            Map<String, Object> options = new HashMap<String, Object>();
            // The user does not matter
            options.put("username", "dummy");
            options.put("groups", groups);
            AppConfigurationEntry entry = new AppConfigurationEntry(SimpleLoginModule.class.getName(), LoginModuleControlFlag.REQUIRED, options);
            return new AppConfigurationEntry[] { entry };
        }
    };
    try {
        LoginContext lc = new LoginContext("test", new Subject(), null, config);
        lc.login();
        Subject.doAs(lc.getSubject(), action);
        lc.logout();
    } catch (LoginException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) LoginContext(javax.security.auth.login.LoginContext) Configuration(javax.security.auth.login.Configuration) HashMap(java.util.HashMap) LoginException(javax.security.auth.login.LoginException) Subject(javax.security.auth.Subject)

Example 97 with Configuration

use of javax.security.auth.login.Configuration in project tomcat by apache.

the class JAASRealm method getConfig.

/**
 * Load custom JAAS Configuration.
 * @return the loaded configuration
 */
protected Configuration getConfig() {
    // Local copy to avoid possible NPE due to concurrent change
    String configFile = this.configFile;
    try {
        if (jaasConfigurationLoaded) {
            return jaasConfiguration;
        }
        synchronized (this) {
            if (configFile == null) {
                jaasConfigurationLoaded = true;
                return null;
            }
            URL resource = Thread.currentThread().getContextClassLoader().getResource(configFile);
            URI uri = resource.toURI();
            @SuppressWarnings("unchecked") Class<Configuration> sunConfigFile = (Class<Configuration>) Class.forName("com.sun.security.auth.login.ConfigFile");
            Constructor<Configuration> constructor = sunConfigFile.getConstructor(URI.class);
            Configuration config = constructor.newInstance(uri);
            this.jaasConfiguration = config;
            this.jaasConfigurationLoaded = true;
            return this.jaasConfiguration;
        }
    } catch (InvocationTargetException ex) {
        throw new RuntimeException(ex.getCause());
    } catch (SecurityException | URISyntaxException | ReflectiveOperationException | IllegalArgumentException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Configuration(javax.security.auth.login.Configuration) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URL(java.net.URL) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 98 with Configuration

use of javax.security.auth.login.Configuration in project keycloak by keycloak.

the class LoginModulesTest method createJaasConfigurationForDirectGrant.

private Configuration createJaasConfigurationForDirectGrant(String scope) {
    return new Configuration() {

        @Override
        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
            Map<String, Object> options = new HashMap<>();
            options.put(AbstractKeycloakLoginModule.KEYCLOAK_CONFIG_FILE_OPTION, DIRECT_GRANT_CONFIG_FILE.getAbsolutePath());
            if (scope != null) {
                options.put(DirectAccessGrantsLoginModule.SCOPE_OPTION, scope);
            }
            AppConfigurationEntry LMConfiguration = new AppConfigurationEntry(DirectAccessGrantsLoginModule.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, options);
            return new AppConfigurationEntry[] { LMConfiguration };
        }
    };
}
Also used : AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) Configuration(javax.security.auth.login.Configuration) HashMap(java.util.HashMap) DirectAccessGrantsLoginModule(org.keycloak.adapters.jaas.DirectAccessGrantsLoginModule)

Example 99 with Configuration

use of javax.security.auth.login.Configuration in project karaf by apache.

the class KarafJaasAuthenticatorTest method init.

@Before
public void init() throws Exception {
    configuration = Configuration.getConfiguration();
    Configuration.setConfiguration(new Configuration() {

        @Override
        public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
            return new AppConfigurationEntry[] { new AppConfigurationEntry(SayYes.class.getName(), AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, emptyMap()) };
        }
    });
    final SshServer server = new SshServer();
    IoHandler ioHandler = new IoHandler() {

        @Override
        public void sessionCreated(IoSession ioSession) throws Exception {
        }

        @Override
        public void sessionClosed(IoSession ioSession) throws Exception {
        }

        @Override
        public void exceptionCaught(IoSession ioSession, Throwable throwable) throws Exception {
        }

        @Override
        public void messageReceived(IoSession ioSession, Readable readable) throws Exception {
        }
    };
    IoProcessor ioProcessor = new IoProcessor() {

        @Override
        public boolean isDisposing() {
            return false;
        }

        @Override
        public boolean isDisposed() {
            return false;
        }

        @Override
        public void dispose() {
        }

        @Override
        public void add(org.apache.mina.core.session.IoSession ioSession) {
        }

        @Override
        public void flush(org.apache.mina.core.session.IoSession ioSession) {
        }

        @Override
        public void write(org.apache.mina.core.session.IoSession ioSession, WriteRequest writeRequest) {
        }

        @Override
        public void updateTrafficControl(org.apache.mina.core.session.IoSession ioSession) {
        }

        @Override
        public void remove(org.apache.mina.core.session.IoSession ioSession) {
        }
    };
    server.setRandomFactory(new SingletonRandomFactory(SecurityUtils.getRandomFactory()));
    this.session = new ServerSessionImpl(server, new MinaSession(new MinaConnector(server, ioHandler, ioProcessor), new DummySession(), SshdSocketAddress.LOCALHOST_ADDRESS));
}
Also used : Configuration(javax.security.auth.login.Configuration) WriteRequest(org.apache.mina.core.write.WriteRequest) SingletonRandomFactory(org.apache.sshd.common.random.SingletonRandomFactory) IoProcessor(org.apache.mina.core.service.IoProcessor) MinaSession(org.apache.sshd.mina.MinaSession) SshServer(org.apache.sshd.server.SshServer) IoHandler(org.apache.sshd.common.io.IoHandler) AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) MinaConnector(org.apache.sshd.mina.MinaConnector) ServerSessionImpl(org.apache.sshd.server.session.ServerSessionImpl) Readable(org.apache.sshd.common.util.Readable) DummySession(org.apache.mina.core.session.DummySession) IoSession(org.apache.sshd.common.io.IoSession) Before(org.junit.Before)

Example 100 with Configuration

use of javax.security.auth.login.Configuration in project knox by apache.

the class RemoteConfigurationRegistryClientServiceTestBase method setupAndStartSecureTestZooKeeper.

/*
     * Setup and start a secure test ZooKeeper cluster.
     */
protected TestingCluster setupAndStartSecureTestZooKeeper(String principal, String digestPassword) throws Exception {
    final boolean applyAuthentication = (principal != null);
    // Configure security for the ZK cluster instances
    Map<String, Object> customInstanceSpecProps = new HashMap<>();
    customInstanceSpecProps.put("admin.enableServer", false);
    if (applyAuthentication) {
        customInstanceSpecProps.put("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider");
        customInstanceSpecProps.put("requireClientAuthScheme", "sasl");
    }
    // Define the test cluster
    List<InstanceSpec> instanceSpecs = new ArrayList<>();
    for (int i = 0; i < 1; i++) {
        InstanceSpec is = new InstanceSpec(null, -1, -1, -1, false, (i + 1), -1, -1, customInstanceSpecProps);
        instanceSpecs.add(is);
    }
    TestingCluster zkCluster = new TestingCluster(instanceSpecs);
    if (applyAuthentication) {
        // Setup ZooKeeper server SASL
        Map<String, String> digestOptions = new HashMap<>();
        digestOptions.put("user_" + principal, digestPassword);
        final AppConfigurationEntry[] serverEntries = { new AppConfigurationEntry("org.apache.zookeeper.server.auth.DigestLoginModule", AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, digestOptions) };
        Configuration.setConfiguration(new Configuration() {

            @Override
            public AppConfigurationEntry[] getAppConfigurationEntry(String name) {
                return ("Server".equalsIgnoreCase(name)) ? serverEntries : null;
            }
        });
    }
    // Start the cluster
    zkCluster.start();
    return zkCluster;
}
Also used : InstanceSpec(org.apache.curator.test.InstanceSpec) Configuration(javax.security.auth.login.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AppConfigurationEntry(javax.security.auth.login.AppConfigurationEntry) TestingCluster(org.apache.curator.test.TestingCluster)

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