001/**
002 * Copyright 2005-2016 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.rice.kim.client.acegi;
017
018import org.acegisecurity.AuthenticationException;
019import org.acegisecurity.userdetails.UserDetails;
020import org.acegisecurity.userdetails.UserDetailsService;
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.springframework.util.Assert;
024
025/**
026 * Populates the <code>UserDetails</code> associated with a CAS 
027 * authenticated user by reading the response.  This is required to pass
028 * the Distributed Session Ticket around.
029 *  
030 * @author Kuali Rice Team (rice.collab@kuali.org)
031 *
032 */
033public class KualiCasAuthoritiesPopulatorImpl implements KualiCasAuthoritiesPopulator {
034    private KualiUserDetailsService userDetailsService;
035    private static final Log logger = LogFactory.getLog(KualiCasAuthoritiesPopulatorImpl.class);
036
037    
038    /**
039     * This method validates the Spring configuration
040     * 
041     * @throws Exception
042     */
043    public void afterPropertiesSet() throws Exception {
044        Assert.notNull(this.userDetailsService, "A UserDetailsService must be set");
045    }
046    
047    /**
048     * @param userDetailsService the UserDetailsService to set
049     */
050    public void setUserDetailsService(UserDetailsService userDetailsService) {
051        this.userDetailsService = (KualiUserDetailsService)userDetailsService;
052    }
053    
054    /**
055     * This overridden method should never be used but is required by the 
056     * UserDetails interface
057     * 
058     * @see org.acegisecurity.providers.cas.CasAuthoritiesPopulator#getUserDetails(java.lang.String)
059     */
060    public UserDetails getUserDetails(String casUserId)
061        throws AuthenticationException {
062        if (logger.isDebugEnabled()) {
063            logger.debug("getUserDetails(userID)");
064        }
065        return this.userDetailsService.loadUserByUsername(casUserId);
066    }
067    
068    /**
069     * This overridden method is used to pass the Distributed Session 
070     * Ticket around via the {@link KualiTicketResponse}
071     * 
072     * @see org.kuali.rice.kim.client.acegi.KualiCasAuthoritiesPopulator#getUserDetails(org.kuali.rice.kim.client.acegi.KualiTicketResponse)
073     */
074    public UserDetails getUserDetails(KualiTicketResponse response) 
075        throws AuthenticationException {
076        if (logger.isDebugEnabled()) {
077            logger.debug("getUserDetails(response)");
078        }
079        return this.userDetailsService.loadUserByTicketResponse(response);
080    }
081
082}