| 1 |  |   | 
  | 2 |  |   | 
  | 3 |  |   | 
  | 4 |  |   | 
  | 5 |  |   | 
  | 6 |  |   | 
  | 7 |  |   | 
  | 8 |  |   | 
  | 9 |  |   | 
  | 10 |  |   | 
  | 11 |  |   | 
  | 12 |  |   | 
  | 13 |  |   | 
  | 14 |  |   | 
  | 15 |  |   | 
  | 16 |  |  package org.kuali.rice.kim.client.acegi; | 
  | 17 |  |   | 
  | 18 |  |  import org.acegisecurity.Authentication; | 
  | 19 |  |  import org.acegisecurity.AuthenticationException; | 
  | 20 |  |  import org.acegisecurity.BadCredentialsException; | 
  | 21 |  |  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; | 
  | 22 |  |  import org.acegisecurity.providers.cas.CasAuthenticationProvider; | 
  | 23 |  |  import org.acegisecurity.providers.cas.CasAuthenticationToken; | 
  | 24 |  |  import org.acegisecurity.providers.cas.StatelessTicketCache; | 
  | 25 |  |  import org.acegisecurity.ui.cas.CasProcessingFilter; | 
  | 26 |  |  import org.acegisecurity.userdetails.UserDetails; | 
  | 27 |  |  import org.apache.commons.logging.Log; | 
  | 28 |  |  import org.apache.commons.logging.LogFactory; | 
  | 29 |  |   | 
  | 30 |  |   | 
  | 31 |  |   | 
  | 32 |  |   | 
  | 33 |  |   | 
  | 34 |  |   | 
  | 35 |  |   | 
  | 36 |  |   | 
  | 37 |  |   | 
  | 38 |  |   | 
  | 39 |  |   | 
  | 40 |  |   | 
  | 41 |  |   | 
  | 42 |  |   | 
  | 43 | 0 |  public class KualiCasAuthenticationProvider extends CasAuthenticationProvider { | 
  | 44 |  |       | 
  | 45 | 0 |      private static final Log logger = LogFactory.getLog(KualiCasAuthenticationProvider.class); | 
  | 46 |  |   | 
  | 47 |  |       | 
  | 48 |  |   | 
  | 49 |  |   | 
  | 50 |  |   | 
  | 51 |  |   | 
  | 52 |  |   | 
  | 53 |  |   | 
  | 54 |  |      public Authentication authenticate(Authentication authentication) throws AuthenticationException { | 
  | 55 | 0 |          StatelessTicketCache statelessTicketCache = this.getStatelessTicketCache(); | 
  | 56 | 0 |          String key = this.getKey(); | 
  | 57 | 0 |          if (!supports(authentication.getClass())) { | 
  | 58 | 0 |              return null; | 
  | 59 |  |          } | 
  | 60 |  |   | 
  | 61 | 0 |          if (authentication instanceof UsernamePasswordAuthenticationToken | 
  | 62 |  |              && (!CasProcessingFilter.CAS_STATEFUL_IDENTIFIER.equals(authentication.getPrincipal().toString()) | 
  | 63 |  |              && !CasProcessingFilter.CAS_STATELESS_IDENTIFIER.equals(authentication.getPrincipal().toString()))) { | 
  | 64 |  |               | 
  | 65 | 0 |              return null; | 
  | 66 |  |          } | 
  | 67 |  |   | 
  | 68 |  |           | 
  | 69 | 0 |          if (authentication instanceof CasAuthenticationToken) { | 
  | 70 | 0 |              if (key.hashCode() == ((CasAuthenticationToken) authentication).getKeyHash()) { | 
  | 71 | 0 |                  return authentication; | 
  | 72 |  |              } else { | 
  | 73 | 0 |                  throw new BadCredentialsException(messages.getMessage("CasAuthenticationProvider.incorrectKey", | 
  | 74 |  |                          "The presented CasAuthenticationToken does not contain the expected key")); | 
  | 75 |  |              } | 
  | 76 |  |          } | 
  | 77 |  |   | 
  | 78 |  |           | 
  | 79 | 0 |          if ((authentication.getCredentials() == null) || "".equals(authentication.getCredentials())) { | 
  | 80 | 0 |              throw new BadCredentialsException(messages.getMessage("CasAuthenticationProvider.noServiceTicket", | 
  | 81 |  |                      "Failed to provide a CAS service ticket to validate")); | 
  | 82 |  |          } | 
  | 83 |  |   | 
  | 84 | 0 |          boolean stateless = false; | 
  | 85 |  |   | 
  | 86 | 0 |          if (authentication instanceof UsernamePasswordAuthenticationToken | 
  | 87 |  |              && CasProcessingFilter.CAS_STATELESS_IDENTIFIER.equals(authentication.getPrincipal())) { | 
  | 88 | 0 |              stateless = true; | 
  | 89 |  |          } | 
  | 90 |  |   | 
  | 91 | 0 |          CasAuthenticationToken result = null; | 
  | 92 |  |   | 
  | 93 | 0 |          if (stateless) { | 
  | 94 |  |               | 
  | 95 | 0 |              result = statelessTicketCache.getByTicketId(authentication.getCredentials().toString()); | 
  | 96 |  |          } | 
  | 97 |  |   | 
  | 98 | 0 |          if (result == null) { | 
  | 99 | 0 |              result = this.authenticateNow(authentication); | 
  | 100 | 0 |              result.setDetails(authentication.getDetails()); | 
  | 101 |  |          } | 
  | 102 |  |   | 
  | 103 | 0 |          if (stateless) { | 
  | 104 |  |               | 
  | 105 | 0 |              statelessTicketCache.putTicketInCache(result); | 
  | 106 |  |          } | 
  | 107 |  |   | 
  | 108 | 0 |          return result; | 
  | 109 |  |      } | 
  | 110 |  |       | 
  | 111 |  |       | 
  | 112 |  |   | 
  | 113 |  |   | 
  | 114 |  |   | 
  | 115 |  |   | 
  | 116 |  |   | 
  | 117 |  |      private CasAuthenticationToken authenticateNow(Authentication authentication) throws AuthenticationException { | 
  | 118 |  |           | 
  | 119 | 0 |          KualiTicketResponse response = (KualiTicketResponse)this.getTicketValidator().confirmTicketValid(authentication.getCredentials().toString()); | 
  | 120 |  |   | 
  | 121 |  |           | 
  | 122 | 0 |          this.getCasProxyDecider().confirmProxyListTrusted(response.getProxyList()); | 
  | 123 | 0 |          if (logger.isDebugEnabled()) { | 
  | 124 | 0 |              logger.debug("authenticationNOW:" + response); | 
  | 125 |  |          } | 
  | 126 |  |           | 
  | 127 | 0 |          logger.debug("\n\npopulating authorities\n\n"); | 
  | 128 | 0 |          UserDetails userDetails = ((KualiCasAuthoritiesPopulator)this.getCasAuthoritiesPopulator()).getUserDetails(response);         | 
  | 129 |  |   | 
  | 130 |  |           | 
  | 131 | 0 |          return new CasAuthenticationToken(this.getKey(), userDetails, authentication.getCredentials(), | 
  | 132 |  |              userDetails.getAuthorities(), userDetails, response.getProxyList(), response.getProxyGrantingTicketIou()); | 
  | 133 |  |      } | 
  | 134 |  |  } |