KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > security > bridge > AbstractPrincipalMappingUserPasswordRealmBridge


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.security.bridge;
19
20 import java.security.Principal JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.Set 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
31 import org.apache.geronimo.gbean.GBeanInfo;
32 import org.apache.geronimo.gbean.GBeanInfoBuilder;
33
34
35 /**
36  * @version $Rev: 109314 $ $Date: 2004-12-01 00:01:37 -0800 (Wed, 01 Dec 2004) $
37  */

38 public abstract class AbstractPrincipalMappingUserPasswordRealmBridge extends AbstractRealmBridge {
39     protected final Map JavaDoc principalMap = new HashMap JavaDoc();
40     private Class JavaDoc principalSourceType;
41     private String JavaDoc principalTargetCallbackName;
42     protected final Map JavaDoc userNameMap = new HashMap JavaDoc();
43     private Class JavaDoc userNameSourceType;
44     private String JavaDoc userNameTargetCallbackName;
45     protected final Map JavaDoc passwordMap = new HashMap JavaDoc();
46     private Class JavaDoc passwordSourceType;
47
48
49     public AbstractPrincipalMappingUserPasswordRealmBridge() {
50     }
51
52     public AbstractPrincipalMappingUserPasswordRealmBridge(String JavaDoc targetRealm,
53             Class JavaDoc principalSourceType,
54             String JavaDoc principalTargetCallbackName,
55             Class JavaDoc userNameSourceType,
56             String JavaDoc userNameTargetCallbackName,
57             Class JavaDoc passwordSourceType) {
58
59         super(targetRealm);
60         this.principalSourceType = principalSourceType;
61         this.principalTargetCallbackName = principalTargetCallbackName;
62         this.userNameSourceType = userNameSourceType;
63         this.userNameTargetCallbackName = userNameTargetCallbackName;
64         this.passwordSourceType = passwordSourceType;
65     }
66
67     public Class JavaDoc getPrincipalSourceType() {
68         return principalSourceType;
69     }
70
71     public void setPrincipalSourceType(Class JavaDoc principalSourceType) {
72         this.principalSourceType = principalSourceType;
73     }
74
75     public String JavaDoc getPrincipalTargetCallbackName() {
76         return principalTargetCallbackName;
77     }
78
79     public void setPrincipalTargetCallbackName(String JavaDoc principalTargetCallbackName) {
80         this.principalTargetCallbackName = principalTargetCallbackName;
81     }
82
83     public Class JavaDoc getUserNameSourceType() {
84         return userNameSourceType;
85     }
86
87     public void setUserNameSourceType(Class JavaDoc userNameSourceType) {
88         this.userNameSourceType = userNameSourceType;
89     }
90
91     public String JavaDoc getUserNameTargetCallbackName() {
92         return userNameTargetCallbackName;
93     }
94
95     public void setUserNameTargetCallbackName(String JavaDoc userNameTargetCallbackName) {
96         this.userNameTargetCallbackName = userNameTargetCallbackName;
97     }
98
99     public Class JavaDoc getPasswordSourceType() {
100         return passwordSourceType;
101     }
102
103     public void setPasswordSourceType(Class JavaDoc passwordSourceType) {
104         this.passwordSourceType = passwordSourceType;
105     }
106
107     protected CallbackHandler JavaDoc getCallbackHandler(final Subject JavaDoc sourceSubject) {
108         return new CallbackHandler JavaDoc() {
109             public void handle(Callback JavaDoc[] callbacks) throws UnsupportedCallbackException JavaDoc {
110                 Principal JavaDoc principalSourcePrincipal = findPrincipalOfType(sourceSubject, principalSourceType);
111                 Principal JavaDoc userNameSourcePrincipal;
112                 if (userNameSourceType == principalSourceType) {
113                     userNameSourcePrincipal = principalSourcePrincipal;
114                 } else {
115                     userNameSourcePrincipal = findPrincipalOfType(sourceSubject, userNameSourceType);
116                 }
117                 Principal JavaDoc passwordSourcePrincipal;
118                 if (passwordSourceType == principalSourceType) {
119                     passwordSourcePrincipal = principalSourcePrincipal;
120                 } else {
121                     passwordSourcePrincipal = findPrincipalOfType(sourceSubject, passwordSourceType);
122                 }
123                 for (int i = 0; i < callbacks.length; i++) {
124                     Callback JavaDoc callback = callbacks[i];
125                     if (callback instanceof NameCallback JavaDoc) {
126                         NameCallback JavaDoc nameCallback = (NameCallback JavaDoc) callback;
127                         if (nameCallback.getPrompt().equals(principalTargetCallbackName)) {
128                             nameCallback.setName((String JavaDoc) principalMap.get(principalSourcePrincipal.getName()));
129                         } else if (nameCallback.getPrompt().equals(userNameTargetCallbackName)) {
130                             nameCallback.setName((String JavaDoc) userNameMap.get(userNameSourcePrincipal.getName()));
131                         } else {
132                             throw new UnsupportedCallbackException JavaDoc(callback, "Only name callbacks with prompts " + principalTargetCallbackName + " or " + userNameTargetCallbackName + " are supported");
133                         }
134                     } else if (callback instanceof PasswordCallback JavaDoc) {
135                         ((PasswordCallback JavaDoc) callback).setPassword((char[]) passwordMap.get(passwordSourcePrincipal.getName()));
136                     } else {
137                         throw new UnsupportedCallbackException JavaDoc(callback, "Only name and password callbacks supported");
138                     }
139
140                 }
141             }
142
143             private Principal JavaDoc findPrincipalOfType(final Subject JavaDoc sourceSubject, Class JavaDoc principalClass) throws UnsupportedCallbackException JavaDoc {
144                 Set JavaDoc principalPrincipals = sourceSubject.getPrincipals(principalClass);
145                 if (principalPrincipals == null || principalPrincipals.size() != 1) {
146                     throw new UnsupportedCallbackException JavaDoc(null, "No principals of type " + principalClass + " to read");
147                 }
148                 Principal JavaDoc principal = (Principal JavaDoc) principalPrincipals.iterator().next();
149                 return principal;
150             }
151
152         };
153     }
154
155     public static final GBeanInfo GBEAN_INFO;
156
157     static {
158         GBeanInfoBuilder infoFactory = new GBeanInfoBuilder(CallerIdentityUserPasswordRealmBridge.class, AbstractRealmBridge.GBEAN_INFO);
159
160         infoFactory.addAttribute("principalSourceType", Class JavaDoc.class, true);
161         infoFactory.addAttribute("principalTargetCallbackName", String JavaDoc.class, true);
162         infoFactory.addAttribute("userNameSourceType", Class JavaDoc.class, true);
163         infoFactory.addAttribute("userNameTargetCallbackName", String JavaDoc.class, true);
164         infoFactory.addAttribute("passwordSourceType", Class JavaDoc.class, true);
165
166         GBEAN_INFO = infoFactory.getBeanInfo();
167     }
168
169     public static GBeanInfo getGBeanInfo() {
170         return GBEAN_INFO;
171     }
172
173 }
174
Popular Tags