use of org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException in project cxf by apache.
the class JAXRSOAuth2Test method testSAMLBadSubjectName.
@Test
public void testSAMLBadSubjectName() throws Exception {
String address = "https://localhost:" + PORT + "/oauth2-auth/token";
WebClient wc = createWebClient(address);
String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token";
// Create the SAML Assertion
SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
samlCallbackHandler.setSubjectName("bob");
samlCallbackHandler.setAudience(audienceURI);
SAMLCallback samlCallback = new SAMLCallback();
SAMLUtil.doSAMLCallback(samlCallbackHandler, samlCallback);
SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback);
if (samlCallback.isSignAssertion()) {
samlAssertion.signAssertion(samlCallback.getIssuerKeyName(), samlCallback.getIssuerKeyPassword(), samlCallback.getIssuerCrypto(), samlCallback.isSendKeyValue(), samlCallback.getCanonicalizationAlgorithm(), samlCallback.getSignatureAlgorithm());
}
String assertion = samlAssertion.assertionToString();
String encodedAssertion = Base64UrlUtility.encode(assertion);
Map<String, String> extraParams = new HashMap<>();
extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE, Constants.CLIENT_AUTH_SAML2_BEARER);
extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, encodedAssertion);
try {
OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
fail("Failure expected on a bad subject name");
} catch (OAuthServiceException ex) {
// expected
}
}
use of org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException in project cxf by apache.
the class OAuthDataProviderImpl method convertScopeToPermissions.
@Override
public List<OAuthPermission> convertScopeToPermissions(Client client, List<String> requestedScopes) {
if (requestedScopes.isEmpty()) {
return Collections.emptyList();
}
List<OAuthPermission> permissions = new ArrayList<>();
for (String requestedScope : requestedScopes) {
if ("read_book".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("read_book");
permission.setHttpVerbs(Collections.singletonList("GET"));
List<String> uris = new ArrayList<>();
String partnerAddress = "/secured/bookstore/books/*";
uris.add(partnerAddress);
permission.setUris(uris);
permissions.add(permission);
} else if ("create_book".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("create_book");
permission.setHttpVerbs(Collections.singletonList("POST"));
List<String> uris = new ArrayList<>();
String partnerAddress = "/secured/bookstore/books/*";
uris.add(partnerAddress);
permission.setUris(uris);
permissions.add(permission);
} else if ("create_image".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("create_image");
permission.setHttpVerbs(Collections.singletonList("POST"));
List<String> uris = new ArrayList<>();
String partnerAddress = "/secured/bookstore/image/*";
uris.add(partnerAddress);
permission.setUris(uris);
permissions.add(permission);
} else if ("read_balance".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("read_balance");
permission.setHttpVerbs(Collections.singletonList("GET"));
List<String> uris = new ArrayList<>();
String partnerAddress = "/partners/balance/*";
uris.add(partnerAddress);
permission.setUris(uris);
permissions.add(permission);
} else if ("create_balance".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("create_balance");
permission.setHttpVerbs(Collections.singletonList("POST"));
List<String> uris = new ArrayList<>();
String partnerAddress = "/partners/balance/*";
uris.add(partnerAddress);
permission.setUris(uris);
permissions.add(permission);
} else if ("read_data".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("read_data");
permission.setHttpVerbs(Collections.singletonList("GET"));
List<String> uris = new ArrayList<>();
String partnerAddress = "/partners/data/*";
uris.add(partnerAddress);
permission.setUris(uris);
permissions.add(permission);
} else if ("openid".equals(requestedScope)) {
OAuthPermission permission = new OAuthPermission("openid", "Authenticate user");
permissions.add(permission);
} else {
throw new OAuthServiceException("invalid_scope");
}
}
return permissions;
}
use of org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException in project cxf by apache.
the class JAXRSOAuth2Test method testConfidentialClientIdOnly.
@Test()
public void testConfidentialClientIdOnly() throws Exception {
String address = "https://localhost:" + PORT + "/oauth2/token";
WebClient wc = createWebClient(address);
try {
OAuthClientUtils.getAccessToken(wc, new Consumer("fredNoPassword"), new CustomGrant(), false);
fail("NotAuthorizedException exception is expected");
} catch (OAuthServiceException ex) {
assertEquals("invalid_client", ex.getError().getError());
}
}
use of org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException in project cxf by apache.
the class JAXRSOAuth2Test method testSAMLHolderOfKey.
@Test
public void testSAMLHolderOfKey() throws Exception {
String address = "https://localhost:" + PORT + "/oauth2-auth/token";
WebClient wc = createWebClient(address);
String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token";
// Create the SAML Assertion
SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
samlCallbackHandler.setConfirmationMethod(SAML2Constants.CONF_HOLDER_KEY);
samlCallbackHandler.setSubjectName("alice");
samlCallbackHandler.setAudience(audienceURI);
SAMLCallback samlCallback = new SAMLCallback();
SAMLUtil.doSAMLCallback(samlCallbackHandler, samlCallback);
SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback);
if (samlCallback.isSignAssertion()) {
samlAssertion.signAssertion(samlCallback.getIssuerKeyName(), samlCallback.getIssuerKeyPassword(), samlCallback.getIssuerCrypto(), samlCallback.isSendKeyValue(), samlCallback.getCanonicalizationAlgorithm(), samlCallback.getSignatureAlgorithm());
}
String assertion = samlAssertion.assertionToString();
String encodedAssertion = Base64UrlUtility.encode(assertion);
Map<String, String> extraParams = new HashMap<>();
extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE, Constants.CLIENT_AUTH_SAML2_BEARER);
extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, encodedAssertion);
try {
OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
fail("Failure expected on a bad subject confirmation method");
} catch (OAuthServiceException ex) {
// expected
}
}
use of org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException in project cxf by apache.
the class JAXRSOAuth2Test method testBasicAuthClientCred.
@Test
public void testBasicAuthClientCred() throws Exception {
String address = "https://localhost:" + PORT + "/oauth2/token";
WebClient wc = createWebClient(address);
ClientCredentialsGrant grant = new ClientCredentialsGrant();
// Pass client_id & client_secret as form properties
// (instead WebClient can be initialized with username & password)
grant.setClientId("bob");
grant.setClientSecret("bobPassword");
try {
OAuthClientUtils.getAccessToken(wc, grant);
fail("Form based authentication is not supported");
} catch (OAuthServiceException ex) {
assertEquals(OAuthConstants.UNAUTHORIZED_CLIENT, ex.getError().getError());
}
ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new Consumer("bob", "bobPassword"), new ClientCredentialsGrant(), true);
assertNotNull(at.getTokenKey());
}
Aggregations