1 17 18 package org.apache.geronimo.security.bridge; 19 20 import java.security.Principal ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 import java.util.Set ; 24 import javax.security.auth.Subject ; 25 import javax.security.auth.callback.Callback ; 26 import javax.security.auth.callback.CallbackHandler ; 27 import javax.security.auth.callback.NameCallback ; 28 import javax.security.auth.callback.PasswordCallback ; 29 import javax.security.auth.callback.UnsupportedCallbackException ; 30 31 import org.apache.geronimo.gbean.GBeanInfo; 32 import org.apache.geronimo.gbean.GBeanInfoBuilder; 33 34 35 38 public abstract class AbstractPrincipalMappingUserPasswordRealmBridge extends AbstractRealmBridge { 39 protected final Map principalMap = new HashMap (); 40 private Class principalSourceType; 41 private String principalTargetCallbackName; 42 protected final Map userNameMap = new HashMap (); 43 private Class userNameSourceType; 44 private String userNameTargetCallbackName; 45 protected final Map passwordMap = new HashMap (); 46 private Class passwordSourceType; 47 48 49 public AbstractPrincipalMappingUserPasswordRealmBridge() { 50 } 51 52 public AbstractPrincipalMappingUserPasswordRealmBridge(String targetRealm, 53 Class principalSourceType, 54 String principalTargetCallbackName, 55 Class userNameSourceType, 56 String userNameTargetCallbackName, 57 Class 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 getPrincipalSourceType() { 68 return principalSourceType; 69 } 70 71 public void setPrincipalSourceType(Class principalSourceType) { 72 this.principalSourceType = principalSourceType; 73 } 74 75 public String getPrincipalTargetCallbackName() { 76 return principalTargetCallbackName; 77 } 78 79 public void setPrincipalTargetCallbackName(String principalTargetCallbackName) { 80 this.principalTargetCallbackName = principalTargetCallbackName; 81 } 82 83 public Class getUserNameSourceType() { 84 return userNameSourceType; 85 } 86 87 public void setUserNameSourceType(Class userNameSourceType) { 88 this.userNameSourceType = userNameSourceType; 89 } 90 91 public String getUserNameTargetCallbackName() { 92 return userNameTargetCallbackName; 93 } 94 95 public void setUserNameTargetCallbackName(String userNameTargetCallbackName) { 96 this.userNameTargetCallbackName = userNameTargetCallbackName; 97 } 98 99 public Class getPasswordSourceType() { 100 return passwordSourceType; 101 } 102 103 public void setPasswordSourceType(Class passwordSourceType) { 104 this.passwordSourceType = passwordSourceType; 105 } 106 107 protected CallbackHandler getCallbackHandler(final Subject sourceSubject) { 108 return new CallbackHandler () { 109 public void handle(Callback [] callbacks) throws UnsupportedCallbackException { 110 Principal principalSourcePrincipal = findPrincipalOfType(sourceSubject, principalSourceType); 111 Principal userNameSourcePrincipal; 112 if (userNameSourceType == principalSourceType) { 113 userNameSourcePrincipal = principalSourcePrincipal; 114 } else { 115 userNameSourcePrincipal = findPrincipalOfType(sourceSubject, userNameSourceType); 116 } 117 Principal 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 callback = callbacks[i]; 125 if (callback instanceof NameCallback ) { 126 NameCallback nameCallback = (NameCallback ) callback; 127 if (nameCallback.getPrompt().equals(principalTargetCallbackName)) { 128 nameCallback.setName((String ) principalMap.get(principalSourcePrincipal.getName())); 129 } else if (nameCallback.getPrompt().equals(userNameTargetCallbackName)) { 130 nameCallback.setName((String ) userNameMap.get(userNameSourcePrincipal.getName())); 131 } else { 132 throw new UnsupportedCallbackException (callback, "Only name callbacks with prompts " + principalTargetCallbackName + " or " + userNameTargetCallbackName + " are supported"); 133 } 134 } else if (callback instanceof PasswordCallback ) { 135 ((PasswordCallback ) callback).setPassword((char[]) passwordMap.get(passwordSourcePrincipal.getName())); 136 } else { 137 throw new UnsupportedCallbackException (callback, "Only name and password callbacks supported"); 138 } 139 140 } 141 } 142 143 private Principal findPrincipalOfType(final Subject sourceSubject, Class principalClass) throws UnsupportedCallbackException { 144 Set principalPrincipals = sourceSubject.getPrincipals(principalClass); 145 if (principalPrincipals == null || principalPrincipals.size() != 1) { 146 throw new UnsupportedCallbackException (null, "No principals of type " + principalClass + " to read"); 147 } 148 Principal principal = (Principal ) 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 .class, true); 161 infoFactory.addAttribute("principalTargetCallbackName", String .class, true); 162 infoFactory.addAttribute("userNameSourceType", Class .class, true); 163 infoFactory.addAttribute("userNameTargetCallbackName", String .class, true); 164 infoFactory.addAttribute("passwordSourceType", Class .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 |