Skip to content

Commit

Permalink
Spoof config if running in terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Faust committed Dec 4, 2016
1 parent 904283b commit 6b047ae
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
23 changes: 21 additions & 2 deletions src/Helpers/ConfigRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use SocialiteProviders\Manager\Config;
use SocialiteProviders\Manager\Contracts\ConfigInterface;
use SocialiteProviders\Manager\Exception\MissingConfigException;
use SocialiteProviders\Manager\Contracts\Helpers\ConfigRetrieverInterface;
use SocialiteProviders\Manager\Exception\MissingConfigException;
use SocialiteProviders\Manager\SocialiteWasCalled;

class ConfigRetriever implements ConfigRetrieverInterface
{
Expand Down Expand Up @@ -150,6 +151,13 @@ private function getFromEnv($key)

// REQUIRED value is empty
if (empty($item)) {
// If we are running in console we should spoof values to make Socialite happy...
if (app()->runningInConsole()) {
$item = $providerKey;

SocialiteWasCalled::$spoofedConfig = true;
}

throw new MissingConfigException("Configuration for $providerKey is missing.");
}

Expand All @@ -166,9 +174,20 @@ private function getFromEnv($key)
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_secret' => "{$this->providerIdentifier}_SECRET",
'redirect' => "{$this->providerIdentifier}_REDIRECT_URI",
];

SocialiteWasCalled::$spoofedConfig = true;
}

throw new MissingConfigException("There is no services entry for $providerName");
}

Expand Down
15 changes: 14 additions & 1 deletion src/SocialiteWasCalled.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class SocialiteWasCalled
*/
private $configRetriever;

/**
* @var bool
*/
public static $spoofedConfig = false;

/**
* @param LaravelApp $app
* @param ConfigRetrieverInterface $configRetriever
Expand Down Expand Up @@ -144,7 +149,12 @@ protected function getConfig($providerClass, $providerName)
try {
$config = $this->configRetriever->fromEnv($providerClass::IDENTIFIER, $additionalConfigKeys);

return $config;
// We will use the $spoofedConfig variable for now as a way to find out if there was no
// configuration in the .env file which means we should not return anything and jump
// to the service config check to check if something can be found there.
if (!static::$spoofedConfig) {
return $config;
}
} catch (MissingConfigException $e) {
$exceptionMessages[] = $e->getMessage();
}
Expand All @@ -153,6 +163,9 @@ protected function getConfig($providerClass, $providerName)
try {
$config = $this->configRetriever->fromServices($providerName, $additionalConfigKeys);

// Here we don't need to check for the $spoofedConfig variable because this will be
// the end of checking for config and should contain either the proper data or an
// array containing spoofed values.
return $config;
} catch (MissingConfigException $e) {
$exceptionMessages[] = $e->getMessage();
Expand Down

0 comments on commit 6b047ae

Please sign in to comment.