Skip to content

Commit

Permalink
Fixed SocialiteWasCalled event
Browse files Browse the repository at this point in the history
  • Loading branch information
antonkomarev authored and m1guelpf committed Jan 16, 2019
1 parent ec23078 commit 58b72a6
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/Contracts/Helpers/ConfigRetrieverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ interface ConfigRetrieverInterface
* @param string $providerName
* @param array $additionalConfigKeys
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*
* @return \SocialiteProviders\Manager\Contracts\ConfigInterface
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
public function fromServices($providerName, array $additionalConfigKeys = []);
}
4 changes: 3 additions & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SocialiteProviders\Manager\Exception;

class InvalidArgumentException extends \InvalidArgumentException
use InvalidArgumentException as BaseInvalidArgumentException;

class InvalidArgumentException extends BaseInvalidArgumentException
{
}
4 changes: 3 additions & 1 deletion src/Exception/MissingConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace SocialiteProviders\Manager\Exception;

class MissingConfigException extends \Exception
use Exception;

class MissingConfigException extends Exception
{
}
24 changes: 12 additions & 12 deletions src/Helpers/ConfigRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SocialiteProviders\Manager\Helpers;

use Closure;
use SocialiteProviders\Manager\Config;
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
use SocialiteProviders\Manager\Exception\MissingConfigException;
Expand Down Expand Up @@ -32,9 +33,9 @@ class ConfigRetriever implements ConfigRetrieverInterface
* @param string $providerName
* @param array $additionalConfigKeys
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*
* @return \SocialiteProviders\Manager\Contracts\ConfigInterface
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
public function fromServices($providerName, array $additionalConfigKeys = [])
{
Expand All @@ -59,7 +60,7 @@ public function fromServices($providerName, array $additionalConfigKeys = [])
*
* @return array
*/
protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosure)
protected function getConfigItems(array $configKeys, Closure $keyRetrievalClosure)
{
if (count($configKeys) < 1) {
return [];
Expand All @@ -74,7 +75,7 @@ protected function getConfigItems(array $configKeys, \Closure $keyRetrievalClosu
*
* @return array
*/
protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClosure)
protected function retrieveItemsFromConfig(array $keys, Closure $keyRetrievalClosure)
{
$out = [];

Expand All @@ -88,9 +89,9 @@ protected function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalCl
/**
* @param string $key
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*
* @return string
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
protected function getFromServices($key)
{
Expand All @@ -112,22 +113,21 @@ protected function getFromServices($key)
/**
* @param string $providerName
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*
* @return array
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
protected function getConfigFromServicesArray($providerName)
{
/** @var array $configArray */
$configArray = config("services.$providerName");
$configArray = config("services.{$providerName}");

if (empty($configArray)) {
// If we are running in console we should spoof values to make Socialite happy...
if (app()->runningInConsole()) {
$configArray = [
'client_id' => "{$this->providerIdentifier}_KEY",
'client_id' => "{$this->providerIdentifier}_KEY",
'client_secret' => "{$this->providerIdentifier}_SECRET",
'redirect' => "{$this->providerIdentifier}_REDIRECT_URI",
'redirect' => "{$this->providerIdentifier}_REDIRECT_URI",
];
} else {
throw new MissingConfigException("There is no services entry for $providerName");
Expand Down
70 changes: 41 additions & 29 deletions src/SocialiteWasCalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace SocialiteProviders\Manager;

use Illuminate\Container\Container as Application;
use Illuminate\Contracts\Container\Container as Application;
use Laravel\Socialite\Contracts\Factory as SocialiteFactory;
use Laravel\Socialite\One\AbstractProvider as SocialiteOAuth1AbstractProvider;
use Laravel\Socialite\SocialiteManager;
use Laravel\Socialite\Two\AbstractProvider as SocialiteOAuth2AbstractProvider;
use League\OAuth1\Client\Server\Server as OAuth1Server;
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
use SocialiteProviders\Manager\Exception\InvalidArgumentException;
use SocialiteProviders\Manager\Exception\MissingConfigException;

class SocialiteWasCalled
{
const SERVICE_CONTAINER_PREFIX = 'SocialiteProviders.config.';

/**
* @var \Illuminate\Container\Container
* @var \Illuminate\Contracts\Container\Container
*/
protected $app;

Expand All @@ -23,20 +26,22 @@ class SocialiteWasCalled
private $configRetriever;

/**
* @var bool
* @var array
*/
private $spoofedConfig = [
'client_id' => 'spoofed_client_id',
'client_id' => 'spoofed_client_id',
'client_secret' => 'spoofed_client_secret',
'redirect' => 'spoofed_redirect',
'redirect' => 'spoofed_redirect',
];

/**
* @param \Illuminate\Container\Container $app
* @param \Illuminate\Contracts\Container\Container $app
* @param \SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface $configRetriever
*/
public function __construct(Application $app)
public function __construct(Application $app, ConfigRetrieverInterface $configRetriever)
{
$this->app = $app;
$this->configRetriever = $configRetriever;
}

/**
Expand All @@ -46,17 +51,19 @@ public function __construct(Application $app)
* Laravel\Socialite\One\AbstractProvider
* @param string $oauth1Server 'Your\Name\Space\ClassNameServer' must extend League\OAuth1\Client\Server\Server
*
* @throws \InvalidArgumentException
* @return void
*
* @throws \SocialiteProviders\Manager\Exception\InvalidArgumentException
*/
public function extendSocialite($providerName, $providerClass, $oauth1Server = null)
{
/** @var SocialiteManager $socialite */
$socialite = $this->app->make(\Laravel\Socialite\Contracts\Factory::class);
$socialite = $this->app->make(SocialiteFactory::class);

$this->classExists($providerClass);
if ($this->isOAuth1($oauth1Server)) {
$this->classExists($oauth1Server);
$this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class);
$this->classExtends($providerClass, SocialiteOAuth1AbstractProvider::class);
}

$socialite->extend(
Expand All @@ -74,11 +81,13 @@ function () use ($socialite, $providerName, $providerClass, $oauth1Server) {

/**
* @param \Laravel\Socialite\SocialiteManager $socialite
* @param $providerName
* @param string $providerName
* @param string $providerClass
* @param null|string $oauth1Server
*
* @return \Laravel\Socialite\One\AbstractProvider|\Laravel\Socialite\Two\AbstractProvider
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
protected function buildProvider(SocialiteManager $socialite, $providerName, $providerClass, $oauth1Server)
{
Expand All @@ -92,16 +101,18 @@ protected function buildProvider(SocialiteManager $socialite, $providerName, $pr
/**
* Build an OAuth 1 provider instance.
*
* @param \Laravel\Socialite\SocialiteManager $socialite
* @param string $providerClass must extend Laravel\Socialite\One\AbstractProvider
* @param string $providerName
* @param string $oauth1Server must extend League\OAuth1\Client\Server\Server
* @param array $config
* @param mixed $providerName
*
* @return \Laravel\Socialite\One\AbstractProvider
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
protected function buildOAuth1Provider(SocialiteManager $socialite, $providerClass, $providerName, $oauth1Server)
{
$this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class);
$this->classExtends($oauth1Server, OAuth1Server::class);

$config = $this->getConfig($providerClass, $providerName);

Expand All @@ -121,14 +132,15 @@ protected function buildOAuth1Provider(SocialiteManager $socialite, $providerCla
*
* @param SocialiteManager $socialite
* @param string $providerClass must extend Laravel\Socialite\Two\AbstractProvider
* @param array $config
* @param mixed $providerName
* @param string $providerName
*
* @return \Laravel\Socialite\Two\AbstractProvider
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, $providerName)
{
$this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class);
$this->classExtends($providerClass, SocialiteOAuth2AbstractProvider::class);

$config = $this->getConfig($providerClass, $providerName);

Expand All @@ -143,19 +155,15 @@ protected function buildOAuth2Provider(SocialiteManager $socialite, $providerCla
* @param string $providerClass
* @param string $providerName
*
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
* @return \SocialiteProviders\Manager\Contracts\ConfigInterface
*
* @return array
* @throws \SocialiteProviders\Manager\Exception\MissingConfigException
*/
protected function getConfig($providerClass, $providerName)
{
try {
return resolve(ConfigRetrieverInterface::class)->fromServices(
$providerName, $providerClass::additionalConfigKeys()
);
} catch (MissingConfigException $e) {
throw new MissingConfigException($e->getMessage());
}
return $this->configRetriever->fromServices(
$providerName, $providerClass::additionalConfigKeys()
);

return new Config(
$this->spoofedConfig['client_id'],
Expand All @@ -180,7 +188,9 @@ private function isOAuth1($oauth1Server)
* @param string $class
* @param string $baseClass
*
* @throws \InvalidArgumentException
* @return void
*
* @throws \SocialiteProviders\Manager\Exception\InvalidArgumentException
*/
private function classExtends($class, $baseClass)
{
Expand All @@ -192,7 +202,9 @@ private function classExtends($class, $baseClass)
/**
* @param string $providerClass
*
* @throws \InvalidArgumentException
* @return void
*
* @throws \SocialiteProviders\Manager\Exception\InvalidArgumentException
*/
private function classExists($providerClass)
{
Expand Down

0 comments on commit 58b72a6

Please sign in to comment.