Skip to content

Commit 2dde092

Browse files
Add test for authenticate function
1 parent 144f855 commit 2dde092

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

tests/OpenIDConnectClientTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,70 @@ public function testAuthenticateDoesNotThrowExceptionIfClaimsIsMissingNonce()
142142
}
143143
}
144144

145+
public function testAuthenticateWithCodeThrowsExceptionIfStateDoesNotMatch()
146+
{
147+
$_REQUEST['code'] = 'some-code';
148+
$_REQUEST['state'] = "incorrect-state-from-user";
149+
$_SESSION['openid_connect_state'] = "random-generated-state";
150+
151+
$client = new OpenIDConnectClient();
152+
153+
try {
154+
$client->authenticate();
155+
} catch ( OpenIDConnectClientException $e ) {
156+
$this->assertEquals('Unable to determine state', $e->getMessage());
157+
return;
158+
}
159+
160+
$this->fail('OpenIDConnectClientException was not thrown when it should have been.');
161+
}
162+
163+
public function testAuthenticateWithCodeMockedVerify()
164+
{
165+
$mockCode = 'some-code';
166+
167+
$_REQUEST['code'] = $mockCode;
168+
$_REQUEST['state'] = "random-generated-state";
169+
$_SESSION['openid_connect_state'] = "random-generated-state";
170+
171+
$mockClaims = (object)['email' => 'test@example.com'];
172+
$mockIdToken = implode('.', [base64_encode('{}'), base64_encode(json_encode($mockClaims)), '']);
173+
$mockAccessToken = 'some-access-token';
174+
$mockRefreshToken = 'some-access-token';
175+
176+
$mockTokenResponse = (object)[
177+
'id_token' => $mockIdToken,
178+
'access_token' => $mockAccessToken,
179+
'refresh_token' => $mockRefreshToken,
180+
];
181+
182+
$client = $this->getMockBuilder(OpenIDConnectClient::class)
183+
->setMethods(['requestTokens', 'verifySignatures', 'verifyJWTClaims'])
184+
->getMock();
185+
$client->method('requestTokens')
186+
->with($mockCode)
187+
->willReturn($mockTokenResponse);
188+
$client->method('verifySignatures')
189+
->with($mockIdToken);
190+
$client->method('verifyJWTClaims')
191+
->with($mockClaims, $mockAccessToken)
192+
->willReturn(true);
193+
194+
try {
195+
// In this mocked case we should be authenticated
196+
// because we are not actually verifying the JWT
197+
$authenticated = $client->authenticate();
198+
$this->assertTrue($authenticated);
199+
$this->assertEquals($mockIdToken, $client->getIdToken());
200+
$this->assertEquals($mockAccessToken, $client->getAccessToken());
201+
$this->assertEquals($mockTokenResponse, $client->getTokenResponse());
202+
$this->assertEquals($mockClaims, $client->getVerifiedClaims());
203+
$this->assertEquals($mockRefreshToken, $client->getRefreshToken());
204+
} catch ( OpenIDConnectClientException $e ) {
205+
$this->fail('OpenIDConnectClientException was thrown when it should not have been.');
206+
}
207+
}
208+
145209
public function testSerialize()
146210
{
147211
$client = new OpenIDConnectClient('https://example.com', 'foo', 'bar', 'baz');

0 commit comments

Comments
 (0)