1   /* Copyright 2004 Acegi Technology Pty Limited
2    *
3    * Licensed under the Apache License, Version 2.0 (the "License");
4    * you may not use this file except in compliance with the License.
5    * You may obtain a copy of the License at
6    *
7    *     http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * 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  /***
29   * DOCUMENT ME!
30   *
31   * @author Ray Krueger
32   * @version $Id: TestLoginModule.java,v 1.5 2005/11/17 00:55:47 benalex Exp $
33   */
34  public class TestLoginModule implements LoginModule {
35      //~ Instance fields ========================================================
36  
37      private String password;
38      private String user;
39      private Subject subject;
40  
41      //~ Methods ================================================================
42  
43      public boolean abort() throws LoginException {
44          return true;
45      }
46  
47      public boolean commit() throws LoginException {
48          return true;
49      }
50  
51      public void initialize(Subject subject, CallbackHandler callbackHandler,
52          Map sharedState, Map options) {
53          this.subject = subject;
54  
55          try {
56              TextInputCallback textCallback = new TextInputCallback("prompt");
57              NameCallback nameCallback = new NameCallback("prompt");
58              PasswordCallback passwordCallback = new PasswordCallback("prompt",
59                      false);
60  
61              callbackHandler.handle(new Callback[] {textCallback, nameCallback, passwordCallback});
62  
63              password = new String(passwordCallback.getPassword());
64              user = nameCallback.getName();
65          } catch (Exception e) {
66              throw new RuntimeException(e);
67          }
68      }
69  
70      public boolean login() throws LoginException {
71          if (!user.equals("user")) {
72              throw new LoginException("Bad User");
73          }
74  
75          if (!password.equals("password")) {
76              throw new LoginException("Bad Password");
77          }
78  
79          subject.getPrincipals().add(new Principal() {
80                  public String getName() {
81                      return "TEST_PRINCIPAL";
82                  }
83              });
84  
85          subject.getPrincipals().add(new Principal() {
86                  public String getName() {
87                      return "NULL_PRINCIPAL";
88                  }
89              });
90  
91          return true;
92      }
93  
94      public boolean logout() throws LoginException {
95          return true;
96      }
97  }