@@ -142,6 +142,70 @@ public function testAuthenticateDoesNotThrowExceptionIfClaimsIsMissingNonce()
142
142
}
143
143
}
144
144
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
+
145
209
public function testSerialize ()
146
210
{
147
211
$ client = new OpenIDConnectClient ('https://example.com ' , 'foo ' , 'bar ' , 'baz ' );
0 commit comments