Search in sources :

Example 51 with Subject

use of javax.security.auth.Subject in project robovm by robovm.

the class myPrivilegedExceptionAction method test_doAs_02.

/**
     * javax.security.auth.Subject#doAs(Subject subject, PrivilegedExceptionAction action)
     */
public void test_doAs_02() {
    Subject subj = new Subject();
    PrivilegedExceptionAction<Object> pea = new myPrivilegedExceptionAction();
    PrivilegedExceptionAction<Object> peaNull = null;
    try {
        Object obj = Subject.doAs(null, pea);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAs(subj, pea);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAs(subj, peaNull);
        fail("NullPointerException wasn't thrown");
    } catch (NullPointerException npe) {
    } catch (Exception e) {
        fail(e + " was thrown instead of NullPointerException");
    }
    try {
        Subject.doAs(subj, new PrivilegedExceptionAction<Object>() {

            public Object run() throws PrivilegedActionException {
                throw new PrivilegedActionException(null);
            }
        });
        fail("PrivilegedActionException wasn't thrown");
    } catch (PrivilegedActionException e) {
    }
}
Also used : PrivilegedActionException(java.security.PrivilegedActionException) Subject(javax.security.auth.Subject) PrivilegedActionException(java.security.PrivilegedActionException)

Example 52 with Subject

use of javax.security.auth.Subject in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testInitSecurityAwarePrototypeBean.

@SuppressWarnings("unchecked")
@Test
public void testInitSecurityAwarePrototypeBean() {
    final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestSecuredBean.class);
    bd.setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE);
    bd.setInitMethodName("init");
    lbf.registerBeanDefinition("test", bd);
    final Subject subject = new Subject();
    subject.getPrincipals().add(new TestPrincipal("user1"));
    TestSecuredBean bean = (TestSecuredBean) Subject.doAsPrivileged(subject, new PrivilegedAction() {

        @Override
        public Object run() {
            return lbf.getBean("test");
        }
    }, null);
    assertNotNull(bean);
    assertEquals("user1", bean.getUserName());
}
Also used : PrivilegedAction(java.security.PrivilegedAction) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 53 with Subject

use of javax.security.auth.Subject in project spring-framework by spring-projects.

the class CallbacksSecurityTests method testInitSecurityAwarePrototypeBean.

@Test
public void testInitSecurityAwarePrototypeBean() {
    final DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    BeanDefinitionBuilder bdb = BeanDefinitionBuilder.genericBeanDefinition(NonPrivilegedBean.class).setScope(ConfigurableBeanFactory.SCOPE_PROTOTYPE).setInitMethodName("init").setDestroyMethodName("destroy").addConstructorArgValue("user1");
    lbf.registerBeanDefinition("test", bdb.getBeanDefinition());
    final Subject subject = new Subject();
    subject.getPrincipals().add(new TestPrincipal("user1"));
    NonPrivilegedBean bean = Subject.doAsPrivileged(subject, new PrivilegedAction<NonPrivilegedBean>() {

        @Override
        public NonPrivilegedBean run() {
            return lbf.getBean("test", NonPrivilegedBean.class);
        }
    }, null);
    assertNotNull(bean);
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Subject(javax.security.auth.Subject) Test(org.junit.Test)

Example 54 with Subject

use of javax.security.auth.Subject in project jstorm by alibaba.

the class BlobStoreTest method getSubject.

public Subject getSubject(String name) {
    Subject subject = new Subject();
    SingleUserPrincipal user = new SingleUserPrincipal(name);
    subject.getPrincipals().add(user);
    return subject;
}
Also used : SingleUserPrincipal(backtype.storm.security.auth.SingleUserPrincipal) Subject(javax.security.auth.Subject)

Example 55 with Subject

use of javax.security.auth.Subject in project jstorm by alibaba.

the class KerberosSaslTransportPlugin method getServerTransportFactory.

public TTransportFactory getServerTransportFactory() throws IOException {
    // create an authentication callback handler
    CallbackHandler server_callback_handler = new ServerCallbackHandler(login_conf, storm_conf);
    // login our principal
    Subject subject = null;
    try {
        // specify a configuration object to be used
        Configuration.setConfiguration(login_conf);
        // now login
        Login login = new Login(AuthUtils.LOGIN_CONTEXT_SERVER, server_callback_handler);
        subject = login.getSubject();
    } catch (LoginException ex) {
        LOG.error("Server failed to login in principal:" + ex, ex);
        throw new RuntimeException(ex);
    }
    // check the credential of our principal
    if (subject.getPrivateCredentials(KerberosTicket.class).isEmpty()) {
        throw new RuntimeException("Fail to verify user principal with section \"" + AuthUtils.LOGIN_CONTEXT_SERVER + "\" in login configuration file " + login_conf);
    }
    String principal = AuthUtils.get(login_conf, AuthUtils.LOGIN_CONTEXT_SERVER, "principal");
    LOG.debug("principal:" + principal);
    KerberosName serviceKerberosName = new KerberosName(principal);
    String serviceName = serviceKerberosName.getServiceName();
    String hostName = serviceKerberosName.getHostName();
    Map<String, String> props = new TreeMap<String, String>();
    props.put(Sasl.QOP, "auth");
    props.put(Sasl.SERVER_AUTH, "false");
    // create a transport factory that will invoke our auth callback for digest
    TSaslServerTransport.Factory factory = new TSaslServerTransport.Factory();
    factory.addServerDefinition(KERBEROS, serviceName, hostName, props, server_callback_handler);
    // create a wrap transport factory so that we could apply user credential during connections
    TUGIAssumingTransportFactory wrapFactory = new TUGIAssumingTransportFactory(factory, subject);
    LOG.info("SASL GSSAPI transport factory will be used");
    return wrapFactory;
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) LoggerFactory(org.slf4j.LoggerFactory) TTransportFactory(org.apache.thrift.transport.TTransportFactory) Login(org.apache.zookeeper.Login) KerberosName(org.apache.zookeeper.server.auth.KerberosName) TreeMap(java.util.TreeMap) Subject(javax.security.auth.Subject) TSaslServerTransport(org.apache.thrift.transport.TSaslServerTransport) LoginException(javax.security.auth.login.LoginException)

Aggregations

Subject (javax.security.auth.Subject)669 Test (org.testng.annotations.Test)131 Test (org.junit.Test)122 HashMap (java.util.HashMap)120 Principal (java.security.Principal)114 HashSet (java.util.HashSet)109 Set (java.util.Set)82 EntitlementException (com.sun.identity.entitlement.EntitlementException)64 LoginContext (javax.security.auth.login.LoginContext)62 LoginException (javax.security.auth.login.LoginException)49 ConditionDecision (com.sun.identity.entitlement.ConditionDecision)47 ResourceResponse (org.forgerock.json.resource.ResourceResponse)47 RealmContext (org.forgerock.openam.rest.RealmContext)46 Context (org.forgerock.services.context.Context)41 SSOToken (com.iplanet.sso.SSOToken)40 IOException (java.io.IOException)40 ClientContext (org.forgerock.services.context.ClientContext)40 Map (java.util.Map)38 SSOTokenContext (org.forgerock.openam.rest.resource.SSOTokenContext)38 ResourceException (org.forgerock.json.resource.ResourceException)37