KickJava   Java API By Example, From Geeks To Geeks.

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


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.DestroyFailedException JavaDoc;
23 import javax.security.auth.Subject 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 named 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 NamedUPCredentialLoginModule implements LoginModule JavaDoc {
42
43     public static final String JavaDoc CREDENTIAL_NAME = "org.apache.geronimo.jaas.NamedUPCredentialLoginModule.Name";
44
45     private String JavaDoc name;
46     private Subject JavaDoc subject;
47     private CallbackHandler JavaDoc callbackHandler;
48     private NamedUsernamePasswordCredential nupCredential;
49
50     public boolean abort() throws LoginException JavaDoc {
51
52         return logout();
53     }
54
55     public boolean commit() throws LoginException JavaDoc {
56
57         if (subject.isReadOnly()) {
58             throw new LoginException JavaDoc("Subject is ReadOnly");
59         }
60
61         Set JavaDoc pvtCreds = subject.getPrivateCredentials();
62         if (nupCredential != null && !pvtCreds.contains(nupCredential)) {
63             pvtCreds.add(nupCredential);
64         }
65
66         return true;
67     }
68
69     public boolean login() throws LoginException JavaDoc {
70
71         Callback JavaDoc[] callbacks = new Callback JavaDoc[2];
72
73         callbacks[0] = new NameCallback JavaDoc("User name");
74         callbacks[1] = new PasswordCallback JavaDoc("Password", false);
75         try {
76             callbackHandler.handle(callbacks);
77         } catch (IOException JavaDoc ioe) {
78             throw (LoginException JavaDoc) new LoginException JavaDoc().initCause(ioe);
79         } catch (UnsupportedCallbackException JavaDoc uce) {
80             throw (LoginException JavaDoc) new LoginException JavaDoc().initCause(uce);
81         }
82
83         String JavaDoc username = ((NameCallback JavaDoc) callbacks[0]).getName();
84         char[] password = ((PasswordCallback JavaDoc) callbacks[1]).getPassword();
85
86         if (username == null || password == null) return true;
87
88         nupCredential = new NamedUsernamePasswordCredential(username, password, name);
89
90         return true;
91     }
92
93     public boolean logout() throws LoginException JavaDoc {
94
95         if (nupCredential == null) return true;
96
97         Set JavaDoc pvtCreds = subject.getPrivateCredentials(NamedUsernamePasswordCredential.class);
98         if (pvtCreds.contains(nupCredential)) {
99             pvtCreds.remove(nupCredential);
100         }
101
102         try {
103             nupCredential.destroy();
104         } catch (DestroyFailedException JavaDoc e) {
105             // do nothing
106
}
107         nupCredential = null;
108
109         return true;
110     }
111
112     public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler, Map JavaDoc sharedState, Map JavaDoc options) {
113
114         this.subject = subject;
115         this.callbackHandler = callbackHandler;
116         this.name = (String JavaDoc) options.get(CREDENTIAL_NAME);
117     }
118 }
119
Popular Tags