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\Assertion\Outbound; |
13
|
|
|
|
14
|
|
|
use LightSaml\Action\Assertion\AbstractAssertionAction; |
15
|
|
|
use LightSaml\Context\Profile\AssertionContext; |
16
|
|
|
use LightSaml\Context\Profile\ProfileContext; |
17
|
|
|
use LightSaml\Error\LightSamlContextException; |
18
|
|
|
use LightSaml\Model\Assertion\EncryptedAssertionWriter; |
19
|
|
|
use LightSaml\Resolver\Credential\CredentialResolverInterface; |
20
|
|
|
use LightSaml\SamlConstants; |
21
|
|
|
use LightSaml\Credential\CredentialInterface; |
22
|
|
|
use LightSaml\Credential\UsageType; |
23
|
|
|
use LightSaml\Credential\Criteria\EntityIdCriteria; |
24
|
|
|
use LightSaml\Credential\Criteria\MetadataCriteria; |
25
|
|
|
use LightSaml\Credential\Criteria\UsageCriteria; |
26
|
|
|
use Psr\Log\LoggerInterface; |
27
|
|
|
|
28
|
|
|
class EncryptAssertionAction extends AbstractAssertionAction |
29
|
|
|
{ |
30
|
|
|
/** @var CredentialResolverInterface */ |
31
|
|
|
protected $credentialResolver; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param LoggerInterface $logger |
35
|
|
|
* @param CredentialResolverInterface $credentialResolver |
36
|
|
|
*/ |
37
|
|
|
public function __construct(LoggerInterface $logger, CredentialResolverInterface $credentialResolver) |
38
|
|
|
{ |
39
|
|
|
parent::__construct($logger); |
40
|
|
|
|
41
|
|
|
$this->credentialResolver = $credentialResolver; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param AssertionContext $context |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
*/ |
49
|
|
|
protected function doExecute(AssertionContext $context) |
50
|
|
|
{ |
51
|
|
|
$profileContext = $context->getProfileContext(); |
52
|
|
|
$trustOptions = $profileContext->getTrustOptions(); |
53
|
|
|
if (false === $trustOptions->getEncryptAssertions()) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (null == $assertion = $context->getAssertion()) { |
58
|
|
|
throw new LightSamlContextException($context, 'Assertion for encryption is not set'); |
59
|
|
|
} |
60
|
|
|
$context->setAssertion(null); |
61
|
|
|
|
62
|
|
|
$query = $this->credentialResolver->query(); |
63
|
|
|
$query |
64
|
|
|
->add(new EntityIdCriteria($profileContext->getPartyEntityDescriptor()->getEntityID())) |
65
|
|
|
->add(new MetadataCriteria( |
66
|
|
|
ProfileContext::ROLE_IDP === $profileContext->getOwnRole() |
67
|
|
|
? MetadataCriteria::TYPE_SP |
68
|
|
|
: MetadataCriteria::TYPE_IDP, |
69
|
|
|
SamlConstants::PROTOCOL_SAML2 |
70
|
|
|
)) |
71
|
|
|
->add(new UsageCriteria(UsageType::ENCRYPTION)) |
72
|
|
|
; |
73
|
|
|
$query->resolve(); |
74
|
|
|
|
75
|
|
|
/** @var CredentialInterface $credential */ |
76
|
|
|
$credential = $query->firstCredential(); |
77
|
|
|
if (null == $credential) { |
78
|
|
|
throw new LightSamlContextException($context, 'Unable to resolve encrypting credential'); |
79
|
|
|
} |
80
|
|
|
if (null == $credential->getPublicKey()) { |
81
|
|
|
throw new LightSamlContextException($context, 'Credential resolved for assertion encryption does not have a public key'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$encryptedAssertionWriter = new EncryptedAssertionWriter( |
85
|
|
|
$trustOptions->getBlockEncryptionAlgorithm(), |
86
|
|
|
$trustOptions->getKeyTransportEncryptionAlgorithm() |
87
|
|
|
); |
88
|
|
|
$encryptedAssertionWriter->encrypt($assertion, $credential->getPublicKey()); |
89
|
|
|
|
90
|
|
|
$context->setEncryptedAssertion($encryptedAssertionWriter); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|