001: package ru.emdev.EmForge.security;
002:
003: import java.security.Principal;
004: import java.util.Vector;
005:
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008: import org.apache.cxf.interceptor.Fault;
009: import org.apache.cxf.message.Message;
010: import org.apache.cxf.phase.AbstractPhaseInterceptor;
011: import org.apache.cxf.phase.Phase;
012: import org.apache.ws.security.WSConstants;
013: import org.apache.ws.security.WSSecurityEngineResult;
014: import org.apache.ws.security.WSUsernameTokenPrincipal;
015: import org.apache.ws.security.handler.WSHandlerConstants;
016: import org.apache.ws.security.handler.WSHandlerResult;
017: import org.springframework.beans.BeansException;
018: import org.springframework.context.ApplicationContext;
019: import org.springframework.context.ApplicationContextAware;
020:
021: public class ValidateUserTokenInterceptor extends
022: AbstractPhaseInterceptor implements ApplicationContextAware {
023: private final Log log = LogFactory.getLog(getClass());
024:
025: private ApplicationContext applicationContext;
026:
027: public ValidateUserTokenInterceptor() {
028: super (Phase.UNMARSHAL);
029: }
030:
031: public void setApplicationContext(
032: ApplicationContext i_applicationContext)
033: throws BeansException {
034: this .applicationContext = i_applicationContext;
035: }
036:
037: public void handleMessage(Message i_message) throws Fault {
038: //connector = (SecurityConnector)ctx.getBean("securityConnector");
039:
040: boolean userTokenValidated = true;
041: WSUsernameTokenPrincipal principal = null;
042:
043: Vector<Object> result = (Vector<Object>) i_message
044: .getContextualProperty(WSHandlerConstants.RECV_RESULTS);
045:
046: for (int i = 0; i < result.size(); i++) {
047: WSHandlerResult res = (WSHandlerResult) result.get(i);
048: String actor = res.getActor();
049:
050: for (int j = 0; j < res.getResults().size(); j++) {
051:
052: WSSecurityEngineResult secRes = (WSSecurityEngineResult) res
053: .getResults().get(j);
054:
055: //
056: int action = secRes.getAction();
057: switch (action) {
058: case WSConstants.UT:
059: principal = (WSUsernameTokenPrincipal) secRes
060: .getPrincipal();
061: log.info(principal.getName() + ":"
062: + principal.getPassword());
063:
064: break;
065:
066: case WSConstants.SIGN:
067: /* Do not knoe why it is requried, but I've got here class cast exception sun.security.x509.X500Name
068: principal = (WSUsernameTokenPrincipal)secRes.getPrincipal();
069: log.info(principal.getName());
070:
071: java.security.cert.X509Certificate cert = secRes.getCertificate();
072:
073: Principal subjectPrincipal = cert.getSubjectDN();
074: log.info(subjectPrincipal.getName());
075:
076: Principal issuerPrincipal = cert.getIssuerDN();
077: log.info(issuerPrincipal.getName());
078: */
079: break;
080:
081: case WSConstants.ENCR:
082: log.info("encrypted");
083:
084: break;
085:
086: case WSConstants.TS:
087: log.info("timestamp created: "
088: + secRes.getTimestamp().getCreated());
089: log.info("timestamp expires: "
090: + secRes.getTimestamp().getExpires());
091:
092: break;
093:
094: case WSConstants.NO_SECURITY:
095: log.info("No security");
096: }
097: }
098: }
099:
100: if (!userTokenValidated) {
101: throw new RuntimeException("Security processing failed");
102: }
103: }
104: }
|