5
5
namespace MinVWS \OpenIDConnectLaravel \Tests \Feature \Http \Controllers ;
6
6
7
7
use Illuminate \Http \Client \Request ;
8
+ use Illuminate \Http \Response ;
8
9
use Illuminate \Support \Facades \Config ;
9
10
use Illuminate \Support \Facades \Http ;
10
11
use Illuminate \Support \Facades \Session ;
11
12
use Illuminate \Testing \TestResponse ;
13
+ use Jumbojett \OpenIDConnectClientException ;
12
14
use MinVWS \OpenIDConnectLaravel \OpenIDConfiguration \OpenIDConfiguration ;
13
15
use MinVWS \OpenIDConnectLaravel \OpenIDConfiguration \OpenIDConfigurationLoader ;
16
+ use MinVWS \OpenIDConnectLaravel \Services \ExceptionHandlerInterface ;
14
17
use MinVWS \OpenIDConnectLaravel \Tests \TestCase ;
15
18
use Mockery ;
16
19
@@ -129,11 +132,53 @@ public function codeChallengeMethodProvider(): array
129
132
];
130
133
}
131
134
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
133
177
{
134
178
$ idToken = generateJwt ([
135
179
"iss " => "https://provider.rdobeheer.nl " ,
136
180
"aud " => 'test-client-id ' ,
181
+ "sub " => 'test-subject ' ,
137
182
], 'the-secret-client-secret ' );
138
183
139
184
Http::fake ([
@@ -146,7 +191,7 @@ public function testTokenSignedWithClientSecret(): void
146
191
]),
147
192
// User info requested by OpenIDConnectClient::requestUserInfo() function.
148
193
'https://provider.rdobeheer.nl/userinfo?schema=openid ' => Http::response ([
149
- 'email ' => 'teste @rdobeheer.nl ' ,
194
+ 'email ' => 'tester @rdobeheer.nl ' ,
150
195
]),
151
196
]);
152
197
@@ -157,16 +202,16 @@ public function testTokenSignedWithClientSecret(): void
157
202
Config::set ('oidc.client_id ' , 'test-client-id ' );
158
203
Config::set ('oidc.client_secret ' , 'the-secret-client-secret ' );
159
204
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 .
162
207
Session::put ('openid_connect_state ' , 'some-state ' );
163
208
164
209
// We simulate here that the user now comes back after successful login at issuer.
165
210
$ response = $ this ->getRoute ('oidc.login ' , ['code ' => 'some-code ' , 'state ' => 'some-state ' ]);
166
211
$ response ->assertStatus (200 );
167
212
$ response ->assertJson ([
168
213
'userInfo ' => [
169
- 'email ' => 'teste @rdobeheer.nl ' ,
214
+ 'email ' => 'tester @rdobeheer.nl ' ,
170
215
]
171
216
]);
172
217
@@ -234,8 +279,8 @@ public function testTokenSignedWithPrivateKey(): void
234
279
[$ key , $ keyResource ] = generateOpenSSLKey ();
235
280
Config::set ('oidc.client_authentication.signing_private_key_path ' , stream_get_meta_data ($ keyResource )['uri ' ]);
236
281
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 .
239
284
Session::put ('openid_connect_state ' , 'some-state ' );
240
285
241
286
// We simulate here that the user now comes back after successful login at issuer.
0 commit comments