<?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-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.context.support.PropertySourcesPlaceholderConfigurer"/>
    <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> 
    <bean id="oauthJson" class="org.apache.cxf.rs.security.oauth2.provider.OAuthJSONProvider"/>
    
    <httpj:engine-factory id="tls-config">
        <httpj:engine port="${testutil.ports.jaxrs-oauth2-filtersJwt}">
            <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:sessionSupport>true</httpj:sessionSupport>
        </httpj:engine>
    </httpj:engine-factory>
   
   <bean id="serviceBean" class="org.apache.cxf.systest.jaxrs.security.jose.BookStore"/>
   
   <bean id="jackson" class="com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider"/>
    
   <bean id="tvServiceClientFactory" class="org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean">
         <property name="address" value="https://localhost:${testutil.ports.jaxrs-oauth2-serviceJwt}/services/validate"/>
         <property name="headers">
            <map>
               <entry key="Accept" value="application/json"/>
               <entry key="Content-Type" value="application/x-www-form-urlencoded"/>
            </map>
         </property>
         <property name="providers">
             <list>
                 <ref bean="jackson"/>
             </list>
         </property>
         <property name="username" value="service"/>
         <property name="password" value="service-pass"/>
   </bean>
      
   <bean id="tvServiceClient" factory-bean="tvServiceClientFactory" factory-method="createWebClient"/>
 
   <bean id="tvServiceClientFactory2" class="org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean">
         <property name="address" value="https://localhost:${testutil.ports.jaxrs-oauth2-serviceJwt}/services2/validate"/>
         <property name="headers">
            <map>
               <entry key="Accept" value="application/json"/>
               <entry key="Content-Type" value="application/x-www-form-urlencoded"/>
            </map>
         </property>
         <property name="providers">
             <list>
                 <ref bean="jackson"/>
             </list>
         </property>
         <property name="username" value="service"/>
         <property name="password" value="service-pass"/>
   </bean>
      
   <bean id="tvServiceClient2" factory-bean="tvServiceClientFactory2" factory-method="createWebClient"/>
 
   <bean id="remoteTokenValidator" class="org.apache.cxf.rs.security.oauth2.filters.AccessTokenValidatorClient">
       <property name="tokenValidatorClient" ref="tvServiceClient"/>
   </bean>
     
   <bean id="oAuthFilterRemoteValidation" class="org.apache.cxf.rs.security.oauth2.filters.OAuthRequestFilter">
       <property name="tokenValidator" ref="remoteTokenValidator"/>
   </bean>
   
   <bean id="remoteTokenValidator2" class="org.apache.cxf.rs.security.oauth2.filters.AccessTokenValidatorClient">
       <property name="tokenValidatorClient" ref="tvServiceClient2"/>
   </bean>
     
   <bean id="oAuthFilterRemoteValidation2" class="org.apache.cxf.rs.security.oauth2.filters.OAuthRequestFilter">
       <property name="tokenValidator" ref="remoteTokenValidator2"/>
   </bean>
   
   <jaxrs:server 
       depends-on="tls-config" 
       address="https://localhost:${testutil.ports.jaxrs-oauth2-filtersJwt}/secured">
       <jaxrs:serviceBeans>
           <ref bean="serviceBean"/>
       </jaxrs:serviceBeans>
       <jaxrs:providers>
           <ref bean="oAuthFilterRemoteValidation"/>
       </jaxrs:providers>
   </jaxrs:server>
   
   <jaxrs:server 
       depends-on="tls-config" 
       address="https://localhost:${testutil.ports.jaxrs-oauth2-filtersJwt}/secured2">
       <jaxrs:serviceBeans>
           <ref bean="serviceBean"/>
       </jaxrs:serviceBeans>
       <jaxrs:providers>
           <ref bean="oAuthFilterRemoteValidation2"/>
       </jaxrs:providers>
   </jaxrs:server>
   
   <bean id="jwtTokenValidator" class="org.apache.cxf.rs.security.oauth2.filters.JwtAccessTokenValidator"/>
     
   <bean id="oAuthFilterLocalValidation" class="org.apache.cxf.rs.security.oauth2.filters.OAuthRequestFilter">
       <property name="tokenValidator" ref="jwtTokenValidator"/>
   </bean>
   
   <jaxrs:server 
       depends-on="tls-config" 
       address="https://localhost:${testutil.ports.jaxrs-oauth2-filtersJwt}/securedLocalValidation">
       <jaxrs:serviceBeans>
           <ref bean="serviceBean"/>
       </jaxrs:serviceBeans>
       <jaxrs:providers>
           <ref bean="oAuthFilterLocalValidation"/>
       </jaxrs:providers>
       <jaxrs:properties>
            <entry key="rs.security.signature.in.properties" value="org/apache/cxf/systest/jaxrs/security/alice.rs.properties"/>
       </jaxrs:properties>
   </jaxrs:server>

   <bean id="jwsJwksJwtAccessTokenValidator" class="org.apache.cxf.rs.security.oauth2.filters.JwsJwksJwtAccessTokenValidator">
       <property name="jwksURL" value="https://localhost:${testutil.ports.jaxrs-oauth2-serviceJwt}/services/keys"/>
   </bean>

   <bean id="oAuthFilterJwsJwksValidation" class="org.apache.cxf.rs.security.oauth2.filters.OAuthRequestFilter">
       <property name="tokenValidator" ref="jwsJwksJwtAccessTokenValidator"/>
   </bean>

   <jaxrs:server 
       depends-on="tls-config" 
       address="https://localhost:${testutil.ports.jaxrs-oauth2-filtersJwt}/securedJwsJwksValidation">
       <jaxrs:serviceBeans>
           <ref bean="serviceBean"/>
       </jaxrs:serviceBeans>
       <jaxrs:providers>
           <ref bean="oAuthFilterJwsJwksValidation"/>
       </jaxrs:providers>
   </jaxrs:server>

   <http:conduit name="https://localhost.*">
        <http:client ConnectionTimeout="3000000" ReceiveTimeout="3000000"/>
        <http:tlsClientParameters disableCNCheck="true">
            <sec:trustManagers>
                <sec:keyStore type="JKS" password="password" resource="keys/Truststore.jks"/>
            </sec:trustManagers>
        </http:tlsClientParameters>
        <http:authorization>
            <sec:UserName>service</sec:UserName>
            <sec:Password>service-pass</sec:Password>
            <sec:AuthorizationType>Basic</sec:AuthorizationType>
       </http:authorization>
    </http:conduit>

</beans>
