01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.acegisecurity.providers.jaas;
17:
18: import java.security.Principal;
19:
20: import java.util.Map;
21:
22: import javax.security.auth.Subject;
23: import javax.security.auth.callback.*;
24: import javax.security.auth.login.LoginException;
25: import javax.security.auth.spi.LoginModule;
26:
27: /**
28: * DOCUMENT ME!
29: *
30: * @author Ray Krueger
31: * @version $Id: TestLoginModule.java 1496 2006-05-23 13:38:33Z benalex $
32: */
33: public class TestLoginModule implements LoginModule {
34: //~ Instance fields ================================================================================================
35:
36: private String password;
37: private String user;
38: private Subject subject;
39:
40: //~ Methods ========================================================================================================
41:
42: public boolean abort() throws LoginException {
43: return true;
44: }
45:
46: public boolean commit() throws LoginException {
47: return true;
48: }
49:
50: public void initialize(Subject subject,
51: CallbackHandler callbackHandler, Map sharedState,
52: Map options) {
53: this .subject = subject;
54:
55: try {
56: TextInputCallback textCallback = new TextInputCallback(
57: "prompt");
58: NameCallback nameCallback = new NameCallback("prompt");
59: PasswordCallback passwordCallback = new PasswordCallback(
60: "prompt", false);
61:
62: callbackHandler.handle(new Callback[] { textCallback,
63: nameCallback, passwordCallback });
64:
65: password = new String(passwordCallback.getPassword());
66: user = nameCallback.getName();
67: } catch (Exception e) {
68: throw new RuntimeException(e);
69: }
70: }
71:
72: public boolean login() throws LoginException {
73: if (!user.equals("user")) {
74: throw new LoginException("Bad User");
75: }
76:
77: if (!password.equals("password")) {
78: throw new LoginException("Bad Password");
79: }
80:
81: subject.getPrincipals().add(new Principal() {
82: public String getName() {
83: return "TEST_PRINCIPAL";
84: }
85: });
86:
87: subject.getPrincipals().add(new Principal() {
88: public String getName() {
89: return "NULL_PRINCIPAL";
90: }
91: });
92:
93: return true;
94: }
95:
96: public boolean logout() throws LoginException {
97: return true;
98: }
99: }
|