-
Notifications
You must be signed in to change notification settings - Fork 100
/
RecipientValidatorActionTest.php
136 lines (108 loc) · 6.34 KB
/
RecipientValidatorActionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
namespace LightSaml\Tests\Action\Assertion\Inbound;
use LightSaml\Action\Assertion\Inbound\RecipientValidatorAction;
use LightSaml\Criteria\CriteriaSet;
use LightSaml\Model\Assertion\Assertion;
use LightSaml\Model\Assertion\AuthnStatement;
use LightSaml\Model\Assertion\Subject;
use LightSaml\Model\Assertion\SubjectConfirmation;
use LightSaml\Model\Assertion\SubjectConfirmationData;
use LightSaml\Model\Metadata\AssertionConsumerService;
use LightSaml\Model\Metadata\EntityDescriptor;
use LightSaml\Model\Metadata\SpSsoDescriptor;
use LightSaml\Resolver\Endpoint\Criteria\DescriptorTypeCriteria;
use LightSaml\Resolver\Endpoint\Criteria\LocationCriteria;
use LightSaml\Resolver\Endpoint\Criteria\ServiceTypeCriteria;
use LightSaml\SamlConstants;
use LightSaml\Tests\BaseTestCase;
class RecipientValidatorActionTest extends BaseTestCase
{
public function test_constructs_with_logger()
{
new RecipientValidatorAction($this->getLoggerMock(), $this->getEndpointResolverMock());
$this->assertTrue(true);
}
public function test_does_nothing_when_assertion_has_bearer_subject_but_no_authn_statement()
{
$action = new RecipientValidatorAction($loggerMock = $this->getLoggerMock(), $this->getEndpointResolverMock());
$assertionContext = $this->getAssertionContext($assertion = new Assertion());
$assertion->setSubject(new Subject());
$assertion->getSubject()->addSubjectConfirmation((new SubjectConfirmation())->setMethod(SamlConstants::CONFIRMATION_METHOD_BEARER));
$action->execute($assertionContext);
$this->assertTrue(true);
}
public function test_does_nothing_when_assertion_has_authn_statement_but_no_bearer_subject()
{
$action = new RecipientValidatorAction($loggerMock = $this->getLoggerMock(), $this->getEndpointResolverMock());
$assertionContext = $this->getAssertionContext($assertion = new Assertion());
$assertion->addItem(new AuthnStatement());
$action->execute($assertionContext);
$this->assertTrue(true);
}
public function test_throws_context_exception_when_bearer_confirmation_has_no_recipient()
{
$action = new RecipientValidatorAction($loggerMock = $this->getLoggerMock(), $this->getEndpointResolverMock());
$assertionContext = $this->getAssertionContext($assertion = new Assertion());
$assertion->addItem(new AuthnStatement());
$assertion->setSubject(new Subject());
$assertion->getSubject()->addSubjectConfirmation($subjectConfirmation = (new SubjectConfirmation())->setMethod(SamlConstants::CONFIRMATION_METHOD_BEARER));
$subjectConfirmation->setSubjectConfirmationData(new SubjectConfirmationData());
$loggerMock->expects($this->once())
->method('error')
->with('Bearer SubjectConfirmation must contain Recipient attribute');
$this->expectExceptionMessage("Bearer SubjectConfirmation must contain Recipient attribute");
$this->expectException(\LightSaml\Error\LightSamlContextException::class);
$action->execute($assertionContext);
}
public function test_throws_context_exception_when_recipient_does_not_match_any_own_acs_service_location()
{
$this->expectExceptionMessage("Recipient 'http://recipient.com' does not match SP descriptor");
$this->expectException(\LightSaml\Error\LightSamlContextException::class);
$action = new RecipientValidatorAction(
$loggerMock = $this->getLoggerMock(),
$endpointResolver = $this->getEndpointResolverMock()
);
$assertionContext = $this->getAssertionContext($assertion = new Assertion());
$assertion->addItem(new AuthnStatement());
$assertion->setSubject(new Subject());
$assertion->getSubject()->addSubjectConfirmation($subjectConfirmation = (new SubjectConfirmation())->setMethod(SamlConstants::CONFIRMATION_METHOD_BEARER));
$subjectConfirmation->setSubjectConfirmationData((new SubjectConfirmationData())->setRecipient($recipient = 'http://recipient.com'));
$profileContext = $this->getProfileContext();
$profileContext->getOwnEntityContext()->setEntityDescriptor($ownEntityDescriptor = new EntityDescriptor());
$assertionContext->setParent($profileContext);
$endpointResolver->expects($this->once())
->method('resolve')
->with($this->isInstanceOf(CriteriaSet::class), $this->isType('array'))
->willReturnCallback(function (CriteriaSet $criteriaSet) use ($recipient) {
$this->assertCriteria($criteriaSet, DescriptorTypeCriteria::class, 'getDescriptorType', SpSsoDescriptor::class);
$this->assertCriteria($criteriaSet, ServiceTypeCriteria::class, 'getServiceType', AssertionConsumerService::class);
$this->assertCriteria($criteriaSet, LocationCriteria::class, 'getLocation', $recipient);
return [];
});
$loggerMock->expects($this->once())
->method('error')
->with("Recipient 'http://recipient.com' does not match SP descriptor");
$action->execute($assertionContext);
}
public function test_does_nothing_if_recipient_matches_own_acs_service_location()
{
$action = new RecipientValidatorAction(
$loggerMock = $this->getLoggerMock(),
$endpointResolver = $this->getEndpointResolverMock()
);
$assertionContext = $this->getAssertionContext($assertion = new Assertion());
$assertion->addItem(new AuthnStatement());
$assertion->setSubject(new Subject());
$assertion->getSubject()->addSubjectConfirmation($subjectConfirmation = (new SubjectConfirmation())->setMethod(SamlConstants::CONFIRMATION_METHOD_BEARER));
$subjectConfirmation->setSubjectConfirmationData((new SubjectConfirmationData())->setRecipient($recipient = 'http://recipient.com'));
$profileContext = $this->getProfileContext();
$profileContext->getOwnEntityContext()->setEntityDescriptor($ownEntityDescriptor = new EntityDescriptor());
$assertionContext->setParent($profileContext);
$endpointResolver->expects($this->once())
->method('resolve')
->willReturnCallback(function () use ($recipient) {
return [$this->getEndpointReferenceMock(new AssertionConsumerService())];
});
$action->execute($assertionContext);
}
}