Skip to content

Commit 5216e35

Browse files
test: add test for state does not match exception and added needed sub claim
1 parent 0ffd977 commit 5216e35

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

tests/Feature/Http/Controllers/LoginControllerResponseTest.php

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@
55
namespace MinVWS\OpenIDConnectLaravel\Tests\Feature\Http\Controllers;
66

77
use Illuminate\Http\Client\Request;
8+
use Illuminate\Http\Response;
89
use Illuminate\Support\Facades\Config;
910
use Illuminate\Support\Facades\Http;
1011
use Illuminate\Support\Facades\Session;
1112
use Illuminate\Testing\TestResponse;
13+
use Jumbojett\OpenIDConnectClientException;
1214
use MinVWS\OpenIDConnectLaravel\OpenIDConfiguration\OpenIDConfiguration;
1315
use MinVWS\OpenIDConnectLaravel\OpenIDConfiguration\OpenIDConfigurationLoader;
16+
use MinVWS\OpenIDConnectLaravel\Services\ExceptionHandlerInterface;
1417
use MinVWS\OpenIDConnectLaravel\Tests\TestCase;
1518
use Mockery;
1619

@@ -129,11 +132,53 @@ public function codeChallengeMethodProvider(): array
129132
];
130133
}
131134

132-
public function testTokenSignedWithClientSecret(): void
135+
public function testStateDoesNotMatch(): void
136+
{
137+
Http::fake([
138+
// Token requested by OpenIDConnectClient::authenticate() function.
139+
// Currently needed because the package requests the token endpoint before checking the state.
140+
// TODO: Remove if https://github.com/jumbojett/OpenID-Connect-PHP/pull/447 is merged.
141+
'https://provider.rdobeheer.nl/token' => Http::response([
142+
'access_token' => 'access-token-from-token-endpoint',
143+
'id_token' => 'some-valid-token-not-needed-for-this-state-check',
144+
'token_type' => 'Bearer',
145+
'expires_in' => 3600,
146+
]),
147+
]);
148+
149+
// Set OIDC config
150+
$this->mockOpenIDConfigurationLoader();
151+
Config::set('oidc.issuer', 'https://provider.rdobeheer.nl');
152+
Config::set('oidc.client_id', 'test-client-id');
153+
Config::set('oidc.client_secret', 'the-secret-client-secret');
154+
155+
// Mock LoginResponseHandlerInterface to check handleExceptionWhileAuthenticate is called.
156+
$mock = Mockery::mock(ExceptionHandlerInterface::class);
157+
$mock
158+
->shouldReceive('handleExceptionWhileAuthenticate')
159+
->withArgs(function (OpenIDConnectClientException $e) {
160+
return $e->getMessage() === 'Unable to determine state';
161+
})
162+
->once()
163+
->andReturn(new Response('', 400));
164+
$this->app->instance(ExceptionHandlerInterface::class, $mock);
165+
166+
// Set the current state, which is usually generated and saved in the session before login,
167+
// and sent to the issuer during the login redirect.
168+
Session::put('openid_connect_state', 'some-state');
169+
170+
// We simulate here that the state does not match with the state in the session.
171+
// And that the repsonse of ExceptionHandlerInterface is returned.
172+
$response = $this->getRoute('oidc.login', ['code' => 'some-code', 'state' => 'a-different-state']);
173+
$response->assertStatus(400);
174+
}
175+
176+
public function testIdTokenSignedWithClientSecret(): void
133177
{
134178
$idToken = generateJwt([
135179
"iss" => "https://provider.rdobeheer.nl",
136180
"aud" => 'test-client-id',
181+
"sub" => 'test-subject',
137182
], 'the-secret-client-secret');
138183

139184
Http::fake([
@@ -146,7 +191,7 @@ public function testTokenSignedWithClientSecret(): void
146191
]),
147192
// User info requested by OpenIDConnectClient::requestUserInfo() function.
148193
'https://provider.rdobeheer.nl/userinfo?schema=openid' => Http::response([
149-
'email' => 'teste@rdobeheer.nl',
194+
'email' => 'tester@rdobeheer.nl',
150195
]),
151196
]);
152197

@@ -157,16 +202,16 @@ public function testTokenSignedWithClientSecret(): void
157202
Config::set('oidc.client_id', 'test-client-id');
158203
Config::set('oidc.client_secret', 'the-secret-client-secret');
159204

160-
// Set current state, normally this is generated before logging in and send
161-
// to the issuer, when the user is redirected for login.
205+
// Set the current state, which is usually generated and saved in the session before login,
206+
// and sent to the issuer during the login redirect.
162207
Session::put('openid_connect_state', 'some-state');
163208

164209
// We simulate here that the user now comes back after successful login at issuer.
165210
$response = $this->getRoute('oidc.login', ['code' => 'some-code', 'state' => 'some-state']);
166211
$response->assertStatus(200);
167212
$response->assertJson([
168213
'userInfo' => [
169-
'email' => 'teste@rdobeheer.nl',
214+
'email' => 'tester@rdobeheer.nl',
170215
]
171216
]);
172217

@@ -234,8 +279,8 @@ public function testTokenSignedWithPrivateKey(): void
234279
[$key, $keyResource] = generateOpenSSLKey();
235280
Config::set('oidc.client_authentication.signing_private_key_path', stream_get_meta_data($keyResource)['uri']);
236281

237-
// Set current state, normally this is generated before logging in and send
238-
// to the issuer, when the user is redirected for login.
282+
// Set the current state, which is usually generated and saved in the session before login,
283+
// and sent to the issuer during the login redirect.
239284
Session::put('openid_connect_state', 'some-state');
240285

241286
// We simulate here that the user now comes back after successful login at issuer.

0 commit comments

Comments
 (0)