Search in sources :

Example 46 with Subject

use of javax.security.auth.Subject in project orientdb by orientechnologies.

the class OKerberosCredentialInterceptor method intercept.

public void intercept(final String url, final String principal, final String spn) throws OSecurityException {
    // it may contain multiple principals.
    if (principal == null || principal.isEmpty())
        throw new OSecurityException("OKerberosCredentialInterceptor Principal cannot be null!");
    this.principal = principal;
    String actualSPN = spn;
    // spn should be the SPN of the service.
    if (spn == null || spn.isEmpty()) {
        //		OrientDB/host
        if (url == null || url.isEmpty())
            throw new OSecurityException("OKerberosCredentialInterceptor URL and SPN cannot both be null!");
        try {
            String tempURL = url;
            // Without the // URI can't parse URLs correctly, so we add //.
            if (tempURL.startsWith("remote:") && !tempURL.startsWith("remote://"))
                tempURL = tempURL.replace("remote:", "remote://");
            URI remoteURI = new URI(tempURL);
            String host = remoteURI.getHost();
            if (host == null)
                throw new OSecurityException("OKerberosCredentialInterceptor Could not create SPN from URL: " + url);
            actualSPN = "OrientDB/" + host;
        } catch (URISyntaxException ex) {
            throw new OSecurityException("OKerberosCredentialInterceptor Could not create SPN from URL: " + url);
        }
    }
    // Defaults to the environment variable.
    String config = System.getenv("KRB5_CONFIG");
    String ckc = OGlobalConfiguration.CLIENT_KRB5_CONFIG.getValueAsString();
    if (ckc != null)
        config = ckc;
    // Defaults to the environment variable.
    String ccname = System.getenv("KRB5CCNAME");
    String ccn = OGlobalConfiguration.CLIENT_KRB5_CCNAME.getValueAsString();
    if (ccn != null)
        ccname = ccn;
    // Defaults to the environment variable.
    String ktname = System.getenv("KRB5_CLIENT_KTNAME");
    String ckn = OGlobalConfiguration.CLIENT_KRB5_KTNAME.getValueAsString();
    if (ckn != null)
        ktname = ckn;
    if (config == null)
        throw new OSecurityException("OKerberosCredentialInterceptor KRB5 Config cannot be null!");
    if (ccname == null && ktname == null)
        throw new OSecurityException("OKerberosCredentialInterceptor KRB5 Credential Cache and KeyTab cannot both be null!");
    LoginContext lc = null;
    try {
        System.setProperty("java.security.krb5.conf", config);
        OKrb5ClientLoginModuleConfig cfg = new OKrb5ClientLoginModuleConfig(principal, ccname, ktname);
        lc = new LoginContext("ignore", null, null, cfg);
        lc.login();
    } catch (LoginException lie) {
        OLogManager.instance().debug(this, "intercept() LoginException", lie);
        throw new OSecurityException("OKerberosCredentialInterceptor Client Validation Exception!");
    }
    Subject subject = lc.getSubject();
    // Assign the client's principal name.
    //		this.principal = getFirstPrincipal(subject);
    //		if(this.principal == null) throw new OSecurityException("OKerberosCredentialInterceptor Cannot obtain client principal!");
    this.serviceTicket = getServiceTicket(subject, principal, actualSPN);
    try {
        lc.logout();
    } catch (LoginException loe) {
        OLogManager.instance().debug(this, "intercept() LogoutException", loe);
    }
    if (this.serviceTicket == null)
        throw new OSecurityException("OKerberosCredentialInterceptor Cannot obtain the service ticket!");
}
Also used : LoginContext(javax.security.auth.login.LoginContext) LoginException(javax.security.auth.login.LoginException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) Subject(javax.security.auth.Subject)

Example 47 with Subject

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

the class GitHub668Test method shouldMockJavaxSystemFinalClasses.

@Test
public void shouldMockJavaxSystemFinalClasses() {
    Subject subject = mock(Subject.class);
    final HashSet<Object> value = new HashSet<Object>();
    when(subject.getPrivateCredentials()).thenReturn(value);
    assertThat(subject.getPrivateCredentials()).isSameAs(value);
}
Also used : Subject(javax.security.auth.Subject) HashSet(java.util.HashSet) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 48 with Subject

use of javax.security.auth.Subject in project presto by prestodb.

the class KerberosAuthentication method getSubject.

public Subject getSubject() {
    Subject subject = new Subject(false, ImmutableSet.of(principal), emptySet(), emptySet());
    try {
        LoginContext loginContext = new LoginContext("", subject, null, configuration);
        loginContext.login();
        return loginContext.getSubject();
    } catch (LoginException e) {
        throw Throwables.propagate(e);
    }
}
Also used : LoginContext(javax.security.auth.login.LoginContext) LoginException(javax.security.auth.login.LoginException) Subject(javax.security.auth.Subject)

Example 49 with Subject

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

the class myPrivilegedExceptionAction method test_doAsPrivileged_01.

/**
     * javax.security.auth.Subject#doAsPrivileged(Subject subject,
     *                                                   PrivilegedAction action,
     *                                                   AccessControlContext acc)
     */
public void test_doAsPrivileged_01() {
    Subject subj = new Subject();
    PrivilegedAction<Object> pa = new myPrivilegedAction();
    PrivilegedAction<Object> paNull = null;
    AccessControlContext acc = AccessController.getContext();
    try {
        Object obj = Subject.doAsPrivileged(null, pa, acc);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAsPrivileged(subj, pa, acc);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAsPrivileged(subj, paNull, acc);
        fail("NullPointerException wasn't thrown");
    } catch (NullPointerException npe) {
    }
}
Also used : AccessControlContext(java.security.AccessControlContext) Subject(javax.security.auth.Subject) PrivilegedActionException(java.security.PrivilegedActionException)

Example 50 with Subject

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

the class myPrivilegedExceptionAction method test_doAs_01.

/**
     * javax.security.auth.Subject#doAs(Subject subject, PrivilegedAction action)
     */
public void test_doAs_01() {
    Subject subj = new Subject();
    PrivilegedAction<Object> pa = new myPrivilegedAction();
    PrivilegedAction<Object> paNull = null;
    try {
        Object obj = Subject.doAs(null, pa);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAs(subj, pa);
    } catch (Exception e) {
        fail("Unexpected exception: " + e);
    }
    try {
        Object obj = Subject.doAs(subj, paNull);
        fail("NullPointerException wasn't thrown");
    } catch (NullPointerException npe) {
    }
}
Also used : Subject(javax.security.auth.Subject) PrivilegedActionException(java.security.PrivilegedActionException)

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