Web Services Security (WS-Security, WSS) is an extension to SOAP to apply security to Web services.
Readmore here -> https://msdn.microsoft.com/en-us/library/ms977327.aspx#understw_topic4
Web Services Security (WS-Security, WSS) is an extension to SOAP to apply security to Web services.
Readmore here -> https://msdn.microsoft.com/en-us/library/ms977327.aspx#understw_topic4
<?php | |
if (! function_exists('soap_request')) { | |
function soap_request($array = []) | |
{ | |
return new \App\Library\SoapRequest(); | |
} | |
} |
<?php | |
/** | |
* Created by PhpStorm. | |
* User: hafiq | |
* Date: 28/02/2018 | |
* Time: 9:30 AM | |
*/ | |
namespace App\Library; | |
use App\Http\Soap\WSSESoapClient; | |
class SoapRequest | |
{ | |
protected $opts = null; | |
protected $soap_option = null; | |
protected $context = null; | |
protected $fullUrl = null; | |
protected $body = null; | |
protected $function = null; | |
protected $method = null; | |
protected $acAsWsToken = false; | |
protected $env = false; // false production | |
private $username; | |
private $password; | |
public function __construct() | |
{ | |
$this->initAgent(); | |
} | |
private function initAgent() | |
{ | |
$this->opts = [ | |
'https' => [ | |
'user_agent' => 'PHPSoapClient' | |
], | |
]; | |
$this->fullUrl = config('cris.url'); | |
} | |
public function setEnv($env = 'local') | |
{ | |
$this->env = $env != 'production'; | |
return $this; | |
} | |
public function setCredential($user, $pass) | |
{ | |
$this->username = $user; | |
$this->password = $pass; | |
return $this; | |
} | |
public function endpoint($url) | |
{ | |
$this->fullUrl = $url ?? config('cris.url'); | |
return $this; | |
} | |
public function setUrl($method, $method2 = null) | |
{ | |
$this->function = ($method2) ? $method2 : $method; | |
$this->method = $method; | |
return $this; | |
} | |
public function setBody(array $body) | |
{ | |
$this->body = $body; | |
return $this; | |
} | |
public function actAsWsUsernameToken() | |
{ | |
$this->acAsWsToken = true; | |
return $this; | |
} | |
public function execute() | |
{ | |
$this->fullUrl = $this->fullUrl .'/'. $this->method . "?wsdl"; | |
if ($this->env) { | |
$this->opts['ssl'] = [ | |
'verify_peer_name' => false, | |
'allow_self_signed' => true, | |
]; | |
} else { | |
$this->opts['ssl'] = [ | |
'verify_peer_name' => false, | |
'allow_self_signed' => true, | |
]; | |
} | |
$this->context = stream_context_create($this->opts); | |
$this->soap_option = [ | |
'stream_context' => $this->context | |
]; | |
if ($this->env) { | |
$this->soap_option['trace'] = true; | |
$this->soap_option['exceptions'] = true; | |
} | |
$this->soap_option['cache_wsdl'] = WSDL_CACHE_NONE; | |
if (!$this->acAsWsToken) { | |
$this->soap_option['login'] = $this->username; | |
$this->soap_option['password'] = $this->password; | |
} | |
try { | |
if (!$this->env) { | |
$this->soap_option['location'] = $this->fullUrl; | |
} | |
$soap = new WSSESoapClient($this->fullUrl, $this->soap_option, $this->acAsWsToken, $this->username, $this->password); | |
$result = $soap->{$this->function}($this->body); | |
return (object) [ | |
'code' => 200, | |
'data' => isset($result->mainData) ? $result->mainData : ($this->acAsWsToken ? $result : null), | |
'header' => $this->acAsWsToken ? $result : $result->head, | |
]; | |
} catch (\Exception $exception) { | |
return (object)[ | |
'code' => 500, | |
'message' => $exception->getMessage() | |
]; | |
} | |
} | |
} |
<?php | |
$body = [ | |
'head' => [ | |
'key1' => [ | |
'name' => 'ACCT_ID', | |
'value' => '12312323829382', | |
] | |
] | |
]; | |
$result = soap_request() | |
->endpoint($url) | |
->setEnv(env('APP_ENV')) | |
->setCredential($username, $password) | |
->setUrl($body_name) // WXFormsList | |
->setBody($body) | |
//->actAsWsUsernameToken() // if soap as usernametoken authentication type. As default is Basic Autorization | |
->execute(); | |
if ($result->code == 200) { | |
return response()->raw( | |
$result->code, | |
(new ApiResource($result->data, true))->toRawArray() | |
); | |
} else { | |
return response()->error(); | |
} |
<?php | |
/** | |
* Created by PhpStorm. | |
* User: hafiq | |
* Date: 27/02/2018 | |
* Time: 3:19 PM | |
*/ | |
namespace App\Http\Soap; | |
use Carbon\Carbon; | |
use SoapHeader; | |
use SoapVar; | |
use stdClass; | |
class WSSEHeader extends SoapHeader | |
{ | |
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; | |
private $wsu_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'; | |
function __construct($ns = null) | |
{ | |
if ($ns) { | |
$this->wss_ns = $ns; | |
} | |
//generate timestamp | |
$created_time = str_replace('+00:00', 'Z', Carbon::now('UTC') | |
->subMinute(1)->toIso8601String()); | |
$expires_time = str_replace('+00:00', 'Z', Carbon::now('UTC') | |
->toIso8601String()); | |
//build variable | |
$timestamp_obj = new stdClass(); | |
$timestamp_obj->Created = new SoapVar( | |
$created_time, | |
XSD_DATE, | |
null, | |
$this->wsu_ns, | |
null, | |
$this->wsu_ns | |
); | |
$timestamp_obj->Expires = new SoapVar( | |
$expires_time, | |
XSD_DATE, | |
null, | |
$this->wsu_ns, | |
null, | |
$this->wsu_ns | |
); | |
//build security node | |
$security_obj = new stdClass(); | |
$security_obj->Timestamp = new SoapVar( | |
$timestamp_obj, | |
SOAP_ENC_OBJECT, | |
null, | |
$this->wsu_ns, | |
'Timestamp', | |
$this->wsu_ns | |
); | |
$security_sv = new SoapVar( | |
new SoapVar( | |
$security_obj, | |
SOAP_ENC_OBJECT, | |
null, | |
$this->wss_ns, | |
'Security', | |
$this->wss_ns | |
), | |
SOAP_ENC_OBJECT, | |
null, | |
$this->wss_ns, | |
'Security', | |
$this->wss_ns | |
); | |
parent::__construct($this->wss_ns, 'Security', $security_sv, true); | |
} | |
} |
<?php | |
/** | |
* Created by PhpStorm. | |
* User: hafiq | |
* Date: 27/02/2018 | |
* Time: 3:22 PM | |
*/ | |
namespace App\Http\Soap; | |
use SoapClient; | |
class WSSESoapClient extends SoapClient | |
{ | |
public function __construct($wsdl, $options = array(), $stupidChanges = false, $username = null, $password = null) | |
{ | |
parent::__construct($wsdl, $options); | |
if ($stupidChanges) { | |
$header = new V2WSSEHeader(null, $username, $password); | |
} else { | |
$header = new WSSEHeader(); | |
} | |
$this->__setSoapHeaders(array( $header )); | |
} | |
} |
<?php | |
/** | |
* Created by PhpStorm. | |
* User: hafiq | |
* Date: 27/02/2018 | |
* Time: 3:19 PM | |
*/ | |
namespace App\Http\Soap; | |
use SoapHeader; | |
use SoapVar; | |
use stdClass; | |
class WSSETokenHeader extends SoapHeader | |
{ | |
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; | |
private $wsu_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'; | |
function __construct($ns = null, $user = null, $pass = null) | |
{ | |
$created = gmdate('Y-m-d\TH:i:s\Z'); | |
$nonce = mt_rand(); | |
$passdigest = base64_encode( pack('H*', sha1( pack('H*', $nonce) . pack('a*',$created). pack('a*',$pass)))); | |
$auth = new stdClass(); | |
$auth->Username = new SoapVar( | |
$user, | |
XSD_STRING, | |
NULL, | |
$this->wss_ns, | |
NULL, | |
$this->wss_ns | |
); | |
$auth->Password = new SoapVar( | |
$pass, | |
XSD_STRING, | |
NULL, | |
$this->wss_ns, | |
NULL, | |
$this->wss_ns | |
); | |
$auth->Nonce = new SoapVar( | |
$passdigest, | |
XSD_STRING, | |
NULL, | |
$this->wss_ns, | |
NULL, | |
$this->wss_ns | |
); | |
$auth->Created = new SoapVar( | |
$created, | |
XSD_STRING, | |
NULL, | |
$this->wss_ns, | |
NULL, | |
$this->wsu_ns | |
); | |
$username_token = new stdClass(); | |
$username_token->UsernameToken = new SoapVar( | |
$auth, | |
SOAP_ENC_OBJECT, | |
NULL, | |
$this->wss_ns, | |
'UsernameToken', | |
$this->wss_ns | |
); | |
$security_sv = new SoapVar( | |
$username_token, | |
SOAP_ENC_OBJECT, | |
NULL, | |
$this->wss_ns, | |
'Security', | |
$this->wss_ns | |
); | |
parent::__construct($this->wss_ns, 'Security', $security_sv, true); | |
} | |
} |