-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenIdLoginAuthenticatorTest.php
More file actions
155 lines (125 loc) · 6.1 KB
/
Copy pathOpenIdLoginAuthenticatorTest.php
File metadata and controls
155 lines (125 loc) · 6.1 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace ItkDev\OpenIdConnectBundle\Tests\Security;
use ItkDev\OpenIdConnect\Exception\ClaimsException;
use ItkDev\OpenIdConnect\Exception\OpenIdConnectExceptionInterface;
use ItkDev\OpenIdConnect\Exception\ValidationException;
use ItkDev\OpenIdConnect\Security\OpenIdConfigurationProvider;
use ItkDev\OpenIdConnectBundle\Security\OpenIdConfigurationProviderManager;
use ItkDev\OpenIdConnectBundle\Security\OpenIdLoginAuthenticator;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
class OpenIdLoginAuthenticatorTest extends TestCase
{
private OpenIdLoginAuthenticator $authenticator;
/** @var OpenIdConfigurationProviderManager&Stub */
private OpenIdConfigurationProviderManager $stubProviderManager;
protected function setUp(): void
{
$this->stubProviderManager = $this->createStub(OpenIdConfigurationProviderManager::class);
$this->authenticator = new TestAuthenticator($this->stubProviderManager);
}
public function testSupports(): void
{
$request = new Request();
$this->assertFalse($this->authenticator->supports($request));
$request->query->set('state', 'abcd');
$this->assertFalse($this->authenticator->supports($request));
$request->query->set('code', 'xyz');
$this->assertTrue($this->authenticator->supports($request));
}
public function testOnAuthenticationFailurePreservesCause(): void
{
$cause = new AuthenticationException('Original cause message');
try {
$this->authenticator->onAuthenticationFailure(new Request(), $cause);
$this->fail('Expected AuthenticationException');
} catch (AuthenticationException $thrown) {
$this->assertSame($cause, $thrown->getPrevious(), 'Original exception must be chained as previous');
$this->assertStringContainsString('Original cause message', $thrown->getMessage(), 'Cause message must be preserved for logs');
}
}
public function testValidateClaimsWrongState(): void
{
$request = new Request(query: ['state' => 'wrong_test_state']);
$this->setSessionOnRequest($request);
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Invalid state');
$this->authenticator->authenticate($request);
}
public function testValidateClaimsEmptyNonce(): void
{
$request = new Request(query: ['state' => 'test_state']);
$this->setSessionOnRequest($request, nonce: null);
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Nonce empty or not found');
$this->authenticator->authenticate($request);
}
public function testValidateClaimsMissingCode(): void
{
$request = new Request(query: ['state' => 'test_state']);
$this->setSessionOnRequest($request);
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('Missing or invalid code');
$this->authenticator->authenticate($request);
}
public function testValidateClaimsCodeDoesNotValidate(): void
{
$cause = new ClaimsException('test message');
$stubProvider = $this->createStub(OpenIdConfigurationProvider::class);
$stubProvider->method('validateIdToken')->willThrowException($cause);
$this->stubProviderManager->method('getProvider')->willReturn($stubProvider);
$request = new Request(query: ['state' => 'test_state', 'code' => 'test_code']);
$this->setSessionOnRequest($request);
try {
$this->authenticator->authenticate($request);
} catch (ValidationException $thrown) {
$this->assertSame('test message', $thrown->getMessage());
$this->assertSame($cause, $thrown->getPrevious(), 'Original cause must be chained');
$this->assertInstanceOf(
OpenIdConnectExceptionInterface::class,
$thrown->getPrevious(),
'Wrapped cause must satisfy the library marker contract',
);
return;
}
$this->fail('Expected ValidationException');
}
public function testValidateClaimsSuccess(): void
{
$stubProvider = $this->createStub(OpenIdConfigurationProvider::class);
$claims = new \stdClass();
$claims->email = 'test@example.org';
$claims->name = 'Test Tester';
$stubProvider->method('validateIdToken')->willReturn($claims);
// Expect the exact provider key from the session, so a lookup with a
// mangled key fails the test instead of silently matching any key.
$mockProviderManager = $this->createMock(OpenIdConfigurationProviderManager::class);
$mockProviderManager->expects($this->once())
->method('getProvider')
->with('test_provider_1')
->willReturn($stubProvider);
$authenticator = new TestAuthenticator($mockProviderManager);
$request = new Request(query: ['state' => 'test_state', 'code' => 'test_code']);
$this->setSessionOnRequest($request);
$passport = $authenticator->authenticate($request);
$this->assertSame('test@example.org', $passport->getUser()->getUserIdentifier());
// The claims contract: the IdP claims plus the provider key that
// authenticated the user.
$this->assertSame('Test Tester', $authenticator->lastClaims['name'] ?? null);
$this->assertSame('test_provider_1', $authenticator->lastClaims['open_id_connect_provider'] ?? null);
}
private function setSessionOnRequest(Request $request, ?string $nonce = 'test_nonce'): void
{
$stubSession = $this->createStub(SessionInterface::class);
$map = [
['oauth2provider', 'test_provider_1'],
['oauth2state', 'test_state'],
['oauth2nonce', $nonce],
];
$stubSession->method('remove')->willReturnMap($map);
$request->setSession($stubSession);
}
}