| 1 28 package net.sf.jguard.jee.authentication.http; 29 30 import java.security.Principal ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 import java.util.Set ; 35 36 import javax.security.auth.Subject ; 37 import javax.servlet.http.HttpServletRequest ; 38 import javax.servlet.http.HttpServletRequestWrapper ; 39 40 import net.sf.jguard.core.CoreConstants; 41 import net.sf.jguard.core.authentication.credentials.JGuardCredential; 42 import net.sf.jguard.core.principals.RolePrincipal; 43 import net.sf.jguard.core.principals.UserPrincipal; 44 45 import org.apache.commons.logging.Log; 46 import org.apache.commons.logging.LogFactory; 47 53 public class JGuardServletRequestWrapper extends HttpServletRequestWrapper { 54 private static final String LOGIN = "login"; 55 private static Log logger = LogFactory.getLog(JGuardServletRequestWrapper.class); 56 private Map headers = null; 57 private HttpServletRequest request; 58 59 public JGuardServletRequestWrapper(HttpServletRequest req) { 60 super(req); 61 this.request = req; 62 headers = new HashMap (); 63 } 64 65 72 public boolean isUserInRole(String role){ 73 String applicationName = (String ) request.getSession(true).getServletContext().getAttribute(CoreConstants.APPLICATION_NAME); 74 role = RolePrincipal.getName(role, applicationName); 75 Subject subject = ((HttpAuthenticationUtils)request.getSession().getAttribute(HttpConstants.AUTHN_UTILS)).getSubject(); 76 Set principals = subject.getPrincipals(RolePrincipal.class); 77 Iterator itPrincipals = principals.iterator(); 78 while(itPrincipals.hasNext()){ 79 Principal principal = (Principal )itPrincipals.next(); 80 if(role.equals(principal.getName())){ 81 return true; 82 } 83 } 84 return false; 85 86 } 87 88 93 public Principal getUserPrincipal(){ 94 Subject subject = ((HttpAuthenticationUtils)request.getSession().getAttribute(HttpConstants.AUTHN_UTILS)).getSubject(); 95 return new UserPrincipal(subject); 96 } 97 98 102 public String getRemoteUser(){ 103 Subject subject = ((HttpAuthenticationUtils)request.getSession().getAttribute(HttpConstants.AUTHN_UTILS)).getSubject(); 104 Set publicCredentials = subject.getPublicCredentials(); 105 Iterator it = publicCredentials.iterator(); 106 while(it.hasNext()){ 107 JGuardCredential cred = (JGuardCredential)it.next(); 108 if(cred.getId().equalsIgnoreCase(JGuardServletRequestWrapper.LOGIN)){ 109 return (String )cred.getValue(); 110 } 111 } 112 try{ 113 Set privateCredentials = subject.getPrivateCredentials(); 114 Iterator it2 = privateCredentials.iterator(); 115 while(it2.hasNext()){ 116 JGuardCredential cred2 = (JGuardCredential)it2.next(); 117 if(cred2.getId().equalsIgnoreCase(JGuardServletRequestWrapper.LOGIN)){ 118 return (String )cred2.getValue(); 119 } 120 } 121 }catch(SecurityException sex){ 122 logger.debug(" you don't have the permission to look into the private Credentials of the user ",sex); 123 } 124 return null; 125 } 126 127 public void setHeader(String headerName,String headerValue){ 128 headers.put(headerName, headerValue); 129 } 130 131 public String getHeader(String headerName){ 132 133 if(headers.containsKey(headerName)){ 134 return (String )headers.get(headerName); 135 }else{ 136 return super.getHeader(headerName); 137 } 138 } 139 } 140 | Popular Tags |