Coverage Report - org.kuali.student.security.spring.KSRiceDefaultUserDetailsService
 
Classes in this File Line Coverage Branch Coverage Complexity
KSRiceDefaultUserDetailsService
0%
0/18
0%
0/10
3.333
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.security.spring;
 17  
 
 18  
 import org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
 19  
 import org.kuali.student.common.util.security.UserWithId;
 20  
 import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
 21  
 import org.springframework.security.userdetails.UserDetails;
 22  
 import org.springframework.security.userdetails.UsernameNotFoundException;
 23  
 
 24  
 /**
 25  
  * This is a description of what this class does. 
 26  
  * 
 27  
  * @author Kuali Student Team
 28  
  *
 29  
  */
 30  0
 public class KSRiceDefaultUserDetailsService extends KSDefaultUserDetailsService{
 31  
         
 32  
     
 33  
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 34  0
             return this.getUserDetails(username, null);
 35  
     }
 36  
     
 37  
     public UserDetails loadUserByUsernameAndToken(String username, UsernamePasswordAuthenticationToken authentication) throws UsernameNotFoundException {
 38  0
         return this.getUserDetails(username, authentication);
 39  
     }
 40  
     
 41  
     protected UserDetails getUserDetails(String username, UsernamePasswordAuthenticationToken authentication) throws UsernameNotFoundException {
 42  0
         if(username==null || username.equals("")){
 43  0
             throw new UsernameNotFoundException("Username cannot be null or empty");
 44  
         }                
 45  
                                
 46  0
         KimPrincipalInfo kimPrincipalInfo = null;
 47  0
         kimPrincipalInfo = getIdentityService().getPrincipalByPrincipalName(username);
 48  
         
 49  
         String userId;
 50  
         String password; 
 51  0
         if (null != kimPrincipalInfo) {
 52  0
             username = kimPrincipalInfo.getPrincipalName();
 53  0
             userId = kimPrincipalInfo.getPrincipalId();
 54  0
             if(authentication == null){
 55  0
                     password = kimPrincipalInfo.getPassword();
 56  
             }else{
 57  
                     // How will this affect CAS?
 58  
                     //password = kimPrincipalInfo.getPassword();
 59  0
                     password = (String)authentication.getCredentials();
 60  
             }
 61  
         } else {
 62  
         // When a UsernameNotFoundException is thrown, spring security will proceed to the next AuthenticationProvider on the list.
 63  
         // When Rice is running and username is not found in KIM, we want authentication to stop and allow the user to enter the correct username.
 64  
         // to do this we need to throw a AccountStatusException and not UsernameNotFoundException.
 65  0
             throw new KimUserNotFoundException("Invalid username or password");  
 66  
         }
 67  
         
 68  0
         password = (password == null ? "":password);
 69  0
         UserWithId ksuser = new UserWithId(username, password, super.enabled, true, true, super.nonlocked, getGrantedAuthority(userId));
 70  0
         ksuser.setUserId(userId);
 71  0
         return ksuser;
 72  
     }
 73  
           
 74  
     
 75  
 }