1 19 20 package com.sslexplorer.core.stringreplacement; 21 22 import java.util.Locale ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import org.apache.struts.Globals; 27 28 import com.sslexplorer.agent.DefaultAgentManager; 29 import com.sslexplorer.boot.PropertyClass; 30 import com.sslexplorer.boot.PropertyClassManager; 31 import com.sslexplorer.core.CoreUtil; 32 import com.sslexplorer.properties.Property; 33 import com.sslexplorer.properties.attributes.AttributeDefinition; 34 import com.sslexplorer.properties.impl.profile.ProfilePropertyKey; 35 import com.sslexplorer.properties.impl.userattributes.UserAttributeKey; 36 import com.sslexplorer.properties.impl.userattributes.UserAttributes; 37 import com.sslexplorer.security.AuthenticationScheme; 38 import com.sslexplorer.security.Constants; 39 import com.sslexplorer.security.LogonControllerFactory; 40 import com.sslexplorer.security.SessionInfo; 41 42 public class SessionInfoReplacer extends AbstractReplacementVariableReplacer { 43 44 private SessionInfo sessionInfo; 45 46 public SessionInfoReplacer(SessionInfo sessionInfo) { 47 super(); 48 this.sessionInfo = sessionInfo; 49 } 50 51 public static String replace(SessionInfo session, String input) { 52 VariableReplacement r = new VariableReplacement(); 53 r.setSession(session); 54 return r.replace(input); 55 } 56 57 @Override 58 public String processReplacementVariable(Pattern pattern, Matcher matcher, String replacementPattern, String type, String key) throws Exception { 59 if (type.equalsIgnoreCase("property")) { 60 if (sessionInfo == null) { 61 return null; 62 } 63 return Property.getProperty(new ProfilePropertyKey(CoreUtil.getCurrentPropertyProfileId(sessionInfo.getHttpSession()), 64 sessionInfo.getUser().getPrincipalName(), 65 key, sessionInfo.getUser().getRealm().getResourceId())); 66 } else if (type.equalsIgnoreCase("session")) { 67 if (sessionInfo == null) { 68 return null; 69 } 70 if (key.equals("username")) { 71 return sessionInfo.getUser().getPrincipalName(); 72 } else if (key.equals("email")) { 73 return sessionInfo.getUser().getEmail(); 74 } else if (key.equals("fullname")) { 75 return sessionInfo.getUser().getFullname(); 76 } else if (key.equals("locale")) { 77 Locale l = (Locale ) sessionInfo.getHttpSession().getAttribute(Globals.LOCALE_KEY); 78 ; 79 return l == null ? Locale.getDefault().toString() : l.toString(); 80 } else if (key.equals("clientProxyURL")) { 81 String proxyURL = CoreUtil.getProxyURL(sessionInfo.getUser(), 82 CoreUtil.getCurrentPropertyProfileId(sessionInfo.getHttpSession())); 83 return proxyURL == null ? "" : proxyURL; 84 } else if (key.equals("password")) { 85 90 AuthenticationScheme scheme = (AuthenticationScheme) sessionInfo.getHttpSession() 91 .getAttribute(Constants.AUTH_SESSION); 92 if (scheme != null) { 93 char[] pw = LogonControllerFactory.getInstance().getPasswordFromCredentials(scheme); 94 return pw == null ? "" : new String (pw); 95 } else { 96 return ""; 97 } 98 } else if (key.equals("userAgent")) { 99 return sessionInfo.getUserAgent(); 100 } else { 101 throw new Exception ("Unknown key " + key + " for type " + type + "."); 102 } 103 } else if (type.equalsIgnoreCase("attr") || type.equals(UserAttributes.NAME)) { 104 if (sessionInfo == null) { 105 return null; 106 } 107 PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(UserAttributes.NAME); 108 AttributeDefinition def = (AttributeDefinition) propertyClass.getDefinition(key); 109 if (def == null) { 110 VariableReplacement.log.warn("Invalid user attribute '" + key + "'"); 111 return null; 112 } else { 113 return Property.getProperty(new UserAttributeKey(sessionInfo.getUser(), key)); 114 } 115 } else if (type.equalsIgnoreCase("ticket")) { 116 if (sessionInfo == null) { 117 return null; 118 } 119 if (key.equals("id")) { 120 return (String ) sessionInfo.getHttpSession().getAttribute(Constants.VPN_AUTHORIZATION_TICKET); 121 122 } if(key.equals("new")) { 123 String agentAuthenticationTicket = DefaultAgentManager.getInstance().registerPendingAgent(sessionInfo); 124 return agentAuthenticationTicket; 125 } else { 126 throw new Exception ("String replacement pattern for ticket only supports the 'id' key. I.e. ${ticket:id}"); 127 } 128 } 129 return null; 130 } 131 132 } | Popular Tags |