diff --git a/src/Config.php b/src/Config.php index da0d6d1..7e0f1e1 100644 --- a/src/Config.php +++ b/src/Config.php @@ -17,9 +17,9 @@ class Config implements Contracts\ConfigInterface public function __construct($key, $secret, $callbackUri, array $additionalProviderConfig = []) { $this->config = array_merge([ - 'client_id' => $key, + 'client_id' => $key, 'client_secret' => $secret, - 'redirect' => $callbackUri, + 'redirect' => $callbackUri, ], $additionalProviderConfig); } diff --git a/src/ConfigTrait.php b/src/ConfigTrait.php new file mode 100644 index 0000000..c0b6a44 --- /dev/null +++ b/src/ConfigTrait.php @@ -0,0 +1,32 @@ +get(); + + $this->config = $config; + $this->clientId = $config['client_id']; + $this->clientSecret = $config['client_secret']; + $this->redirectUrl = $config['redirect']; + + return $this; + } + + protected function getConfig($key = null, $default = null) + { + return $key ? array_get($this->config, $key, $default) : $this->config; + } + + public static function additionalConfigKeys() + { + return []; + } +} diff --git a/src/Contracts/Helpers/ConfigRetrieverInterface.php b/src/Contracts/Helpers/ConfigRetrieverInterface.php index 9dee108..f647596 100644 --- a/src/Contracts/Helpers/ConfigRetrieverInterface.php +++ b/src/Contracts/Helpers/ConfigRetrieverInterface.php @@ -11,9 +11,9 @@ interface ConfigRetrieverInterface * @param string $providerIdentifier * @param array $additionalConfigKeys * + * @throws MissingConfigException * @return ConfigInterface * - * @throws MissingConfigException */ public function fromEnv($providerIdentifier, array $additionalConfigKeys = []); @@ -21,9 +21,9 @@ public function fromEnv($providerIdentifier, array $additionalConfigKeys = []); * @param string $providerName * @param array $additionalConfigKeys * + * @throws MissingConfigException * @return ConfigInterface * - * @throws MissingConfigException */ public function fromServices($providerName, array $additionalConfigKeys = []); } diff --git a/src/Contracts/OAuth1/ProviderInterface.php b/src/Contracts/OAuth1/ProviderInterface.php index d4fcccd..1ed4020 100644 --- a/src/Contracts/OAuth1/ProviderInterface.php +++ b/src/Contracts/OAuth1/ProviderInterface.php @@ -11,5 +11,5 @@ interface ProviderInterface * * @return $this */ - public function config(Config $config); + public function setConfig(Config $config); } diff --git a/src/Contracts/OAuth2/ProviderInterface.php b/src/Contracts/OAuth2/ProviderInterface.php index 3d7ae55..7a7c574 100644 --- a/src/Contracts/OAuth2/ProviderInterface.php +++ b/src/Contracts/OAuth2/ProviderInterface.php @@ -12,5 +12,5 @@ interface ProviderInterface extends SocialiteOauth2ProviderInterface * * @return $this */ - public function config(Config $config); + public function setConfig(Config $config); } diff --git a/src/Helpers/ConfigRetriever.php b/src/Helpers/ConfigRetriever.php index 616d368..a5c67f3 100644 --- a/src/Helpers/ConfigRetriever.php +++ b/src/Helpers/ConfigRetriever.php @@ -27,9 +27,9 @@ class ConfigRetriever implements \SocialiteProviders\Manager\Contracts\Helpers\C * @param string $providerIdentifier * @param array $additionalConfigKeys * + * @throws MissingConfigException * @return ConfigInterface * - * @throws MissingConfigException */ public function fromEnv($providerIdentifier, array $additionalConfigKeys = []) { @@ -48,9 +48,9 @@ public function fromEnv($providerIdentifier, array $additionalConfigKeys = []) * @param string $providerName * @param array $additionalConfigKeys * + * @throws MissingConfigException * @return ConfigInterface * - * @throws MissingConfigException */ public function fromServices($providerName, array $additionalConfigKeys = []) { @@ -101,9 +101,9 @@ private function retrieveItemsFromConfig(array $keys, \Closure $keyRetrievalClos /** * @param string $key * + * @throws MissingConfigException * @return string * - * @throws MissingConfigException */ private function getFromServices($key) { @@ -117,9 +117,9 @@ private function getFromServices($key) /** * @param string $key * + * @throws MissingConfigException * @return string * - * @throws MissingConfigException */ private function getFromEnv($key) { @@ -136,21 +136,21 @@ private function getFromEnv($key) /** * @param string $providerName * + * @throws MissingConfigException * @return array * - * @throws MissingConfigException */ protected function getConfigFromServicesArray($providerName) { /** @var array $configArray */ $configArray = config('services.'.$providerName); - + if (empty($configArray)) { throw new MissingConfigException("There is no services entry for $providerName"); } - + $this->servicesArray = $configArray; - + return $this->servicesArray; } } diff --git a/src/OAuth1/AbstractProvider.php b/src/OAuth1/AbstractProvider.php index 54badd7..10c0ebd 100644 --- a/src/OAuth1/AbstractProvider.php +++ b/src/OAuth1/AbstractProvider.php @@ -2,12 +2,14 @@ namespace SocialiteProviders\Manager\OAuth1; -use SocialiteProviders\Manager\Config; use SocialiteProviders\Manager\SocialiteWasCalled; use Symfony\Component\HttpFoundation\RedirectResponse; +use SocialiteProviders\Manager\ConfigTrait; abstract class AbstractProvider extends \Laravel\Socialite\One\AbstractProvider { + use ConfigTrait; + /** * Indicates if the session state should be utilized. * @@ -25,21 +27,6 @@ public static function serviceContainerKey($providerName) return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName; } - /** - * @param Config $config - * - * @return $this - */ - public function config(Config $config) - { - $config = $config->get(); - $this->clientId = $config['client_id']; - $this->redirectUrl = $config['redirect']; - $this->clientSecret = $config['client_secret']; - - return $this; - } - /** * {@inheritdoc} */ @@ -136,23 +123,11 @@ protected function isStateless() return $this->stateless; } - public static function additionalConfigKeys() - { - return []; - } - - /** - * Map the raw user array to a Socialite User instance. - * - * @param array $user - * @return \Laravel\Socialite\Two\User - */ - abstract protected function mapUserToObject(array $user); - /** * Set the scopes of the requested access. * - * @param array $scopes + * @param array $scopes + * * @return $this */ public function scopes(array $scopes) @@ -165,7 +140,8 @@ public function scopes(array $scopes) /** * Set the custom parameters of the request. * - * @param array $parameters + * @param array $parameters + * * @return $this */ public function with(array $parameters) diff --git a/src/OAuth1/Server.php b/src/OAuth1/Server.php index 2d5e5df..cf8b10a 100644 --- a/src/OAuth1/Server.php +++ b/src/OAuth1/Server.php @@ -50,7 +50,7 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, } $uri = $this->urlTokenCredentials(); - $bodyParameters = array('oauth_verifier' => $verifier); + $bodyParameters = ['oauth_verifier' => $verifier]; $client = $this->createHttpClient(); @@ -63,7 +63,7 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, } return [ - 'tokenCredentials' => $this->createTokenCredentials($response->getBody()), + 'tokenCredentials' => $this->createTokenCredentials($response->getBody()), 'credentialsResponseBody' => $response->getBody(), ]; } @@ -71,7 +71,8 @@ public function getTokenCredentials(TemporaryCredentials $temporaryCredentials, /** * Set the scopes of the requested access. * - * @param array $scopes + * @param array $scopes + * * @return $this */ public function scopes(array $scopes) @@ -84,7 +85,8 @@ public function scopes(array $scopes) /** * Set the custom parameters of the request. * - * @param array $parameters + * @param array $parameters + * * @return $this */ public function with(array $parameters) @@ -93,12 +95,13 @@ public function with(array $parameters) return $this; } - + /** * Format the given scopes. * - * @param array $scopes - * @param string $scopeSeparator + * @param array $scopes + * @param string $scopeSeparator + * * @return string */ protected function formatScopes(array $scopes, $scopeSeparator) diff --git a/src/OAuth2/AbstractProvider.php b/src/OAuth2/AbstractProvider.php index ce3dd71..6be9aa1 100644 --- a/src/OAuth2/AbstractProvider.php +++ b/src/OAuth2/AbstractProvider.php @@ -6,10 +6,12 @@ use Laravel\Socialite\Two\InvalidStateException; use SocialiteProviders\Manager\Contracts\OAuth2\ProviderInterface; use SocialiteProviders\Manager\SocialiteWasCalled; -use SocialiteProviders\Manager\Contracts\ConfigInterface as Config; +use SocialiteProviders\Manager\ConfigTrait; abstract class AbstractProvider extends \Laravel\Socialite\Two\AbstractProvider implements ProviderInterface { + use ConfigTrait; + /** * @var array */ @@ -20,26 +22,6 @@ public static function serviceContainerKey($providerName) return SocialiteWasCalled::SERVICE_CONTAINER_PREFIX.$providerName; } - public static function additionalConfigKeys() - { - return []; - } - - /** - * @param Config $config - * - * @return $this - */ - public function config(Config $config) - { - $config = $config->get(); - $this->clientId = $config['client_id']; - $this->redirectUrl = $config['redirect']; - $this->clientSecret = $config['client_secret']; - - return $this; - } - /** * @return \SocialiteProviders\Manager\OAuth2\User */ @@ -75,7 +57,7 @@ public function getAccessToken($code) $response = $this->getHttpClient()->post($this->getTokenUrl(), [ 'headers' => ['Accept' => 'application/json'], - $postKey => $this->getTokenFields($code), + $postKey => $this->getTokenFields($code), ]); $this->credentialsResponseBody = json_decode($response->getBody(), true); diff --git a/src/SocialiteWasCalled.php b/src/SocialiteWasCalled.php index 6dd44fc..b8ce87a 100644 --- a/src/SocialiteWasCalled.php +++ b/src/SocialiteWasCalled.php @@ -68,14 +68,11 @@ protected function buildProvider(SocialiteManager $socialite, $providerName, $pr if ($this->isOAuth1($oauth1Server)) { $this->classExists($oauth1Server); - $config = $this->getConfig($providerClass, $providerName); - return $this->buildOAuth1Provider($providerClass, $oauth1Server, $socialite->formatConfig($config)); + return $this->buildOAuth1Provider($socialite, $providerClass, $providerName, $oauth1Server); } - $config = $this->getConfig($providerClass, $providerName); - - return $this->buildOAuth2Provider($socialite, $providerClass, $config); + return $this->buildOAuth2Provider($socialite, $providerClass, $providerName); } /** @@ -87,14 +84,22 @@ protected function buildProvider(SocialiteManager $socialite, $providerName, $pr * * @return \Laravel\Socialite\One\AbstractProvider */ - protected function buildOAuth1Provider($providerClass, $oauth1Server, array $config) + protected function buildOAuth1Provider(SocialiteManager $socialite, $providerClass, $providerName, $oauth1Server) { $this->classExtends($providerClass, \Laravel\Socialite\One\AbstractProvider::class); $this->classExtends($oauth1Server, \League\OAuth1\Client\Server\Server::class); - return new $providerClass( - $this->app->offsetGet('request'), new $oauth1Server($config) + $config = $this->getConfig($providerClass, $providerName); + + $configServer = $socialite->formatConfig($config->get()); + + $provider = new $providerClass( + $this->app->offsetGet('request'), new $oauth1Server($configServer) ); + + $provider->setConfig($config); + + return $provider; } /** @@ -106,20 +111,26 @@ protected function buildOAuth1Provider($providerClass, $oauth1Server, array $con * * @return \Laravel\Socialite\Two\AbstractProvider */ - protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, array $config) + protected function buildOAuth2Provider(SocialiteManager $socialite, $providerClass, $providerName) { $this->classExtends($providerClass, \Laravel\Socialite\Two\AbstractProvider::class); - return $socialite->buildProvider($providerClass, $config); + $config = $this->getConfig($providerClass, $providerName); + + $provider = $socialite->buildProvider($providerClass, $config->get()); + + $provider->setConfig($config); + + return $provider; } /** * @param string $providerClass * @param string $providerName * + * @throws MissingConfigException * @return array * - * @throws MissingConfigException */ protected function getConfig($providerClass, $providerName) { @@ -129,7 +140,7 @@ protected function getConfig($providerClass, $providerName) try { $config = $this->configRetriever->fromEnv($providerClass::IDENTIFIER, $additionalConfigKeys); - return $config->get(); + return $config; } catch (MissingConfigException $e) { $exceptionMessages[] = $e->getMessage(); } @@ -138,7 +149,7 @@ protected function getConfig($providerClass, $providerName) try { $config = $this->configRetriever->fromServices($providerName, $additionalConfigKeys); - return $config->get(); + return $config; } catch (MissingConfigException $e) { $exceptionMessages[] = $e->getMessage(); }