Skip to content

Commit

Permalink
WIP. XML login request.
Browse files Browse the repository at this point in the history
  • Loading branch information
rico committed Mar 26, 2023
1 parent 267ccb3 commit 7c69342
Show file tree
Hide file tree
Showing 22 changed files with 319 additions and 148 deletions.
41 changes: 31 additions & 10 deletions src/Methods/CheckConnection.php
Expand Up @@ -2,22 +2,43 @@

namespace Smartprax\Medidoc\Methods;

use Ramsey\Uuid\Uuid;
use Sabre\Xml\Writer;
use Smartprax\Medidoc\XML\Nodes\Envelope\Security\Timestamp;
use Smartprax\Medidoc\XML\Nodes\Node;
use Smartprax\Medidoc\XML\XML_NS;

class CheckConnection extends Method
{

public function xmlSerialize(Writer $writer): void
{
$writer->write([
[
'name' => 'gln',
'value' => \config('medidoc.gln')
],
[
'name' => 'password',
'value' => \config('medidoc.password')
],
]);
$writer->write(
Node::create(
name: XML_NS::trust->clark('RequestSecurityToken'),
value: [
Node::create(
name: XML_NS::trust->clark('TokenType'),
value: 'http://schemas.xmlsoap.org/ws/2005/02/sc/sct'),
Node::create(name: XML_NS::trust->clark('RequestType'), value: 'http://schemas.xmlsoap.org/ws/2005/02/trust/Issue'),
Node::create(name: XML_NS::trust->clark('Entropy'), value: [
Node::create(name: XML_NS::trust->clark('BinarySecret'), attributes: [
XML_NS::wss_utility->attribute('id') => 'uuid-' . Uuid::uuid4() . '-1',
'Type' => 'http://schemas.xmlsoap.org/ws/2005/02/trust/Nonce',
], value: \base64_encode(
pack(
'H*',
sha1(
pack('H*', mt_rand()) .
pack('a*', Timestamp::create($this)->created()) .
pack('a*', \config('medidoc.encryption_key'))
)
)
)
)
]),
]
)
);
}
}
46 changes: 32 additions & 14 deletions src/Methods/Method.php
Expand Up @@ -10,7 +10,15 @@
use Lorisleiva\Actions\Concerns\AsCommand;
use Ramsey\Uuid\Uuid;
use Sabre\Xml\XmlSerializable;
use Smartprax\Medidoc\XML\MedidocXMLService;
use Smartprax\Medidoc\XML\MedidocXML;
use Smartprax\Medidoc\XML\Nodes\Body;
use Smartprax\Medidoc\XML\Nodes\Envelope\Action;
use Smartprax\Medidoc\XML\Nodes\Envelope\LoginSecurity;
use Smartprax\Medidoc\XML\Nodes\Envelope\MessageID;
use Smartprax\Medidoc\XML\Nodes\Envelope\ReplyTo;
use Smartprax\Medidoc\XML\Nodes\Envelope\Security;
use Smartprax\Medidoc\XML\Nodes\Envelope\To;
use Smartprax\Medidoc\XML\Nodes\Header;

abstract class Method implements XmlSerializable
{
Expand Down Expand Up @@ -68,27 +76,37 @@ public function messageId() : string
return CarbonImmutable::now('GMT');
}

public function call()
{
$this->handle();
}

public function handle() : string
{
$this->login();

return MedidocXMLService::make($this)->prettyPrint();
return $this->login();

//return $this->call();
}

public function login()
public function login() : string
{
$login_xml = MedidocXMLService::make($this)->write([

]);
return MedidocXML::write(
Header::create()
->addChild(new Action($this))
->addChild(new MessageID())
->addChild(new ReplyTo())
->addChild(new To())
->addChild(new LoginSecurity($this)),
new Body($this));
}



public function call() : string
{
return MedidocXML::write(
Header::create()
->addChild(new Action($this))
->addChild(new MessageID())
->addChild(new ReplyTo())
->addChild(new To())
->addChild(new Security($this)),
new Body($this));
}

public function getCommandSignature() :string
{
Expand Down
55 changes: 55 additions & 0 deletions src/XML/MedidocXML.php
@@ -0,0 +1,55 @@
<?php

namespace Smartprax\Medidoc\XML;

use Sabre\Xml\Writer;
use Smartprax\Medidoc\XML\Nodes\Body;
use Smartprax\Medidoc\XML\Nodes\Header;

class MedidocXML
{

protected Writer $writer;

public function __construct()
{
$this->writer = New Writer();
$this->writer->openMemory();
$this->writer->setIndent(true);
$this->writer->startDocument('1.0', 'UTF-8');

foreach (XML_NS::cases() as $namespace) {
$this->writer->namespaceMap[$namespace->value] = $namespace->name;
}

}

public static function write(Header $header, Body $body) : self
{
$instance = new self();

$instance->writer->writeElement(XML_NS::envelope->clark('Envelope'), [
$header,
$body,
]);

return $instance;
}


public function prettyPrint() : string
{
$dom = new \DOMDocument('1.0');
$dom->preserveWhiteSpace = true;
$dom->formatOutput = true;
$dom->loadXML($this->writer->outputMemory());

return $dom->saveXML();
}

public function __toString(): string
{
return $this->writer->outputMemory();
}

}
57 changes: 0 additions & 57 deletions src/XML/MedidocXMLService.php

This file was deleted.

16 changes: 10 additions & 6 deletions src/XML/Nodes/Body.php
Expand Up @@ -2,22 +2,26 @@

namespace Smartprax\Medidoc\XML\Nodes;

use Sabre\Xml\Writer;
use Smartprax\Medidoc\Methods\Method;
use Smartprax\Medidoc\XML\XML_NS;

class Body extends Node
{
public function namespace(): ?XML_NS
public Method $method;

public function __construct(Method $method)
{
return XML_NS::envelope;
$this->method = $method;
}

public function attributes(): array
public function namespace(): ?XML_NS
{
return [];
return XML_NS::envelope;
}

public function value(): array
public function xmlSerialize(Writer $writer): void
{
return [];
$this->method->xmlSerialize($writer);
}
}
36 changes: 0 additions & 36 deletions src/XML/Nodes/Envelope.php

This file was deleted.

2 changes: 2 additions & 0 deletions src/XML/Nodes/Envelope/Action.php
Expand Up @@ -8,6 +8,8 @@
class Action extends \Smartprax\Medidoc\XML\Nodes\Node
{

public function __construct(protected Method $method) {}

public function namespace(): ?XML_NS
{
return XML_NS::addressing;
Expand Down
35 changes: 35 additions & 0 deletions src/XML/Nodes/Envelope/LoginSecurity.php
@@ -0,0 +1,35 @@
<?php

namespace Smartprax\Medidoc\XML\Nodes\Envelope;

use Smartprax\Medidoc\Methods\Method;
use Smartprax\Medidoc\XML\Nodes\Envelope\Security\Timestamp;
use Smartprax\Medidoc\XML\Nodes\Envelope\Security\UsernameToken;
use Smartprax\Medidoc\XML\Nodes\Node;
use Smartprax\Medidoc\XML\XML_NS;

class LoginSecurity extends Node
{

public function __construct(protected Method $method) {}

public function namespace(): ?XML_NS
{
return XML_NS::wss_secext;
}

public function attributes(): array
{
return [
XML_NS::envelope->attribute('mustUnderstand') => '1'
];
}

public function value(): array
{
return [
new Timestamp($this->method),
new UsernameToken(),
];
}
}
2 changes: 1 addition & 1 deletion src/XML/Nodes/Envelope/ReplyTo.php
Expand Up @@ -21,7 +21,7 @@ public function attributes(): array
public function value(): string|array
{
return [
'name' => XML_NS::addressing->node('Address'),
'name' => XML_NS::addressing->clark('Address'),
'value' => 'http://www.w3.org/2005/08/addressing/anonymous'
];
}
Expand Down
5 changes: 4 additions & 1 deletion src/XML/Nodes/Envelope/Security.php
Expand Up @@ -2,6 +2,7 @@

namespace Smartprax\Medidoc\XML\Nodes\Envelope;

use Smartprax\Medidoc\Methods\Method;
use Smartprax\Medidoc\XML\Nodes\Envelope\Security\SecurityContextToken;
use Smartprax\Medidoc\XML\Nodes\Envelope\Security\Timestamp;
use Smartprax\Medidoc\XML\Nodes\Node;
Expand All @@ -10,6 +11,8 @@
class Security extends Node
{

public function __construct(protected Method $method) {}

public function namespace(): ?XML_NS
{
return XML_NS::wss_secext;
Expand All @@ -26,7 +29,7 @@ public function value(): array
{
return [
new Timestamp($this->method),
$this->method->loggedIn() ? new SecurityContextToken($this->method) : new UsernameToken,
new SecurityContextToken($this->method),
];
}
}

0 comments on commit 7c69342

Please sign in to comment.