Laravel Socialite: InvalidStateException

后端 未结 25 1173
感动是毒
感动是毒 2020-11-27 13:56

I\'m using Laravel Socialite to add a Facebook connect button on a website. Sometimes, I\'ve got this error on callback:

exception \'Laravel\\Socialite\\Two\         


        
25条回答
  •  隐瞒了意图╮
    2020-11-27 14:32

    tl;dr

    If you need to read a given parameter state returned by a thirdparty service, you can set Socialite to avoid this checking with the stateless method:

       Socialite::driver($provider)->stateless();
    

    I think Socialite is already prepared to avoid this issue.

    https://github.com/laravel/socialite/blob/2.0/src/Two/AbstractProvider.php#L77

     /**
     * Indicates if the session state should be utilized.
     *
     * @var bool
     */
    protected $stateless = false;
    

    https://github.com/laravel/socialite/blob/2.0/src/Two/AbstractProvider.php#L374

    /**
     * Indicates that the provider should operate as stateless.
     *
     * @return $this
     */
    public function stateless()
    {
        $this->stateless = true;
        return $this;
    }
    

    https://github.com/laravel/socialite/blob/2.0/src/Two/AbstractProvider.php#L222

    /**
     * Determine if the current request / session has a mismatching "state".
     *
     * @return bool
     */
    protected function hasInvalidState()
    {
        if ($this->isStateless()) {
            return false; // <--------
        }
        $state = $this->request->getSession()->pull('state');
        return ! (strlen($state) > 0 && $this->request->input('state') === $state);
    }
    

    For instance, state is very useful to pass data throught google:

    Parameter: state (Any string)
    Provides any state that might be useful to your application upon receipt of the response. The Google Authorization Server round-trips this parameter, so your application receives the same value it sent. Possible uses include redirecting the user to the correct resource in your site, and cross-site-request-forgery mitigations.

    ref: https://developers.google.com/identity/protocols/OAuth2UserAgent#overview

提交回复
热议问题