1 /**
2 *
3 * Copyright 2005 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.io.IOException;
20 import java.util.Map;
21 import java.util.Set;
22 import javax.security.auth.Subject;
23 import javax.security.auth.DestroyFailedException;
24 import javax.security.auth.callback.Callback;
25 import javax.security.auth.callback.CallbackHandler;
26 import javax.security.auth.callback.NameCallback;
27 import javax.security.auth.callback.PasswordCallback;
28 import javax.security.auth.callback.UnsupportedCallbackException;
29 import javax.security.auth.login.LoginException;
30 import javax.security.auth.spi.LoginModule;
31
32
33 /**
34 * Inserts Username/Password credential into private credentials of Subject.
35 * <p/>
36 * If either the username or password is not passed in the callback handler,
37 * then the credential is not placed into the Subject.
38 *
39 * @version $Revision: 355877 $ $Date: 2005-12-10 18:48:27 -0800 (Sat, 10 Dec 2005) $
40 */
41 public class UPCredentialLoginModule implements LoginModule {
42
43 private Subject subject;
44 private CallbackHandler callbackHandler;
45 private UsernamePasswordCredential upCredential;
46
47 public boolean abort() throws LoginException {
48
49 return logout();
50 }
51
52 public boolean commit() throws LoginException {
53
54 if (subject.isReadOnly()) {
55 throw new LoginException("Subject is ReadOnly");
56 }
57
58 Set pvtCreds = subject.getPrivateCredentials();
59 if (upCredential != null && !pvtCreds.contains(upCredential)) {
60 pvtCreds.add(upCredential);
61 }
62
63 return true;
64 }
65
66 public boolean login() throws LoginException {
67
68 Callback[] callbacks = new Callback[2];
69
70 callbacks[0] = new NameCallback("User name");
71 callbacks[1] = new PasswordCallback("Password", false);
72 try {
73 callbackHandler.handle(callbacks);
74 } catch (IOException ioe) {
75 throw (LoginException) new LoginException().initCause(ioe);
76 } catch (UnsupportedCallbackException uce) {
77 throw (LoginException) new LoginException().initCause(uce);
78 }
79
80 String username = ((NameCallback) callbacks[0]).getName();
81 char[] password = ((PasswordCallback) callbacks[1]).getPassword();
82
83 if (username == null || password == null) return true;
84
85 upCredential = new UsernamePasswordCredential(username, password);
86
87 return true;
88 }
89
90 public boolean logout() throws LoginException {
91
92 if (upCredential == null) return true;
93
94 Set pvtCreds = subject.getPrivateCredentials(UsernamePasswordCredential.class);
95 if (pvtCreds.contains(upCredential)) {
96 pvtCreds.remove(upCredential);
97 }
98
99 try {
100 upCredential.destroy();
101 } catch (DestroyFailedException e) {
102
103 }
104 upCredential = null;
105
106 return true;
107 }
108
109 public void initialize(Subject subject, CallbackHandler callbackHandler, Map sharedState, Map options) {
110
111 this.subject = subject;
112 this.callbackHandler = callbackHandler;
113 }
114 }