View Javadoc

1   /*
2    * $Id: AcegiProviderAdapter.java 11517 2008-03-31 21:34:19Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.acegi;
12  
13  import org.mule.api.lifecycle.InitialisationException;
14  import org.mule.api.security.Authentication;
15  import org.mule.api.security.SecurityContext;
16  import org.mule.api.security.SecurityContextFactory;
17  import org.mule.api.security.SecurityException;
18  import org.mule.api.security.SecurityProvider;
19  import org.mule.api.security.UnknownAuthenticationTypeException;
20  
21  import java.util.Map;
22  
23  import org.acegisecurity.AuthenticationException;
24  import org.acegisecurity.providers.AuthenticationProvider;
25  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
26  
27  /**
28   * <code>AcegiProviderAdapter</code> is a wrapper for an Acegi Security provider to
29   * use with the SecurityManager
30   */
31  public class AcegiProviderAdapter implements SecurityProvider, AuthenticationProvider
32  {
33      private AuthenticationProvider delegate;
34      private String name;
35      private SecurityContextFactory factory;
36      private Map securityProperties;
37  
38      public AcegiProviderAdapter()
39      {
40          super();
41      }
42  
43      public AcegiProviderAdapter(AuthenticationProvider delegate)
44      {
45          this.delegate = delegate;
46      }
47  
48      public AcegiProviderAdapter(AuthenticationProvider delegate, String name)
49      {
50          this.delegate = delegate;
51          this.name = name;
52      }
53  
54      public void initialise() throws InitialisationException
55      {
56          // all initialisation should be handled in the spring
57          // intitialisation hook afterPropertiesSet()
58  
59          // register context factory
60          factory = new AcegiSecurityContextFactory();
61      }
62  
63      public void setName(String name)
64      {
65          this.name = name;
66      }
67  
68      public String getName()
69      {
70          return name;
71      }
72  
73      public Authentication authenticate(Authentication authentication) throws SecurityException
74      {
75          org.acegisecurity.Authentication auth = null;
76          if (authentication instanceof AcegiAuthenticationAdapter)
77          {
78              auth = ((AcegiAuthenticationAdapter)authentication).getDelegate();
79          }
80          else
81          {
82              auth = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
83                  authentication.getCredentials());
84  
85          }
86          auth = delegate.authenticate(auth);
87          return new AcegiAuthenticationAdapter(auth, getSecurityProperties());
88      }
89  
90      public org.acegisecurity.Authentication authenticate(org.acegisecurity.Authentication authentication) throws AuthenticationException
91      {
92          return delegate.authenticate(authentication);
93      }
94  
95      public boolean supports(Class aClass)
96      {
97          return Authentication.class.isAssignableFrom(aClass);
98      }
99  
100     public AuthenticationProvider getDelegate()
101     {
102         return delegate;
103     }
104 
105     public void setDelegate(AuthenticationProvider delegate)
106     {
107         this.delegate = delegate;
108     }
109 
110     public SecurityContext createSecurityContext(Authentication auth)
111         throws UnknownAuthenticationTypeException
112     {
113         /*
114          * if (strategy != null){ return factory.create(auth, strategy); } else {
115          */
116         return factory.create(auth);
117         // }
118     }
119 
120     public Map getSecurityProperties()
121     {
122         return securityProperties;
123     }
124 
125     public void setSecurityProperties(Map securityProperties)
126     {
127         this.securityProperties = securityProperties;
128     }
129 }