3131use GuzzleHttp \Client ;
3232use GuzzleHttp \Exception \ClientException ;
3333use GuzzleHttp \Exception \GuzzleException ;
34+ use GuzzleHttp \Exception \RequestException ;
3435use Illuminate \Contracts \Foundation \Application ;
3536use Illuminate \Contracts \View \Factory ;
3637use Illuminate \Http \JsonResponse ;
3738use Illuminate \Http \RedirectResponse ;
3839use Illuminate \Http \Request ;
3940use Illuminate \Routing \Redirector ;
4041use Illuminate \View \View ;
41- use InvalidArgumentException ;
4242use JsonException ;
4343use Str ;
4444use Throwable ;
@@ -61,17 +61,18 @@ class TokenController extends Controller
6161 public function callback (Request $ request )
6262 {
6363 app ('log ' )->debug (sprintf ('Now at %s ' , __METHOD__ ));
64- $ state = (string ) $ request -> session ()->pull ('state ' );
64+ $ state = (string ) session ()->pull ('state ' );
6565 $ codeVerifier = (string ) $ request ->session ()->pull ('code_verifier ' );
6666 $ clientId = (int ) $ request ->session ()->pull ('form_client_id ' );
6767 $ baseURL = (string ) $ request ->session ()->pull ('form_base_url ' );
6868 $ vanityURL = (string ) $ request ->session ()->pull ('form_vanity_url ' );
6969 $ code = $ request ->get ('code ' );
7070
71- throw_unless (
72- strlen ($ state ) > 0 && $ state === $ request ->state ,
73- InvalidArgumentException::class
74- );
71+ if (0 === strlen ($ state ) || $ state !== $ request ->state ) {
72+ app ('log ' )->error (sprintf ('State according to session: "%s" ' , $ state ));
73+ app ('log ' )->error (sprintf ('State returned in request : "%s" ' , $ request ->state ));
74+ throw new ImporterErrorException ('The "state" returned from your server doesn \'t match the state that was sent. ' );
75+ }
7576 // always POST to the base URL, never the vanity URL.
7677 $ finalURL = sprintf ('%s/oauth/token ' , $ baseURL );
7778 $ params = [
@@ -93,11 +94,14 @@ public function callback(Request $request)
9394 ];
9495 try {
9596 $ response = (new Client ($ opts ))->post ($ finalURL , $ params );
96- } catch (ClientException $ e ) {
97- $ body = (string ) $ e ->getResponse ()->getBody ();
98- app ('log ' )->error (sprintf ('Client exception when decoding response: %s ' , $ e ->getMessage ()));
99- app ('log ' )->error (sprintf ('Response from server: "%s" ' , $ body ));
100- app ('log ' )->error ($ e ->getTraceAsString ());
97+ } catch (ClientException | RequestException $ e ) {
98+ $ body = $ e ->getMessage ();
99+ if ($ e ->hasResponse ()) {
100+ $ body = (string ) $ e ->getResponse ()->getBody ();
101+ app ('log ' )->error (sprintf ('Client exception when decoding response: %s ' , $ e ->getMessage ()));
102+ app ('log ' )->error (sprintf ('Response from server: "%s" ' , $ body ));
103+ app ('log ' )->error ($ e ->getTraceAsString ());
104+ }
101105
102106 return view ('error ' )->with ('message ' , $ e ->getMessage ())->with ('body ' , $ body );
103107 }
0 commit comments