KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > jaas > UPCredentialLoginModule


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. 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 JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import javax.security.auth.Subject JavaDoc;
23 import javax.security.auth.DestroyFailedException JavaDoc;
24 import javax.security.auth.callback.Callback JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.NameCallback JavaDoc;
27 import javax.security.auth.callback.PasswordCallback JavaDoc;
28 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
29 import javax.security.auth.login.LoginException JavaDoc;
30 import javax.security.auth.spi.LoginModule JavaDoc;
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: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
40  */

41 public class UPCredentialLoginModule implements LoginModule JavaDoc {
42
43     private Subject JavaDoc subject;
44     private CallbackHandler JavaDoc callbackHandler;
45     private UsernamePasswordCredential upCredential;
46
47     public boolean abort() throws LoginException JavaDoc {
48
49         return logout();
50     }
51
52     public boolean commit() throws LoginException JavaDoc {
53
54         if (subject.isReadOnly()) {
55             throw new LoginException JavaDoc("Subject is ReadOnly");
56         }
57
58         Set JavaDoc 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 JavaDoc {
67
68         Callback JavaDoc[] callbacks = new Callback JavaDoc[2];
69
70         callbacks[0] = new NameCallback JavaDoc("User name");
71         callbacks[1] = new PasswordCallback JavaDoc("Password", false);
72         try {
73             callbackHandler.handle(callbacks);
74         } catch (IOException JavaDoc ioe) {
75             throw (LoginException JavaDoc) new LoginException JavaDoc().initCause(ioe);
76         } catch (UnsupportedCallbackException JavaDoc uce) {
77             throw (LoginException JavaDoc) new LoginException JavaDoc().initCause(uce);
78         }
79
80         String JavaDoc username = ((NameCallback JavaDoc) callbacks[0]).getName();
81         char[] password = ((PasswordCallback JavaDoc) 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 JavaDoc {
91
92         if (upCredential == null) return true;
93
94         Set JavaDoc pvtCreds = subject.getPrivateCredentials(UsernamePasswordCredential.class);
95         if (pvtCreds.contains(upCredential)) {
96             pvtCreds.remove(upCredential);
97         }
98
99         try {
100             upCredential.destroy();
101         } catch (DestroyFailedException JavaDoc e) {
102             // do nothing
103
}
104         upCredential = null;
105
106         return true;
107     }
108
109     public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options) {
110
111         this.subject = subject;
112         this.callbackHandler = callbackHandler;
113     }
114 }
115
Popular Tags