use of javax.security.auth.kerberos.KerberosTicket in project jdk8u_jdk by JetBrains.
the class KerberosTixDateTest method checkEqualsAndHashCode.
private static void checkEqualsAndHashCode(byte[] bytes, KerberosTicket t) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
KerberosTicket deserializedTicket = (KerberosTicket) (new ObjectInputStream(bais).readObject());
if (!deserializedTicket.equals(t)) {
throw new RuntimeException("equals() check fails!");
}
if (deserializedTicket.hashCode() != t.hashCode()) {
throw new RuntimeException("hashCode() check fails!");
}
}
use of javax.security.auth.kerberos.KerberosTicket in project calcite-avatica by apache.
the class HttpServerSpnegoWithoutJaasTest method testAuthenticatedClientsAllowed.
@Test
public void testAuthenticatedClientsAllowed() throws Exception {
// Create the subject for the client
final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(SpnegoTestUtil.CLIENT_PRINCIPAL, clientKeytab);
final Set<Principal> clientPrincipals = clientSubject.getPrincipals();
// Make sure the subject has a principal
assertFalse(clientPrincipals.isEmpty());
// Get a TGT for the subject (might have many, different encryption types). The first should
// be the default encryption type.
Set<KerberosTicket> privateCredentials = clientSubject.getPrivateCredentials(KerberosTicket.class);
assertFalse(privateCredentials.isEmpty());
KerberosTicket tgt = privateCredentials.iterator().next();
assertNotNull(tgt);
LOG.info("Using TGT with etype: {}", tgt.getSessionKey().getAlgorithm());
// The name of the principal
final String principalName = clientPrincipals.iterator().next().getName();
// Run this code, logged in as the subject (the client)
byte[] response = Subject.doAs(clientSubject, new PrivilegedExceptionAction<byte[]>() {
@Override
public byte[] run() throws Exception {
// Logs in with Kerberos via GSS
GSSManager gssManager = GSSManager.getInstance();
Oid oid = new Oid(SpnegoTestUtil.JGSS_KERBEROS_TICKET_OID);
GSSName gssClient = gssManager.createName(principalName, GSSName.NT_USER_NAME);
GSSCredential credential = gssManager.createCredential(gssClient, GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY);
// Passes the GSSCredential into the HTTP client implementation
final AvaticaCommonsHttpClientSpnegoImpl httpClient = new AvaticaCommonsHttpClientSpnegoImpl(httpServerUrl, credential);
return httpClient.send(new byte[0]);
}
});
// We should get a response which is "OK" with our client's name
assertNotNull(response);
assertEquals("OK " + SpnegoTestUtil.CLIENT_PRINCIPAL, new String(response, StandardCharsets.UTF_8));
}
use of javax.security.auth.kerberos.KerberosTicket in project calcite-avatica by apache.
the class HttpServerSpnegoWithJaasTest method testAuthenticatedClientsAllowed.
@Test
public void testAuthenticatedClientsAllowed() throws Exception {
Assume.assumeThat("Test disabled on Windows", File.separatorChar, is('/'));
// Create the subject for the client
final Subject clientSubject = JaasKrbUtil.loginUsingKeytab(SpnegoTestUtil.CLIENT_PRINCIPAL, clientKeytab);
final Set<Principal> clientPrincipals = clientSubject.getPrincipals();
// Make sure the subject has a principal
assertFalse(clientPrincipals.isEmpty());
// Get a TGT for the subject (might have many, different encryption types). The first should
// be the default encryption type.
Set<KerberosTicket> privateCredentials = clientSubject.getPrivateCredentials(KerberosTicket.class);
assertFalse(privateCredentials.isEmpty());
KerberosTicket tgt = privateCredentials.iterator().next();
assertNotNull(tgt);
LOG.info("Using TGT with etype: {}", tgt.getSessionKey().getAlgorithm());
// The name of the principal
final String principalName = clientPrincipals.iterator().next().getName();
// Run this code, logged in as the subject (the client)
byte[] response = Subject.doAs(clientSubject, new PrivilegedExceptionAction<byte[]>() {
@Override
public byte[] run() throws Exception {
// Logs in with Kerberos via GSS
GSSManager gssManager = GSSManager.getInstance();
Oid oid = new Oid(SpnegoTestUtil.JGSS_KERBEROS_TICKET_OID);
GSSName gssClient = gssManager.createName(principalName, GSSName.NT_USER_NAME);
GSSCredential credential = gssManager.createCredential(gssClient, GSSCredential.DEFAULT_LIFETIME, oid, GSSCredential.INITIATE_ONLY);
// Passes the GSSCredential into the HTTP client implementation
final AvaticaCommonsHttpClientSpnegoImpl httpClient = new AvaticaCommonsHttpClientSpnegoImpl(httpServerUrl, credential);
return httpClient.send(new byte[0]);
}
});
// We should get a response which is "OK" with our client's name
assertNotNull(response);
assertEquals("OK " + SpnegoTestUtil.CLIENT_PRINCIPAL, new String(response, StandardCharsets.UTF_8));
}
use of javax.security.auth.kerberos.KerberosTicket in project karaf by apache.
the class Krb5LoginModuleTest method testLoginSuccess.
@Test
public void testLoginSuccess() throws Exception {
Subject subject = new Subject();
Krb5LoginModule module = new Krb5LoginModule();
module.initialize(subject, new NamePasswordCallbackHandler("hnelson", "secret"), null, new HashMap<>());
assertEquals("Precondition", 0, subject.getPrincipals().size());
Assert.assertTrue(module.login());
Assert.assertTrue(module.commit());
assertEquals(1, subject.getPrincipals().size());
boolean foundUser = false;
for (Principal pr : subject.getPrincipals()) {
if (pr instanceof KerberosPrincipal) {
assertEquals("hnelson@EXAMPLE.COM", pr.getName());
foundUser = true;
break;
}
}
assertTrue(foundUser);
boolean foundToken = false;
for (Object crd : subject.getPrivateCredentials()) {
if (crd instanceof KerberosTicket) {
assertEquals("hnelson@EXAMPLE.COM", ((KerberosTicket) crd).getClient().getName());
assertEquals("krbtgt/EXAMPLE.COM@EXAMPLE.COM", ((KerberosTicket) crd).getServer().getName());
foundToken = true;
break;
}
}
assertTrue(foundToken);
Assert.assertTrue(module.logout());
}
use of javax.security.auth.kerberos.KerberosTicket in project karaf by apache.
the class GSSAPILdapLoginModuleTest method testSuccess.
@Test
public void testSuccess() throws Exception {
Properties options = ldapLoginModuleOptions();
GSSAPILdapLoginModule module = new GSSAPILdapLoginModule();
Subject subject = new Subject();
module.initialize(subject, new NamePasswordCallbackHandler("hnelson", "secret"), null, options);
assertEquals("Precondition", 0, subject.getPrincipals().size());
assertTrue(module.login());
assertTrue(module.commit());
assertEquals(3, subject.getPrincipals().size());
boolean foundKrb5User = false;
boolean foundUser = false;
boolean foundRole = false;
boolean foundTicket = false;
for (Principal pr : subject.getPrincipals()) {
if (pr instanceof KerberosPrincipal) {
assertEquals("hnelson@EXAMPLE.COM", pr.getName());
foundKrb5User = true;
} else if (pr instanceof UserPrincipal) {
assertEquals("hnelson", pr.getName());
foundUser = true;
} else if (pr instanceof RolePrincipal) {
assertEquals("admin", pr.getName());
foundRole = true;
}
}
for (Object crd : subject.getPrivateCredentials()) {
if (crd instanceof KerberosTicket) {
assertEquals("hnelson@EXAMPLE.COM", ((KerberosTicket) crd).getClient().getName());
assertEquals("krbtgt/EXAMPLE.COM@EXAMPLE.COM", ((KerberosTicket) crd).getServer().getName());
foundTicket = true;
break;
}
}
assertTrue("Principals should contains kerberos user", foundKrb5User);
assertTrue("Principals should contains ldap user", foundUser);
assertTrue("Principals should contains ldap role", foundRole);
assertTrue("PricatePrincipals should contains kerberos ticket", foundTicket);
assertTrue(module.logout());
assertEquals("Principals should be gone as the user has logged out", 0, subject.getPrincipals().size());
}
Aggregations