01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19:
20: package org.apache.geronimo.itest;
21:
22: import java.util.Map;
23: import java.util.Set;
24: import java.util.Arrays;
25: import java.util.HashSet;
26: import java.io.IOException;
27: import java.security.Principal;
28:
29: import javax.security.auth.spi.LoginModule;
30: import javax.security.auth.Subject;
31: import javax.security.auth.login.LoginException;
32: import javax.security.auth.callback.CallbackHandler;
33: import javax.security.auth.callback.Callback;
34: import javax.security.auth.callback.NameCallback;
35: import javax.security.auth.callback.PasswordCallback;
36: import javax.security.auth.callback.UnsupportedCallbackException;
37:
38: import org.apache.geronimo.security.realm.providers.GeronimoUserPrincipal;
39:
40: /**
41: * @version $Rev: 565840 $ $Date: 2007-08-14 10:12:53 -0700 (Tue, 14 Aug 2007) $
42: */
43: public class TestLoginModule implements LoginModule {
44: private Subject subject;
45: private CallbackHandler callbackHandler;
46: private Set<String> users;
47: private String user;
48:
49: public void initialize(Subject subject,
50: CallbackHandler callbackHandler,
51: Map<String, ?> sharedState, Map<String, ?> options) {
52: this .subject = subject;
53: this .callbackHandler = callbackHandler;
54: String userList = (String) options.get("users");
55: String[] userArray = userList.split(",");
56: users = new HashSet<String>(Arrays.asList(userArray));
57: }
58:
59: public boolean login() throws LoginException {
60: Callback[] callbacks = new Callback[] {
61: new NameCallback("user"),
62: new PasswordCallback("password", false) };
63: try {
64: callbackHandler.handle(callbacks);
65: } catch (IOException e) {
66: throw new LoginException(e.getMessage());
67: } catch (UnsupportedCallbackException e) {
68: throw new LoginException(e.getMessage());
69: }
70: user = ((NameCallback) callbacks[0]).getName();
71: String password = new String(((PasswordCallback) callbacks[1])
72: .getPassword());
73: if (user.equals(password) && users.contains(user)) {
74: return true;
75: }
76: throw new LoginException();
77: }
78:
79: public boolean commit() throws LoginException {
80: Principal principal = new GeronimoUserPrincipal(user);
81: subject.getPrincipals().add(principal);
82: return true;
83: }
84:
85: public boolean abort() throws LoginException {
86: return true;
87: }
88:
89: public boolean logout() throws LoginException {
90: return true;
91: }
92: }
|