1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LightSAML-Core package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LightSaml\Action\Profile\Inbound\Message; |
13
|
|
|
|
14
|
|
|
use LightSaml\Action\Profile\AbstractProfileAction; |
15
|
|
|
use LightSaml\Context\Profile\Helper\LogHelper; |
16
|
|
|
use LightSaml\Context\Profile\Helper\MessageContextHelper; |
17
|
|
|
use LightSaml\Context\Profile\ProfileContext; |
18
|
|
|
use LightSaml\Credential\Criteria\MetadataCriteria; |
19
|
|
|
use LightSaml\Error\LightSamlModelException; |
20
|
|
|
use LightSaml\Model\XmlDSig\AbstractSignatureReader; |
21
|
|
|
use LightSaml\Validator\Model\Signature\SignatureValidatorInterface; |
22
|
|
|
use Psr\Log\LoggerInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validates the signature, if any, of the inbound message. |
26
|
|
|
*/ |
27
|
|
|
class MessageSignatureValidatorAction extends AbstractProfileAction |
28
|
|
|
{ |
29
|
|
|
/** @var SignatureValidatorInterface */ |
30
|
|
|
protected $signatureValidator; |
31
|
|
|
|
32
|
|
|
public function __construct(LoggerInterface $logger, SignatureValidatorInterface $signatureValidator) |
33
|
|
|
{ |
34
|
|
|
parent::__construct($logger); |
35
|
|
|
|
36
|
|
|
$this->signatureValidator = $signatureValidator; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return void |
41
|
|
|
*/ |
42
|
|
|
protected function doExecute(ProfileContext $context) |
43
|
|
|
{ |
44
|
|
|
$message = MessageContextHelper::asSamlMessage($context->getInboundContext()); |
45
|
|
|
|
46
|
|
|
$signature = $message->getSignature(); |
47
|
|
|
if (null === $signature) { |
48
|
|
|
$this->logger->debug('Message is not signed', LogHelper::getActionContext($context, $this)); |
49
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($signature instanceof AbstractSignatureReader) { |
54
|
|
|
$metadataType = ProfileContext::ROLE_IDP === $context->getOwnRole() ? MetadataCriteria::TYPE_SP : MetadataCriteria::TYPE_IDP; |
55
|
|
|
$credential = $this->signatureValidator->validate($signature, $message->getIssuer()->getValue(), $metadataType); |
56
|
|
|
if ($credential) { |
57
|
|
|
$keyNames = $credential->getKeyNames(); |
58
|
|
|
$this->logger->debug( |
59
|
|
|
sprintf('Message signature validated with key "%s"', implode(', ', $keyNames)), |
60
|
|
|
LogHelper::getActionContext($context, $this, [ |
61
|
|
|
'credential' => $credential, |
62
|
|
|
]) |
63
|
|
|
); |
64
|
|
|
} else { |
65
|
|
|
$this->logger->warning( |
66
|
|
|
'Signature verification was not performed', |
67
|
|
|
LogHelper::getActionContext($context, $this) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$message = 'Expected AbstractSignatureReader'; |
72
|
|
|
$this->logger->critical($message, LogHelper::getActionErrorContext($context, $this)); |
73
|
|
|
throw new LightSamlModelException($message); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|