Skip to content

Commit 9e04095

Browse files
Add test for authenticate function
1 parent 144f855 commit 9e04095

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
@@ -142,6 +142,72 @@ 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+
$mockState = 'some-code';
167+
168+
$_REQUEST['code'] = $mockCode;
169+
$_REQUEST['state'] = $mockState;
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', 'getState'])
184+
->getMock();
185+
$client->method('getState')
186+
->willReturn($mockState);
187+
$client->method('requestTokens')
188+
->with($mockCode)
189+
->willReturn($mockTokenResponse);
190+
$client->method('verifySignatures')
191+
->with($mockIdToken);
192+
$client->method('verifyJWTClaims')
193+
->with($mockClaims, $mockAccessToken)
194+
->willReturn(true);
195+
196+
try {
197+
// In this mocked case we should be authenticated
198+
// because we are not actually verifying the JWT
199+
$authenticated = $client->authenticate();
200+
$this->assertTrue($authenticated);
201+
$this->assertEquals($mockIdToken, $client->getIdToken());
202+
$this->assertEquals($mockAccessToken, $client->getAccessToken());
203+
$this->assertEquals($mockTokenResponse, $client->getTokenResponse());
204+
$this->assertEquals($mockClaims, $client->getVerifiedClaims());
205+
$this->assertEquals($mockRefreshToken, $client->getRefreshToken());
206+
} catch ( OpenIDConnectClientException $e ) {
207+
$this->fail('OpenIDConnectClientException was thrown when it should not have been. Received exception: ' . $e->getMessage());
208+
}
209+
}
210+
145211
public function testSerialize()
146212
{
147213
$client = new OpenIDConnectClient('https://example.com', 'foo', 'bar', 'baz');

0 commit comments

Comments
 (0)