1
2
3
4
5
6
7
8
9
10
11
12
13
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
36
37 private String password;
38 private String user;
39 private Subject subject;
40
41
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 }