Skip to content

feat(laravel-soap-125): Adding auth middleware for DHL #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Facades/Soap.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @method static \CodeDredd\Soap\SoapClient withWsa()
* @method static \CodeDredd\Soap\SoapClient withRemoveEmptyNodes()
* @method static \CodeDredd\Soap\SoapClient withBasicAuth(string $username, string $password)
* @method static \CodeDredd\Soap\SoapClient withCisDHLAuth($user, ?string $signature = null)
* @method \CodeDredd\Soap\Client\Response call(string $method, array $arguments = [])
* @method static \GuzzleHttp\Promise\PromiseInterface response($body = null, $status = 200, array $headers = [])
* @method static \CodeDredd\Soap\Client\ResponseSequence sequence(array $responses = [])
Expand Down
79 changes: 79 additions & 0 deletions src/Middleware/CisDhlMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace CodeDredd\Soap\Middleware;

use Http\Promise\Promise;
use Phpro\SoapClient\Middleware\Middleware;
use Phpro\SoapClient\Xml\SoapXml;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class CisDhlMiddleware extends Middleware
{
/**
* @var string
*/
const CIS_NS = 'http://dhl.de/webservice/cisbase';

/**
* @var string
*/
private $user;

/**
* @var string
*/
private $signature;

public function __construct(string $user, string $signature)
{
$this->user = $user;
$this->signature = $signature;
}

public function getName(): string
{
return 'cis_dhl_middleware';
}

/**
* @param callable $handler
* @param RequestInterface $request
*
* @return Promise
*/
public function beforeRequest(callable $handler, RequestInterface $request): Promise
{
$xml = SoapXml::fromStream($request->getBody());
$xml->registerNamespace('cis', 'http://dhl.de/webservice/cisbase');
$envelope = $xml->xpath('/soap:Envelope')->item(0);

$domDoc = $xml->getXmlDocument();
$domDoc->documentElement->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:cis', self::CIS_NS);

$header = $domDoc->createElementNS('http://schemas.xmlsoap.org/soap/envelope/', 'SOAP-ENV:Header');
$cisAuth = $domDoc->createElementNS(self::CIS_NS, 'cis:Authentification');

$envelope->insertBefore($header, $envelope->firstChild);

if (! empty($this->user) && ! empty($this->signature)) {
$cisUser = $domDoc->createElementNS(self::CIS_NS, 'cis:user', $this->user);
$cisSig = $domDoc->createElementNS(self::CIS_NS, 'cis:signature', $this->signature);
$cisAuth->appendChild($cisUser);
$cisAuth->appendChild($cisSig);
}
$header->appendChild($cisAuth);

return $handler($request->withBody($xml->toStream()));
}

/**
* @param ResponseInterface $response
*
* @return ResponseInterface
*/
public function afterResponse(ResponseInterface $response): ResponseInterface
{
return $response;
}
}
19 changes: 19 additions & 0 deletions src/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use CodeDredd\Soap\Exceptions\NotFoundConfigurationException;
use CodeDredd\Soap\Exceptions\SoapException;
use CodeDredd\Soap\Handler\HttPlugHandle;
use CodeDredd\Soap\Middleware\CisDhlMiddleware;
use CodeDredd\Soap\Middleware\WsseMiddleware;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
Expand Down Expand Up @@ -212,6 +213,24 @@ public function withBasicAuth($username, ?string $password = null)
return $this;
}

/**
* @param string|array $user
* @param string|null $signature
* @return $this
*/
public function withCisDHLAuth($user, ?string $signature = null)
{
if (is_array($user)) {
['username' => $user, 'password' => $signature] = $user;
}

$this->middlewares = array_merge_recursive($this->middlewares, [
'dhl' => new CisDhlMiddleware($user, $signature),
]);

return $this;
}

/**
* @return $this
*/
Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/Middleware/CisDhlMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace CodeDredd\Soap\Tests\Unit\Middleware;

use CodeDredd\Soap\Facades\Soap;
use CodeDredd\Soap\Tests\TestCase;

class CisDhlMiddlewareTest extends TestCase
{
public function testCisDHLMiddleware()
{
Soap::fake();
$client = Soap::withCisDHLAuth('test', 'dhl')->baseWsdl('https://laravel-soap.wsdl');
$response = $client->call('Get_User');
$lastRequest = $client->getEngine()->collectLastRequestInfo()->getLastRequest();

self::assertTrue($response->ok());
self::assertStringContainsString('<cis:Authentification>', $lastRequest);
}
}