Skip to content

Commit ab8f44c

Browse files
authored
feat(laravel-soap-125): Adding auth middleware for DHL (#127)
* feat(laravel-soap-125): Adding auth middleware for DHL * Apply fixes from StyleCI (#128) Co-authored-by: Gregor Becker <[email protected]> * refactor(laravel-soap-125): Typo Auth * refactor(laravel-soap-125): fix test Co-authored-by: Gregor Becker <[email protected]>
1 parent 6b3dcce commit ab8f44c

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

src/Facades/Soap.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* @method static \CodeDredd\Soap\SoapClient withWsa()
2020
* @method static \CodeDredd\Soap\SoapClient withRemoveEmptyNodes()
2121
* @method static \CodeDredd\Soap\SoapClient withBasicAuth(string $username, string $password)
22+
* @method static \CodeDredd\Soap\SoapClient withCisDHLAuth($user, ?string $signature = null)
2223
* @method \CodeDredd\Soap\Client\Response call(string $method, array $arguments = [])
2324
* @method static \GuzzleHttp\Promise\PromiseInterface response($body = null, $status = 200, array $headers = [])
2425
* @method static \CodeDredd\Soap\Client\ResponseSequence sequence(array $responses = [])

src/Middleware/CisDhlMiddleware.php

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
namespace CodeDredd\Soap\Middleware;
4+
5+
use Http\Promise\Promise;
6+
use Phpro\SoapClient\Middleware\Middleware;
7+
use Phpro\SoapClient\Xml\SoapXml;
8+
use Psr\Http\Message\RequestInterface;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class CisDhlMiddleware extends Middleware
12+
{
13+
/**
14+
* @var string
15+
*/
16+
const CIS_NS = 'http://dhl.de/webservice/cisbase';
17+
18+
/**
19+
* @var string
20+
*/
21+
private $user;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $signature;
27+
28+
public function __construct(string $user, string $signature)
29+
{
30+
$this->user = $user;
31+
$this->signature = $signature;
32+
}
33+
34+
public function getName(): string
35+
{
36+
return 'cis_dhl_middleware';
37+
}
38+
39+
/**
40+
* @param callable $handler
41+
* @param RequestInterface $request
42+
*
43+
* @return Promise
44+
*/
45+
public function beforeRequest(callable $handler, RequestInterface $request): Promise
46+
{
47+
$xml = SoapXml::fromStream($request->getBody());
48+
$xml->registerNamespace('cis', 'http://dhl.de/webservice/cisbase');
49+
$envelope = $xml->xpath('/soap:Envelope')->item(0);
50+
51+
$domDoc = $xml->getXmlDocument();
52+
$domDoc->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cis', self::CIS_NS);
53+
54+
$header = $domDoc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV:Header');
55+
$cisAuth = $domDoc->createElementNS(self::CIS_NS, 'cis:Authentification');
56+
57+
$envelope->insertBefore($header, $envelope->firstChild);
58+
59+
if (! empty($this->user) && ! empty($this->signature)) {
60+
$cisUser = $domDoc->createElementNS(self::CIS_NS, 'cis:user', $this->user);
61+
$cisSig = $domDoc->createElementNS(self::CIS_NS, 'cis:signature', $this->signature);
62+
$cisAuth->appendChild($cisUser);
63+
$cisAuth->appendChild($cisSig);
64+
}
65+
$header->appendChild($cisAuth);
66+
67+
return $handler($request->withBody($xml->toStream()));
68+
}
69+
70+
/**
71+
* @param ResponseInterface $response
72+
*
73+
* @return ResponseInterface
74+
*/
75+
public function afterResponse(ResponseInterface $response): ResponseInterface
76+
{
77+
return $response;
78+
}
79+
}

src/SoapClient.php

+19
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeDredd\Soap\Exceptions\NotFoundConfigurationException;
99
use CodeDredd\Soap\Exceptions\SoapException;
1010
use CodeDredd\Soap\Handler\HttPlugHandle;
11+
use CodeDredd\Soap\Middleware\CisDhlMiddleware;
1112
use CodeDredd\Soap\Middleware\WsseMiddleware;
1213
use GuzzleHttp\Client;
1314
use GuzzleHttp\HandlerStack;
@@ -212,6 +213,24 @@ public function withBasicAuth($username, ?string $password = null)
212213
return $this;
213214
}
214215

216+
/**
217+
* @param string|array $user
218+
* @param string|null $signature
219+
* @return $this
220+
*/
221+
public function withCisDHLAuth($user, ?string $signature = null)
222+
{
223+
if (is_array($user)) {
224+
['username' => $user, 'password' => $signature] = $user;
225+
}
226+
227+
$this->middlewares = array_merge_recursive($this->middlewares, [
228+
'dhl' => new CisDhlMiddleware($user, $signature),
229+
]);
230+
231+
return $this;
232+
}
233+
215234
/**
216235
* @return $this
217236
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace CodeDredd\Soap\Tests\Unit\Middleware;
4+
5+
use CodeDredd\Soap\Facades\Soap;
6+
use CodeDredd\Soap\Tests\TestCase;
7+
8+
class CisDhlMiddlewareTest extends TestCase
9+
{
10+
public function testCisDHLMiddleware()
11+
{
12+
Soap::fake();
13+
$client = Soap::withCisDHLAuth('test', 'dhl')->baseWsdl('https://laravel-soap.wsdl');
14+
$response = $client->call('Get_User');
15+
$lastRequest = $client->getEngine()->collectLastRequestInfo()->getLastRequest();
16+
17+
self::assertTrue($response->ok());
18+
self::assertStringContainsString('<cis:Authentification>', $lastRequest);
19+
}
20+
}

0 commit comments

Comments
 (0)