Skip to content

Commit

Permalink
fix: Attach signature if exists in all message
Browse files Browse the repository at this point in the history
  • Loading branch information
william-suppo committed Apr 25, 2024
1 parent fca3f8d commit bbf37d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
36 changes: 30 additions & 6 deletions src/ConcernsRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use LightSaml\Model\Protocol as LightSaml;
use LightSaml\Binding\BindingFactory;
use LightSaml\Context\Profile\MessageContext;
use LightSaml\Model\XmlDSig\SignatureStringReader;
use Litesaml\Exceptions\SamlException;
use Litesaml\Models\Descriptors\Role;
use Litesaml\Models\Messages\Attribute;
Expand All @@ -16,6 +17,7 @@
use Litesaml\Models\Messages\LogoutRequest;
use Litesaml\Models\Messages\LogoutResponse;
use Litesaml\Models\Messages\Message;
use Litesaml\Models\Messages\Signature;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

trait ConcernsRecipient
Expand Down Expand Up @@ -44,7 +46,8 @@ public function handleAuthnResponse(SymfonyRequest $request): AuthnResponse
return new AuthnResponse(
id: $message->getID(),
issuer: $message->getIssuer()->getValue(),
attributes: $attributes
attributes: $attributes,
signature: $this->extractSignature($message),
);
}

Expand All @@ -59,6 +62,7 @@ public function handleAuthnRequest(SymfonyRequest $request): AuthnRequest
return new AuthnRequest(
id: $message->getID(),
issuer: $message->getIssuer()->getValue(),
signature: $this->extractSignature($message),
);
}

Expand All @@ -73,6 +77,7 @@ public function handleLogoutRequest(SymfonyRequest $request): LogoutRequest
return new LogoutRequest(
id: $message->getID(),
issuer: $message->getIssuer()->getValue(),
signature: $this->extractSignature($message),
);
}

Expand All @@ -84,13 +89,10 @@ public function handleLogoutResponse(SymfonyRequest $request): LogoutResponse
throw new SamlException('Wrong request received');
}

/** @var \LightSaml\Model\XmlDSig\SignatureXmlReader $signatureReader */
$signatureReader = $message->getSignature();

return new LogoutResponse(
id: $message->getID(),
issuer: $message->getIssuer()->getValue(),
signature: $signatureReader?->getSignature()
signature: $this->extractSignature($message),
);
}

Expand All @@ -105,7 +107,13 @@ public function validateSignature(Message $message, Role $issuer): bool
(new X509Certificate())->loadPem($issuer->signing->publicKey->toPem())
);

if (! $message->signature->verify($key)) {
$signatureReader = new SignatureStringReader(
$message->signature->value,
$message->signature->algorithm,
$message->signature->data,
);

if (! $signatureReader->validate($key)) {
return false;
}

Expand All @@ -131,4 +139,20 @@ private function unpack(SymfonyRequest $request): LightSaml\SamlMessage

return $message;
}

private function extractSignature(LightSaml\SamlMessage $message): ?Signature
{
/** @var \LightSaml\Model\XmlDSig\SignatureStringReader $signatureReader */
$signatureReader = $message->getSignature();

if (! $signatureReader) {
return null;
}

return new Signature(
value: $signatureReader->getSignature(),
algorithm: $signatureReader->getAlgorithm(),
data: $signatureReader->getData()
);
}
}
2 changes: 1 addition & 1 deletion src/Models/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ abstract class Message extends DataTransferObject

public string $issuer;

public ?XMLSecurityDSig $signature;
public ?Signature $signature;
}
12 changes: 12 additions & 0 deletions src/Models/Messages/Signature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Litesaml\Models\Messages;

use Bluestone\DataTransferObject\DataTransferObject;

class Signature extends DataTransferObject
{
public string $value;
public string $algorithm;
public string $data;
}

0 comments on commit bbf37d9

Please sign in to comment.