1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.geronimo.security.jaas;
18
19 import java.util.Map;
20 import java.util.Set;
21 import javax.security.auth.spi.LoginModule;
22 import javax.security.auth.Subject;
23 import javax.security.auth.DestroyFailedException;
24 import javax.security.auth.login.LoginException;
25 import javax.security.auth.callback.CallbackHandler;
26
27 /**
28 * @version $Rev: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
29 */
30 public class ConfiguredIdentityNamedUsernamePasswordLoginModule implements LoginModule {
31 public static final String CREDENTIAL_NAME = "org.apache.geronimo.jaas.NamedUsernamePasswordCredential.Name";
32 public static final String USER_NAME = "org.apache.geronimo.jaas.NamedUsernamePasswordCredential.Username";
33 public static final String PASSWORD = "org.apache.geronimo.jaas.NamedUsernamePasswordCredential.Password";
34
35 private Subject subject;
36 private NamedUsernamePasswordCredential namedUsernamePasswordCredential;
37
38 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
39 this.subject = subject;
40 String name = (String) options.get(CREDENTIAL_NAME);
41 String username = (String) options.get(USER_NAME);
42 String password = (String) options.get(PASSWORD);
43 namedUsernamePasswordCredential = new NamedUsernamePasswordCredential(username, password.toCharArray(), name);
44 }
45
46 public boolean login() throws LoginException {
47 return true;
48 }
49
50 public boolean commit() throws LoginException {
51 if (subject.isReadOnly()) {
52 throw new LoginException("Subject is ReadOnly");
53 }
54
55 Set pvtCreds = subject.getPrivateCredentials();
56 if (namedUsernamePasswordCredential != null && !pvtCreds.contains(namedUsernamePasswordCredential)) {
57 pvtCreds.add(namedUsernamePasswordCredential);
58 }
59 return true;
60 }
61
62 public boolean abort() throws LoginException {
63 return logout();
64 }
65
66 public boolean logout() throws LoginException {
67 if (namedUsernamePasswordCredential == null) {
68 return true;
69 }
70
71 Set pvtCreds = subject.getPrivateCredentials(UsernamePasswordCredential.class);
72 if (pvtCreds.contains(namedUsernamePasswordCredential)) {
73 pvtCreds.remove(namedUsernamePasswordCredential);
74 }
75
76 try {
77 namedUsernamePasswordCredential.destroy();
78 } catch (DestroyFailedException e) {
79
80 }
81 namedUsernamePasswordCredential = null;
82
83 return true;
84 }
85 }