View Javadoc

1   /*
2    * $Id: AcegiProviderAdapter.java 7976 2007-08-21 14:26:13Z 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.extras.acegi;
12  
13  import org.mule.umo.lifecycle.InitialisationException;
14  import org.mule.umo.security.SecurityException;
15  import org.mule.umo.security.UMOAuthentication;
16  import org.mule.umo.security.UMOSecurityContext;
17  import org.mule.umo.security.UMOSecurityContextFactory;
18  import org.mule.umo.security.UMOSecurityProvider;
19  import org.mule.umo.security.UnknownAuthenticationTypeException;
20  
21  import java.util.Map;
22  
23  import org.acegisecurity.Authentication;
24  import org.acegisecurity.AuthenticationException;
25  import org.acegisecurity.providers.AuthenticationProvider;
26  import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
27  
28  /**
29   * <code>AcegiProviderAdapter</code> is a wrapper for an Acegi Security provider to
30   * use with the UMOSecurityManager
31   */
32  public class AcegiProviderAdapter implements UMOSecurityProvider, AuthenticationProvider
33  {
34      private AuthenticationProvider delegate;
35      private String name;
36      private UMOSecurityContextFactory factory;
37      private Map securityProperties;
38  
39      public AcegiProviderAdapter()
40      {
41          super();
42      }
43  
44      public AcegiProviderAdapter(AuthenticationProvider delegate)
45      {
46          this.delegate = delegate;
47      }
48  
49      public AcegiProviderAdapter(AuthenticationProvider delegate, String name)
50      {
51          this.delegate = delegate;
52          this.name = name;
53      }
54  
55      public void initialise() throws InitialisationException
56      {
57          // //all initialisation should be handled in the spring
58          // intitialisation hook afterPropertiesSet()
59  
60          // register context factory
61          factory = new AcegiSecurityContextFactory();
62      }
63  
64      public void setName(String name)
65      {
66          this.name = name;
67      }
68  
69      public String getName()
70      {
71          return name;
72      }
73  
74      public UMOAuthentication authenticate(UMOAuthentication authentication) throws SecurityException
75      {
76          Authentication auth = null;
77          if (authentication instanceof AcegiAuthenticationAdapter)
78          {
79              auth = ((AcegiAuthenticationAdapter)authentication).getDelegate();
80          }
81          else
82          {
83              auth = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
84                  authentication.getCredentials());
85  
86          }
87          auth = delegate.authenticate(auth);
88          return new AcegiAuthenticationAdapter(auth, getSecurityProperties());
89      }
90  
91      public Authentication authenticate(Authentication authentication) throws AuthenticationException
92      {
93          return delegate.authenticate(authentication);
94      }
95  
96      public boolean supports(Class aClass)
97      {
98          return UMOAuthentication.class.isAssignableFrom(aClass);
99      }
100 
101     public AuthenticationProvider getDelegate()
102     {
103         return delegate;
104     }
105 
106     public void setDelegate(AuthenticationProvider delegate)
107     {
108         this.delegate = delegate;
109     }
110 
111     public UMOSecurityContext createSecurityContext(UMOAuthentication auth)
112         throws UnknownAuthenticationTypeException
113     {
114         /*
115          * if (strategy != null){ return factory.create(auth, strategy); } else {
116          */
117         return factory.create(auth);
118         // }
119     }
120 
121     public Map getSecurityProperties()
122     {
123         return securityProperties;
124     }
125 
126     public void setSecurityProperties(Map securityProperties)
127     {
128         this.securityProperties = securityProperties;
129     }
130 }