1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.impl.security; |
12 | |
|
13 | |
import org.mule.umo.UMOEncryptionStrategy; |
14 | |
import org.mule.umo.lifecycle.InitialisationException; |
15 | |
import org.mule.umo.security.SecurityException; |
16 | |
import org.mule.umo.security.SecurityProviderNotFoundException; |
17 | |
import org.mule.umo.security.UMOAuthentication; |
18 | |
import org.mule.umo.security.UMOSecurityContext; |
19 | |
import org.mule.umo.security.UMOSecurityManager; |
20 | |
import org.mule.umo.security.UMOSecurityProvider; |
21 | |
import org.mule.umo.security.UnknownAuthenticationTypeException; |
22 | |
|
23 | |
import java.util.ArrayList; |
24 | |
import java.util.Collections; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.List; |
27 | |
import java.util.Map; |
28 | |
|
29 | |
import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; |
30 | |
import org.apache.commons.logging.Log; |
31 | |
import org.apache.commons.logging.LogFactory; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | 0 | public class MuleSecurityManager implements UMOSecurityManager |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | 0 | protected static final Log logger = LogFactory.getLog(MuleSecurityManager.class); |
44 | |
|
45 | 0 | private Map providers = new ConcurrentHashMap(); |
46 | 0 | private Map cryptoStrategies = new ConcurrentHashMap(); |
47 | |
|
48 | |
public void initialise() throws InitialisationException |
49 | |
{ |
50 | 0 | for (Iterator iterator = providers.values().iterator(); iterator.hasNext();) |
51 | |
{ |
52 | 0 | UMOSecurityProvider provider = (UMOSecurityProvider) iterator.next(); |
53 | 0 | provider.initialise(); |
54 | |
} |
55 | |
|
56 | 0 | for (Iterator iterator = cryptoStrategies.values().iterator(); iterator.hasNext();) |
57 | |
{ |
58 | 0 | UMOEncryptionStrategy strategy = (UMOEncryptionStrategy) iterator.next(); |
59 | 0 | strategy.initialise(); |
60 | |
} |
61 | 0 | } |
62 | |
|
63 | |
public UMOAuthentication authenticate(UMOAuthentication authentication) |
64 | |
throws SecurityException, SecurityProviderNotFoundException |
65 | |
{ |
66 | 0 | Iterator iter = providers.values().iterator(); |
67 | |
|
68 | 0 | Class toTest = authentication.getClass(); |
69 | |
|
70 | 0 | while (iter.hasNext()) |
71 | |
{ |
72 | 0 | UMOSecurityProvider provider = (UMOSecurityProvider) iter.next(); |
73 | |
|
74 | 0 | if (provider.supports(toTest)) |
75 | |
{ |
76 | 0 | if (logger.isDebugEnabled()) |
77 | |
{ |
78 | 0 | logger.debug("Authentication attempt using " + provider.getClass().getName()); |
79 | |
} |
80 | |
|
81 | 0 | UMOAuthentication result = provider.authenticate(authentication); |
82 | |
|
83 | 0 | if (result != null) |
84 | |
{ |
85 | 0 | return result; |
86 | |
} |
87 | |
} |
88 | |
} |
89 | |
|
90 | 0 | throw new SecurityProviderNotFoundException(toTest.getName()); |
91 | |
} |
92 | |
|
93 | |
public void addProvider(UMOSecurityProvider provider) |
94 | |
{ |
95 | 0 | if (getProvider(provider.getName()) != null) |
96 | |
{ |
97 | 0 | throw new IllegalArgumentException("Provider already registered: " + provider.getName()); |
98 | |
} |
99 | 0 | providers.put(provider.getName(), provider); |
100 | 0 | } |
101 | |
|
102 | |
public UMOSecurityProvider getProvider(String name) |
103 | |
{ |
104 | 0 | if (name == null) |
105 | |
{ |
106 | 0 | throw new IllegalArgumentException("provider Name cannot be null"); |
107 | |
} |
108 | 0 | return (UMOSecurityProvider) providers.get(name); |
109 | |
} |
110 | |
|
111 | |
public UMOSecurityProvider removeProvider(String name) |
112 | |
{ |
113 | 0 | return (UMOSecurityProvider) providers.remove(name); |
114 | |
} |
115 | |
|
116 | |
public List getProviders() |
117 | |
{ |
118 | 0 | return Collections.unmodifiableList(new ArrayList(providers.values())); |
119 | |
} |
120 | |
|
121 | |
public void setProviders(List providers) |
122 | |
{ |
123 | 0 | for (Iterator iterator = providers.iterator(); iterator.hasNext();) |
124 | |
{ |
125 | 0 | UMOSecurityProvider provider = (UMOSecurityProvider) iterator.next(); |
126 | 0 | addProvider(provider); |
127 | |
} |
128 | 0 | } |
129 | |
|
130 | |
public UMOSecurityContext createSecurityContext(UMOAuthentication authentication) |
131 | |
throws UnknownAuthenticationTypeException |
132 | |
{ |
133 | 0 | Iterator iter = providers.values().iterator(); |
134 | |
|
135 | 0 | Class toTest = authentication.getClass(); |
136 | |
|
137 | 0 | while (iter.hasNext()) |
138 | |
{ |
139 | 0 | UMOSecurityProvider provider = (UMOSecurityProvider) iter.next(); |
140 | |
|
141 | 0 | if (provider.supports(toTest)) |
142 | |
{ |
143 | 0 | return provider.createSecurityContext(authentication); |
144 | |
} |
145 | |
} |
146 | 0 | throw new UnknownAuthenticationTypeException(authentication); |
147 | |
} |
148 | |
|
149 | |
public UMOEncryptionStrategy getEncryptionStrategy(String name) |
150 | |
{ |
151 | 0 | return (UMOEncryptionStrategy) cryptoStrategies.get(name); |
152 | |
} |
153 | |
|
154 | |
public void addEncryptionStrategy(String name, UMOEncryptionStrategy strategy) |
155 | |
{ |
156 | 0 | cryptoStrategies.put(name, strategy); |
157 | 0 | } |
158 | |
|
159 | |
public UMOEncryptionStrategy removeEncryptionStrategy(String name) |
160 | |
{ |
161 | 0 | return (UMOEncryptionStrategy) cryptoStrategies.remove(name); |
162 | |
|
163 | |
} |
164 | |
|
165 | |
public void setEncryptionStrategies(Map strategies) |
166 | |
{ |
167 | 0 | cryptoStrategies.putAll(strategies); |
168 | 0 | } |
169 | |
} |