KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > connector > outbound > security > PasswordCredentialLoginModule


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
18 package org.apache.geronimo.connector.outbound.security;
19
20 import java.io.IOException JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.resource.spi.security.PasswordCredential JavaDoc;
24 import javax.security.auth.Subject JavaDoc;
25 import javax.security.auth.callback.Callback JavaDoc;
26 import javax.security.auth.callback.CallbackHandler JavaDoc;
27 import javax.security.auth.callback.NameCallback JavaDoc;
28 import javax.security.auth.callback.PasswordCallback JavaDoc;
29 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
30 import javax.security.auth.login.LoginException JavaDoc;
31 import javax.security.auth.spi.LoginModule JavaDoc;
32
33 /**
34  *
35  *
36  * @version $Rev: 46019 $ $Date: 2004-09-14 02:56:06 -0700 (Tue, 14 Sep 2004) $
37  *
38  * */

39 public class PasswordCredentialLoginModule implements LoginModule JavaDoc {
40
41     private Subject JavaDoc subject;
42     private CallbackHandler JavaDoc callbackHandler;
43
44     private PasswordCredentialRealm passwordCredentialRealm;
45
46     private String JavaDoc resourcePrincipalName;
47     private String JavaDoc userName;
48     private char[] password;
49
50     public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler,
51             Map JavaDoc sharedState, Map JavaDoc options) {
52         this.subject = subject;
53         this.callbackHandler = callbackHandler;
54         passwordCredentialRealm = (PasswordCredentialRealm) options.get(PasswordCredentialRealm.REALM_INSTANCE);
55         if (passwordCredentialRealm == null) {
56             throw new IllegalArgumentException JavaDoc("No realm supplied in options");
57         }
58     }
59
60     public boolean login() throws LoginException JavaDoc {
61         if (passwordCredentialRealm == null || passwordCredentialRealm.managedConnectionFactory == null) {
62             return false;
63         }
64         Callback JavaDoc[] callbacks = new Callback JavaDoc[3];
65
66         callbacks[0] = new NameCallback JavaDoc("Resource Principal");
67         callbacks[1] = new NameCallback JavaDoc("User name");
68         callbacks[2] = new PasswordCallback JavaDoc("Password", false);
69         try {
70             callbackHandler.handle(callbacks);
71         } catch (IOException JavaDoc ioe) {
72             throw (LoginException JavaDoc) new LoginException JavaDoc().initCause(ioe);
73         } catch (UnsupportedCallbackException JavaDoc uce) {
74             throw (LoginException JavaDoc) new LoginException JavaDoc().initCause(uce);
75         }
76         resourcePrincipalName = ((NameCallback JavaDoc) callbacks[0]).getName();
77         userName = ((NameCallback JavaDoc) callbacks[1]).getName();
78         password = ((PasswordCallback JavaDoc) callbacks[2]).getPassword();
79         return resourcePrincipalName != null && userName != null && password != null;
80     }
81
82     public boolean commit() throws LoginException JavaDoc {
83         subject.getPrincipals().add(new ResourcePrincipal(resourcePrincipalName));
84         PasswordCredential JavaDoc passwordCredential = new PasswordCredential JavaDoc(userName, password);
85         passwordCredential.setManagedConnectionFactory(passwordCredentialRealm.getManagedConnectionFactory());
86         subject.getPrivateCredentials().add(passwordCredential);
87         return true;
88     }
89
90     public boolean abort() throws LoginException JavaDoc {
91         subject = null;
92         userName = null;
93         password = null;
94         return true;
95     }
96
97     public boolean logout() throws LoginException JavaDoc {
98         subject = null;
99         userName = null;
100         password = null;
101         return true;
102     }
103 }
104
Popular Tags