FileDocCategorySizeDatePackage
WebServiceSecurity.javaAPI DocGlassfish v2 API10256Fri May 04 22:35:38 BST 2007com.sun.enterprise.security.wss

WebServiceSecurity

public class WebServiceSecurity extends Object
Load Container auth spi. This is for JAXRPC only.
author
Harpreet Singh

Fields Summary
private static Logger
_logger
private static com.sun.enterprise.security.audit.AuditManager
auditManager
private static final String
SHARED_CLIENT_STATE
private static final String
SHARED_SERVER_STATE
Constructors Summary
private WebServiceSecurity()


       
    
Methods Summary
public static voidauditInvocation(javax.xml.rpc.handler.soap.SOAPMessageContext context, com.sun.enterprise.deployment.WebServiceEndpoint endpoint, boolean status)


	if (auditManager.isAuditOn()) {

	    // TODO: replace the string literal with the correct constant
	    // MessageContextProperties.HTTP_SERVLET_REQUEST);

	    HttpServletRequest req = (HttpServletRequest)context.getProperty
		("com.sun.xml.rpc.server.http.HttpServletRequest");
       
	    String uri = null;

	    if( req != null ) {
		uri = req.getRequestURI();
	    }
	    
	    String epName = null;

	    if( endpoint != null ) {
		epName = endpoint.getEndpointName();
	    }

	    auditManager.webServiceInvocation
		( ((uri==null) ? "(no uri)" : uri), 
		  ((epName==null) ? "(no endpoint)" : epName), 
		  status);
	}
    
public static voidsecureRequest(javax.xml.rpc.handler.soap.SOAPMessageContext context, ClientAuthContext cAC, boolean isAppClient)

	// put sharedState in MessageContext for use by validateResponse
	HashMap sharedState = new HashMap();
        sharedState.put(javax.xml.ws.handler.MessageContext.WSDL_SERVICE,
            context.getProperty(javax.xml.ws.handler.MessageContext.WSDL_SERVICE));
	context.setProperty(SHARED_CLIENT_STATE, sharedState);

	secureRequest
	    (WsUtil.getMessage(context), sharedState, cAC, isAppClient);
    
private static voidsecureRequest(javax.xml.soap.SOAPMessage request, java.util.HashMap sharedState, ClientAuthContext cAC, boolean isAppClient)


        if(_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE,
            "Container Auth: ClientAuthContext.secureRequest");
        }

	SOAPAuthParam param = new SOAPAuthParam(request, null);

	Subject subject = null;
	if (isAppClient) {
	    ClientSecurityContext sc = ClientSecurityContext.getCurrent();
	    if (sc != null) {
		subject = sc.getSubject();
	    }
	} else {
	    SecurityContext sc = SecurityContext.getCurrent();
	    if (sc != null && !sc.didServerGenerateCredentials()) {
		// make sure we don't use default unauthenticated subject, 
		// so that module cannot change this important (constant) 
		// subject.
		subject = sc.getSubject();
	    }
	}
	if (subject == null) subject = new Subject();
	
	cAC.secureRequest ( param, subject, sharedState);
    
public static voidsecureResponse(javax.xml.rpc.handler.soap.SOAPMessageContext context, ServerAuthContext sAC)

	secureResponse(WsUtil.getMessage(context),
		       (HashMap) context.getProperty(SHARED_SERVER_STATE),
		       sAC);
    
private static voidsecureResponse(javax.xml.soap.SOAPMessage response, java.util.HashMap sharedState, ServerAuthContext sAC)

        if(_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE,
            "Container Auth: ServerAuthContext.secureResponse");
        }

	// subject may change if runAs identity differs from caller's.
	// Therefore, session state is saved in sharedState not subject
	SecurityContext sc = SecurityContext.getCurrent();
	Subject subject = sc.getSubject();

	SOAPAuthParam param = new SOAPAuthParam(null, response);

        try{
            sAC.secureResponse((AuthParam)param, subject, sharedState);
        } finally {
	    sAC.disposeSubject(subject,sharedState);
	}

        return;
    
public static booleanvalidateRequest(javax.xml.rpc.handler.soap.SOAPMessageContext context, ServerAuthContext sAC)

	boolean rvalue = true;
	SOAPAuthParam param = 
	    new SOAPAuthParam(WsUtil.getMessage(context), null);

	// put sharedState in MessageContext for use by secureResponse
	HashMap sharedState = new HashMap();
	context.setProperty(SHARED_SERVER_STATE, sharedState);

	try {
	    rvalue = validateRequest(param, sharedState, sAC);
	} catch(PendingException pe){
            _logger.log(Level.FINE,
			"Container-auth: wss: Error validating request  ",pe);
	    context.setMessage(param.getResponse());
	    rvalue = false;
	} catch(FailureException fe){
            _logger.log(Level.FINE,
			"Container-auth: wss: Error validating request  ",fe);
	    context.setMessage(param.getResponse());
	    throw fe;
        }
	return rvalue;
    
private static booleanvalidateRequest(AuthParam param, java.util.HashMap sharedState, ServerAuthContext sAC)

	boolean rvalue = true;

        if(_logger.isLoggable(Level.FINE)) {
            _logger.log(Level.FINE,
            "Container Auth: ServerAuthContext.validateRequest");
        }
	
	Subject subject = null;
	boolean firstAuthentication = true;
	SecurityContext sc = SecurityContext.getCurrent();
	if (sc == null || sc.didServerGenerateCredentials()) {
	    subject = new Subject();
	} else {
	    subject = sc.getSubject();
	    firstAuthentication = false;
	}

	sAC.validateRequest((AuthParam)param, subject, sharedState);

	if (rvalue && firstAuthentication) {
	    Set principalSet = subject.getPrincipals();
	    // must be at least one new principal to establish
	    // non-default security contex
	    if (principalSet != null && !principalSet.isEmpty()) {
		// define and add initiator to Subject - note that this may add
		// a second principal (of type PrincipalImpl) for initiator.
		String initiator = ((Principal)principalSet.iterator().next()).
		    getName();
		SecurityContext newSC = new SecurityContext(initiator,subject);
		SecurityContext.setCurrent(newSC);
	    }
	}

        return rvalue;
    
public static booleanvalidateResponse(javax.xml.rpc.handler.soap.SOAPMessageContext context, ClientAuthContext cAC)

	return validateResponse
	    (WsUtil.getMessage(context),
	     (HashMap) context.getProperty(SHARED_CLIENT_STATE), cAC);
    
private static booleanvalidateResponse(javax.xml.soap.SOAPMessage response, java.util.HashMap sharedState, ClientAuthContext cAC)

        boolean rvalue = true;

	// get a subject to be filled in with the principals of the responder
	Subject responderSubject = new Subject();

	SOAPAuthParam param = new SOAPAuthParam(null, response);

        try{
            cAC.validateResponse( param, responderSubject, sharedState);
        } catch(AuthException ae){
            _logger.log(Level.SEVERE,
			"Container-auth: wss: Error validating response ", ae);
	    rvalue = false;
            throw ae;
        } finally {
	    cAC.disposeSubject(responderSubject,sharedState);
	}
        
        return rvalue;