01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.test;
23:
24: import java.util.Map;
25: import javax.security.auth.Subject;
26: import javax.security.auth.callback.CallbackHandler;
27: import javax.security.auth.login.LoginException;
28: import javax.security.auth.spi.LoginModule;
29: import org.jboss.security.SimplePrincipal;
30:
31: public class TestLoginModule implements LoginModule {
32: Subject subject;
33: String principal;
34: String name;
35: boolean succeed;
36: boolean throwEx;
37:
38: public TestLoginModule() {
39: }
40:
41: public void initialize(Subject subject, CallbackHandler handler,
42: Map sharedState, Map options) {
43: this .subject = subject;
44: principal = (String) options.get("principal");
45: if (principal == null)
46: principal = "guest";
47: name = (String) options.get("name");
48: String opt = (String) options.get("succeed");
49: succeed = Boolean.valueOf(opt).booleanValue();
50: opt = (String) options.get("throwEx");
51: throwEx = Boolean.valueOf(opt).booleanValue();
52: System.out.println("initialize, name=" + name);
53: opt = (String) options.get("initEx");
54: if (Boolean.valueOf(opt) == Boolean.TRUE)
55: throw new IllegalArgumentException(
56: "Failed during init, name=" + name);
57: }
58:
59: public boolean login() throws LoginException {
60: System.out.println("login, name=" + name + ", succeed="
61: + succeed);
62: if (throwEx)
63: throw new LoginException("Failed during login, name="
64: + name);
65: return succeed;
66: }
67:
68: public boolean commit() throws LoginException {
69: System.out.println("commit, name=" + name);
70: subject.getPrincipals().add(new SimplePrincipal(principal));
71: subject.getPublicCredentials().add("A public credential");
72: subject.getPrivateCredentials().add("A private credential");
73: return true;
74: }
75:
76: public boolean abort() throws LoginException {
77: System.out.println("abort, name=" + name);
78: return true;
79: }
80:
81: public boolean logout() throws LoginException {
82: System.out.println("logout, name=" + name);
83: subject.getPrincipals().remove(new SimplePrincipal(principal));
84: return succeed;
85: }
86:
87: }
|