/** * Constructor. * * @param Grav $grav Grav instance * @param string $action The name of the action * @param array $post An array of values passed to the action */ public function __construct(Grav $grav, $action, $post = null) { parent::__construct($grav, ucfirst($action), $post); // Session storage $this->storage = new Session(false, 'oauth_token', 'oauth_state'); /** @var $serviceFactory \OAuth\ServiceFactory */ $this->factory = new ServiceFactory(); // Use curl client instead of fopen stream if (extension_loaded('curl')) { $this->factory->setHttpClient(new CurlCLient()); } }
public static function create(string $service) : AbstractProvider { // persistent storage to save the token $storage = new SessionStorage(); // Setup the credentials for the requests $credentials = new Credentials(DI::config()->get('socials/' . $service . '/key'), DI::config()->get('socials/' . $service . '/secret'), self::uri('oauth/end', ['service' => $service])->get(false)); // Oauth Service $serviceFactory = new ServiceFactory(); $serviceFactory->setHttpClient(new HttpClient()); $class = 'Cawa\\Oauth\\Providers\\' . ucfirst($service); /** @var AbstractProvider $provider */ $provider = new $class(); $provider->type = $service; // scope $scopes = DI::config()->getIfExists('socials/' . $service . '/scopes'); if (!$scopes && defined($class . '::DEFAULT_SCOPES')) { $scopes = constant($class . '::DEFAULT_SCOPES'); } if (!$scopes) { $scopes = []; } // version $version = null; if (defined($class . '::API_VERSION')) { $version = constant($class . '::API_VERSION'); } $service = $serviceFactory->createService($service, $credentials, $storage, $scopes, null, $version); $provider->service = $service; return $provider; }
/** * Set the HTTP client. * * @param string $client */ public function setHttpClient($client) { if (!in_array($client, ['StreamClient', 'CurlClient'])) { throw new \InvalidArgumentException('Invalid HTTP client'); } $class = '\\OAuth\\Common\\Http\\Client\\' . $client; $this->factory->setHttpClient(new $class()); }
/** * Constructor * * @param $url */ public function __construct($url) { $this->hlp = plugin_load('helper', 'oauth'); $credentials = new Credentials($this->hlp->getKey($this->getAdapterName()), $this->hlp->getSecret($this->getAdapterName()), $url); $this->storage = new oAuthStorage(); $serviceFactory = new ServiceFactory(); $serviceFactory->setHttpClient(new oAuthHTTPClient()); $this->oAuth = $serviceFactory->createService($this->getServiceName(), $credentials, $this->storage, $this->getScope()); }
/** * Set the HTTP Client * * @param string|ClientInterface Client name as string without `Client`, or object * @throws Exception If the object provided does not implement the ClientInterface */ public function setHttpClient($client) { if (null === $client) { return; } elseif (is_string($client)) { $client_class = "\\OAuth\\Common\\Http\\Client\\{$client}Client"; $client = new $client_class(); } elseif (!in_array('ClientInterface', class_implements($client))) { throw new Exception("HTTP Client must implement ClientInterface", 1); } $this->serviceFactory->setHttpClient($client); }
public function register() { $this->app->bind('oauth/factory/service', function ($app, $params = array()) { $factory = new ServiceFactory(); $factory->setHttpClient($client = new CurlClient()); $client->setCurlParameters((array) $params); return $factory; }); $this->app->bindShared('oauth/factory/extractor', function () { return new ExtractorFactory(); }); $this->app->bind('oauth_extractor', function ($app, $params = array()) { if (!is_array($params)) { $params = array($params); } if (!($service = head($params))) { throw new \InvalidArgumentException('No Service given.'); } $extractor_factory = $app->make('oauth/factory/extractor'); return $extractor_factory->get($service); }); \Route::register('/ccm/system/authentication/oauth2/{type}/{action}', function ($type, $action) { try { $type = \AuthenticationType::getByHandle($type); if ($type && is_object($type) && !$type->isError()) { /** @var GenericOauthTypeController $controller */ $controller = $type->getController(); if ($controller instanceof GenericOauthTypeController) { switch ($action) { case 'attempt_auth': return $controller->handle_authentication_attempt(); break; case 'callback': return $controller->handle_authentication_callback(); break; case 'attempt_attach': return $controller->handle_attach_attempt(); break; case 'attach_callback': return $controller->handle_attach_callback(); break; } } } } catch (\Exception $e) { \Log::addNotice('OAUTH ERROR: ' . $e->getMessage()); } }); }
protected function init($callbackUrl = null) { if (!$this->init) { if (!$this->storage) { $this->storage = new Session(); } $uriFactory = new UriFactory(); $this->currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER); $credentials = new Credentials($this->apiKey, $this->apiSecret, $callbackUrl ? $callbackUrl : $this->currentUri->getAbsoluteUri()); $serviceFactory = new ServiceFactory(); $serviceFactory->setHttpClient(new CurlClient()); $serviceFactory->registerService('DoYouBuzz', 'DoYouBuzz\\ApiHelper\\DoYouBuzzService'); $this->service = $serviceFactory->createService('DoYouBuzz', $credentials, $this->storage); $this->init = true; } }
/** * Creates a Connector object for the given provider type. The $id parameter * may be null but should only be used like that when getting * * @param string $provider The provider type you want. * @param TokenStorageInterface $storage The storage for PHPoAuthLib. * @param array $options * @param string $id The ID we're connecting to. * @param null|ClientInterface $http_client The HTTP client for PHPoAuthLib. * @param null|ServiceFactory $service_factory The PHPoAuthLib service factory. * @param null|CredentialsInterface $credentials The credentials for PHPoAuthLib. * @return ConnectorInterface An instance of the requested connector type. * @throws InvalidProviderException */ public function createConnector($provider, TokenStorageInterface $storage, array $options = [], $id = null, ClientInterface $http_client = null, ServiceFactory $service_factory = null, CredentialsInterface $credentials = null) { // Only allow configured providers. if (!array_key_exists($provider, $this->config['providers'])) { throw new InvalidProviderException($provider); } // Default to CurlClient (why isn't this the default? :( ) if (is_null($http_client)) { $http_client = new CurlClient(); } // Just if we want to be lazy and not pass this as an argument. if (is_null($service_factory)) { $service_factory = new ServiceFactory(); } // Simplify config access for this provider. $config = $this->getFlatConfig($provider); // We're already getting the credentials via $this->config, we might not // want to always pass them as an argument. if (is_null($credentials)) { $credentials = new Credentials($config['consumer_key'], $config['consumer_secret'], $config['callback']); } // Let's make use of CurlClient. $service_factory->setHttpClient($http_client); // Temporary (or so I hope) hack to overcome PHPoAuthLib not being ready // for Facebook's Graph API 1.0 deprecation. // If this is Facebook, let's specify we want API v2.2 // $api_version = null; // if (strtolower($provider) == 'facebook') { // $api_version = '2.2'; // } $uri = null; if (in_array($provider, ['Facebook', 'FacebookGroup', 'FacebookPage'])) { $uri = new Uri('https://graph.facebook.com/v2.2/'); } // Finally, create the service already! $service = $service_factory->createService($config['service'], $credentials, $storage, $config['scopes'], $uri); $connector_class = '\\Borfast\\Socializr\\Connectors\\' . $provider; $connector = new $connector_class($config, $service, $options, $id); return $connector; }
/** * Set the http client object * * @param string $httpClientName * @return void */ public function setHttpClient($httpClientName) { $httpClientClass = '\\OAuth\\Common\\Http\\Client\\$httpClientName'; $this->_serviceFactory->setHttpClient(new $httpClientClass()); }
/** * Get Withings service * * @access protected * @throws \Withings\Exception * @return \OAuth\OAuth1\Service\ServiceInterface */ protected function getService() { if (!$this->consumerKey) { throw new Exception('Empty consumer key.'); } if (!$this->consumerSecret) { throw new Exception('Empty consumer secret.'); } if (!$this->callbackURL) { throw new Exception('Empty callback URL.'); } if (!$this->storageAdapter) { throw new Exception('Missing storage adapter.'); } if (!$this->service) { $credentials = new Credentials($this->consumerKey, $this->consumerSecret, $this->callbackURL); $factory = new ServiceFactory(); if ($this->httpClient) { $factory->setHttpClient($this->httpClient); } $this->service = $factory->createService('Withings', $credentials, $this->storageAdapter, array(), null, $this->proxy); } return $this->service; }
public function register(Container $app) { $app['oauth.login_route'] = '_auth_service'; $app['oauth.callback_route'] = '_auth_service_callback'; $app['oauth.check_route'] = '_auth_service_check'; $app['oauth.register_routes'] = true; $app['oauth.services'] = array(); $app['oauth.http_client'] = function ($app) { return new CurlClient(); }; $app['oauth.factory'] = function ($app) { $factory = new ServiceFactory(); $factory->setHttpClient($app['oauth.http_client']); return $factory; }; $app['oauth.storage'] = function ($app) { return new SymfonySession($app['session']); }; $app['oauth.url_generator'] = function ($app) { return $app['url_generator']; }; $app['oauth.csrf_token'] = $app->protect(function ($intention) use($app) { if (!isset($app['form.csrf_provider'])) { return null; } $tokenManagerInterface = 'Symfony\\Component\\Security\\Csrf\\CsrfTokenManagerInterface'; if ($app['form.csrf_provider'] instanceof $tokenManagerInterface) { return strval($app['form.csrf_provider']->getToken($intention)); } return $app['form.csrf_provider']->generateCsrfToken($intention); }); $app['oauth'] = function ($app) { return new OAuthServiceRegistry($app['oauth.factory'], $app['oauth.storage'], $app['oauth.url_generator'], $app['oauth.services'], array('callback_route' => $app['oauth.callback_route'])); }; $app['oauth.login_paths'] = function ($app) { $services = array_map('strtolower', array_keys($app['oauth.services'])); $token = $app['oauth.csrf_token']('oauth'); return array_map(function ($service) use($app, $token) { return $app['url_generator']->generate($app['oauth.login_route'], array('service' => $service, '_csrf_token' => $token)); }, array_combine($services, $services)); }; $app['oauth.user_info_listener'] = function ($app) { return new UserInfoListener($app['oauth'], $app['oauth.services']); }; $app['oauth.user_provider_listener'] = function ($app) { return new UserProviderListener(); }; $app['security.authentication_listener.factory.oauth'] = $app->protect(function ($name, $options) use($app) { if (!isset($app['security.authentication_listener.' . $name . '.oauth'])) { $app['security.authentication_listener.' . $name . '.oauth'] = $app['security.authentication_listener.oauth._proto']($name, $options); } if (!isset($app['security.authentication_provider.' . $name . '.oauth'])) { $app['security.authentication_provider.' . $name . '.oauth'] = $app['security.authentication_provider.oauth._proto']($name); } return array('security.authentication_provider.' . $name . '.oauth', 'security.authentication_listener.' . $name . '.oauth', null, 'pre_auth'); }); $app['security.authentication_listener.oauth._proto'] = $app->protect(function ($name, $options) use($app) { return function () use($app, $name, $options) { $options['login_route'] = $app['oauth.login_route']; $options['callback_route'] = $app['oauth.callback_route']; $options['check_route'] = $app['oauth.check_route']; if ($app['oauth.register_routes']) { $app->match(isset($options['login_path']) ? $options['login_path'] : '/auth/{service}', function () { })->bind($options['login_route']); $app->get(isset($options['callback_path']) ? $options['callback_path'] : '/auth/{service}/callback', function () { })->bind($options['callback_route']); $app->get(isset($options['check_path']) ? $options['check_path'] : '/auth/{service}/check', function () { })->bind($options['check_route']); } if (!isset($app['security.authentication.success_handler.' . $name . '.oauth'])) { $app['security.authentication.success_handler.' . $name . '.oauth'] = $app['security.authentication.success_handler._proto']($name, $options); } if (!isset($app['security.authentication.failure_handler.' . $name . '.oauth'])) { $app['security.authentication.failure_handler.' . $name . '.oauth'] = $app['security.authentication.failure_handler._proto']($name, $options); } return new OAuthAuthenticationListener($app['security.token_storage'], $app['security.authentication_manager'], $app['security.session_strategy'], $app['security.http_utils'], $name, $app['oauth'], $app['security.authentication.success_handler.' . $name . '.oauth'], $app['security.authentication.failure_handler.' . $name . '.oauth'], $options, $app['logger'], $app['dispatcher'], isset($options['with_csrf']) && $options['with_csrf'] && isset($app['form.csrf_provider']) ? $app['form.csrf_provider'] : null); }; }); $app['security.authentication_provider.oauth._proto'] = $app->protect(function ($name) use($app) { return function () use($app, $name) { return new OAuthAuthenticationProvider($app['security.user_provider.' . $name], $app['security.user_checker'], $name, $app['dispatcher']); }; }); }
/** * @covers OAuth\ServiceFactory::setHttpClient */ public function testSetHttpClient() { $factory = new ServiceFactory(); $this->assertInstanceOf('\\OAuth\\ServiceFactory', $factory->setHttpClient($this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'))); }