/**
     * @param bool $need_refresh_token
     *
     * @return array|null
     * @throws \OAuth\OAuth2\Service\Exception\MissingRefreshTokenException
     */
    protected function oauth_login($need_refresh_token = false)
    {
        $settings = $this->get_plugin_settings();
        if (!$settings) {
            $settings = $this->get_current_settings();
        }
        $redirect_url = get_admin_url(null, 'admin.php?page=gf_settings&subview=gravityformsbitrix24');
        /** @var $serviceFactory \OAuth\ServiceFactory An OAuth service factory. */
        $serviceFactory = new \OAuth\ServiceFactory();
        // Session storage
        $storage = new \OAuth\Common\Storage\Memory();
        // Setup the credentials for the requests
        $credentials = new \OAuth\Common\Consumer\Credentials($settings['clientId'], $settings['clientSecret'], $redirect_url);
        /** @var $provider \OAuth\OAuth2\Service\Bitrix24 */
        $provider = $serviceFactory->createService('Bitrix24', $credentials, $storage, array('crm'), new \OAuth\Common\Http\Uri\Uri('https://' . $settings['domain']));
        if ($need_refresh_token) {
            $token = new OAuth\OAuth2\Token\StdOAuth2Token();
            $token->setAccessToken($settings['authId']);
            $token->setRefreshToken($settings['refreshId']);
            $token = $provider->refreshAccessToken($token);
            $settings['authId'] = $token->getAccessToken();
            $settings['refreshId'] = $token->getRefreshToken();
            $settings['endOfLife'] = $token->getEndOfLife();
            $this->update_plugin_settings($settings);
            self::$api = null;
            return $settings;
        }
        if (!isset($_GET['code']) && empty($settings['oauthCode'])) {
            echo '<div class="error" style="padding: 10px; font-size: 12px;">' . __('Please enter the code', 'gravityformsbitrix24') . '</div>';
            echo '<script>window.open("' . $provider->getAuthorizationUri() . '", "_blank", "width=300, height=200");
			</script>';
        } else {
            $code = isset($_GET['code']) ? $_GET['code'] : $settings['oauthCode'];
            try {
                $token = $provider->requestAccessToken($code);
                $extra_params = $token->getExtraParams();
                $settings['memberId'] = $extra_params['member_id'];
                $settings['authId'] = $token->getAccessToken();
                $settings['refreshId'] = $token->getRefreshToken();
                $settings['endOfLife'] = $token->getEndOfLife();
                unset($settings['oauthCode']);
                $this->update_plugin_settings($settings);
                if (isset($_GET['code'])) {
                    wp_redirect($redirect_url);
                }
            } catch (\Exception $ex) {
                $markup = '<div class="error" style="padding: 10px; font-size: 12px;">' . __('Oauth code is not right.', 'gravityformsbitrix24') . '</div>';
                echo $markup;
            }
        }
    }
Esempio n. 2
0
 private function _getUser()
 {
     $fb = OAuth::consumer('Facebook');
     if (Cookie::get('uid')) {
         $accessToken = $this->_getAccessTokenByUser(Cookie::get('uid'));
         if ($accessToken) {
             $token = new OAuth\OAuth2\Token\StdOAuth2Token();
             $token->setAccessToken($accessToken);
             $token->setEndOfLife(time());
             $fb->getStorage()->storeAccessToken('Facebook', $token);
             $user = json_decode($fb->request('/me'), true);
             $user['access_token'] = $accessToken;
             return $user;
         }
     }
     return false;
 }
Esempio n. 3
0
 /**
  * {@inheritdoc}
  */
 public function setAccessToken(AccessTokenInterface $accessToken)
 {
     $this->accessToken = $accessToken;
     // Get OAuth libray Service instance
     $libService = $this->getLibService();
     // Create and store Oauth library token using access token
     $libStorage = $libService->getStorage();
     if ($libService::OAUTH_VERSION == 1) {
         $libToken = new \OAuth\OAuth1\Token\StdOAuth1Token();
         $libToken->setRequestToken($accessToken->getToken());
         $libToken->setRequestTokenSecret($accessToken->getSecret());
         $libToken->setAccessTokenSecret($accessToken->getSecret());
     } else {
         $libToken = new \OAuth\OAuth2\Token\StdOAuth2Token();
     }
     $libToken->setAccessToken($accessToken->getToken());
     $libStorage->storeAccessToken($libService->service(), $libToken);
 }