use of org.apache.cxf.rs.security.oauth2.grants.code.CodeVerifierTransformer in project cxf by apache.
the class PublicClientTest method testAuthorizationCodeGrantNoRedirectURI.
@org.junit.Test
public void testAuthorizationCodeGrantNoRedirectURI() throws Exception {
URL busFile = PublicClientTest.class.getResource("publicclient.xml");
String address = "https://localhost:" + port + "/services/";
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);
// Get Authorization Code
try {
// Get Authorization Code
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("fredPublic");
String codeVerifier = Base64UrlUtility.encode(CryptoUtils.generateSecureRandomBytes(32));
CodeVerifierTransformer transformer = new PlainCodeVerifier();
parameters.setCodeChallenge(transformer.transformCodeVerifier(codeVerifier));
parameters.setCodeChallengeMethod(transformer.getChallengeMethod());
parameters.setResponseType(OAuthConstants.CODE_RESPONSE_TYPE);
parameters.setPath("authorize/");
OAuth2TestUtils.getLocation(client, parameters);
fail("Failure expected on a missing (registered) redirectURI");
} catch (Exception ex) {
// expected
}
}
use of org.apache.cxf.rs.security.oauth2.grants.code.CodeVerifierTransformer in project cxf by apache.
the class PublicClientTest method testPKCEMissingVerifier.
private void testPKCEMissingVerifier(CodeVerifierTransformer transformer) {
URL busFile = PublicClientTest.class.getResource("publicclient.xml");
String address = "https://localhost:" + port + "/services/";
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);
// Get Authorization Code
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("consumer-id");
String codeVerifier = Base64UrlUtility.encode(CryptoUtils.generateSecureRandomBytes(32));
parameters.setCodeChallenge(transformer.transformCodeVerifier(codeVerifier));
parameters.setCodeChallengeMethod(transformer.getChallengeMethod());
parameters.setResponseType(OAuthConstants.CODE_RESPONSE_TYPE);
parameters.setPath("authorize/");
String location = OAuth2TestUtils.getLocation(client, parameters);
String code = OAuth2TestUtils.getSubstring(location, "code");
assertNotNull(code);
// Now get the access token
client = WebClient.create(tokenServiceAddress, busFile.toString());
try {
OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code, "consumer-id", null);
fail("Failure expected on a missing verifier");
} catch (OAuthServiceException ex) {
assertFalse(ex.getError().getError().isEmpty());
}
}
use of org.apache.cxf.rs.security.oauth2.grants.code.CodeVerifierTransformer in project cxf by apache.
the class PublicClientTest method testPKCE.
private void testPKCE(CodeVerifierTransformer transformer) {
URL busFile = PublicClientTest.class.getResource("publicclient.xml");
String address = "https://localhost:" + port + "/services/";
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);
// Get Authorization Code
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("consumer-id");
String codeVerifier = Base64UrlUtility.encode(CryptoUtils.generateSecureRandomBytes(32));
parameters.setCodeChallenge(transformer.transformCodeVerifier(codeVerifier));
parameters.setCodeChallengeMethod(transformer.getChallengeMethod());
parameters.setResponseType(OAuthConstants.CODE_RESPONSE_TYPE);
parameters.setPath("authorize/");
String location = OAuth2TestUtils.getLocation(client, parameters);
String code = OAuth2TestUtils.getSubstring(location, "code");
assertNotNull(code);
// Now get the access token
client = WebClient.create(tokenServiceAddress, busFile.toString());
ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code, "consumer-id", null, codeVerifier);
assertNotNull(accessToken.getTokenKey());
}
use of org.apache.cxf.rs.security.oauth2.grants.code.CodeVerifierTransformer in project cxf by apache.
the class PublicClientTest method testPKCEDifferentVerifier.
private void testPKCEDifferentVerifier(CodeVerifierTransformer transformer) {
URL busFile = PublicClientTest.class.getResource("publicclient.xml");
String address = "https://localhost:" + port + "/services/";
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);
// Get Authorization Code
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("consumer-id");
String codeVerifier = Base64UrlUtility.encode(CryptoUtils.generateSecureRandomBytes(32));
parameters.setCodeChallenge(transformer.transformCodeVerifier(codeVerifier));
parameters.setCodeChallengeMethod(transformer.getChallengeMethod());
parameters.setResponseType(OAuthConstants.CODE_RESPONSE_TYPE);
parameters.setPath("authorize/");
String location = OAuth2TestUtils.getLocation(client, parameters);
String code = OAuth2TestUtils.getSubstring(location, "code");
assertNotNull(code);
// Now get the access token
client = WebClient.create(tokenServiceAddress, busFile.toString());
codeVerifier = Base64UrlUtility.encode(CryptoUtils.generateSecureRandomBytes(32));
try {
OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code, "consumer-id", null, codeVerifier);
fail("Failure expected on a different verifier");
} catch (OAuthServiceException ex) {
assertFalse(ex.getError().getError().isEmpty());
}
}
use of org.apache.cxf.rs.security.oauth2.grants.code.CodeVerifierTransformer in project cxf by apache.
the class OIDCFlowTest method testAuthorizationCodeFlowWithPKCE.
@org.junit.Test
public void testAuthorizationCodeFlowWithPKCE() throws Exception {
String address = "https://localhost:" + port + "/services/";
WebClient client = WebClient.create(address, OAuth2TestUtils.setupProviders(), "alice", "security", null);
// Save the Cookie for the second request...
WebClient.getConfig(client).getRequestContext().put(org.apache.cxf.message.Message.MAINTAIN_SESSION, Boolean.TRUE);
// Get Authorization Code
AuthorizationCodeParameters parameters = new AuthorizationCodeParameters();
parameters.setConsumerId("consumer-id");
parameters.setScope(OidcUtils.OPENID_SCOPE);
parameters.setResponseType(OAuthConstants.CODE_RESPONSE_TYPE);
parameters.setPath("authorize/");
String codeVerifier = Base64UrlUtility.encode(CryptoUtils.generateSecureRandomBytes(32));
CodeVerifierTransformer transformer = new DigestCodeVerifier();
parameters.setCodeChallenge(transformer.transformCodeVerifier(codeVerifier));
parameters.setCodeChallengeMethod(transformer.getChallengeMethod());
String location = OAuth2TestUtils.getLocation(client, parameters);
String code = OAuth2TestUtils.getSubstring(location, "code");
assertNotNull(code);
// Now get the access token
client = WebClient.create(address, "consumer-id", "this-is-a-secret", null);
ClientAccessToken accessToken = OAuth2TestUtils.getAccessTokenWithAuthorizationCode(client, code, "consumer-id", null, codeVerifier);
assertNotNull(accessToken.getTokenKey());
if (isAccessTokenInJWTFormat()) {
validateAccessToken(accessToken.getTokenKey());
}
}
Aggregations