use of org.apache.cxf.rs.security.jose.jws.JwsHeaders in project cxf by apache.
the class OIDCNegativeTest method testJWTRequestNonmatchingResponseType.
@org.junit.Test
public void testJWTRequestNonmatchingResponseType() throws Exception {
URL busFile = OIDCNegativeTest.class.getResource("client.xml");
String address = "https://localhost:" + port + "/unsignedjwtservices/";
WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
JwtClaims claims = new JwtClaims();
claims.setIssuer("consumer-id");
claims.setIssuedAt(Instant.now().getEpochSecond());
claims.setAudiences(Collections.singletonList("https://localhost:" + port + "/unsignedjwtservices/"));
claims.setProperty("response_type", "token");
JwsHeaders headers = new JwsHeaders();
headers.setAlgorithm("none");
JwtToken token = new JwtToken(headers, claims);
JwsJwtCompactProducer jws = new JwsJwtCompactProducer(token);
String request = jws.getSignedEncodedJws();
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("consumer-id");
parameters.setScope("openid");
parameters.setResponseType("code");
parameters.setPath("authorize/");
parameters.setRequest(request);
// Get Authorization Code
try {
OAuth2TestUtils.getLocation(client, parameters);
fail("Failure expected on a non-matching response_type");
} catch (ResponseProcessingException ex) {
// expected
}
}
use of org.apache.cxf.rs.security.jose.jws.JwsHeaders in project cxf by apache.
the class OIDCNegativeTest method testJWTRequestNonmatchingClientId.
@org.junit.Test
public void testJWTRequestNonmatchingClientId() throws Exception {
URL busFile = OIDCNegativeTest.class.getResource("client.xml");
String address = "https://localhost:" + port + "/unsignedjwtservices/";
WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", busFile.toString());
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
JwtClaims claims = new JwtClaims();
claims.setIssuer("consumer-id");
claims.setIssuedAt(Instant.now().getEpochSecond());
claims.setAudiences(Collections.singletonList("https://localhost:" + port + "/unsignedjwtservices/"));
claims.setProperty("client_id", "consumer-id2");
JwsHeaders headers = new JwsHeaders();
headers.setAlgorithm("none");
JwtToken token = new JwtToken(headers, claims);
JwsJwtCompactProducer jws = new JwsJwtCompactProducer(token);
String request = jws.getSignedEncodedJws();
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("consumer-id");
parameters.setScope("openid");
parameters.setResponseType("code");
parameters.setPath("authorize/");
parameters.setRequest(request);
// Get Authorization Code
try {
OAuth2TestUtils.getLocation(client, parameters);
fail("Failure expected on a non-matching client id");
} catch (ResponseProcessingException ex) {
// expected
}
}
use of org.apache.cxf.rs.security.jose.jws.JwsHeaders in project testcases by coheigea.
the class JWTClientAuthenticationTest method createToken.
private String createToken(String issuer, String subject, String audience, boolean expiry, boolean sign) {
// Create the JWT Token
JwtClaims claims = new JwtClaims();
claims.setSubject(subject);
if (issuer != null) {
claims.setIssuer(issuer);
}
claims.setIssuedAt(new Date().getTime() / 1000L);
if (expiry) {
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 60);
claims.setExpiryTime(cal.getTimeInMillis() / 1000L);
}
if (audience != null) {
claims.setAudiences(Collections.singletonList(audience));
}
if (sign) {
// Sign the JWT Token
Properties signingProperties = new Properties();
signingProperties.put("rs.security.keystore.type", "jks");
signingProperties.put("rs.security.keystore.password", "cspass");
signingProperties.put("rs.security.keystore.alias", "myclientkey");
signingProperties.put("rs.security.keystore.file", "clientstore.jks");
signingProperties.put("rs.security.key.password", "ckpass");
signingProperties.put("rs.security.signature.algorithm", "RS256");
JwsHeaders jwsHeaders = new JwsHeaders(signingProperties);
JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);
JwsSignatureProvider sigProvider = JwsUtils.loadSignatureProvider(signingProperties, jwsHeaders);
return jws.signWith(sigProvider);
}
JwsHeaders jwsHeaders = new JwsHeaders(SignatureAlgorithm.NONE);
JwsJwtCompactProducer jws = new JwsJwtCompactProducer(jwsHeaders, claims);
return jws.getSignedEncodedJws();
}
use of org.apache.cxf.rs.security.jose.jws.JwsHeaders 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();
}
use of org.apache.cxf.rs.security.jose.jws.JwsHeaders in project testcases by coheigea.
the class CXFKrbToken method sign.
public void sign() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
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);
setTokenValue(signedToken.getBytes());
}
Aggregations