<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd"> 
    <description>Shiro CAS Configuration</description>

    <bean id="securityManager" parent="securityManagerParent">
        <property name="subjectFactory">
            <bean class="io.buji.pac4j.subject.Pac4jSubjectFactory" />
        </property>
        <property name="realm">
            <bean class="io.buji.pac4j.realm.Pac4jRealm" />
        </property>
    </bean>

    <bean id="casConfig" class="org.pac4j.core.config.Config">
        <constructor-arg>
            <bean class="org.pac4j.cas.client.CasClient">
                <constructor-arg>
                    <bean class="org.pac4j.cas.config.CasConfiguration">
                        <constructor-arg value="${tigon.shiro.cas.login-url:${tigon.shiro.cas.server.addr}/login}" />
                        <property name="protocol" value="${tigon.shiro.cas.protocol:CAS20}" />
                        <property name="acceptAnyProxy" value="true" />
                    </bean>
                </constructor-arg>
                <property name="name" value="${spring.application.name}" />
                <property name="callbackUrl" value="${tigon.shiro.cas.login-callback-url:${tigon.shiro.cas.client.addr}${tigon.shiro.cas.login-callback-path:/cas/login}?client_name=${spring.application.name}}" />
                <property name="profileCreator">
                    <bean class="me.chyxion.tigon.shiro.cas.DefaultProfileCreator" />
                </property>
            </bean>
        </constructor-arg>
    </bean>

    <bean id="shiroFilter" parent="shiroFilterParent">
        <property name="securityManager" ref="securityManager" />
        <property name="loginUrl" value="${tigon.shiro.login-url:${tigon.shiro.cas.server.login-url:${tigon.shiro.cas.server.addr}/login}}?service=${tigon.shiro.cas.client.addr}${tigon.shiro.cas.client.login.path:/cas/login}?client_name=${spring.application.name}}" />
        <property name="successUrl" value="${tigon.shiro.login-success-url:/login-success}" />

        <property name="filters">
            <map>
                <entry key="${tigon.shiro.cas.login-callback-filter-name:cas}">
                    <bean class="io.buji.pac4j.filter.CallbackFilter">
                        <property name="config" ref="casConfig" />
                    </bean>
                </entry>
                <entry key="${tigon.shiro.cas.security-filter-name:user}">
                    <bean class="io.buji.pac4j.filter.SecurityFilter">
                        <property name="config" ref="casConfig" />
                        <property name="clients" value="${spring.application.name}" />
                    </bean>
                </entry>
                <entry key="${tigon.shiro.cas.logout-filter-name:logout}">
                    <bean class="io.buji.pac4j.filter.LogoutFilter">
                        <property name="config" ref="casConfig" />
                        <property name="localLogout" value="${tigon.shiro.cas.local-logout:true}" />
                        <property name="centralLogout" value="${tigon.shiro.cas.central-logout:true}" />
                        <property name="logoutUrlPattern" value="${tigon.shiro.logout-path:/logout}" />
                        <property name="defaultUrl" value="${tigon.shiro.logout-success-url:/logout-success}" />
                    </bean>
                </entry>
            </map>
        </property>

        <property name="filterChainDefinitionMap">
            <bean class="me.chyxion.tigon.shiro.FilterChainDefinitionMap">
                <constructor-arg value="${tigon.shiro.filter-chain-definition.config-location:classpath:shiro/auth.properties}" />
                <constructor-arg>
                    <map>
                        <entry key="${tigon.shiro.cas.login-callback-path:/cas/login}" value="${tigon.shiro.cas.login-callback-filter-name:cas}" />
                        <entry key="${tigon.shiro.cas.logout-path:/logout}" value="${tigon.shiro.cas.logout-filter-name:logout}" />
                        <entry key="${tigon.shiro.cas.logout-success-url:/logout-success}" value="anon" />
                    </map>
                </constructor-arg>
            </bean>
        </property>
    </bean>
</beans>
