SignMessageAction   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 29
c 1
b 0
f 0
dl 0
loc 59
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A doExecute() 0 24 3
A shouldSignMessage() 0 17 4
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\Outbound\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\Model\Protocol\AuthnRequest;
19
use LightSaml\Model\Protocol\LogoutRequest;
20
use LightSaml\Model\Protocol\Response;
21
use LightSaml\Resolver\Signature\SignatureResolverInterface;
22
use Psr\Log\LoggerInterface;
23
24
/**
25
 * Signs the outbound message, according to TrustOptions settings.
26
 */
27
class SignMessageAction extends AbstractProfileAction
28
{
29
    /** @var SignatureResolverInterface */
30
    protected $signatureResolver;
31
32
    public function __construct(LoggerInterface $logger, SignatureResolverInterface $signatureResolver)
33
    {
34
        parent::__construct($logger);
35
36
        $this->signatureResolver = $signatureResolver;
37
    }
38
39
    protected function doExecute(ProfileContext $context)
40
    {
41
        $shouldSign = $this->shouldSignMessage($context);
42
        if ($shouldSign) {
43
            $signature = $this->signatureResolver->getSignature($context);
44
            if ($signature) {
45
                MessageContextHelper::asSamlMessage($context->getOutboundContext())
46
                    ->setSignature($signature)
47
                ;
48
49
                $this->logger->debug(
50
                    sprintf('Message signed with fingerprint "%s"', $signature->getCertificate()->getFingerprint()),
51
                    LogHelper::getActionContext($context, $this, [
52
                        'certificate' => $signature->getCertificate()->getInfo(),
53
                    ])
54
                );
55
            } else {
56
                $this->logger->critical(
57
                    'No signature resolved, although signing enabled',
58
                    LogHelper::getActionErrorContext($context, $this, [])
59
                );
60
            }
61
        } else {
62
            $this->logger->debug('Signing disabled', LogHelper::getActionContext($context, $this));
63
        }
64
    }
65
66
    /**
67
     * @return bool
68
     */
69
    private function shouldSignMessage(ProfileContext $context)
70
    {
71
        $message = $context->getOutboundMessage();
72
73
        if ($message instanceof LogoutRequest) {
74
            return true;
75
        }
76
77
        $trustOptions = $context->getTrustOptions();
78
79
        if ($message instanceof AuthnRequest) {
80
            return $trustOptions->getSignAuthnRequest();
81
        } elseif ($message instanceof Response) {
82
            return $trustOptions->getSignResponse();
83
        }
84
85
        throw new \LogicException(sprintf('Unexpected message type "%s"', get_class($message)));
86
    }
87
}
88