Skip to content

Commit 3949a9c

Browse files
Add test for authenticate function
1 parent e9a438e commit 3949a9c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

tests/OpenIDConnectClientTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,72 @@ public function testAuthenticateDoesNotThrowExceptionIfClaimsIsMissingNonce()
157157
}
158158
}
159159

160+
public function testAuthenticateWithCodeThrowsExceptionIfStateDoesNotMatch()
161+
{
162+
$_REQUEST['code'] = 'some-code';
163+
$_REQUEST['state'] = "incorrect-state-from-user";
164+
$_SESSION['openid_connect_state'] = "random-generated-state";
165+
166+
$client = new OpenIDConnectClient();
167+
168+
try {
169+
$client->authenticate();
170+
} catch ( OpenIDConnectClientException $e ) {
171+
$this->assertEquals('Unable to determine state', $e->getMessage());
172+
return;
173+
}
174+
175+
$this->fail('OpenIDConnectClientException was not thrown when it should have been.');
176+
}
177+
178+
public function testAuthenticateWithCodeMockedVerify()
179+
{
180+
$mockCode = 'some-code';
181+
$mockState = 'some-code';
182+
183+
$_REQUEST['code'] = $mockCode;
184+
$_REQUEST['state'] = $mockState;
185+
186+
$mockClaims = (object)['email' => '[email protected]'];
187+
$mockIdToken = implode('.', [base64_encode('{}'), base64_encode(json_encode($mockClaims)), '']);
188+
$mockAccessToken = 'some-access-token';
189+
$mockRefreshToken = 'some-access-token';
190+
191+
$mockTokenResponse = (object)[
192+
'id_token' => $mockIdToken,
193+
'access_token' => $mockAccessToken,
194+
'refresh_token' => $mockRefreshToken,
195+
];
196+
197+
$client = $this->getMockBuilder(OpenIDConnectClient::class)
198+
->setMethods(['requestTokens', 'verifySignatures', 'verifyJWTClaims', 'getState'])
199+
->getMock();
200+
$client->method('getState')
201+
->willReturn($mockState);
202+
$client->method('requestTokens')
203+
->with($mockCode)
204+
->willReturn($mockTokenResponse);
205+
$client->method('verifySignatures')
206+
->with($mockIdToken);
207+
$client->method('verifyJWTClaims')
208+
->with($mockClaims, $mockAccessToken)
209+
->willReturn(true);
210+
211+
try {
212+
// In this mocked case we should be authenticated
213+
// because we are not actually verifying the JWT
214+
$authenticated = $client->authenticate();
215+
$this->assertTrue($authenticated);
216+
$this->assertEquals($mockIdToken, $client->getIdToken());
217+
$this->assertEquals($mockAccessToken, $client->getAccessToken());
218+
$this->assertEquals($mockTokenResponse, $client->getTokenResponse());
219+
$this->assertEquals($mockClaims, $client->getVerifiedClaims());
220+
$this->assertEquals($mockRefreshToken, $client->getRefreshToken());
221+
} catch ( OpenIDConnectClientException $e ) {
222+
$this->fail('OpenIDConnectClientException was thrown when it should not have been. Received exception: ' . $e->getMessage());
223+
}
224+
}
225+
160226
public function testSerialize()
161227
{
162228
$client = new OpenIDConnectClient('https://example.com', 'foo', 'bar', 'baz');

0 commit comments

Comments
 (0)