Search in sources :

Example 1 with Credential

use of org.apache.kerby.kerberos.kerb.ccache.Credential 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();
}
Also used : TgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) TgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket) Ticket(org.apache.kerby.kerberos.kerb.type.ticket.Ticket) SgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.SgtTicket) GSSCredential(org.ietf.jgss.GSSCredential) Credential(org.apache.kerby.kerberos.kerb.ccache.Credential) KrbTokenClient(org.apache.kerby.kerberos.kerb.client.KrbTokenClient) AuthorizationData(org.apache.kerby.kerberos.kerb.type.ad.AuthorizationData) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) SgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.SgtTicket) AuthorizationDataEntry(org.apache.kerby.kerberos.kerb.type.ad.AuthorizationDataEntry) EncryptionKey(org.apache.kerby.kerberos.kerb.type.base.EncryptionKey) KrbClient(org.apache.kerby.kerberos.kerb.client.KrbClient) EncTicketPart(org.apache.kerby.kerberos.kerb.type.ticket.EncTicketPart) Date(java.util.Date) GSSException(org.ietf.jgss.GSSException) KrbException(org.apache.kerby.kerberos.kerb.KrbException) KrbToken(org.apache.kerby.kerberos.kerb.type.base.KrbToken) CredentialCache(org.apache.kerby.kerberos.kerb.ccache.CredentialCache) AdToken(org.apache.kerby.kerberos.kerb.type.ad.AdToken) File(java.io.File)

Example 2 with Credential

use of org.apache.kerby.kerberos.kerb.ccache.Credential in project testcases by coheigea.

the class TokenPreAuthTest method unitTokenAuthGSSTest.

// Use the TokenAuthLoginModule in Kerby to log in to the KDC using a JWT token
@org.junit.Test
public void unitTokenAuthGSSTest() throws Exception {
    // 1. Get a TGT from the KDC for the client + create an armor cache
    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);
    // Now read in JAAS config + substitute in the armor cache file path value
    String basedir = System.getProperty("basedir");
    if (basedir == null) {
        basedir = new File(".").getCanonicalPath();
    }
    File f = new File(basedir + "/target/test-classes/kerberos/kerberos.jaas");
    FileInputStream inputStream = new FileInputStream(f);
    String content = IOUtils.toString(inputStream, "UTF-8");
    inputStream.close();
    content = content.replaceAll("armorCacheVal", cCacheFile.getPath());
    File f2 = new File(basedir + "/target/test-classes/kerberos/kerberos.jaas");
    FileOutputStream outputStream = new FileOutputStream(f2);
    IOUtils.write(content, outputStream, "UTF-8");
    outputStream.close();
    // 2. 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 = "krbtgt/service.ws.apache.org@service.ws.apache.org";
    claims.setAudiences(Collections.singletonList(address));
    KeyStore keystore = KeyStore.getInstance("JKS");
    keystore.load(Loader.getResourceAsStream("clientstore.jks"), "cspass".toCharArray());
    Properties signingProperties = new Properties();
    signingProperties.put(JoseConstants.RSSEC_SIGNATURE_ALGORITHM, SignatureAlgorithm.RS256.name());
    signingProperties.put(JoseConstants.RSSEC_KEY_STORE, keystore);
    signingProperties.put(JoseConstants.RSSEC_KEY_STORE_ALIAS, "myclientkey");
    signingProperties.put(JoseConstants.RSSEC_KEY_PSWD, "ckpass");
    JwsHeaders jwsHeaders = new JwsHeaders(signingProperties);
    JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);
    JwsSignatureProvider sigProvider = JwsUtils.loadSignatureProvider(signingProperties, jwsHeaders);
    String signedToken = jws.signWith(sigProvider);
    // Store the JWT token in the token cache
    File tokenCache = new File(basedir + "/target/tokencache.txt");
    if (!tokenCache.exists()) {
        tokenCache.createNewFile();
    }
    TokenCache.writeToken(signedToken, tokenCache.getPath());
    // 3. Now log in using JAAS
    LoginContext loginContext = new LoginContext("aliceTokenAuth", new KerberosCallbackHandler());
    loginContext.login();
    Subject clientSubject = loginContext.getSubject();
    // Set<Principal> clientPrincipals = clientSubject.getPrincipals();
    // assertFalse(clientPrincipals.isEmpty());
    // Get the TGT
    Set<KerberosTicket> privateCredentials = clientSubject.getPrivateCredentials(KerberosTicket.class);
    assertFalse(privateCredentials.isEmpty());
    // Get the service ticket using GSS
    KerberosClientExceptionAction action = new KerberosClientExceptionAction(new KerberosPrincipal("alice@service.ws.apache.org"), "bob@service.ws.apache.org");
    byte[] ticket = (byte[]) Subject.doAs(clientSubject, action);
    assertNotNull(ticket);
    loginContext.logout();
    validateServiceTicket(ticket);
    cCacheFile.delete();
    tokenCache.delete();
}
Also used : TgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket) KerberosPrincipal(javax.security.auth.kerberos.KerberosPrincipal) GSSCredential(org.ietf.jgss.GSSCredential) Credential(org.apache.kerby.kerberos.kerb.ccache.Credential) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) KrbClient(org.apache.kerby.kerberos.kerb.client.KrbClient) Properties(java.util.Properties) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Date(java.util.Date) Subject(javax.security.auth.Subject) JwsHeaders(org.apache.cxf.rs.security.jose.jws.JwsHeaders) JwsJwtCompactProducer(org.apache.cxf.rs.security.jose.jws.JwsJwtCompactProducer) LoginContext(javax.security.auth.login.LoginContext) FileOutputStream(java.io.FileOutputStream) CredentialCache(org.apache.kerby.kerberos.kerb.ccache.CredentialCache) File(java.io.File) JwsSignatureProvider(org.apache.cxf.rs.security.jose.jws.JwsSignatureProvider)

Example 3 with Credential

use of org.apache.kerby.kerberos.kerb.ccache.Credential in project testcases by coheigea.

the class TokenPreAuthTest method jwtUnitTestIdentity.

@org.junit.Test
public void jwtUnitTestIdentity() 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 = "krbtgt/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, true);
    krbToken.sign();
    // Now get a TGT using the JWT token
    tgt = tokenClient.requestTgt(krbToken, cCacheFile.getPath());
    // Now get a SGT using the TGT
    SgtTicket tkt;
    try {
        tkt = tokenClient.requestSgt(tgt, "bob/service.ws.apache.org@service.ws.apache.org");
        assertTrue(tkt != null);
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail();
    }
    cCacheFile.delete();
}
Also used : TgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket) GSSCredential(org.ietf.jgss.GSSCredential) Credential(org.apache.kerby.kerberos.kerb.ccache.Credential) KrbTokenClient(org.apache.kerby.kerberos.kerb.client.KrbTokenClient) JwtClaims(org.apache.cxf.rs.security.jose.jwt.JwtClaims) SgtTicket(org.apache.kerby.kerberos.kerb.type.ticket.SgtTicket) CredentialCache(org.apache.kerby.kerberos.kerb.ccache.CredentialCache) KrbClient(org.apache.kerby.kerberos.kerb.client.KrbClient) File(java.io.File) Date(java.util.Date) GSSException(org.ietf.jgss.GSSException) KrbException(org.apache.kerby.kerberos.kerb.KrbException)

Aggregations

File (java.io.File)3 Date (java.util.Date)3 JwtClaims (org.apache.cxf.rs.security.jose.jwt.JwtClaims)3 Credential (org.apache.kerby.kerberos.kerb.ccache.Credential)3 CredentialCache (org.apache.kerby.kerberos.kerb.ccache.CredentialCache)3 KrbClient (org.apache.kerby.kerberos.kerb.client.KrbClient)3 TgtTicket (org.apache.kerby.kerberos.kerb.type.ticket.TgtTicket)3 GSSCredential (org.ietf.jgss.GSSCredential)3 KerberosTicket (javax.security.auth.kerberos.KerberosTicket)2 KrbException (org.apache.kerby.kerberos.kerb.KrbException)2 KrbTokenClient (org.apache.kerby.kerberos.kerb.client.KrbTokenClient)2 SgtTicket (org.apache.kerby.kerberos.kerb.type.ticket.SgtTicket)2 GSSException (org.ietf.jgss.GSSException)2 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 KeyStore (java.security.KeyStore)1 Properties (java.util.Properties)1 Subject (javax.security.auth.Subject)1 KerberosPrincipal (javax.security.auth.kerberos.KerberosPrincipal)1 LoginContext (javax.security.auth.login.LoginContext)1