001/*
002 * Copyright 2008-2009 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.service.impl;
017
018import javax.servlet.http.HttpServletRequest;
019import org.kuali.rice.kim.api.identity.AuthenticationService;
020
021import org.kuali.student.common.util.security.UserWithId;
022import org.springframework.security.core.Authentication;
023import org.springframework.security.core.context.SecurityContextHolder;
024import org.springframework.security.core.userdetails.UserDetails;
025
026public class AuthenticationServiceImpl implements AuthenticationService {
027    
028        public String getPrincipalName(HttpServletRequest request) {
029            String username=null;
030            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
031            
032            if(auth!=null){
033            Object obj = auth.getPrincipal();
034            if(obj instanceof UserWithId){
035                //This is actually the user Id
036                username = ((UserWithId)obj).getUserId();
037            }else if (obj instanceof UserDetails) {
038                username = ((UserDetails)obj).getUsername();
039            } else {
040                username = request.getRemoteUser();
041            }
042        }
043            return username;
044        }
045}