public function validateState(HTTPRequest $request)
    {
        $state = $request->getVar('state');
        $session = $request->getSession();
        $data = $session->get('oauth2');

        // If we're lacking any required data, or the session state doesn't match
        // the one the provider returned, the request is invalid
        if (empty($data['state']) || empty($data['provider']) || empty($data['scope']) || $state !== $data['state']) {
            $session->clear('oauth2');
            return false;
        }

        return true;
    }