/**
  * Performs an OAuth authentication
  */
 public function execute()
 {
     /** @var \Grav\Common\Language\Language */
     $t = $this->grav['language'];
     $provider = strtolower($this->action);
     $config = $this->grav['config']->get('plugins.login.oauth.providers.' . $this->action, []);
     if (isset($config['credentials'])) {
         // Setup the credentials for the requests
         $credentials = new Credentials($config['credentials']['key'], $config['credentials']['secret'], $this->grav['uri']->url(true));
         // Instantiate service using the credentials, http client
         // and storage mechanism for the token
         $scope = isset($this->scopes[$provider]) ? $this->scopes[$provider] : [];
         $this->service = $this->factory->createService($this->action, $credentials, $this->storage, $scope);
     }
     if (!$this->service || empty($config)) {
         $this->setMessage($t->translate(['LOGIN_PLUGIN.OAUTH_PROVIDER_NOT_SUPPORTED', $this->action]));
         return true;
     }
     // Check OAuth authentication status
     $authenticated = parent::execute();
     if (is_bool($authenticated)) {
         $this->reset();
         if ($authenticated) {
             $this->setMessage($t->translate('LOGIN_PLUGIN.LOGIN_SUCCESSFUL'));
         } else {
             $this->setMessage($t->translate('LOGIN_PLUGIN.ACCESS_DENIED'));
         }
         // Redirect to current URI
         $referrer = $this->grav['uri']->url(true);
         $this->setRedirect($referrer);
     } elseif (!$this->grav['session']->oauth) {
         $this->setMessage($t->translate(['LOGIN_PLUGIN.OAUTH_PROVIDER_NOT_SUPPORTED', $this->action]));
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Return an instance of the requested service.
  *
  * @param string $provider
  * @param string $url
  * @param array  $scope
  * 
  * @return \OAuth\Common\Service\AbstractService
  * @throws \Exception
  */
 public function service($provider, $url = null, $scope = null)
 {
     $info = Config::get('laravel-social::providers.' . strtolower($provider));
     if (empty($info) || !is_array($info)) {
         throw new Exception('Missing configuration details for Social service: ' . $provider);
     }
     $client_id = array_get($info, 'client_id');
     $client_secret = array_get($info, 'client_secret');
     $scope = is_null($scope) ? array_get($info, 'scope') : $scope;
     if (empty($client_id) || empty($client_secret)) {
         throw new Exception('Missing client id/secret for Social service: ' . $provider);
     }
     return $this->factory->createService(ucfirst($provider), new Credentials($client_id, $client_secret, $url ?: URL::current()), $this->storage, $scope);
 }
Ejemplo n.º 3
0
 /**
  * @param Application $app
  * @param Request $request
  * @param string $service
  * 
  * @return string
  */
 public function auth(Application $app, Request $request, $service)
 {
     // Construir los servicios
     $serviceFactory = new ServiceFactory();
     // Almacenamiento en session
     $storage = new Session();
     // Obtener credenciales
     $servicesCredentials = SocialsCredentials::getArrayCredentials();
     $url = $app['url_generator']->generate('auth_service', array('service' => $service), UrlGenerator::ABSOLUTE_URL);
     if ($service == 'facebook') {
         // Credenciales de facebook
         $facebookCredentials = new Credentials($servicesCredentials['facebook']['key'], $servicesCredentials['facebook']['secret'], $url);
         // Servicio de auth de facebook
         $serviceAuth = $serviceFactory->createService('facebook', $facebookCredentials, $storage, array());
     } else {
         if ($service == 'google') {
             // Credenciales de google
             $googleCredentials = new Credentials($servicesCredentials['google']['key'], $servicesCredentials['google']['secret'], $url);
             // Servicio de auth de google
             $serviceAuth = $serviceFactory->createService('google', $googleCredentials, $storage, array('userinfo_email', 'userinfo_profile'));
         }
     }
     $menuModelo = new Menu($app['db']);
     $menuItems = $menuModelo->getItems();
     if ($request->get('code') && $service == 'facebook') {
         try {
             // Callback de facebook para obtener el access token
             $serviceAuth->requestAccessToken($request->get('code'));
         } catch (TokenResponseException $ex) {
         }
         // Se adquiere la informacion del usuario
         $usuario = json_decode($serviceAuth->request('/me'), true);
         $usuario['service'] = 'facebook';
         return $app['twig']->render('frontend/usuario/signup.html.twig', array('menu_items' => $menuItems, 'usuario' => $usuario));
     } else {
         if ($request->get('code') && $service == 'google') {
             try {
                 // Callback de google para obtener el access token
                 $serviceAuth->requestAccessToken($request->get('code'));
             } catch (TokenResponseException $ex) {
             }
             // Se adquiere la informacion del usuario
             $usuario = json_decode($serviceAuth->request('https://www.googleapis.com/oauth2/v1/userinfo'), true);
             $usuario['service'] = 'google';
             return $app['twig']->render('frontend/usuario/signup.html.twig', array('menu_items' => $menuItems, 'usuario' => $usuario));
         } else {
             return "<script> location.href = '{$serviceAuth->getAuthorizationUri()}'; </script>";
         }
     }
 }
Ejemplo n.º 4
0
 /**
  * @param $resourceOwnerName
  * @return \OAuth\Common\Service\ServiceInterface
  * @throws Exception
  */
 public function createService($resourceOwnerName)
 {
     if (!isset(ResourceOwners::$all[$resourceOwnerName])) {
         throw new Exception('Resource owner ' . $resourceOwnerName . ' is not available');
     }
     if (isset($this->serviceCache[$resourceOwnerName])) {
         return $this->serviceCache[$resourceOwnerName];
     }
     $lowerResourceOwnerName = $string = strtolower(preg_replace('/(?<=\\w)(?=[A-Z])/', "_\$1", $resourceOwnerName));
     $paramName = 'apinnecke_oauth.resource_owners.' . $lowerResourceOwnerName . '.callback_url';
     if (!$this->container->hasParameter($paramName) || !($callbackUrl = $this->container->getParameter($paramName)) || null === $callbackUrl) {
         $callbackUrl = $this->container->get('router')->getContext()->getBaseUrl();
     }
     $credentials = new Credentials($this->container->getParameter('apinnecke_oauth.resource_owners.' . $lowerResourceOwnerName . '.client_id'), $this->container->getParameter('apinnecke_oauth.resource_owners.' . $lowerResourceOwnerName . '.client_secret'), $callbackUrl);
     return $this->serviceCache[$resourceOwnerName] = $this->factory->createService($resourceOwnerName, $credentials, $this->storage);
 }
Ejemplo n.º 5
0
 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;
 }
 /**
  * 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());
 }
Ejemplo n.º 7
0
 protected function buildDefaultAuthenticator($clientID, $clientSecret, $callbackUrl)
 {
     $credentials = new Credentials($clientID, $clientSecret, $callbackUrl);
     $facotry = new ServiceFactory();
     $session = new Session();
     $service = $facotry->createService('wunderlist', $credentials, $session);
     $request = Request::createFromGlobals();
     return new OAuthLibAuthentication($service, $request);
 }
Ejemplo n.º 8
0
 /**
  * Get the GitHub service instance
  *
  * @access public
  * @return \OAuth\OAuth2\Service\GitHub
  */
 public function getService()
 {
     $uriFactory = new UriFactory();
     $currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
     $currentUri->setQuery('controller=user&action=gitHub');
     $storage = new Session(false);
     $credentials = new Credentials(GITHUB_CLIENT_ID, GITHUB_CLIENT_SECRET, $currentUri->getAbsoluteUri());
     $serviceFactory = new ServiceFactory();
     return $serviceFactory->createService('gitHub', $credentials, $storage, array(''));
 }
Ejemplo n.º 9
0
 /**
  * Get the Google service instance
  *
  * @access public
  * @return \OAuth\OAuth2\Service\Google
  */
 public function getService()
 {
     $uriFactory = new UriFactory();
     $currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
     $currentUri->setQuery('controller=user&action=google');
     $storage = new Session(false);
     $credentials = new Credentials(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, $currentUri->getAbsoluteUri());
     $serviceFactory = new ServiceFactory();
     return $serviceFactory->createService('google', $credentials, $storage, array('userinfo_email', 'userinfo_profile'));
 }
Ejemplo n.º 10
0
 /**
  * Create an OAuth consumer.
  *
  * @param  string $service
  * @param  string $url
  * @param  array  $scope
  * @param  string $baseUri
  * @param  string $version
  * @return \OAuth\Common\Service\AbstractService
  */
 public function consumer($service, $url = null, $scope = null, $baseUri = null, $version = null)
 {
     // Get service configuration
     $config = Config::get('services.' . strtolower($service));
     // Create credentials object.
     $credentials = new Credentials(array_get($config, 'client_id'), array_get($config, 'client_secret'), $url ?: URL::current());
     // Get default scope.
     if (is_null($scope)) {
         $scope = array_get($config, 'scope', []);
     }
     // Get default base uri.
     if (is_null($baseUri)) {
         $baseUri = array_get($config, 'baseUri');
     }
     // Get default api version.
     if (is_null($version)) {
         $version = array_get($config, 'version', '');
     }
     return $this->factory->createService($service, $credentials, $this->storage, $scope, $baseUri, $version);
 }
Ejemplo n.º 11
0
 /**
  * @param $id
  * @return \OAuth\OAuth2\Service\AbstractService
  */
 protected function getService($id)
 {
     $credential = $this->getCredentials($id);
     // Setup the credentials for the requests
     $credentials = new Credentials($credential['id'], $credential['secret'], $this->getRootUri() . $this->getCallbackPath($id));
     $serviceFactory = new ServiceFactory();
     $serviceFactory->registerService('IsseHom', '\\Paliari\\Oauth2ClientFacade\\IsseHom');
     $serviceFactory->registerService('IssePro', '\\Paliari\\Oauth2ClientFacade\\IssePro');
     $service = $serviceFactory->createService($id, $credentials, $this->getStorage(), $credential['scopes']);
     return $service;
 }
Ejemplo n.º 12
0
 /**
  * Return a provider for a specific OAuth service implementation
  *
  * @param   string          $provider       The name of the service
  * @param   string|null     $callbackUrl    The URL to be used for the credentials
  * @param   array|null      $scope          The scope of the access (OAuth2 only)
  * @return  \OAuth\Common\Service\AbstractService
  */
 public function provider($provider, $callbackUrl = null, array $scope = null, $storage = null)
 {
     $config = $this->getConfig($provider);
     if (null !== $scope) {
         $config['scope'] = $scope;
     } elseif (!isset($config['scope'])) {
         $config['scope'] = array();
     }
     $storage = $storage ?: $this->storage;
     $credentials = new Credentials($config['client_id'], $config['client_secret'], $this->makeCallbackUrl($callbackUrl, $config));
     return $this->serviceFactory->createService($provider, $credentials, $storage, $config['scope']);
 }
Ejemplo n.º 13
0
 public function init()
 {
     if (isset($this->service)) {
         return;
     }
     $serviceFactory = new ServiceFactory();
     // Session storage
     $storage = new Session();
     // Setup the credentials for the requests
     $credentials = new Credentials($this->clientId, $this->clientSecret, Url::modify($this->redirectUrl, Url::ABS));
     $this->service = $serviceFactory->createService('facebook', $credentials, $storage, $this->scopes);
 }
 /**
  * @param  string $service
  * @param  array $credentials
  * @param  string $url
  * @param  array $scope
  *
  * @return \OAuth\Common\Service\AbstractService
  */
 protected function consumer($service, $credentials = array(), $url = null, $scope = null)
 {
     if (!empty($this->oauth)) {
         return $this->oauth;
     }
     $scope = array_get($credentials, "scope", []);
     // get storage object
     $storage = $this->createStorageInstance($this->storageClass);
     // return the service consumer object
     $this->oauth = $this->serviceFactory->createService($service, $this->getCredentials($credentials, $url), $storage, $scope);
     return $this->oauth;
 }
Ejemplo n.º 15
0
 public function getContent()
 {
     $serviceFactory = new ServiceFactory();
     $storage = new OauThSession();
     $credentials = new Credentials(Config::get('google.key'), Config::get('google.secret'), URLSITE . 'google/auth');
     $googleService = $serviceFactory->createService('google', $credentials, $storage, array('userinfo_email', 'userinfo_profile'));
     $state = isset($_GET['state']) ? $_GET['state'] : null;
     // This was a callback request from google, get the token
     $googleService->requestAccessToken($_GET['code'], $state);
     // Send a request with it
     return json_decode($googleService->request('userinfo'), true);
 }
Ejemplo n.º 16
0
 public function init()
 {
     if (isset($this->service)) {
         return;
     }
     $serviceFactory = new ServiceFactory();
     // Session storage
     $storage = new Session(false);
     // Setup the credentials for the requests
     $credentials = new Credentials($this->clientId, $this->clientSecret, Url::modify($this->redirectUrl, Url::ABS));
     // Instantiate the Google service using the credentials, http client and storage mechanism for the token
     $this->service = $serviceFactory->createService('google', $credentials, $storage, $this->scopes);
 }
Ejemplo n.º 17
0
 function it_allows_for_dynamic_calls_to_providers_methods_with_the_given_decorator(Repository $config, ServiceFactory $serviceFactory)
 {
     $serviceFactory->createService('Foo', Argument::type(self::INTERFACE_CREDENTIALS), Argument::type(self::INTERFACE_STORAGE))->shouldBeCalledTimes(1)->willReturn(new Foo());
     $config->has('oauth.providers.Foo')->willReturn(true);
     $config->has('oauth.providers.Foo.class')->willReturn(false);
     $config->has('oauth.providers.Foo.decorators')->willReturn(true);
     $config->get('oauth.providers.Foo.decorators')->willReturn(['tests\\Pasadinhas\\LaravelOAuth\\Decorator1', 'tests\\Pasadinhas\\LaravelOAuth\\Decorator2']);
     $config->get('oauth.providers.Foo.consumer_key')->willReturn('foo');
     $config->get('oauth.providers.Foo.consumer_secret')->willReturn('bar');
     $config->get('oauth.providers.Foo.callback_url')->willReturn('http://baz.com/login');
     $foo = $this->make('Foo');
     $foo->testDynamicMethods1()->shouldReturn('1');
     $foo->testDynamicMethods2()->shouldReturn('2');
 }
Ejemplo n.º 18
0
 /**
  * Get the Flickr service.
  * @return AbstractService
  */
 public function getService()
 {
     //      if ( $this->flickrService instanceof ServiceInterface ) {
     //          return $this->flickrService;
     //      }
     $credentials = new Credentials($this->apiKey, $this->apiSecret, 'oob');
     $storage = new Memory();
     $accessToken = $this->getStoredCredentials();
     if ($accessToken instanceof TokenInterface) {
         $storage->storeAccessToken('Flickr', $accessToken);
     }
     $serviceFactory = new ServiceFactory();
     $this->flickrService = $serviceFactory->createService('Flickr', $credentials, $storage);
     return $this->flickrService;
 }
Ejemplo n.º 19
0
 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;
     }
 }
Ejemplo n.º 20
0
 /**
  * @param  string $service
  * @param  string $url
  * @param  array  $scope
  *
  * @return \OAuth\Common\Service\AbstractService
  */
 public function consumer($service, $url = null, $scope = null)
 {
     // get config
     $this->setConfig($service);
     // get storage object
     $storage = $this->createStorageInstance($this->_storage_name);
     // create credentials object
     $credentials = new Credentials($this->_client_id, $this->_client_secret, $url ?: URL::current());
     // check if scopes were provided
     if (is_null($scope)) {
         // get scope from config (default to empty array)
         $scope = $this->_scope;
     }
     // return the service consumer object
     return $this->_serviceFactory->createService($service, $credentials, $storage, $scope);
 }
Ejemplo n.º 21
0
 /**
  * Check the authentication. Will be called two times : first to request a token, and redirect
  * to the Twitter api url, then to authenticate the user.
  *
  * @param  [type] $request [description]
  * @param  array  $options [description]
  * @return [type]          [description]
  */
 public function check($request, array $options = array())
 {
     $here = new Uri($request->to('url'));
     $here->setQuery('');
     $credentials = new Credentials($this->_config['key'], $this->_config['secret'], $here->getAbsoluteUri());
     $serviceFactory = new ServiceFactory();
     $service = $serviceFactory->createService(static::NAME, $credentials, $this->_storage, $this->_config['scope']);
     if (empty($request->query['code'])) {
         $url = $service->getAuthorizationUri();
         header('Location: ' . $url);
         exit(0);
     } else {
         $service->requestAccessToken($request->query['code']);
         $result = json_decode($service->request(static::USER_INFO), true);
         return $result;
     }
 }
 /**
  * Create a service object given a ServiceFactory object
  *
  * @param \OAuth\ServiceFactory $factory
  * @return \OAuth\Common\Service\ServiceInterface
  */
 public function createService(ServiceFactory $factory)
 {
     $appId = $this->config->get('auth.community.appid');
     $appSecret = $this->config->get('auth.community.secret');
     // Get the callback url
     /** @var Url $callbackUrl */
     $callbackUrl = $this->urlResolver->resolve(['/ccm/system/authentication/oauth2/community/callback/']);
     if ($callbackUrl->getHost() == '') {
         $callbackUrl = $callbackUrl->setHost($this->request->getHost());
         $callbackUrl = $callbackUrl->setScheme($this->request->getScheme());
     }
     // Create a credential object with our ID, Secret, and callback url
     $credentials = new Credentials($appId, $appSecret, (string) $callbackUrl);
     // Create a new session storage object and pass it the active session
     $storage = new SymfonySession($this->session, false);
     // Create the service using the oauth service factory
     return $factory->createService('community', $credentials, $storage);
 }
Ejemplo n.º 23
0
 /**
  * Check the authentication. Will be called two times : first to request a token, and redirect
  * to the Twitter api url, then to authenticate the user.
  *
  * @param  [type] $request [description]
  * @param  array  $options [description]
  * @return [type]          [description]
  */
 public function check($request, array $options = array())
 {
     $credentials = new Credentials($this->_config['key'], $this->_config['secret'], $request->to('url'));
     $serviceFactory = new ServiceFactory();
     $service = $serviceFactory->createService('twitter', $credentials, $this->_storage);
     if (!empty($request->query['denied'])) {
         return false;
     } elseif (empty($request->query['oauth_token'])) {
         $token = $service->requestRequestToken();
         $url = $service->getAuthorizationUri(array('oauth_token' => $token->getRequestToken()));
         header('Location: ' . $url);
     } else {
         $token = $this->_storage->retrieveAccessToken('Twitter');
         $service->requestAccessToken($request->query['oauth_token'], $request->query['oauth_verifier'], $token->getRequestTokenSecret());
         $result = json_decode($service->request('account/verify_credentials.json'), true);
         return $result;
     }
 }
Ejemplo n.º 24
0
 /**
  * @param  string $service
  * @param  string $url
  * @param  array  $scope
  * @return \OAuth\Common\Service\AbstractService
  */
 public function consumer($service, $url = null, $scope = null, $uri = null, $client_id = null, $client_secret = null)
 {
     // get config
     $this->loadConfig($service);
     // create credentials object
     $credentials = new Credentials($client_id ?: $this->_client_id, $client_secret ?: $this->_client_secret, $url ?: URL::current());
     // check if scopes were provided
     if (is_null($scope)) {
         $scope = $this->_scope ? $this->_scope : array();
     }
     // check if uri were provided
     if (is_null($uri)) {
         $uri = $this->_uri ? $this->_uri : array();
     }
     $uri = new Uri($uri);
     // dd($uri);
     // return the service consumer object
     return $this->_serviceFactory->createService($service, $credentials, $this->_storage, $scope, $uri);
 }
Ejemplo n.º 25
0
 /**
  * @param  string $service
  * @param  string $url
  * @param  array  $scope
  * @return \OAuth\Common\Service\AbstractService
  */
 public function consumer($service, $baseUri = null, $user, $key = 'oauth_access_token', $stateKey = 'oauth_authorization_state', $url = null, $scope = null)
 {
     // get config
     $this->setConfig($service);
     // get storage object
     $storage = $this->createStorageInstance($this->_storage_name, $user, $key, $stateKey);
     // create credentials object
     $credentials = new Credentials($this->_client_id, $this->_client_secret, $url ?: URL::current());
     // check if scopes were provided
     if (is_null($scope)) {
         // get scope from config (default to empty array)
         $scope = $this->_scope;
     }
     if (!is_null($baseUri)) {
         $baseUri = new Uri($baseUri);
     }
     // return the service consumer object
     return $this->_serviceFactory->createService($service, $credentials, $storage, $scope, $baseUri);
 }
Ejemplo n.º 26
0
 /**
  * Create and configure OAuth service object.
  * 
  * @param array $ini_data
  * @param object $payload
  * @throws ErrorException
  */
 public function __construct($ini_data, $payload)
 {
     $payload = $this->parsePayload($payload);
     $this->environment = $payload->env;
     $this->ini_data = $ini_data;
     if (!isset($this->ini_data[$this->environment])) {
         throw new ErrorException("No configuration data found for this environment: {$this->environment}\n");
     }
     if (isset($payload->authorization_code)) {
         $this->authorization_code = $payload->authorization_code;
     }
     $service_factory = new \OAuth\ServiceFactory();
     $session_storage = new Session();
     // Iron.io callback URL.
     // That will be used to pass authorization code back to the worker.
     $webhook_url = 'https://worker-aws-us-east-1.iron.io/2/projects/' . $this->getIniData('project_id') . '/tasks/webhook?code_name=' . $this->getIniData('worker_name') . '&oauth=' . $this->getIniData('token') . '&env=' . $this->environment;
     // Setup credentials for OAuth requests.
     $credentials = new Credentials($this->getIniData('username'), $this->getIniData('password'), $webhook_url);
     $api_uri = new Uri\Uri($this->getIniData('api_uri'));
     $this->service = $service_factory->createService('IronIoOAuthService', $credentials, $session_storage, array(), $api_uri);
     $this->service->setAuthorizationEndpoint($this->getIniData('oauth_auth_endpoint'));
     $this->service->setAccessTokenEndpoint($this->getIniData('oauth_token_endpoint'));
 }
 /**
  * Retrieve a user by the given credentials.
  *
  * @param  array  $credentials
  * @return \Illuminate\Auth\UserInterface|null
  */
 public function retrieveByCredentials(array $credentials)
 {
     require_once base_path() . '/vendor/lusitanian/OAuth/bootstrap.php';
     $url = url('/');
     if (!empty($this->auth->oauthGoogleId) and !empty($this->auth->oauthGoogleSecret)) {
         // Setup the credentials for the requests
         $credentials = new Credentials($this->auth->oauthGoogleId, $this->auth->oauthGoogleSecret, url('user/login'));
         // Session storage
         $storage = new Session();
         // Instantiate the Google service using the credentials, http client and storage mechanism for the token
         $service = new ServiceFactory();
         $google = $service->createService('google', $credentials, $storage, array('userinfo_email', 'groups_provisioning'));
         // Google responded with a code
         if (Input::has('code')) {
             // This was a callback request from google, get the token
             $google->requestAccessToken(Input::get('code'));
             // Send a request with it
             $result = json_decode($google->request(Site::config('services')->googleUrlOAuth), TRUE);
             // Process user
             if (is_string($result['id']) and is_string($result['email']) and isset($result['verified_email'])) {
                 if ($result['verified_email']) {
                     // First we will add each credential element to the query as a where clause.
                     // Then we can execute the query and, if we found a user, return it in a
                     // Eloquent User "model" that will be utilized by the Guard instances.
                     $query = $this->createModel()->newQuery();
                     // We search by email and user type. A filter for type=oauth is added to avoid
                     // getting users created by other auth methods
                     $query->where('email', $result['email'])->where('type', 'oauth');
                     // If a user is not found, we need to create one automagically
                     // Thats why even if count is 0, we return a new model instance
                     $user = $query->count() > 0 ? $query->first() : $this->createModel();
                     // Determine if user is an admin
                     $googleAdmins = explode("\n", $this->auth->oauthGoogleAdmins);
                     $isAdmin = in_array($result['email'], $googleAdmins);
                     // We extract the username from the email address of the user
                     $parts = explode('@', $result['email']);
                     // Insert/Update user info
                     $user->username = $parts[0];
                     $user->password = '';
                     $user->salt = '';
                     $user->email = $result['email'];
                     $user->type = 'oauth';
                     $user->active = 1;
                     $user->admin = $isAdmin;
                     $user->save();
                     // Log the user in. We need to do it manually because we don't have an username
                     // that we can 'attempt' to log in.
                     Auth::login($user);
                     return $user;
                 }
             }
             App::abort(401);
             // Unauthorized
         } else {
             $url = $google->getAuthorizationUri()->getAbsoluteUri();
         }
     }
     App::after(function ($request, $response) use($url) {
         $response->headers->set('Location', $url);
     });
     return NULL;
 }
Ejemplo n.º 28
0
 /**
  * [service description]
  * @param  [type] $name [description]
  * @return [type]       [description]
  */
 protected function getService($name, $url)
 {
     $key = $this->config->get("social::{$name}.key");
     $secret = $this->config->get("social::{$name}.secret");
     $scopes = $this->config->get("social::{$name}.scopes");
     $credentials = new Credentials($key, $secret, $url);
     $storage = new Session();
     $factory = new ServiceFactory();
     return $factory->createService($name, $credentials, $storage, $scopes);
 }
Ejemplo n.º 29
0
 /**
  * Main factory
  *
  * @param string type
  * @param SessionInterface
  * @param array Scope
  * @return Social
  */
 public static function factory($provider = 'facebook', SessionInterface $store, $scopes = array())
 {
     // Create a new instance of the URI class with the current URI,
     // stripping the query string
     $uriFactory = new SocialUriFactory();
     $currentUri = $uriFactory->createFromSuperGlobalArray($_SERVER);
     $currentUri->setQuery('');
     $servicesCredentials = Config::get('auth.social');
     $serviceFactory = new SocialServiceFactory();
     // Session storage
     $storage = new SocialSession($store, false, Config::get('session.cookie'));
     // Setup the credentials for the requests
     $credentials = new SocialCredentials($servicesCredentials[$provider]['key'], $servicesCredentials[$provider]['secret'], $currentUri->getAbsoluteUri());
     $selectedService = $serviceFactory->createService($provider, $credentials, $storage, $scopes);
     return new static($selectedService, $currentUri, $storage, $credentials);
 }
Ejemplo n.º 30
0
 private function getServiceObject()
 {
     $oUrl = new Uri(BX_DOL_URL_STUDIO . 'store.php?page=purchases');
     $oCredentials = new Credentials($this->sKey, $this->sSecret, $oUrl->getAbsoluteUri());
     $oServiceFactory = new ServiceFactory();
     return $oServiceFactory->createService($this->sService, $oCredentials, $this->oStorage);
 }