1 22 package org.jboss.test.security.interceptors; 23 24 import java.security.Principal ; 25 import java.util.Arrays ; 26 import javax.naming.InitialContext ; 27 import javax.management.MBeanInfo ; 28 29 import org.jboss.mx.interceptor.AbstractInterceptor; 30 import org.jboss.mx.server.MBeanInvoker; 31 import org.jboss.mx.server.Invocation; 32 import org.jboss.logging.Logger; 33 import org.jboss.security.srp.SRPSessionKey; 34 import org.jboss.security.srp.SRPServerSession; 35 import org.jboss.security.srp.jaas.SRPPrincipal; 36 import org.jboss.util.CachePolicy; 37 38 43 public class SRPCacheInterceptor 44 extends AbstractInterceptor 45 { 46 private static Logger log = Logger.getLogger(SRPCacheInterceptor.class); 47 private String cacheJndiName; 48 49 public SRPCacheInterceptor() 50 { 51 super("SRPCacheInterceptor"); 52 } 53 54 public void setAuthenticationCacheJndiName(String cacheJndiName) 55 { 56 this.cacheJndiName = cacheJndiName; 57 } 58 59 public Object invoke(Invocation invocation) throws Throwable 61 { 62 String opName = invocation.getName(); 63 log.info("invoke, opName=" + opName); 64 if( opName == null || opName.equals("testSession") == false ) 65 { 66 Object value = invocation.nextInterceptor().invoke(invocation); 67 return value; 68 } 69 70 Object [] args = invocation.getArgs(); 71 Principal userPrincipal = (Principal ) args[0]; 72 String username = userPrincipal.getName(); 73 byte[] clientChallenge = (byte[]) args[1]; 74 75 try 76 { 77 InitialContext iniCtx = new InitialContext (); 78 CachePolicy cache = (CachePolicy) iniCtx.lookup(cacheJndiName); 79 SRPSessionKey key; 80 if (userPrincipal instanceof SRPPrincipal) 81 { 82 SRPPrincipal srpPrincpal = (SRPPrincipal) userPrincipal; 83 key = new SRPSessionKey(username, srpPrincpal.getSessionID()); 84 } 85 else 86 { 87 key = new SRPSessionKey(username); 88 } 89 Object cacheCredential = cache.get(key); 90 if (cacheCredential == null) 91 { 92 throw new SecurityException ("No SRP session found for: " + key); 93 } 94 log.debug("Found SRP cache credential: " + cacheCredential); 95 98 if (cacheCredential instanceof SRPServerSession) 99 { 100 SRPServerSession session = (SRPServerSession) cacheCredential; 101 byte[] challenge = session.getClientResponse(); 102 boolean isValid = Arrays.equals(challenge, clientChallenge); 103 if ( isValid == false ) 104 throw new SecurityException ("Failed to validate SRP session key for: " + key); 105 } 106 else 107 { 108 throw new SecurityException ("Unknown type of cache credential: " + cacheCredential.getClass()); 109 } 110 log.debug("Validated SRP cache credential for: "+key); 111 } 112 catch (Exception e) 113 { 114 log.error("Invocation failed", e); 115 throw e; 116 } 117 118 Object value = invocation.nextInterceptor().invoke(invocation); 119 return value; 120 } 121 } 122 | Popular Tags |