OAuth2Client::authenticate PHP Method

authenticate() public method

public authenticate ( $code )
    public function authenticate($code)
    {
        $params = array("client_id" => $this->client_id, "client_secret" => $this->client_secret, "grant_type" => "authorization_code", "redirect_uri" => $this->redirect_uri, "code" => $code);
        $response = $this->request($this->token_url, $params, $this->curl_authenticate_method);
        $response = $this->parseRequestResult($response);
        if (!$response || !isset($response->access_token)) {
            throw new Exception("The Authorization Service has return: " . $response->error);
        }
        if (isset($response->access_token)) {
            $this->access_token = $response->access_token;
        }
        if (isset($response->refresh_token)) {
            $this->refresh_token = $response->refresh_token;
        }
        if (isset($response->expires_in)) {
            $this->access_token_expires_in = $response->expires_in;
        }
        // calculate when the access token expire
        if (isset($response->expires_in)) {
            $this->access_token_expires_at = time() + $response->expires_in;
        }
        return $response;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * {@inheritdoc}
  */
 function loginFinish()
 {
     $error = array_key_exists('error', $_REQUEST) ? $_REQUEST['error'] : "";
     // check for errors
     if ($error) {
         throw new Exception("Authentication failed! {$this->providerId} returned an error: {$error}", 5);
     }
     // try to authenticate user
     $code = array_key_exists('code', $_REQUEST) ? $_REQUEST['code'] : "";
     try {
         $this->api->authenticate($code);
     } catch (Exception $e) {
         throw new Exception("User profile request failed! {$this->providerId} returned an error: {$e}", 6);
     }
     // check if authenticated
     if (!$this->api->access_token) {
         throw new Exception("Authentication failed! {$this->providerId} returned an invalid access token.", 5);
     }
     // store tokens
     $this->token("access_token", $this->api->access_token);
     $this->token("refresh_token", $this->api->refresh_token);
     $this->token("expires_in", $this->api->access_token_expires_in);
     $this->token("expires_at", $this->api->access_token_expires_at);
     // set user connected locally
     $this->setUserConnected();
 }