MessageSignatureValidatorAction   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 26
c 1
b 0
f 0
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A doExecute() 0 32 5
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