You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by se...@apache.org on 2017/04/06 15:56:25 UTC

cxf git commit: More OAuth2 tls work

Repository: cxf
Updated Branches:
  refs/heads/master 8dd8b14da -> 8b78d8c82


More OAuth2 tls work


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/8b78d8c8
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/8b78d8c8
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/8b78d8c8

Branch: refs/heads/master
Commit: 8b78d8c82accc1f0beca55eef8d5bce87c7af792
Parents: 8dd8b14
Author: Sergey Beryozkin <sb...@gmail.com>
Authored: Thu Apr 6 16:56:10 2017 +0100
Committer: Sergey Beryozkin <sb...@gmail.com>
Committed: Thu Apr 6 16:56:10 2017 +0100

----------------------------------------------------------------------
 .../cxf/rs/security/oauth2/client/Consumer.java |   4 +
 .../oauth2/services/AbstractTokenService.java   | 107 ++++++++++------
 .../security/oauth2/utils/OAuthConstants.java   |   3 +-
 .../oauth2/common/OAuthDataProviderTlsImpl.java |  56 ---------
 .../oauth2/grants/BookServerOAuth2Tls.java      |  59 ---------
 .../oauth2/grants/JAXRSOAuth2TlsTest.java       |  95 ---------------
 .../oauth2/tls/BookServerOAuth2Tls.java         |  59 +++++++++
 .../security/oauth2/tls/JAXRSOAuth2TlsTest.java | 122 +++++++++++++++++++
 .../oauth2/tls/OAuthDataProviderImpl.java       |  77 ++++++++++++
 .../jaxrs/security/oauth2/grants/serverTls.xml  |  80 ------------
 .../jaxrs/security/oauth2/tls/client.xml        |  38 ++++++
 .../jaxrs/security/oauth2/tls/serverTls.xml     |  81 ++++++++++++
 12 files changed, 455 insertions(+), 326 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/Consumer.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/Consumer.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/Consumer.java
index 650d464..9f8c7a7 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/Consumer.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/client/Consumer.java
@@ -27,6 +27,10 @@ public class Consumer {
 
     }
 
+    public Consumer(String id) {
+        this.clientId = id;
+    }
+    
     public Consumer(String id, String secret) {
         this.clientId = id;
         this.clientSecret = secret;

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/AbstractTokenService.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/AbstractTokenService.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/AbstractTokenService.java
index 0e08f50..5992d04 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/AbstractTokenService.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/AbstractTokenService.java
@@ -62,13 +62,12 @@ public class AbstractTokenService extends AbstractOAuthService {
         if (principal == null) {
             String clientId = retrieveClientId(params);
             if (clientId != null) {
-                if (!isMutualTls(sc, getTlsSessionInfo())) {
-                    client = getAndValidateClientFromIdAndSecret(clientId,
-                                                  params.getFirst(OAuthConstants.CLIENT_SECRET),
-                                                                 params);
-                } else {
+                String clientSecret = params.getFirst(OAuthConstants.CLIENT_SECRET);
+                if (clientSecret != null) {
+                    client = getAndValidateClientFromIdAndSecret(clientId, clientSecret, params);
+                } else if (isMutualTls(sc, getTlsSessionInfo())) {
                     client = getClient(clientId, params);
-                    // Certificates will be compared below
+                    checkCertificateBinding(client, getTlsSessionInfo());
                 }
             }
         } else {
@@ -91,21 +90,12 @@ public class AbstractTokenService extends AbstractOAuthService {
                 client = getClientFromBasicAuthScheme(params);
             }
         }
-        if (client != null && !client.getApplicationCertificates().isEmpty()) {
-            // Validate the client application certificates
-            compareTlsCertificates(getTlsSessionInfo(), client.getApplicationCertificates());
-        }
         if (client == null) {
             reportInvalidClient();
         }
         return client;
     }
 
-    private TLSSessionInfo getTlsSessionInfo() {
-
-        return (TLSSessionInfo)getMessageContext().get(TLSSessionInfo.class.getName());
-    }
-
     protected String retrieveClientId(MultivaluedMap<String, String> params) {
         String clientId = params.getFirst(OAuthConstants.CLIENT_ID);
         if (clientId == null) {
@@ -158,14 +148,46 @@ public class AbstractTokenService extends AbstractOAuthService {
         }
     }
 
+    protected void checkCertificateBinding(Client client, TLSSessionInfo tlsSessionInfo) {
+        String subjectDn = client.getProperties().get(OAuthConstants.TLS_CLIENT_AUTH_SUBJECT_DN);
+        if (subjectDn == null && client.getApplicationCertificates().isEmpty()) {
+            LOG.warning("Client \"" + client.getClientId() + "\" can not be bound to the TLS cerificate");
+            reportInvalidClient();
+        }
+        
+        if (subjectDn != null 
+            && !subjectDn.equals(getSubjectDnFromTLSCertificates(tlsSessionInfo))) {
+            LOG.warning("Client \"" + client.getClientId() + "\" can not be bound to the TLS cerificate");
+            reportInvalidClient();
+        }
+        String issuerDn = client.getProperties().get(OAuthConstants.TLS_CLIENT_AUTH_ISSUER_DN);
+        if (issuerDn != null 
+            && !issuerDn.equals(getIssuerDnFromTLSCertificates(tlsSessionInfo))) {
+            LOG.warning("Client \"" + client.getClientId() + "\" can not be bound to the TLS cerificate");
+            reportInvalidClient();
+        }
+        if (!client.getApplicationCertificates().isEmpty()) {
+            compareTlsCertificates(tlsSessionInfo, client.getApplicationCertificates());
+        }
+    }
+
+    private TLSSessionInfo getTlsSessionInfo() {
+
+        return (TLSSessionInfo)getMessageContext().get(TLSSessionInfo.class.getName());
+    }
+
+    
     protected Client getClientFromTLSCertificates(SecurityContext sc,
                                                   TLSSessionInfo tlsSessionInfo,
                                                   MultivaluedMap<String, String> params) {
         Client client = null;
         if (isMutualTls(sc, tlsSessionInfo)) {
-            String clientId = getClientIdFromTLSCertificates(sc, tlsSessionInfo);
-            if (!StringUtils.isEmpty(clientId)) {
-                client = getClient(clientId, params);
+            String subjectDn = getSubjectDnFromTLSCertificates(tlsSessionInfo);
+            if (!StringUtils.isEmpty(subjectDn)) {
+                client = getClient(subjectDn, params);
+                // The certificates must be registered with the client and match TLS certificates
+                // in case of the binding where Client's clientId is a subject distinguished name
+                compareTlsCertificates(tlsSessionInfo, client.getApplicationCertificates());
             }
         }
         return client;
@@ -175,33 +197,48 @@ public class AbstractTokenService extends AbstractOAuthService {
         return tlsSessionInfo != null && StringUtils.isEmpty(sc.getAuthenticationScheme());
     }
 
-    protected String getClientIdFromTLSCertificates(SecurityContext sc, TLSSessionInfo tlsInfo) {
+    protected String getSubjectDnFromTLSCertificates(TLSSessionInfo tlsInfo) {
+        X509Certificate cert = getRootTLSCertificate(tlsInfo);
+        if (cert != null) {
+            X500Principal x509Principal = cert.getSubjectX500Principal();
+            return x509Principal.getName();
+        }
+        return null;
+    }
+    
+    protected String getIssuerDnFromTLSCertificates(TLSSessionInfo tlsInfo) {
+        X509Certificate cert = getRootTLSCertificate(tlsInfo);
+        if (cert != null) {
+            X500Principal x509Principal = cert.getIssuerX500Principal();
+            return x509Principal.getName();
+        }
+        return null;
+    }
+    
+    protected X509Certificate getRootTLSCertificate(TLSSessionInfo tlsInfo) {
         Certificate[] clientCerts = tlsInfo.getPeerCertificates();
         if (clientCerts != null && clientCerts.length > 0) {
-            X500Principal x509Principal = ((X509Certificate)clientCerts[0]).getSubjectX500Principal();
-            return x509Principal.getName();
+            return (X509Certificate)clientCerts[0];
         }
         return null;
     }
 
     protected void compareTlsCertificates(TLSSessionInfo tlsInfo,
                                           List<String> base64EncodedCerts) {
-        if (tlsInfo != null) {
-            Certificate[] clientCerts = tlsInfo.getPeerCertificates();
-            if (clientCerts.length == base64EncodedCerts.size()) {
-                try {
-                    for (int i = 0; i < clientCerts.length; i++) {
-                        X509Certificate x509Cert = (X509Certificate)clientCerts[i];
-                        byte[] encodedKey = x509Cert.getEncoded();
-                        byte[] clientKey = Base64Utility.decode(base64EncodedCerts.get(i));
-                        if (!Arrays.equals(encodedKey, clientKey)) {
-                            reportInvalidClient();
-                        }
+        Certificate[] clientCerts = tlsInfo.getPeerCertificates();
+        if (clientCerts.length == base64EncodedCerts.size()) {
+            try {
+                for (int i = 0; i < clientCerts.length; i++) {
+                    X509Certificate x509Cert = (X509Certificate)clientCerts[i];
+                    byte[] encodedKey = x509Cert.getEncoded();
+                    byte[] clientKey = Base64Utility.decode(base64EncodedCerts.get(i));
+                    if (!Arrays.equals(encodedKey, clientKey)) {
+                        reportInvalidClient();
                     }
-                    return;
-                } catch (Exception ex) {
-                    // throw exception later
                 }
+                return;
+            } catch (Exception ex) {
+                // throw exception later
             }
         }
         reportInvalidClient();

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java
index 751a88a..6b3fd35 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java
@@ -143,7 +143,8 @@ public final class OAuthConstants {
     public static final String CLIENT_SECRET_CONTENT_ENCRYPTION_ALGORITHM =
         "client.secret.content.encryption.algorithm";
 
-    // Client Secret Encrypting Algorithm
+    public static final String TLS_CLIENT_AUTH_SUBJECT_DN = "tls_client_auth_subject_dn";
+    public static final String TLS_CLIENT_AUTH_ISSUER_DN = "tls_client_auth_issuer_dn";
     private OAuthConstants() {
     }
 

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/common/OAuthDataProviderTlsImpl.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/common/OAuthDataProviderTlsImpl.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/common/OAuthDataProviderTlsImpl.java
deleted file mode 100644
index e253110..0000000
--- a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/common/OAuthDataProviderTlsImpl.java
+++ /dev/null
@@ -1,56 +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.common;
-
-import java.io.InputStream;
-import java.security.cert.Certificate;
-import java.util.Collections;
-
-import org.apache.cxf.common.util.Base64Utility;
-import org.apache.cxf.rs.security.oauth2.common.Client;
-import org.apache.cxf.rs.security.oauth2.grants.code.DefaultEHCacheCodeDataProvider;
-import org.apache.cxf.rt.security.crypto.CryptoUtils;
-import org.apache.xml.security.utils.ClassLoaderUtils;
-
-/**
- * Extend the DefaultEHCacheCodeDataProvider to allow refreshing of tokens
- */
-public class OAuthDataProviderTlsImpl extends DefaultEHCacheCodeDataProvider {
-    public OAuthDataProviderTlsImpl() throws Exception {
-
-        Certificate cert = loadCert();
-        String encodedCert = Base64Utility.encode(cert.getEncoded());
-
-        Client client = new Client("CN=whateverhost.com,OU=Morpit,O=ApacheTest,L=Syracuse,C=US",
-                                    null,
-                                    true,
-                                    null,
-                                    null);
-        client.getAllowedGrantTypes().add("custom_grant");
-        client.setApplicationCertificates(Collections.singletonList(encodedCert));
-        this.setClient(client);
-
-    }
-
-    private Certificate loadCert() throws Exception {
-        try (InputStream is = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", this.getClass())) {
-            return CryptoUtils.loadCertificate(is, new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}, "morpit", null);
-        }
-    }
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/BookServerOAuth2Tls.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/BookServerOAuth2Tls.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/BookServerOAuth2Tls.java
deleted file mode 100644
index 7244c76..0000000
--- a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/BookServerOAuth2Tls.java
+++ /dev/null
@@ -1,59 +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.net.URL;
-
-import org.apache.cxf.Bus;
-import org.apache.cxf.BusFactory;
-import org.apache.cxf.bus.spring.SpringBusFactory;
-import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
-import org.apache.cxf.testutil.common.TestUtil;
-
-public class BookServerOAuth2Tls extends AbstractBusTestServerBase {
-    public static final String PORT = TestUtil.getPortNumber("jaxrs-oauth2-tls");
-    private static final URL SERVER_CONFIG_FILE =
-        BookServerOAuth2Tls.class.getResource("serverTls.xml");
-
-    protected void run() {
-        SpringBusFactory bf = new SpringBusFactory();
-        Bus springBus = bf.createBus(SERVER_CONFIG_FILE);
-        BusFactory.setDefaultBus(springBus);
-        setBus(springBus);
-
-        try {
-            new BookServerOAuth2Tls();
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
-    }
-
-    public static void main(String[] args) {
-        try {
-            BookServerOAuth2Tls s = new BookServerOAuth2Tls();
-            s.start();
-        } catch (Exception ex) {
-            ex.printStackTrace();
-            System.exit(-1);
-        } finally {
-            System.out.println("done!");
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2TlsTest.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2TlsTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2TlsTest.java
deleted file mode 100644
index 9450ddf..0000000
--- a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/grants/JAXRSOAuth2TlsTest.java
+++ /dev/null
@@ -1,95 +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.net.URL;
-
-import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.MultivaluedMap;
-
-import org.apache.cxf.Bus;
-import org.apache.cxf.bus.spring.SpringBusFactory;
-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.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.utils.OAuthConstants;
-import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
-
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-public class JAXRSOAuth2TlsTest extends AbstractBusClientServerTestBase {
-    public static final String PORT = BookServerOAuth2Tls.PORT;
-    
-    @BeforeClass
-    public static void startServers() throws Exception {
-        assertTrue("server did not launch correctly",
-                   launchServer(BookServerOAuth2Tls.class, true));
-    }
-
-
-    @Test
-    public void testTwoWayTLSAuthenticationCustomGrant() throws Exception {
-        String address = "https://localhost:" + PORT + "/oauth2/token";
-        WebClient wc = createWebClient(address);
-
-        ClientAccessToken at = OAuthClientUtils.getAccessToken(wc, new CustomGrant());
-        assertNotNull(at.getTokenKey());
-    }
-
-
-
-    private WebClient createWebClient(String address) {
-        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
-        bean.setAddress(address);
-
-        SpringBusFactory bf = new SpringBusFactory();
-        URL busFile = JAXRSOAuth2TlsTest.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 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/8b78d8c8/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/BookServerOAuth2Tls.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/BookServerOAuth2Tls.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/BookServerOAuth2Tls.java
new file mode 100644
index 0000000..f95758c
--- /dev/null
+++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/BookServerOAuth2Tls.java
@@ -0,0 +1,59 @@
+/**
+ * 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.tls;
+
+import java.net.URL;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.BusFactory;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.apache.cxf.testutil.common.TestUtil;
+
+public class BookServerOAuth2Tls extends AbstractBusTestServerBase {
+    public static final String PORT = TestUtil.getPortNumber("jaxrs-oauth2-tls");
+    private static final URL SERVER_CONFIG_FILE =
+        BookServerOAuth2Tls.class.getResource("serverTls.xml");
+
+    protected void run() {
+        SpringBusFactory bf = new SpringBusFactory();
+        Bus springBus = bf.createBus(SERVER_CONFIG_FILE);
+        BusFactory.setDefaultBus(springBus);
+        setBus(springBus);
+
+        try {
+            new BookServerOAuth2Tls();
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static void main(String[] args) {
+        try {
+            BookServerOAuth2Tls s = new BookServerOAuth2Tls();
+            s.start();
+        } catch (Exception ex) {
+            ex.printStackTrace();
+            System.exit(-1);
+        } finally {
+            System.out.println("done!");
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/JAXRSOAuth2TlsTest.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/JAXRSOAuth2TlsTest.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/JAXRSOAuth2TlsTest.java
new file mode 100644
index 0000000..ad9f4bb
--- /dev/null
+++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/JAXRSOAuth2TlsTest.java
@@ -0,0 +1,122 @@
+/**
+ * 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.tls;
+
+import java.net.URL;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+
+import org.apache.cxf.Bus;
+import org.apache.cxf.bus.spring.SpringBusFactory;
+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.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.provider.OAuthServiceException;
+import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class JAXRSOAuth2TlsTest extends AbstractBusClientServerTestBase {
+    public static final String PORT = BookServerOAuth2Tls.PORT;
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        assertTrue("server did not launch correctly",
+                   launchServer(BookServerOAuth2Tls.class, true));
+    }
+
+
+    @Test
+    public void testTwoWayTLSClientIdIsSubjectDn() 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 testTwoWayTLSClientIdBound() throws Exception {
+        String address = "https://localhost:" + PORT + "/oauth2/token";
+        WebClient wc = createWebClient(address);
+
+        ClientAccessToken at = OAuthClientUtils.getAccessToken(wc,
+                                        new Consumer("bound"),
+                                        new CustomGrant());
+        assertNotNull(at.getTokenKey());
+    }
+    
+    @Test
+    public void testTwoWayTLSClientUnbound() throws Exception {
+        String address = "https://localhost:" + PORT + "/oauth2/token";
+        WebClient wc = createWebClient(address);
+        try {
+            OAuthClientUtils.getAccessToken(wc,
+                                            new Consumer("unbound"),
+                                            new CustomGrant());
+            fail("exception_expected");
+        } catch (OAuthServiceException ex) {
+            assertEquals("invalid_client", ex.getError().getError());
+        }
+        
+    }
+    
+
+    private WebClient createWebClient(String address) {
+        JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
+        bean.setAddress(address);
+
+        SpringBusFactory bf = new SpringBusFactory();
+        URL busFile = JAXRSOAuth2TlsTest.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 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/8b78d8c8/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/OAuthDataProviderImpl.java
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/OAuthDataProviderImpl.java b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/OAuthDataProviderImpl.java
new file mode 100644
index 0000000..47f1d71
--- /dev/null
+++ b/systests/rs-security/src/test/java/org/apache/cxf/systest/jaxrs/security/oauth2/tls/OAuthDataProviderImpl.java
@@ -0,0 +1,77 @@
+/**
+ * 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.tls;
+
+import java.io.InputStream;
+import java.security.cert.Certificate;
+import java.util.Collections;
+
+import org.apache.cxf.common.util.Base64Utility;
+import org.apache.cxf.rs.security.oauth2.common.Client;
+import org.apache.cxf.rs.security.oauth2.grants.code.DefaultEHCacheCodeDataProvider;
+import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants;
+import org.apache.cxf.rt.security.crypto.CryptoUtils;
+import org.apache.xml.security.utils.ClassLoaderUtils;
+
+/**
+ * Extend the DefaultEHCacheCodeDataProvider to allow refreshing of tokens
+ */
+public class OAuthDataProviderImpl extends DefaultEHCacheCodeDataProvider {
+    public OAuthDataProviderImpl() throws Exception {
+
+        Client client1 = new Client("CN=whateverhost.com,OU=Morpit,O=ApacheTest,L=Syracuse,C=US",
+                                    null,
+                                    true,
+                                    null,
+                                    null);
+        client1.getAllowedGrantTypes().add("custom_grant");
+        registerCert(client1);
+        
+        Client client2 = new Client("bound",
+                                   null,
+                                   true,
+                                   null,
+                                   null);
+        client2.getProperties().put(OAuthConstants.TLS_CLIENT_AUTH_SUBJECT_DN, 
+                                    "CN=whateverhost.com,OU=Morpit,O=ApacheTest,L=Syracuse,C=US");
+        client2.getAllowedGrantTypes().add("custom_grant");
+        this.setClient(client2);
+
+        Client client3 = new Client("unbound",
+                                    null,
+                                    true,
+                                    null,
+                                    null);
+        this.setClient(client3);
+
+    }
+
+    private void registerCert(Client client) throws Exception {
+        Certificate cert = loadCert();
+        String encodedCert = Base64Utility.encode(cert.getEncoded());
+        client.setApplicationCertificates(Collections.singletonList(encodedCert));
+        
+    }
+
+    private Certificate loadCert() throws Exception {
+        try (InputStream is = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", this.getClass())) {
+            return CryptoUtils.loadCertificate(is, new char[]{'p', 'a', 's', 's', 'w', 'o', 'r', 'd'}, "morpit", null);
+        }
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/serverTls.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/serverTls.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/serverTls.xml
deleted file mode 100644
index bcd3187..0000000
--- a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/grants/serverTls.xml
+++ /dev/null
@@ -1,80 +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: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-4.2.xsd
-             http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-4.2.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-tls}">
-            <httpj:tlsServerParameters>
-                <sec:keyManagers keyPassword="password">
-                    <sec:keyStore type="JKS" password="password" resource="keys/Bethal.jks"/>
-                </sec:keyManagers>
-                <sec:trustManagers>
-                    <sec:keyStore type="JKS" password="password" resource="keys/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.OAuthDataProviderTlsImpl"/>
-    <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="customGrantHandler"/>
-            </list>
-        </property>
-    </bean>
-    <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-oauth2-tls}/oauth2">
-        <jaxrs:serviceBeans>
-            <ref bean="serviceBean"/>
-        </jaxrs:serviceBeans>
-    </jaxrs:server>
-    
-</beans>

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/client.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/client.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/client.xml
new file mode 100644
index 0000000..567b06d
--- /dev/null
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/client.xml
@@ -0,0 +1,38 @@
+<?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-4.2.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/
 policy.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" resource="keys/Morpit.jks"/>
+            </sec:keyManagers>
+            <sec:trustManagers>
+                <sec:keyStore type="JKS" password="password" resource="keys/Truststore.jks"/>
+            </sec:trustManagers>
+        </http:tlsClientParameters>
+    </http:conduit>
+</beans>

http://git-wip-us.apache.org/repos/asf/cxf/blob/8b78d8c8/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/serverTls.xml
----------------------------------------------------------------------
diff --git a/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/serverTls.xml b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/serverTls.xml
new file mode 100644
index 0000000..1901ba5
--- /dev/null
+++ b/systests/rs-security/src/test/resources/org/apache/cxf/systest/jaxrs/security/oauth2/tls/serverTls.xml
@@ -0,0 +1,81 @@
+<?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-4.2.xsd
+             http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util-4.2.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-tls}">
+            <httpj:tlsServerParameters>
+                <sec:keyManagers keyPassword="password">
+                    <sec:keyStore type="JKS" password="password" resource="keys/Bethal.jks"/>
+                </sec:keyManagers>
+                <sec:trustManagers>
+                    <sec:keyStore type="JKS" password="password" resource="keys/Truststore.jks"/>
+                </sec:trustManagers>
+                <sec:clientAuthentication want="true" required="true"/>
+            </httpj:tlsServerParameters>
+        </httpj:engine>
+    </httpj:engine-factory>
+    <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="dataProvider" class="org.apache.cxf.systest.jaxrs.security.oauth2.tls.OAuthDataProviderImpl"/>
+    <bean id="accessTokenService" class="org.apache.cxf.rs.security.oauth2.services.AccessTokenService">
+        <property name="dataProvider" ref="dataProvider"/>
+        <property name="grantHandlers">
+            <list>
+                <ref bean="customGrantHandler"/>
+            </list>
+        </property>
+    </bean>
+    
+    <jaxrs:server address="https://localhost:${testutil.ports.jaxrs-oauth2-tls}/oauth2">
+        <jaxrs:serviceBeans>
+            <ref bean="accessTokenService"/>
+        </jaxrs:serviceBeans>
+    </jaxrs:server>
+    
+</beans>