wos2 WSF/PHP soap header 认证实现

用SwA模式上传附件, 下载的包里有相关实例。但是看产生的soap消息包不是SwA模式的, 依然是MTOM模式的.

 

$uri = "http://wsi.portalservice.dpf.huawei.com";
$key = "asdf";
$pwd = "asdf";
$time = date("mdhms");
$pwd = base64_encode(hash("SHA256",$key.$pwd.$time));

$url = "http://127.0.0.1:1234/Service";
$reqPayload = <<<XML
<ns1:AddRequest xmlns:ns1="http://localhost/namespace">
<albumId>1106011156080031</albumId>
<fileName>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:content"></xop:Include>
</fileName>
</ns1:AddRequest>
XML;

$auth_data = array(
	new WSHeader(array('name'=>'operatorId','data'=>$key)),
	new WSHeader(array('name'=>'password','data'=>$pwd)),
	new WSHeader(array('name'=>'timeStamp','data'=>$time)),
);

$header_options = array(
	"ns"=>$uri,
	'prefix'=>"ns1",
	"name"=>"RequestSOAPHeader",
	"data"=>$auth_data,
);
$header = new WSHeader($header_options);

$filePath = "/path/to/file/file.jpg";
$f = file_get_contents($filePath);

$msg = new WSMessage($reqPayload,
	array(
		'inputHeaders'=>array($header),
		'attachments'=>array('content'=>$f),
		'defaultAttachmentContentType'=>"image/jpeg",
	)
);
$options = array(
	'to'=>$url,
	'useMTOM'=>"swa",
);
$api = new WSClient($options);

try {
	$resMsg = $api->request($msg);
} catch (Exception $e) {
	if ($e instanceof WSFault)
		printf("Soap Fault: %s\n", $e->Reason);
	else
		printf("Message = %s\n", $e->getMessage());
}

 

PHP: Soap 通过 soap header 认证(non-wsdl 模式)

 

soap service 是 java的 axis2

soap client 是 php. = =

                 $url = "http://localhost:8000/PortalService";
                // 对应wsdl 文件定义中的targetNamespace
		$uri = "http://localhost/partalService";

		$key = "xxx";
		$pwd = "xxx";

		$options=array(
			//'soap_version'=>SOAP_1_2,
			'trace'=>1,
			//"exceptions"=>1,
			'location'=>$url,
			'uri'=>$uri,
		);

		$auth_header = array(
			'user'=>$key,
			'password'=>$pwd,
		);
                // 下面的RequestSOAPHeader 对应 wsdl 定义里面的 <xsd:element name="RequestSOAPHeader">.....
		$authvalues = new SoapVar($auth_header, SOAP_ENC_OBJECT,"RequestSOAPHeader",$uri);  
		$header = new SoapHeader($uri, 'RequestSOAPHeader', $authvalues);

		$api = new SoapClient(null,$options);
		$api->__setSoapHeaders(array($header));
		
                // 在调用 java axis2 soap 的使用要这么定义, 不然会变成 <param0><item>xxx</item><value>xxx</value></param0>
                // 所以会报错. 用一下定义生成的就是这样的格式: <user>asdf</user>
		$p = array(
			new SoapParam("asdf",'user'),
			new SoapParam(1,'password'),
		);
		try {
			$call="Login";
			$result = $api->__soapCall($call,$p);
			print_r($result);
		} catch(SoapFault $e) {
			if(1) {
				var_dump($api->__getLastRequestHeaders());
				var_dump($api->__getLastRequest());
				var_dump($api->__getLastResponseHeaders());
				var_dump($api->__getLastResponse());
				echo $e->getMessage();
			}
		}

 

triple_des(des3) 算法 - php,python 实现

调用电信那边的接口. 要用到 triple_des 加密算法. 整了python 和 php 两个版本的实现.

iv, key 变量指向的都是 hex 字符串.

php:

 

#!/usr/bin/php
<?php

function PaddingPKCS7($data) {
	$block_size = mcrypt_get_block_size("tripledes", "cbc");
	$padding_char = $block_size - (strlen($data) % $block_size);
	$data .= str_repeat(chr($padding_char),$padding_char);
	return $data;
}

function fmt3DESEx($data, $key, $iv) {
	$td = mcrypt_module_open(MCRYPT_3DES,"", MCRYPT_MODE_CBC, "");
	$key = pack("H48",$key);
	$iv = pack("H16",$iv);
	mcrypt_generic_init($td, $key, $iv);
	$data = PaddingPKCS7($data);
	$desResult = mcrypt_generic($td, $data);
	mcrypt_generic_deinit($td);
	mcrypt_module_close($td);
	return base64_encode($desResult);     
}



$data = "asdf";
$key = "313233343536373839303132333435363738393031323334";
$iv = "3132333435363738";
$code = fmt3DESEx(mhash(MHASH_SHA1,$data), $key, $iv);
echo $code;
?>

python:

 

#!/usr/bin/env python
# -*- coding:utf-8 -*-

import binascii
import hashlib
import base64
import pyDes

iv = '3132333435363738'
key = '313233343536373839303132333435363738393031323334'
data = "asdf"

iv = binascii.unhexlify(iv)
key = binascii.unhexlify(key)
data = hashlib.sha1(data)

k = pyDes.triple_des(key, pyDes.CBC, iv, pad=None, padmode=pyDes.PAD_PKCS5)
d = k.encrypt(data.digest())
print base64.encodestring(d)