KickJava   Java API By Example, From Geeks To Geeks.

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


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
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.resource.spi.ManagedConnectionFactory JavaDoc;
25 import javax.security.auth.Subject JavaDoc;
26 import javax.security.auth.callback.Callback JavaDoc;
27 import javax.security.auth.callback.CallbackHandler JavaDoc;
28 import javax.security.auth.callback.NameCallback JavaDoc;
29 import javax.security.auth.callback.PasswordCallback JavaDoc;
30 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
31 import javax.security.auth.login.LoginException JavaDoc;
32 import javax.security.auth.spi.LoginModule JavaDoc;
33
34 /**
35  *
36  *
37  * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
38  *
39  * */

40 public class CallerIdentityPasswordCredentialLoginModule implements LoginModule JavaDoc {
41
42     private Subject JavaDoc subject;
43     private CallbackHandler JavaDoc callbackHandler;
44
45     private ManagedConnectionFactory JavaDoc managedConnectionFactory;
46
47     private String JavaDoc resourcePrincipalName;
48     private String JavaDoc userName;
49     private char[] password;
50
51     public void initialize(Subject JavaDoc subject, CallbackHandler JavaDoc callbackHandler,
52             Map JavaDoc sharedState, Map JavaDoc options) {
53         this.subject = subject;
54         this.callbackHandler = callbackHandler;
55         managedConnectionFactory = (ManagedConnectionFactory JavaDoc) options.get(PasswordCredentialLoginModuleWrapper.MANAGED_CONNECTION_FACTORY_OPTION);
56         if (managedConnectionFactory == null) {
57             throw new IllegalArgumentException JavaDoc("No ManagedConnectionFactory supplied in options");
58         }
59     }
60
61     public boolean login() throws LoginException JavaDoc {
62         if (managedConnectionFactory == null) {
63             return false;
64         }
65         Callback JavaDoc[] callbacks = new Callback JavaDoc[2];
66
67         callbacks[0] = new NameCallback JavaDoc("User name");
68         callbacks[1] = 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[0]).getName();
78         password = ((PasswordCallback JavaDoc) callbacks[1]).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(managedConnectionFactory);
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