Esempio n. 1
0
/**
@fn _wsse__Security* soap_wsse_add_Security(struct soap *soap)
@brief Adds Security header element.
@param soap context
@return _wsse__Security object
*/
struct _wsse__Security*
soap_wsse_add_Security(struct soap *soap)
{ DBGFUN("soap_wsse_add_Security");
  /* if we don't have a SOAP Header, create one */
  soap_header(soap);
  /* if we don't have a wsse:Security element in the SOAP Header, create one */
  if (!soap->header->wsse__Security)
  { soap->header->wsse__Security = (_wsse__Security*)soap_malloc(soap, sizeof(_wsse__Security));
    if (!soap->header->wsse__Security)
      return NULL;
    soap_default__wsse__Security(soap, soap->header->wsse__Security);
  }
  return soap->header->wsse__Security;
}
int CSetSoapSecurityDigest2Impl::soap_wsse_add_UsernameTokenDigest(struct soap *psoap,   
    const char *username, const char *password)  
{   
	struct SOAP_ENV__Header* header = psoap->header;
	if (NULL == header) {
		return RET_CODE_ERROR_NULL_OBJECT;
	}
	header->wsse__Security = (struct _wsse__Security *)CSoapUtils::getInstance().my_soap_malloc(psoap, sizeof(struct _wsse__Security));	
	soap_default__wsse__Security(psoap, header->wsse__Security);  
    _wsse__Security *security = header->wsse__Security;
//    char *created = soap_strdup(psoap, "2015-01-04T15:17:30Z");  
	char *created = CSoapUtils::getInstance().createSoapDateTimeStr(psoap);  
    char HA[SOAP_SMD_SHA1_SIZE] = {0}, HABase64[29] = {0};  
    char nonce[SOAP_WSSE_NONCELEN] = {0};
	char *nonceBase64;  
    /* generate a nonce */  
//     calc_nonce(psoap, nonce);  
//     nonceBase64 = soap_s2base64(psoap, (unsigned char*)nonce, NULL, SOAP_WSSE_NONCELEN);  
	nonceBase64 = soap_strdup(psoap, "3c8g874lw1LQeC3UdoUZzg==");
	std::auto_ptr<IDecodeBase64> apDecodeBase64(CFactoryImpl::getInstance().createDecodeBase64());
	int result = apDecodeBase64->decode(nonceBase64, psoap, nonce, SOAP_WSSE_NONCELEN);	
	if (!CAppTools::getInstance().isRetSuccess(result))
		return result;
	std::auto_ptr<ICalculateDigest> ap(CFactoryImpl::getInstance().createCalculateDigest());
    result =  ap->calc(nonce, created, password, HA);
	if (!CAppTools::getInstance().isRetSuccess(result))
		return result;
	std::auto_ptr<IEncodeBase64> apEncodeBase64(CFactoryImpl::getInstance().createEncodeBase64());
	result = apEncodeBase64->encode(HA, strlen(HA), psoap, HABase64);
	if (!CAppTools::getInstance().isRetSuccess(result))
		return result;
    // soap_s2base64(psoap, (unsigned char*)HA, HABase64, SOAP_SMD_SHA1_SIZE);  
    /* populate the UsernameToken with digest */  
    soap_wsse_add_UsernameTokenText(psoap,username, HABase64);  
    /* populate the remainder of the password, nonce, and created */  
    security->UsernameToken->Password->Type = soap_strdup(psoap, SOAP_PASS_DIGEST_URI);  
    security->UsernameToken->Nonce = nonceBase64;  
    security->UsernameToken->wsu__Created = created;  
  
    return RET_CODE_SUCCESS;  
}