1 23 24 package wrappers.catalina; 25 26 import java.security.Principal ; 27 import java.io.File ; 28 import java.io.Serializable ; 29 import java.util.Enumeration ; 30 import java.util.Hashtable ; 31 import java.util.Vector ; 32 import org.apache.catalina.Container; 33 import org.apache.catalina.LifecycleException; 34 import org.apache.catalina.Logger; 35 import org.apache.catalina.Realm; 36 import org.apache.catalina.realm.RealmBase; 37 import org.apache.catalina.util.LifecycleSupport; 38 import org.apache.catalina.util.StringManager; 39 40 import org.apache.slide.common.NamespaceAccessToken; 41 import org.apache.slide.common.Domain; 42 import org.apache.slide.common.SlideToken; 43 import org.apache.slide.common.SlideTokenImpl; 44 import org.apache.slide.common.SlideException; 45 import org.apache.slide.content.Content; 46 import org.apache.slide.content.NodeRevisionDescriptors; 47 import org.apache.slide.content.NodeRevisionDescriptor; 48 import org.apache.slide.content.NodeProperty; 49 import org.apache.slide.structure.ObjectNode; 50 import org.apache.slide.authenticate.CredentialsToken; 51 import org.apache.slide.authenticate.SecurityToken; 52 import org.apache.slide.security.Security; 53 54 55 65 66 public final class SlideRealm 67 extends RealmBase { 68 69 70 72 73 76 private static final String info = 77 "wrappers.catalina.SlideRealm/1.2"; 78 79 80 83 private NamespaceAccessToken accessToken; 84 85 86 89 private Content contentHelper; 90 91 92 95 private Security securityHelper; 96 97 98 101 private String usersPath; 102 103 104 107 private CredentialsToken rootCredentials; 108 109 110 113 private String namespace; 114 115 116 118 119 122 public void setNamespace(String namespace) { 123 this.namespace = namespace; 124 } 125 126 127 130 public void setAccessToken(NamespaceAccessToken accessToken) { 131 this.accessToken = accessToken; 132 } 133 134 135 138 public String getName() { 139 return "Slide realm"; 140 } 141 142 143 145 146 154 public boolean hasRole(Principal principal, String role) { 155 156 CredentialsToken credToken = new CredentialsToken(principal); 157 SlideToken slideToken = new SlideTokenImpl(credToken); 158 try { 159 return securityHelper.hasRole(slideToken, role); 160 } catch (SlideException e) { 161 return (false); 162 } 163 164 } 165 166 167 170 public void start() throws LifecycleException { 171 172 super.start(); 173 174 if (namespace == null) 175 namespace = container.getName(); 176 177 if (accessToken == null) 178 accessToken = Domain.accessNamespace 179 (new SecurityToken(container), namespace); 180 181 if (accessToken == null) 182 throw new IllegalStateException 183 ("Invalid Slide Realm configuration : " 184 + "Couldn't access namespace " + namespace); 185 186 contentHelper = accessToken.getContentHelper(); 187 securityHelper = accessToken.getSecurityHelper(); 188 189 usersPath = accessToken.getNamespaceConfig().getUsersPath(); 190 191 } 192 193 194 196 197 200 protected String getPassword(String username) { 201 202 Principal userPrincipal = getPrincipal(username); 203 CredentialsToken credToken = new CredentialsToken(userPrincipal); 204 SlideToken slideToken = new SlideTokenImpl(credToken); 205 206 try { 208 209 ObjectNode user = securityHelper.getPrincipal(slideToken); 210 211 } catch (SlideException e) { 212 return null; 213 } 214 215 String passwordValue = null; 216 217 try { 218 219 NodeRevisionDescriptors revisionDescriptors = 220 contentHelper.retrieve(slideToken, usersPath + "/" + username); 221 NodeRevisionDescriptor revisionDescriptor = 222 contentHelper.retrieve(slideToken, revisionDescriptors); 223 NodeProperty password = 224 revisionDescriptor.getProperty 225 ("password", NodeProperty.SLIDE_NAMESPACE); 226 if (password != null) { 227 passwordValue = (String ) password.getValue(); 228 } 229 230 } catch (SlideException e) { 231 } 234 235 if (passwordValue == null) { 236 log("User " + username 237 + " doesn't have his password property set : " 238 + "can't authenticate"); 239 } 240 241 return passwordValue; 242 243 } 244 245 246 249 protected Principal getPrincipal(String username) { 250 return new SlideRealmPrincipal(username); 251 } 252 253 254 } 255 256 257 260 261 final class SlideRealmPrincipal implements Principal , Serializable { 262 263 266 private String username = null; 267 268 269 274 public SlideRealmPrincipal(String username) { 275 276 this.username = username; 277 278 } 279 280 281 284 public String getName() { 285 286 return (username); 287 288 } 289 290 291 } 292 293 294 | Popular Tags |