You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by co...@apache.org on 2016/01/20 16:14:27 UTC
[3/7] cxf git commit: Reshuffle of the tests to share some common code
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2Test.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2Test.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2Test.java
new file mode 100644
index 0000000..d21dc9b
--- /dev/null
+++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2Test.java
@@ -0,0 +1,508 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.systest.jaxrs.security.oauth2.grants;
+
+import java.net.URL;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.common.util.Base64UrlUtility;
+import org.apache.cxf.helpers.DOMUtils;
+import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.impl.MetadataMap;
+import org.apache.cxf.rs.security.common.CryptoLoader;
+import org.apache.cxf.rs.security.oauth2.auth.saml.Saml2BearerAuthOutInterceptor;
+import org.apache.cxf.rs.security.oauth2.client.Consumer;
+import org.apache.cxf.rs.security.oauth2.client.OAuthClientUtils;
+import org.apache.cxf.rs.security.oauth2.common.AccessTokenGrant;
+import org.apache.cxf.rs.security.oauth2.common.ClientAccessToken;
+import org.apache.cxf.rs.security.oauth2.grants.jwt.JwtBearerGrant;
+import org.apache.cxf.rs.security.oauth2.grants.saml.Saml2BearerGrant;
+import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException;
+import org.apache.cxf.rs.security.oauth2.saml.Constants;
+import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants;
+import org.apache.cxf.rs.security.saml.SAMLUtils;
+import org.apache.cxf.rs.security.saml.SAMLUtils.SelfSignInfo;
+import org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuth2TestUtils;
+import org.apache.cxf.systest.jaxrs.security.oauth2.common.SamlCallbackHandler;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.wss4j.common.crypto.Crypto;
+import org.apache.wss4j.common.saml.SAMLCallback;
+import org.apache.wss4j.common.saml.SAMLUtil;
+import org.apache.wss4j.common.saml.SamlAssertionWrapper;
+import org.apache.wss4j.common.saml.builder.SAML2Constants;
+import org.apache.wss4j.common.util.DOM2Writer;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSOAuth2Test extends AbstractBusClientServerTestBase {
+ public static final String PORT = BookServerOAuth2.PORT;
+ private static final String CRYPTO_RESOURCE_PROPERTIES =
+ "org/apache/cxf/systest/jaxrs/security/alice.properties";
+
+ @BeforeClass
+ public static void startServers() throws Exception {
+ assertTrue("server did not launch correctly",
+ launchServer(BookServerOAuth2.class, true));
+ }
+
+ @Test
+ public void testSAML2BearerGrant() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2/token";
+ WebClient wc = createWebClient(address);
+
+ Crypto crypto = new CryptoLoader().loadCrypto(CRYPTO_RESOURCE_PROPERTIES);
+ SelfSignInfo signInfo = new SelfSignInfo(crypto, "alice", "password");
+
+ SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(false);
+ String audienceURI = "https://localhost:" + PORT + "/oauth2/token";
+ samlCallbackHandler.setAudience(audienceURI);
+ SamlAssertionWrapper assertionWrapper = SAMLUtils.createAssertion(samlCallbackHandler,
+ signInfo);
+ Document doc = DOMUtils.newDocument();
+ Element assertionElement = assertionWrapper.toDOM(doc);
+ String assertion = DOM2Writer.nodeToString(assertionElement);
+
+ Saml2BearerGrant grant = new Saml2BearerGrant(assertion);
+ ClientAccessToken at = OAuthClientUtils.getAccessToken(wc,
+ new Consumer("alice", "alice"),
+ grant,
+ false);
+ assertNotNull(at.getTokenKey());
+ }
+
+ @Test
+ public void testSAML2BearerAuthenticationDirect() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth/token";
+ WebClient wc = createWebClient(address);
+
+ Crypto crypto = new CryptoLoader().loadCrypto(CRYPTO_RESOURCE_PROPERTIES);
+ SelfSignInfo signInfo = new SelfSignInfo(crypto, "alice", "password");
+
+ SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
+ samlCallbackHandler.setIssuer("alice");
+ String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token";
+ samlCallbackHandler.setAudience(audienceURI);
+ SamlAssertionWrapper assertionWrapper = SAMLUtils.createAssertion(samlCallbackHandler,
+ signInfo);
+ Document doc = DOMUtils.newDocument();
+ Element assertionElement = assertionWrapper.toDOM(doc);
+ String assertion = DOM2Writer.nodeToString(assertionElement);
+
+ String encodedAssertion = Base64UrlUtility.encode(assertion);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE, Constants.CLIENT_AUTH_SAML2_BEARER);
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, encodedAssertion);
+
+ ClientAccessToken at = OAuthClientUtils.getAccessToken(wc,
+ new CustomGrant(),
+ extraParams);
+ assertNotNull(at.getTokenKey());
+ }
+
+ @Test
+ public void testTwoWayTLSAuthentication() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2/token";
+ WebClient wc = createWebClient(address);
+
+ ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new CustomGrant());
+ assertNotNull(at.getTokenKey());
+ }
+
+ @Test
+ public void testSAML2BearerAuthenticationInterceptor() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth/token";
+ WebClient wc = createWebClientWithProps(address);
+
+ ClientAccessToken at = OAuthClientUtils.getAccessToken(wc,
+ new CustomGrant());
+ assertNotNull(at.getTokenKey());
+ }
+
+ @Test
+ public void testJWTBearerGrant() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken("resourceOwner", "alice", address, true, true);
+
+ JwtBearerGrant grant = new JwtBearerGrant(token);
+ ClientAccessToken at = OAuthClientUtils.getAccessToken(wc,
+ new Consumer("alice", "alice"),
+ grant,
+ false);
+ assertNotNull(at.getTokenKey());
+ }
+
+ @Test
+ public void testJWTBearerAuthenticationDirect() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth-jwt/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken("resourceOwner", "alice", address, true, true);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE,
+ "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, token);
+
+ ClientAccessToken at = OAuthClientUtils.getAccessToken(wc,
+ new CustomGrant(),
+ extraParams);
+ assertNotNull(at.getTokenKey());
+ }
+
+ //
+ // Some negative tests for authentication
+ //
+
+ @Test
+ public void testSAML11() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth/token";
+ WebClient wc = createWebClient(address);
+
+ String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token";
+ String assertion = OAuth2TestUtils.createToken(audienceURI, false, true);
+ String encodedAssertion = Base64UrlUtility.encode(assertion);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ 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 SAML 1.1 Assertion");
+ } catch (OAuthServiceException ex) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testSAMLAudRestr() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth/token";
+ WebClient wc = createWebClient(address);
+
+ String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token2";
+ String assertion = OAuth2TestUtils.createToken(audienceURI, true, true);
+ String encodedAssertion = Base64UrlUtility.encode(assertion);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ 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 audience restriction");
+ } catch (OAuthServiceException ex) {
+ // expected
+ }
+ }
+
+ @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<String, String>();
+ 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
+ }
+ }
+
+ @Test
+ public void testSAMLUnsigned() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth/token";
+ WebClient wc = createWebClient(address);
+
+ String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token";
+ String assertion = OAuth2TestUtils.createToken(audienceURI, true, false);
+ String encodedAssertion = Base64UrlUtility.encode(assertion);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ 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 an unsigned token");
+ } catch (Exception ex) {
+ // expected
+ }
+ }
+
+ @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<String, String>();
+ 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
+ }
+ }
+
+ @Test
+ public void testJWTBadSubjectName() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth-jwt/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken("resourceOwner", "bob", address, true, true);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE,
+ "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, token);
+
+ try {
+ OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
+ fail("Failure expected on a bad subject name");
+ } catch (OAuthServiceException ex) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testJWTUnsigned() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth-jwt/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken("resourceOwner", "alice", address,
+ true, false);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE,
+ "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, token);
+
+ try {
+ OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
+ fail("Failure expected on an unsigned token");
+ } catch (Exception ex) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testJWTNoIssuer() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth-jwt/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken(null, "alice", address, true, true);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE,
+ "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, token);
+
+ try {
+ OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
+ fail("Failure expected on no issuer");
+ } catch (Exception ex) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testJWTNoExpiry() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth-jwt/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken("resourceOwner", "alice",
+ address, false, true);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE,
+ "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, token);
+
+ try {
+ OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
+ fail("Failure expected on no expiry");
+ } catch (Exception ex) {
+ // expected
+ }
+ }
+
+ @Test
+ public void testJWTBadAudienceRestriction() throws Exception {
+ String address = "https://localhost:" + PORT + "/oauth2-auth-jwt/token";
+ WebClient wc = createWebClient(address);
+
+ // Create the JWT Token
+ String token = OAuth2TestUtils.createToken("resourceOwner", "alice",
+ address + "/badtoken", true, true);
+
+ Map<String, String> extraParams = new HashMap<String, String>();
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_TYPE,
+ "urn:ietf:params:oauth:client-assertion-type:jwt-bearer");
+ extraParams.put(Constants.CLIENT_AUTH_ASSERTION_PARAM, token);
+
+ try {
+ OAuthClientUtils.getAccessToken(wc, new CustomGrant(), extraParams);
+ fail("Failure expected on a bad audience restriction");
+ } catch (Exception ex) {
+ // expected
+ }
+ }
+
+ private WebClient createWebClient(String address) {
+ JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+ bean.setAddress(address);
+
+ SpringBusFactory bf = new SpringBusFactory();
+ URL busFile = JAXRSOAuth2Test.class.getResource("client.xml");
+ Bus springBus = bf.createBus(busFile.toString());
+ bean.setBus(springBus);
+
+ WebClient wc = bean.createWebClient();
+ wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
+ return wc;
+ }
+
+ private WebClient createWebClientWithProps(String address) {
+ JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+ bean.setAddress(address);
+
+ SpringBusFactory bf = new SpringBusFactory();
+ URL busFile = JAXRSOAuth2Test.class.getResource("client.xml");
+ Bus springBus = bf.createBus(busFile.toString());
+ bean.setBus(springBus);
+
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put("security.callback-handler",
+ "org.apache.cxf.systest.jaxrs.security.saml.KeystorePasswordCallback");
+
+ SamlCallbackHandler samlCallbackHandler = new SamlCallbackHandler(true);
+ samlCallbackHandler.setIssuer("alice");
+ String audienceURI = "https://localhost:" + PORT + "/oauth2-auth/token";
+ samlCallbackHandler.setAudience(audienceURI);
+ properties.put("security.saml-callback-handler", samlCallbackHandler);
+
+ properties.put("security.signature.username", "alice");
+ properties.put("security.signature.properties", CRYPTO_RESOURCE_PROPERTIES);
+ bean.setProperties(properties);
+
+ bean.getOutInterceptors().add(new Saml2BearerAuthOutInterceptor());
+
+ WebClient wc = bean.createWebClient();
+ wc.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.APPLICATION_JSON);
+ return wc;
+ }
+
+ private static class CustomGrant implements AccessTokenGrant {
+
+ private static final long serialVersionUID = -4007538779198315873L;
+
+ @Override
+ public String getType() {
+ return "custom_grant";
+ }
+
+ @Override
+ public MultivaluedMap<String, String> toMap() {
+ MultivaluedMap<String, String> map = new MetadataMap<String, String>();
+ map.putSingle(OAuthConstants.GRANT_TYPE, "custom_grant");
+ return map;
+ }
+
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/OAuthDataProviderImpl.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/OAuthDataProviderImpl.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/OAuthDataProviderImpl.java
deleted file mode 100644
index 0ae9708..0000000
--- a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/OAuthDataProviderImpl.java
+++ /dev/null
@@ -1,101 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.cxf.systest.jaxrs.security.oauth2.grants;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.cxf.rs.security.oauth2.common.Client;
-import org.apache.cxf.rs.security.oauth2.common.OAuthPermission;
-import org.apache.cxf.rs.security.oauth2.grants.code.DefaultEHCacheCodeDataProvider;
-import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException;
-
-/**
- * Extend the DefaultEHCacheCodeDataProvider to allow refreshing of tokens
- */
-public class OAuthDataProviderImpl extends DefaultEHCacheCodeDataProvider {
-
- public OAuthDataProviderImpl() {
- Client client = new Client("consumer-id", "this-is-a-secret", true);
- client.setRedirectUris(Collections.singletonList("http://www.blah.apache.org"));
-
- client.getAllowedGrantTypes().add("authorization_code");
- client.getAllowedGrantTypes().add("refresh_token");
- client.getAllowedGrantTypes().add("implicit");
- client.getAllowedGrantTypes().add("password");
- client.getAllowedGrantTypes().add("client_credentials");
- client.getAllowedGrantTypes().add("urn:ietf:params:oauth:grant-type:saml2-bearer");
- client.getAllowedGrantTypes().add("urn:ietf:params:oauth:grant-type:jwt-bearer");
-
- client.getRegisteredScopes().add("read_balance");
- client.getRegisteredScopes().add("create_balance");
- client.getRegisteredScopes().add("read_data");
-
- this.setClient(client);
- }
-
- @Override
- protected boolean isRefreshTokenSupported(List<String> theScopes) {
- return true;
- }
-
- @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_balance".equals(requestedScope)) {
- OAuthPermission permission = new OAuthPermission();
- 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();
- 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();
- permission.setHttpVerbs(Collections.singletonList("GET"));
- List<String> uris = new ArrayList<>();
- String partnerAddress = "/partners/data/*";
- uris.add(partnerAddress);
- permission.setUris(uris);
-
- permissions.add(permission);
- } else {
- throw new OAuthServiceException("invalid_scope");
- }
- }
-
- return permissions;
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/client.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/client.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/client.xml
deleted file mode 100644
index 13eaea1..0000000
--- a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/client.xml
+++ /dev/null
@@ -1,38 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements. See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership. The ASF licenses this file
- to you under the Apache License, Version 2.0 (the
- "License"); you may not use this file except in compliance
- with the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing,
- software distributed under the License is distributed on an
- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- KIND, either express or implied. See the License for the
- specific language governing permissions and limitations
- under the License.
--->
-<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:http="http://cxf.apache.org/transports/http/configuration" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:cxf="http://cxf.apache.org/core" xmlns:p="http://cxf.apache.org/policy" xmlns:sec="http://cxf.apache.org/configuration/security" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd http://cxf.apache.org/policy http://cxf.apache.org/schemas/poli
cy.xsd">
- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
- <cxf:bus>
- <cxf:features>
- <cxf:logging/>
- </cxf:features>
- </cxf:bus>
- <http:conduit name="https://localhost.*">
- <http:client ConnectionTimeout="3000000" ReceiveTimeout="3000000"/>
- <http:tlsClientParameters disableCNCheck="true">
- <sec:keyManagers keyPassword="password">
- <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Morpit.jks"/>
- </sec:keyManagers>
- <sec:trustManagers>
- <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Truststore.jks"/>
- </sec:trustManagers>
- </http:tlsClientParameters>
- </http:conduit>
-</beans>
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/filters/oauth20-server.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/filters/oauth20-server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/filters/oauth20-server.xml
index 3169727..ce7a8d9 100644
--- a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/filters/oauth20-server.xml
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/filters/oauth20-server.xml
@@ -62,7 +62,7 @@ under the License.
</httpj:engine>
</httpj:engine-factory>
- <bean id="oauthProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.filters.OAuthDataProviderImpl" />
+ <bean id="oauthProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuthDataProviderImpl" />
<bean id="authorizationService" class="org.apache.cxf.rs.security.oauth2.services.AuthorizationCodeGrantService">
<property name="dataProvider" ref="oauthProvider"/>
@@ -89,8 +89,8 @@ under the License.
<property name="dataProvider" ref="oauthProvider"/>
</bean>
- <bean id="callbackHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.CallbackHandlerImpl"/>
- <bean id="basicAuthFilter" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.BasicAuthFilter">
+ <bean id="callbackHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.CallbackHandlerImpl"/>
+ <bean id="basicAuthFilter" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.BasicAuthFilter">
<property name="callbackHandler" ref="callbackHandler"/>
</bean>
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-negative-server.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-negative-server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-negative-server.xml
index 5b367f6..75aa22f 100644
--- a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-negative-server.xml
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-negative-server.xml
@@ -62,7 +62,7 @@ under the License.
</httpj:engine>
</httpj:engine-factory>
- <bean id="oauthProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.OAuthDataProviderImpl" />
+ <bean id="oauthProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuthDataProviderImpl" />
<bean id="authorizationService" class="org.apache.cxf.rs.security.oauth2.services.AuthorizationCodeGrantService">
<property name="dataProvider" ref="oauthProvider"/>
@@ -110,8 +110,8 @@ under the License.
</property>
</bean>
- <bean id="callbackHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.CallbackHandlerImpl"/>
- <bean id="basicAuthFilter" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.BasicAuthFilter">
+ <bean id="callbackHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.CallbackHandlerImpl"/>
+ <bean id="basicAuthFilter" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.BasicAuthFilter">
<property name="callbackHandler" ref="callbackHandler"/>
</bean>
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-server.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-server.xml
index 3ef86fb..ccfa370 100644
--- a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-server.xml
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/grants-server.xml
@@ -62,7 +62,7 @@ under the License.
</httpj:engine>
</httpj:engine-factory>
- <bean id="oauthProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.OAuthDataProviderImpl" />
+ <bean id="oauthProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuthDataProviderImpl" />
<bean id="authorizationService" class="org.apache.cxf.rs.security.oauth2.services.AuthorizationCodeGrantService">
<property name="dataProvider" ref="oauthProvider"/>
@@ -110,8 +110,8 @@ under the License.
</property>
</bean>
- <bean id="callbackHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.CallbackHandlerImpl"/>
- <bean id="basicAuthFilter" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.BasicAuthFilter">
+ <bean id="callbackHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.CallbackHandlerImpl"/>
+ <bean id="basicAuthFilter" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.BasicAuthFilter">
<property name="callbackHandler" ref="callbackHandler"/>
</bean>
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/server.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/server.xml
new file mode 100644
index 0000000..2b3d821
--- /dev/null
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/server.xml
@@ -0,0 +1,127 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:http="http://cxf.apache.org/transports/http/configuration"
+ xmlns:httpj="http://cxf.apache.org/transports/http-jetty/configuration"
+ xmlns:sec="http://cxf.apache.org/configuration/security"
+ xmlns:cxf="http://cxf.apache.org/core"
+ xmlns:jaxrs="http://cxf.apache.org/jaxrs"
+ xmlns:util="http://www.springframework.org/schema/util"
+ xsi:schemaLocation="http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
+ http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
+ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
+ http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd
+ http://cxf.apache.org/transports/http-jetty/configuration http://cxf.apache.org/schemas/configuration/http-jetty.xsd
+ http://cxf.apache.org/configuration/security http://cxf.apache.org/schemas/configuration/security.xsd">
+ <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/>
+ <cxf:bus>
+ <cxf:features>
+ <cxf:logging/>
+ </cxf:features>
+ <cxf:properties>
+ <entry key="org.apache.cxf.jaxrs.bus.providers" value-ref="busProviders"/>
+ </cxf:properties>
+ </cxf:bus>
+ <!-- providers -->
+ <util:list id="busProviders">
+ <ref bean="oauthJson"/>
+ </util:list>
+ <httpj:engine-factory id="port-9095-tls-config">
+ <httpj:engine port="${testutil.ports.jaxrs-oauth2}">
+ <httpj:tlsServerParameters>
+ <sec:keyManagers keyPassword="password">
+ <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Bethal.jks"/>
+ </sec:keyManagers>
+ <sec:trustManagers>
+ <sec:keyStore type="JKS" password="password" file="src/test/java/org/apache/cxf/systest/http/resources/Truststore.jks"/>
+ </sec:trustManagers>
+ <sec:clientAuthentication want="true" required="true"/>
+ </httpj:tlsServerParameters>
+ </httpj:engine>
+ </httpj:engine-factory>
+ <bean id="dataProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuthDataProviderImpl"/>
+ <bean id="samlGrantHandler" class="org.apache.cxf.rs.security.oauth2.grants.saml.Saml2BearerGrantHandler">
+ <property name="dataProvider" ref="dataProvider"/>
+ </bean>
+ <bean id="jwtGrantHandler" class="org.apache.cxf.rs.security.oauth2.grants.jwt.JwtBearerGrantHandler">
+ <property name="dataProvider" ref="dataProvider"/>
+ </bean>
+
+ <bean id="samlAuthHandler" class="org.apache.cxf.rs.security.oauth2.auth.saml.Saml2BearerAuthHandler"/>
+ <bean id="jwtAuthHandler" class="org.apache.cxf.rs.security.oauth2.grants.jwt.JwtBearerAuthHandler"/>
+
+ <bean id="customGrantHandler" class="org.apache.cxf.systest.jaxrs.security.oauth2.grants.CustomGrantHandler">
+ <property name="dataProvider" ref="dataProvider"/>
+ </bean>
+ <bean id="oauthJson" class="org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider"/>
+ <bean id="serviceBean" class="org.apache.cxf.rs.security.oauth2.services.AccessTokenService">
+ <property name="dataProvider" ref="dataProvider"/>
+ <property name="grantHandlers">
+ <list>
+ <ref bean="samlGrantHandler"/>
+ <ref bean="jwtGrantHandler"/>
+ <ref bean="customGrantHandler"/>
+ </list>
+ </property>
+ </bean>
+ <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-oauth2}/oauth2">
+ <jaxrs:serviceBeans>
+ <ref bean="serviceBean"/>
+ </jaxrs:serviceBeans>
+ <jaxrs:properties>
+ <entry key="security.signature.properties" value="org/apache/cxf/systest/jaxrs/security/alice.properties"/>
+ <entry key="rs.security.keystore.type" value="jks" />
+ <entry key="rs.security.keystore.alias" value="alice"/>
+ <entry key="rs.security.keystore.password" value="password"/>
+ <entry key="rs.security.keystore.file"
+ value="org/apache/cxf/systest/jaxrs/security/certs/alice.jks" />
+ <entry key="rs.security.signature.algorithm" value="RS256" />
+ </jaxrs:properties>
+ </jaxrs:server>
+ <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-oauth2}/oauth2-auth">
+ <jaxrs:serviceBeans>
+ <ref bean="serviceBean"/>
+ </jaxrs:serviceBeans>
+ <jaxrs:providers>
+ <ref bean="samlAuthHandler"/>
+ </jaxrs:providers>
+ <jaxrs:properties>
+ <entry key="security.signature.properties" value="org/apache/cxf/systest/jaxrs/security/alice.properties"/>
+ </jaxrs:properties>
+ </jaxrs:server>
+ <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-oauth2}/oauth2-auth-jwt">
+ <jaxrs:serviceBeans>
+ <ref bean="serviceBean"/>
+ </jaxrs:serviceBeans>
+ <jaxrs:providers>
+ <ref bean="jwtAuthHandler"/>
+ </jaxrs:providers>
+ <jaxrs:properties>
+ <entry key="rs.security.keystore.type" value="jks" />
+ <entry key="rs.security.keystore.alias" value="alice"/>
+ <entry key="rs.security.keystore.password" value="password"/>
+ <entry key="rs.security.keystore.file"
+ value="org/apache/cxf/systest/jaxrs/security/certs/alice.jks" />
+ <entry key="rs.security.signature.algorithm" value="RS256" />
+ </jaxrs:properties>
+ </jaxrs:server>
+</beans>
http://git-wip-us.apache.org/repos/asf/cxf/blob/49b2b813/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/server.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/server.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/server.xml
index 75fb048..1de107b 100644
--- a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/server.xml
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/server.xml
@@ -58,7 +58,7 @@ under the License.
</httpj:tlsServerParameters>
</httpj:engine>
</httpj:engine-factory>
- <bean id="dataProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.OAuthDataProviderImpl"/>
+ <bean id="dataProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.common.OAuthDataProviderImpl"/>
<bean id="samlGrantHandler" class="org.apache.cxf.rs.security.oauth2.grants.saml.Saml2BearerGrantHandler">
<property name="dataProvider" ref="dataProvider"/>
</bean>