1 25 package org.objectweb.jonas.security; 26 27 import java.util.ArrayList ; 28 29 import org.objectweb.jonas.common.JProp; 30 import org.objectweb.jonas.security.realm.factory.JResource; 31 import org.objectweb.jonas.security.realm.factory.JResourceException; 32 import org.objectweb.jonas.security.realm.principals.User; 33 import org.objectweb.jonas.service.ServiceManager; 34 import org.objectweb.security.context.SecurityContext; 35 import org.objectweb.security.context.SecurityCurrent; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 import org.objectweb.util.monolog.api.Logger; 38 39 45 public abstract class AbsSecurityContextHelper { 46 47 50 private static JResource jResource = null; 51 52 55 abstract protected Logger getLogger(); 56 57 60 abstract protected String getRealmKey(); 61 62 65 abstract protected String getRealmDefault(); 66 67 72 public void login(String principalName, String credential) { 73 74 if (principalName == null) { 76 getLogger().log(BasicLevel.ERROR, "No username so no authentication"); 77 return; 78 } 79 80 User user = null; 82 try { 83 user = getJResource().findUser(principalName); 84 } catch (Exception jre) { 85 getLogger().log(BasicLevel.ERROR, "Can not find the user : " + jre.getMessage()); 87 return; 88 } 89 90 if (user == null) { 92 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 93 getLogger().log(BasicLevel.DEBUG, "User " + principalName + " not found."); 94 } 95 return; 96 } 97 98 boolean validated = getJResource().isValidUser(user, credential); 99 if (!validated) { 100 getLogger().log(BasicLevel.ERROR, "The password for the user " + principalName + " is not valid"); 101 return; 102 } 103 104 ArrayList combinedRoles = null; 105 try { 106 combinedRoles = getJResource().getArrayListCombinedRoles(user); 107 } catch (JResourceException jre) { 108 getLogger().log(BasicLevel.ERROR, jre.getMessage()); 109 return; 110 } 111 112 SecurityContext ctx = new SecurityContext(principalName, combinedRoles); 113 SecurityCurrent current = SecurityCurrent.getCurrent(); 114 current.setSecurityContext(ctx); 115 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 116 getLogger().log(BasicLevel.DEBUG, "Login of principalName '" + principalName + "' succeeded."); 117 } 118 119 } 120 121 124 private JResource getJResource() { 125 126 if (jResource != null) { 127 return jResource; 128 } 129 130 SecurityService securityService = null; 131 try { 133 securityService = (SecurityService) ServiceManager.getInstance().getSecurityService(); 134 } catch (Exception e) { 135 throw new IllegalStateException ("can't retrieve Security service"); 137 } 138 139 String resName = null; 140 try { 141 resName = JProp.getInstance().getValue(getRealmKey()); 142 } catch (Exception e) { 143 getLogger().log(BasicLevel.ERROR, "Cannot read properties in jonas.properties file."); 144 } 145 if (resName == null) { 146 if (getLogger().isLoggable(BasicLevel.DEBUG)) { 147 getLogger().log(BasicLevel.DEBUG, "Cannot read property '" + getRealmKey() + "' in jonas.properties file. Use default value = '" + getRealmDefault() + "'."); 148 } 149 resName = getRealmDefault(); 150 } 151 152 jResource = securityService.getJResource(resName); 154 if (jResource == null) { 155 throw new IllegalStateException ("Can't retrieve resource '" + resName + "' from the security service"); 156 } 157 return jResource; 158 } 159 160 161 } 162 | Popular Tags |