use of io.jans.as.model.jwe.Jwe in project jans by JanssenProject.
the class AuthorizationResponseModeJwtResponseTypeCodeSignedEncryptedHttpTest method ensureRequestObjectSignatureAlgorithmIsNotNone.
@Parameters({ "audience", "redirectUri", "redirectUris", "clientJwksUri", "RSA_OAEP_keyId", "dnName", "keyStoreFile", "keyStoreSecret", "sectorIdentifierUri" })
// Enable FAPI to run this test!
@Test(enabled = false)
public void ensureRequestObjectSignatureAlgorithmIsNotNone(final String audience, final String redirectUri, final String redirectUris, final String clientJwksUri, final String encryptionKeyId, final String dnName, final String keyStoreFile, final String keyStoreSecret, final String sectorIdentifierUri) throws Exception {
showTitle("ensureRequestObjectSignatureAlgorithmIsNotNone");
List<ResponseType> responseTypes = Arrays.asList(ResponseType.CODE);
// 1. Dynamic Client Registration
RegisterResponse registerResponse = registerClient(redirectUris, responseTypes, sectorIdentifierUri, clientJwksUri, SignatureAlgorithm.RS256, KeyEncryptionAlgorithm.RSA_OAEP, BlockEncryptionAlgorithm.A256GCM);
String clientId = registerResponse.getClientId();
// 2. Request authorization
List<String> scope = Arrays.asList("openid", "profile", "address", "email");
String state = UUID.randomUUID().toString();
String nonce = UUID.randomUUID().toString();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId, scope, redirectUri, null);
authorizationRequest.setState(state);
AuthCryptoProvider cryptoProvider1 = new AuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
// none Request Object is not permitted by the FAPI-RW specification.
JwtAuthorizationRequest jwsAuthorizationRequest = new JwtAuthorizationRequest(authorizationRequest, SignatureAlgorithm.NONE, cryptoProvider1);
jwsAuthorizationRequest.setAud(audience);
jwsAuthorizationRequest.setIss(clientId);
jwsAuthorizationRequest.setScopes(scope);
jwsAuthorizationRequest.setResponseTypes(responseTypes);
jwsAuthorizationRequest.setRedirectUri(redirectUri);
jwsAuthorizationRequest.setResponseMode(ResponseMode.JWT);
jwsAuthorizationRequest.setClientId(clientId);
jwsAuthorizationRequest.setState(state);
// FAPI: nonce param is required
jwsAuthorizationRequest.setNonce(nonce);
// FAPI: require the request object to contain an exp claim that has a lifetime of no longer than 60 minutes after the nbf claim
jwsAuthorizationRequest.setNbf((int) Instant.now().getEpochSecond());
// FAPI: require the request object to contain an exp claim that has a lifetime of no longer than 60 minutes after the nbf claim
jwsAuthorizationRequest.setExp(jwsAuthorizationRequest.getNbf() + 3600);
Jwt authJws = Jwt.parse(jwsAuthorizationRequest.getEncodedJwt());
JwkClient jwkClient = new JwkClient(jwksUri);
JwkResponse jwkResponse = jwkClient.exec();
String serverKeyId = jwkResponse.getKeyId(Algorithm.RSA_OAEP);
assertNotNull(serverKeyId);
JSONObject jwks = JwtUtil.getJSONWebKeys(jwksUri);
AuthCryptoProvider cryptoProvider2 = new AuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
privateKey = cryptoProvider2.getPrivateKey(encryptionKeyId);
JwtAuthorizationRequest jweAuthorizationRequest = new JwtAuthorizationRequest(authorizationRequest, KeyEncryptionAlgorithm.RSA_OAEP, BlockEncryptionAlgorithm.A256GCM, cryptoProvider2);
jweAuthorizationRequest.setKeyId(serverKeyId);
jweAuthorizationRequest.setNestedPayload(authJws);
String authJwe = jweAuthorizationRequest.getEncodedJwt(jwks);
authorizationRequest.setRequest(authJwe);
AuthorizeClient authorizeClient = new AuthorizeClient(authorizationEndpoint);
authorizeClient.setRequest(authorizationRequest);
AuthorizationResponse authorizationResponse = authorizeClient.exec();
showClient(authorizeClient);
assertNotNull(authorizationResponse.getResponse());
Jwe response = Jwe.parse(authorizationResponse.getResponse(), privateKey, null);
assertJweResponse(response);
// Clear private key to do not affect to other tests
privateKey = null;
}
use of io.jans.as.model.jwe.Jwe in project jans by JanssenProject.
the class AuthorizationResponseModeJwtResponseTypeCodeSignedEncryptedHttpTest method ensureRequestObjectWithBadAudFails.
@Parameters({ "redirectUri", "redirectUris", "clientJwksUri", "RSA_OAEP_keyId", "PS256_keyId", "dnName", "keyStoreFile", "keyStoreSecret", "sectorIdentifierUri" })
// Enable FAPI to run this test!
@Test(enabled = false)
public void ensureRequestObjectWithBadAudFails(final String redirectUri, final String redirectUris, final String clientJwksUri, final String encryptionKeyId, final String signingKeyId, final String dnName, final String keyStoreFile, final String keyStoreSecret, final String sectorIdentifierUri) throws Exception {
showTitle("ensureRequestObjectWithBadAudFails");
List<ResponseType> responseTypes = Arrays.asList(ResponseType.CODE);
// 1. Dynamic Client Registration
RegisterResponse registerResponse = registerClient(redirectUris, responseTypes, sectorIdentifierUri, clientJwksUri, SignatureAlgorithm.PS256, KeyEncryptionAlgorithm.RSA_OAEP, BlockEncryptionAlgorithm.A256GCM);
String clientId = registerResponse.getClientId();
// 2. Request authorization
List<String> scope = Arrays.asList("openid", "profile", "address", "email");
String state = UUID.randomUUID().toString();
String nonce = UUID.randomUUID().toString();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId, scope, redirectUri, null);
AuthCryptoProvider cryptoProvider1 = new AuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
JwtAuthorizationRequest jwsAuthorizationRequest = new JwtAuthorizationRequest(authorizationRequest, SignatureAlgorithm.PS256, cryptoProvider1);
jwsAuthorizationRequest.setKeyId(signingKeyId);
// Added bad aud to request object claims
jwsAuthorizationRequest.setAud("https://www.other1.example.com/");
jwsAuthorizationRequest.setRedirectUri(redirectUri);
jwsAuthorizationRequest.setResponseMode(ResponseMode.JWT);
jwsAuthorizationRequest.setState(state);
// FAPI: nonce param is required
jwsAuthorizationRequest.setNonce(nonce);
// FAPI: require the request object to contain an exp claim that has a lifetime of no longer than 60 minutes after the nbf claim
jwsAuthorizationRequest.setNbf((int) Instant.now().getEpochSecond());
// FAPI: require the request object to contain an exp claim that has a lifetime of no longer than 60 minutes after the nbf claim
jwsAuthorizationRequest.setExp(jwsAuthorizationRequest.getNbf() + 3600);
Jwt authJws = Jwt.parse(jwsAuthorizationRequest.getEncodedJwt());
JwkClient jwkClient = new JwkClient(jwksUri);
JwkResponse jwkResponse = jwkClient.exec();
String serverKeyId = jwkResponse.getKeyId(Algorithm.RSA_OAEP);
assertNotNull(serverKeyId);
JSONObject jwks = JwtUtil.getJSONWebKeys(jwksUri);
AuthCryptoProvider cryptoProvider2 = new AuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
privateKey = cryptoProvider2.getPrivateKey(encryptionKeyId);
JwtAuthorizationRequest jweAuthorizationRequest = new JwtAuthorizationRequest(authorizationRequest, KeyEncryptionAlgorithm.RSA_OAEP, BlockEncryptionAlgorithm.A256GCM, cryptoProvider2);
jweAuthorizationRequest.setKeyId(serverKeyId);
jweAuthorizationRequest.setNestedPayload(authJws);
String authJwe = jweAuthorizationRequest.getEncodedJwt(jwks);
authorizationRequest.setRequest(authJwe);
AuthorizeClient authorizeClient = new AuthorizeClient(authorizationEndpoint);
authorizeClient.setRequest(authorizationRequest);
AuthorizationResponse authorizationResponse = authorizeClient.exec();
showClient(authorizeClient);
assertNotNull(authorizationResponse.getResponse());
Jwe response = Jwe.parse(authorizationResponse.getResponse(), privateKey, null);
assertJweResponse(response);
assertEquals(response.getClaims().getClaimAsString("error"), "invalid_request_object");
// Clear private key to do not affect to other tests
privateKey = null;
}
use of io.jans.as.model.jwe.Jwe in project jans by JanssenProject.
the class AuthorizationResponseModeJwtResponseTypeCodeSignedEncryptedHttpTest method ensureRequestObjectWithNoneSigningAlgorithmFails.
@Parameters({ "redirectUri", "redirectUris", "clientJwksUri", "RSA_OAEP_keyId", "PS256_keyId", "dnName", "keyStoreFile", "keyStoreSecret", "sectorIdentifierUri" })
// Enable FAPI to run this test!
@Test(enabled = false)
public void ensureRequestObjectWithNoneSigningAlgorithmFails(final String redirectUri, final String redirectUris, final String clientJwksUri, final String encryptionKeyId, final String signingKeyId, final String dnName, final String keyStoreFile, final String keyStoreSecret, final String sectorIdentifierUri) throws Exception {
showTitle("ensureRequestObjectWithNoneSigningAlgorithmFails");
List<ResponseType> responseTypes = Arrays.asList(ResponseType.CODE);
List<GrantType> GrantTypes = Arrays.asList(GrantType.AUTHORIZATION_CODE);
// 1. Dynamic Client Registration
RegisterResponse registerResponse = registerClient(redirectUris, responseTypes, GrantTypes, sectorIdentifierUri, clientJwksUri, SignatureAlgorithm.PS256, KeyEncryptionAlgorithm.RSA_OAEP, BlockEncryptionAlgorithm.A256GCM);
String clientId = registerResponse.getClientId();
// 2. Request authorization
List<String> scope = Arrays.asList("openid", "profile", "address", "email");
String state = UUID.randomUUID().toString();
String nonce = UUID.randomUUID().toString();
AuthorizationRequest authorizationRequest = new AuthorizationRequest(responseTypes, clientId, scope, redirectUri, null);
AuthCryptoProvider cryptoProvider1 = new AuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
JwtAuthorizationRequest jwsAuthorizationRequest = new JwtAuthorizationRequest(authorizationRequest, SignatureAlgorithm.PS256, cryptoProvider1);
jwsAuthorizationRequest.setKeyId(signingKeyId);
// Added bad aud to request object claims
jwsAuthorizationRequest.setAud("https://www.other1.example.com/");
jwsAuthorizationRequest.setRedirectUri(redirectUri);
jwsAuthorizationRequest.setResponseMode(ResponseMode.JWT);
jwsAuthorizationRequest.setState(state);
// FAPI: nonce param is required
jwsAuthorizationRequest.setNonce(nonce);
// FAPI: require the request object to
jwsAuthorizationRequest.setNbf((int) Instant.now().getEpochSecond());
// contain an exp claim that has a
// lifetime of no longer than 60 minutes
// after the nbf claim
// FAPI: require the request object to
jwsAuthorizationRequest.setExp(jwsAuthorizationRequest.getNbf() + 3600);
// contain an exp claim that has a
// lifetime of no longer than 60
// minutes after the nbf claim
Jwt authJws = Jwt.parse(jwsAuthorizationRequest.getEncodedJwt());
JwkClient jwkClient = new JwkClient(jwksUri);
JwkResponse jwkResponse = jwkClient.exec();
String serverKeyId = jwkResponse.getKeyId(Algorithm.RSA_OAEP);
assertNotNull(serverKeyId);
JSONObject jwks = JwtUtil.getJSONWebKeys(jwksUri);
AuthCryptoProvider cryptoProvider2 = new AuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
privateKey = cryptoProvider2.getPrivateKey(encryptionKeyId);
JwtAuthorizationRequest jweAuthorizationRequest = new JwtAuthorizationRequest(authorizationRequest, SignatureAlgorithm.NONE, cryptoProvider2);
jweAuthorizationRequest.setKeyId(serverKeyId);
jweAuthorizationRequest.setNestedPayload(authJws);
jweAuthorizationRequest.setKeyId(signingKeyId);
jweAuthorizationRequest.setResponseMode(ResponseMode.JWT);
jweAuthorizationRequest.setState(state);
jweAuthorizationRequest.setScopes(scope);
jweAuthorizationRequest.setResponseTypes(responseTypes);
jweAuthorizationRequest.setRedirectUri(redirectUri);
// FAPI: nonce param is required
jweAuthorizationRequest.setNonce(nonce);
// FAPI: require the request object to
jweAuthorizationRequest.setNbf((int) Instant.now().getEpochSecond());
// contain an exp claim that has a
// lifetime of no longer than 60 minutes
// after the nbf claim
// Added invalid exp value to request
jweAuthorizationRequest.setExp(jwsAuthorizationRequest.getNbf() + 3600);
// object which is 70 minutes in the
// future
String authJwe = jweAuthorizationRequest.getEncodedJwt(jwks);
authorizationRequest.setRequest(authJwe);
AuthorizeClient authorizeClient = new AuthorizeClient(authorizationEndpoint);
authorizeClient.setRequest(authorizationRequest);
AuthorizationResponse authorizationResponse = authorizeClient.exec();
showClient(authorizeClient);
assertNotNull(authorizationResponse.getResponse());
Jwe response = Jwe.parse(authorizationResponse.getResponse(), privateKey, null);
assertJweResponse(response);
assertEquals(response.getClaims().getClaimAsString("error"), "invalid_request_object");
// Clear private key to do not affect to other tests
privateKey = null;
}
use of io.jans.as.model.jwe.Jwe in project jans by JanssenProject.
the class JwtAuthorizationRequest method getEncodedJwt.
public String getEncodedJwt(JSONObject jwks) throws Exception {
String encodedJwt = null;
if (keyEncryptionAlgorithm != null && blockEncryptionAlgorithm != null) {
JweEncrypterImpl jweEncrypter;
if (cryptoProvider != null && jwks != null) {
PublicKey publicKey = cryptoProvider.getPublicKey(keyId, jwks, null);
jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, publicKey);
} else {
jweEncrypter = new JweEncrypterImpl(keyEncryptionAlgorithm, blockEncryptionAlgorithm, sharedKey.getBytes(StandardCharsets.UTF_8));
}
String header = ClientUtil.toPrettyJson(headerToJSONObject());
String encodedHeader = base64urlencode(header);
Jwe jwe = new Jwe();
jwe.setHeader(new JwtHeader(encodedHeader));
if (nestedPayload == null) {
String claims = ClientUtil.toPrettyJson(payloadToJSONObject());
String encodedClaims = base64urlencode(claims);
jwe.setClaims(new JwtClaims(encodedClaims));
} else {
jwe.setSignedJWTPayload(nestedPayload);
}
jweEncrypter.encrypt(jwe);
encodedJwt = jwe.toString();
} else {
if (cryptoProvider == null) {
throw new Exception("The Crypto Provider cannot be null.");
}
JSONObject headerJsonObject = headerToJSONObject();
JSONObject payloadJsonObject = payloadToJSONObject();
String headerString = ClientUtil.toPrettyJson(headerJsonObject);
String payloadString = ClientUtil.toPrettyJson(payloadJsonObject);
String encodedHeader = base64urlencode(headerString);
String encodedPayload = base64urlencode(payloadString);
String signingInput = encodedHeader + "." + encodedPayload;
String encodedSignature = cryptoProvider.sign(signingInput, keyId, sharedKey, signatureAlgorithm);
encodedJwt = encodedHeader + "." + encodedPayload + "." + encodedSignature;
}
return encodedJwt;
}
use of io.jans.as.model.jwe.Jwe in project jans by JanssenProject.
the class AuthorizationResponse method processLocation.
private void processLocation() {
try {
if (StringUtils.isNotBlank(location)) {
Map<String, String> params = null;
int fragmentIndex = location.indexOf("#");
if (fragmentIndex != -1) {
String fragment = location.substring(fragmentIndex + 1);
params = QueryStringDecoder.decode(fragment);
if (params.containsKey("response")) {
responseMode = ResponseMode.FRAGMENT_JWT;
} else {
responseMode = ResponseMode.FRAGMENT;
}
} else {
int queryStringIndex = location.indexOf("?");
if (queryStringIndex != -1) {
String queryString = location.substring(queryStringIndex + 1);
params = QueryStringDecoder.decode(queryString);
if (params.containsKey("response")) {
responseMode = ResponseMode.QUERY_JWT;
} else {
responseMode = ResponseMode.QUERY;
}
}
}
if (params != null) {
if (params.containsKey(RESPONSE)) {
response = params.get(RESPONSE);
params.remove(RESPONSE);
String[] jwtParts = response.split("\\.");
if (jwtParts.length == 5) {
byte[] sharedSymmetricKey = sharedKey != null ? sharedKey.getBytes(StandardCharsets.UTF_8) : null;
Jwe jwe = Jwe.parse(response, privateKey, sharedSymmetricKey);
if (jwe != null) {
for (Map.Entry<String, List<String>> entry : jwe.getClaims().toMap().entrySet()) {
params.put(entry.getKey(), entry.getValue().get(0));
}
}
} else {
Jwt jwt = Jwt.parse(response);
AuthCryptoProvider cryptoProvider = new AuthCryptoProvider();
boolean signatureVerified = cryptoProvider.verifySignature(jwt.getSigningInput(), jwt.getEncodedSignature(), jwt.getHeader().getKeyId(), JwtUtil.getJSONWebKeys(jwksUri), sharedKey, jwt.getHeader().getSignatureAlgorithm());
if (signatureVerified) {
for (Map.Entry<String, List<String>> entry : jwt.getClaims().toMap().entrySet()) {
params.put(entry.getKey(), entry.getValue().get(0));
}
}
}
}
loadParams(params);
}
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
Aggregations