use of org.apache.kerby.kerberos.kerb.type.ticket.EncTicketPart in project testcases by coheigea.
the class TokenPreAuthTest method jwtUnitTestAccess.
@org.junit.Test
public void jwtUnitTestAccess() throws Exception {
// Get a TGT
KrbClient client = new KrbClient();
client.setKdcHost("localhost");
client.setKdcTcpPort(kerbyServer.getKdcPort());
client.setAllowUdp(false);
client.setKdcRealm(kerbyServer.getKdcSetting().getKdcRealm());
client.init();
TgtTicket tgt = client.requestTgt("alice@service.ws.apache.org", "alice");
assertNotNull(tgt);
// Write to cache
Credential credential = new Credential(tgt);
CredentialCache cCache = new CredentialCache();
cCache.addCredential(credential);
cCache.setPrimaryPrincipal(tgt.getClientPrincipal());
File cCacheFile = File.createTempFile("krb5_alice@service.ws.apache.org", "cc");
cCache.store(cCacheFile);
KrbTokenClient tokenClient = new KrbTokenClient(client);
tokenClient.setKdcHost("localhost");
tokenClient.setKdcTcpPort(kerbyServer.getKdcPort());
tokenClient.setAllowUdp(false);
tokenClient.setKdcRealm(kerbyServer.getKdcSetting().getKdcRealm());
tokenClient.init();
// Create a JWT token using CXF
JwtClaims claims = new JwtClaims();
claims.setSubject("alice");
claims.setIssuer("DoubleItSTSIssuer");
claims.setIssuedAt(new Date().getTime() / 1000L);
claims.setExpiryTime(new Date().getTime() + (60L + 1000L));
String address = "bob/service.ws.apache.org@service.ws.apache.org";
claims.setAudiences(Collections.singletonList(address));
// Wrap it in a KrbToken + sign it
CXFKrbToken krbToken = new CXFKrbToken(claims, false);
krbToken.sign();
// Now get a SGT using the JWT
SgtTicket tkt;
try {
tkt = tokenClient.requestSgt(krbToken, "bob/service.ws.apache.org@service.ws.apache.org", cCacheFile.getPath());
assertTrue(tkt != null);
// Decrypt the ticket
Ticket ticket = tkt.getTicket();
String bob = "bob/service.ws.apache.org@service.ws.apache.org";
EncryptionKey key = EncryptionHandler.string2Key(bob, "bob", ticket.getEncryptedEncPart().getEType());
EncTicketPart encPart = EncryptionUtil.unseal(ticket.getEncryptedEncPart(), key, KeyUsage.KDC_REP_TICKET, EncTicketPart.class);
// Examine the authorization data
AuthorizationData authzData = encPart.getAuthorizationData();
assertEquals(1, authzData.getElements().size());
AuthorizationDataEntry dataEntry = authzData.getElements().iterator().next();
AdToken token = dataEntry.getAuthzDataAs(AdToken.class);
KrbToken decodedKrbToken = token.getToken();
assertEquals("alice", decodedKrbToken.getSubject());
assertEquals(address, decodedKrbToken.getAudiences().get(0));
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
cCacheFile.delete();
}
Aggregations