1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.tests.services.security;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.context.ConfigurableApplicationContext;
28 import org.springframework.security.access.AccessDeniedException;
29 import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
30 import org.springframework.security.authentication.BadCredentialsException;
31 import org.springframework.security.core.Authentication;
32 import org.springframework.security.core.GrantedAuthority;
33 import org.springframework.security.core.authority.GrantedAuthorityImpl;
34
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.fail;
37
38 import ch.elca.el4j.core.context.ModuleApplicationContext;
39 import ch.elca.el4j.services.security.authentication.AuthenticationService;
40 import ch.elca.el4j.services.security.encryption.RSACipher;
41 import ch.elca.el4j.tests.services.security.provider.ExtendedTestingAuthenticationProvider;
42 import ch.elca.el4j.tests.services.security.sample.SampleService;
43 import ch.elca.el4j.tests.services.security.server.AuthorizationServer;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 public class AuthorizationDistributedTest {
60
61
62
63 private static Logger s_logger = LoggerFactory
64 .getLogger(AuthorizationDistributedTest.class);
65
66
67
68
69 private static final String METHOD_ACCESS_ROLE = "ROLE_PERMISSION_ADDONE";
70
71
72
73
74 private String[] m_configLocationsServer = new String[] {
75 "classpath*:mandatory/*.xml",
76 "classpath:optional/security-attributes.xml",
77 "classpath:scenarios/services/sampleService.xml",
78 "classpath:scenarios/server/applicationContextTest.xml",
79 "classpath:scenarios/securityscope/distributed-security-scope-server.xml",
80 "classpath:optional/rmi-protocol-config.xml",
81 "classpath:scenarios/services/serviceExporter.xml"};
82
83
84
85
86 private String[] m_configLocationsClient = new String[] {
87 "classpath*:mandatory/*.xml",
88 "classpath:scenarios/services/serviceProxy.xml",
89 "classpath:scenarios/securityscope/distributed-security-scope-client.xml",
90 "classpath:optional/rmi-protocol-config.xml"};
91
92
93
94
95 private ConfigurableApplicationContext m_ac;
96
97
98
99
100
101 @Before
102 public void setUp() {
103 s_logger.debug("Starting server.");
104 AuthorizationServer.main(m_configLocationsServer);
105 s_logger.debug("Server started. Loading client context.");
106
107 m_ac = new ModuleApplicationContext(m_configLocationsClient, false);
108 s_logger.debug("Client context loaded.");
109 }
110
111
112
113
114 @After
115 public void tearDown() {
116 AuthorizationServer.close();
117 m_ac.close();
118 }
119
120
121
122
123
124
125
126 @Test
127 public void testMethodCallWithoutLogin() throws Exception {
128 try {
129 getSampleService().addOne(1234);
130 fail("User should not be able to execute this method "
131 + "without login");
132 } catch (AuthenticationCredentialsNotFoundException e) {
133
134 }
135 }
136
137
138
139
140
141
142
143 @Test
144 public void testCorrectAuthorization() throws Exception {
145 createSecureContext("server", "server", METHOD_ACCESS_ROLE);
146 int result = getSampleService().addOne(1234);
147 assertEquals(result, 1235);
148 }
149
150
151
152
153
154
155
156
157 @Test
158 public void testCorrectAuthorizationAfterLogoutNoAccess() throws Exception {
159 createSecureContext("server", "server", METHOD_ACCESS_ROLE);
160 int result = getSampleService().addOne(1234);
161 assertEquals(result, 1235);
162
163 destroySecureContext("server", "server");
164
165 try {
166 getSampleService().addOne(1234);
167 fail("An AccessDeniedException should have been thrown.");
168 } catch (AccessDeniedException e) {
169
170 }
171 }
172
173
174
175
176
177
178
179
180 @Test
181 public void testFailedAuthorization() throws Exception {
182 createSecureContext("test4", "test4", "ROLE_NO_PERMISSION");
183
184 try {
185 getSampleService().addOne(1234);
186 fail("An AccessDeniedException should have been thrown.");
187 } catch (AccessDeniedException e) {
188
189 }
190 }
191
192
193
194
195
196
197
198 @Test
199 public void testFailedAuthentication() throws Exception {
200 try {
201 createSecureContext("Different username", "than password", "ROLE_TELLER");
202 fail("User should not be able to authenticate since the password "
203 + "is not valid.");
204 } catch (BadCredentialsException e) {
205
206 }
207 }
208
209
210
211
212 private AuthenticationService getAuthenticationService() {
213 return (AuthenticationService) m_ac.getBean("authenticationService");
214 }
215
216
217
218
219
220
221 private ExtendedTestingAuthenticationProvider getAuthenticationProvider() {
222
223 return (ExtendedTestingAuthenticationProvider)
224 AuthorizationServer.getApplicationContext().
225 getBean("extendedTestingAuthenticationProvider");
226 }
227
228
229
230
231 private SampleService getSampleService() {
232 return (SampleService) m_ac.getBean("sampleService");
233 }
234
235
236
237
238
239
240
241
242
243
244
245
246 private void createSecureContext(String principal, String credential,
247 String role) {
248
249 String publicKey = getAuthenticationProvider().getPublicKey();
250 RSACipher rsaCipher = new RSACipher(publicKey);
251 String encryptedCredential = rsaCipher.encrypt(credential);
252
253 Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
254 authorities.add(new GrantedAuthorityImpl("ROLE_TELLER"));
255 authorities.add(new GrantedAuthorityImpl(role));
256
257 Authentication auth = new TestingAuthenticationToken(principal,
258 encryptedCredential, authorities);
259
260 getAuthenticationService().authenticate(auth);
261 }
262
263
264
265
266
267
268
269 private void destroySecureContext(String principal, String credential) {
270
271 String publicKey = getAuthenticationProvider().getPublicKey();
272 RSACipher rsaCipher = new RSACipher(publicKey);
273 String encryptedCredential = rsaCipher.encrypt(credential);
274
275 Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
276
277 Authentication auth = new TestingAuthenticationToken(principal,
278 encryptedCredential, authorities);
279
280 getAuthenticationService().authenticate(auth);
281 }
282 }
283
284