1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LightSAML-IDP package. |
5
|
|
|
* |
6
|
|
|
* (c) Milos Tomic <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the GPL-3 license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace LightSaml\Idp\Action\Profile\Outbound\Response; |
13
|
|
|
|
14
|
|
|
use LightSaml\Action\ActionInterface; |
15
|
|
|
use LightSaml\Action\CompositeActionInterface; |
16
|
|
|
use LightSaml\Action\Profile\AbstractProfileAction; |
17
|
|
|
use LightSaml\Context\Profile\AssertionContext; |
18
|
|
|
use LightSaml\Context\Profile\Helper\LogHelper; |
19
|
|
|
use LightSaml\Context\Profile\Helper\MessageContextHelper; |
20
|
|
|
use LightSaml\Context\Profile\ProfileContext; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Foreach of the given assertion actions, creates an assertion context, executes it, and if it has an assertion, |
25
|
|
|
* adds it to the Response in outbound context. |
26
|
|
|
*/ |
27
|
|
|
class HandleAssertionsAction extends AbstractProfileAction implements CompositeActionInterface |
28
|
|
|
{ |
29
|
|
|
/** @var ActionInterface[] */ |
30
|
|
|
protected $assertionActions; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param LoggerInterface $logger |
34
|
|
|
* @param ActionInterface[] $assertionActions |
35
|
|
|
*/ |
36
|
|
|
public function __construct(LoggerInterface $logger, array $assertionActions = array()) |
37
|
|
|
{ |
38
|
|
|
parent::__construct($logger); |
39
|
|
|
|
40
|
|
|
foreach ($assertionActions as $action) { |
41
|
|
|
$this->add($action); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ActionInterface $assertionAction |
47
|
|
|
* |
48
|
|
|
* @return HandleAssertionsAction |
49
|
|
|
*/ |
50
|
|
|
public function add(ActionInterface $assertionAction) |
51
|
|
|
{ |
52
|
|
|
$this->assertionActions[] = $assertionAction; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param callable $callable |
59
|
|
|
* |
60
|
|
|
* @return ActionInterface|null |
61
|
|
|
*/ |
62
|
|
|
public function map($callable) |
63
|
|
|
{ |
64
|
|
|
foreach ($this->assertionActions as $k => $action) { |
65
|
|
|
$newAction = call_user_func($callable, $action); |
66
|
|
|
if ($newAction) { |
67
|
|
|
$this->assertionActions[$k] = $newAction; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param ProfileContext $context |
74
|
|
|
* |
75
|
|
|
* @return void |
76
|
|
|
*/ |
77
|
|
|
protected function doExecute(ProfileContext $context) |
78
|
|
|
{ |
79
|
|
|
$response = MessageContextHelper::asResponse($context->getOutboundContext()); |
|
|
|
|
80
|
|
|
|
81
|
|
|
foreach ($this->assertionActions as $index => $action) { |
82
|
|
|
$name = sprintf('assertion_%s', $index); |
83
|
|
|
/** @var AssertionContext $assertionContext */ |
84
|
|
|
$assertionContext = $context->getSubContext($name, AssertionContext::class); |
85
|
|
|
$assertionContext->setId($index); |
86
|
|
|
|
87
|
|
|
$action->execute($assertionContext); |
88
|
|
|
|
89
|
|
|
if ($assertionContext->getEncryptedAssertion()) { |
90
|
|
|
$response->addEncryptedAssertion($assertionContext->getEncryptedAssertion()); |
91
|
|
|
} elseif ($assertionContext->getAssertion()) { |
92
|
|
|
$response->addAssertion($assertionContext->getAssertion()); |
93
|
|
|
} else { |
94
|
|
|
$this->logger->warning('No assertion was built', LogHelper::getActionContext($context, $this)); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: