Skip to content

Commit 0cddab7

Browse files
author
Taras Viyatyk
authored
Merge pull request #4 from dreamfactorysoftware/wsse-update
Update WsseAuthHeader class
2 parents 191fa12 + 3cbe925 commit 0cddab7

4 files changed

Lines changed: 88 additions & 29 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateSoapWsseUsernameTokenField extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
if (!Schema::hasColumn('soap_config', 'wsse_username_token')) {
17+
Schema::table(
18+
'soap_config',
19+
function (Blueprint $t){
20+
$t->string('wsse_username_token')->default(0)->nullable();
21+
}
22+
);
23+
}
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
if (Schema::hasColumn('soap_config', 'wsse_username_token')) {
34+
Schema::table(
35+
'soap_config',
36+
function (Blueprint $t){
37+
$t->dropColumn('wsse_username_token');
38+
}
39+
);
40+
}
41+
}
42+
}

src/Components/WsseAuthHeader.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,35 @@
77

88
class WsseAuthHeader extends SoapHeader
99
{
10+
// Namespaces
11+
private $ns_wsse = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
12+
private $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
13+
private $password_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest';
14+
private $encoding_type = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary';
1015

11-
private $wss_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd';
12-
private $wsu_ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
13-
14-
function __construct($user, $pass)
16+
function __construct($username, $password, $wsse_username_token = null)
1517
{
16-
$created = gmdate('Y-m-d\TH:i:s\Z');
17-
$nonce = mt_rand();
18-
$passdigest = base64_encode(pack('H*', sha1(pack('H*', $nonce) . pack('a*', $created) . pack('a*', $pass))));
19-
20-
$auth = new \stdClass();
21-
$auth->Username = new SoapVar($user, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
22-
$auth->Password = new SoapVar($pass, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
23-
$auth->Nonce = new SoapVar($passdigest, XSD_STRING, null, $this->wss_ns, null, $this->wss_ns);
24-
$auth->Created = new SoapVar($created, XSD_STRING, null, $this->wss_ns, null, $this->wsu_ns);
25-
26-
$username_token = new \stdClass();
27-
$username_token->UsernameToken =
28-
new SoapVar($auth, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns);
29-
30-
$security_sv = new SoapVar(
31-
new SoapVar($username_token, SOAP_ENC_OBJECT, null, $this->wss_ns, 'UsernameToken', $this->wss_ns),
32-
SOAP_ENC_OBJECT, null, $this->wss_ns, 'Security', $this->wss_ns);
33-
parent::__construct($this->wss_ns, 'Security', $security_sv, true);
18+
$simple_nonce = mt_rand();
19+
$created_at = gmdate('Y-m-d\TH:i:s\Z');
20+
$encoded_nonce = base64_encode($simple_nonce);
21+
$password_digest = base64_encode(sha1($simple_nonce . $created_at . $password, true));
22+
23+
// Creating WSS identification header using SimpleXML
24+
$root = new \SimpleXMLElement('<root/>');
25+
$security = $root->addChild('wsse:Security', null, $this->ns_wsse);
26+
$usernameToken = $security->addChild('wsse:UsernameToken', null, $this->ns_wsse);
27+
if($wsse_username_token){
28+
$usernameToken->addAttribute('wsu:Id', $wsse_username_token, $this->ns_wsu);
29+
}
30+
$usernameToken->addChild('Username', $username, $this->ns_wsse);
31+
$usernameToken->addChild('Password', $password_digest, $this->ns_wsse)->addAttribute('Type', $this->password_type);
32+
$usernameToken->addChild('Nonce', $encoded_nonce, $this->ns_wsse)->addAttribute('EncodingType', $this->encoding_type);
33+
$usernameToken->addChild('Created', $created_at, $this->ns_wsu);
34+
35+
// Recovering XML value from that object
36+
$root->registerXPathNamespace('wsse', $this->ns_wsse);
37+
$full = $root->xpath('/root/wsse:Security');
38+
$auth = $full[0]->asXML();
39+
parent::__construct($this->ns_wsse, 'Security', new SoapVar($auth, XSD_ANYXML), true);
3440
}
3541
}

src/Models/SoapConfig.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SoapConfig extends BaseServiceConfigModel
1111

1212
protected $table = 'soap_config';
1313

14-
protected $fillable = ['service_id', 'wsdl', 'options', 'headers'];
14+
protected $fillable = ['service_id', 'wsdl', 'options', 'headers', 'wsse_username_token'];
1515

1616
protected $casts = ['options' => 'array', 'headers' => 'array'];
1717

@@ -87,7 +87,14 @@ protected static function prepareConfigSchemaField(array &$schema)
8787
];
8888
$schema['description'] =
8989
'An array of headers for the connection. ' .
90-
'For further info, see http://php.net/manual/en/class.soapheader.php.';
90+
'For further info, see http://php.net/manual/en/class.soapheader.php. ' .
91+
'TIP: Select type "WSSE" and write "password", "username" into ' .
92+
'Name field if you want pass username and password into WSSE header.';
93+
break;
94+
case 'wsse_username_token':
95+
$schema['label'] = 'WSSE Username Token';
96+
$schema['default'] = null;
97+
$schema['description'] = 'WSSE Username Token';
9198
break;
9299
}
93100
}

src/Services/Soap.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,20 +123,24 @@ public function __construct($settings)
123123
// }
124124

125125
$headers = array_get($config, 'headers');
126+
$wsseUsernameToken = array_get($config, 'wsse_username_token');
126127
$soapHeaders = null;
127128

128129
if (!empty($headers)) {
129130
foreach ($headers as $header) {
130131
$headerType = array_get($header, 'type', 'generic');
131132
switch ($headerType) {
132133
case 'wsse':
133-
$data = json_decode(stripslashes(array_get($header, 'data', '{}')), true);
134-
$data = (is_null($data) || !is_array($data)) ? [] : $data;
135-
$username = array_get($data, 'username');
136-
$password = array_get($data, 'password');
134+
$data = (is_null($header) || !is_array($header)) ? [] : $header;
135+
136+
if (array_get($data, 'name') == 'username'){
137+
$username = array_get($data, 'data');
138+
} elseif (array_get($data, 'name') == 'password'){
139+
$password = array_get($data, 'data');
140+
}
137141

138142
if (!empty($username) && !empty($password)) {
139-
$soapHeaders[] = new WsseAuthHeader($username, $password);
143+
$soapHeaders[] = new WsseAuthHeader($username, $password, $wsseUsernameToken);
140144
}
141145

142146
break;

0 commit comments

Comments
 (0)