Skip to content

Commit f170502

Browse files
[5.x] Throw exception if config is incomplete (#736)
* Throw exception if config is incomplete * rename method * Update SocialiteManager.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent ec70b7c commit f170502

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Laravel\Socialite\Exceptions;
4+
5+
use InvalidArgumentException;
6+
7+
class DriverMissingConfigurationException extends InvalidArgumentException
8+
{
9+
/**
10+
* Create a new exception for a missing configuration.
11+
*
12+
* @param string $provider
13+
* @param array<int, string> $keys
14+
* @return static
15+
*/
16+
public static function make($provider, $keys)
17+
{
18+
return new static("Missing required configuration keys [" . implode(', ', $keys) . "] for [{$provider}] OAuth provider.");
19+
}
20+
}

src/SocialiteManager.php

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Manager;
77
use Illuminate\Support\Str;
88
use InvalidArgumentException;
9+
use Laravel\Socialite\Exceptions\DriverMissingConfigurationException;
910
use Laravel\Socialite\One\TwitterProvider;
1011
use Laravel\Socialite\Two\BitbucketProvider;
1112
use Laravel\Socialite\Two\FacebookProvider;
@@ -238,6 +239,14 @@ protected function createSlackOpenidDriver()
238239
*/
239240
public function buildProvider($provider, $config)
240241
{
242+
$requiredKeys = ['client_id', 'client_secret', 'redirect'];
243+
244+
$missingKeys = array_diff($requiredKeys, array_keys($config ?? []));
245+
246+
if (! empty($missingKeys)) {
247+
throw DriverMissingConfigurationException::make($provider, $missingKeys);
248+
}
249+
241250
return (new $provider(
242251
$this->container->make('request'), $config['client_id'],
243252
$config['client_secret'], $this->formatRedirectUrl($config),

tests/SocialiteManagerTest.php

+58
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Laravel\Socialite\Tests;
44

55
use Laravel\Socialite\Contracts\Factory;
6+
use Laravel\Socialite\Exceptions\DriverMissingConfigurationException;
67
use Laravel\Socialite\SocialiteServiceProvider;
78
use Laravel\Socialite\Two\GithubProvider;
89
use Orchestra\Testbench\TestCase;
@@ -77,4 +78,61 @@ public function test_it_can_instantiate_the_github_driver_with_scopes_from_confi
7778
$provider = $factory->driver('github')->setScopes(['read:user']);
7879
$this->assertSame(['read:user'], $provider->getScopes());
7980
}
81+
82+
public function test_it_throws_exception_when_client_secret_is_missing()
83+
{
84+
$this->expectException(DriverMissingConfigurationException::class);
85+
$this->expectExceptionMessage('Missing required configuration keys [client_secret] for [Laravel\Socialite\Two\GithubProvider] OAuth provider.');
86+
87+
$factory = $this->app->make(Factory::class);
88+
89+
$this->app['config']->set('services.github', [
90+
'client_id' => 'github-client-id',
91+
'redirect' => 'http://your-callback-url',
92+
]);
93+
94+
$factory->driver('github');
95+
}
96+
97+
public function test_it_throws_exception_when_client_id_is_missing()
98+
{
99+
$this->expectException(DriverMissingConfigurationException::class);
100+
$this->expectExceptionMessage('Missing required configuration keys [client_id] for [Laravel\Socialite\Two\GithubProvider] OAuth provider.');
101+
102+
$factory = $this->app->make(Factory::class);
103+
104+
$this->app['config']->set('services.github', [
105+
'client_secret' => 'github-client-secret',
106+
'redirect' => 'http://your-callback-url',
107+
]);
108+
109+
$factory->driver('github');
110+
}
111+
112+
public function test_it_throws_exception_when_redirect_is_missing()
113+
{
114+
$this->expectException(DriverMissingConfigurationException::class);
115+
$this->expectExceptionMessage('Missing required configuration keys [redirect] for [Laravel\Socialite\Two\GithubProvider] OAuth provider.');
116+
117+
$factory = $this->app->make(Factory::class);
118+
119+
$this->app['config']->set('services.github', [
120+
'client_id' => 'github-client-id',
121+
'client_secret' => 'github-client-secret',
122+
]);
123+
124+
$factory->driver('github');
125+
}
126+
127+
public function test_it_throws_exception_when_configuration_is_completely_missing()
128+
{
129+
$this->expectException(DriverMissingConfigurationException::class);
130+
$this->expectExceptionMessage('Missing required configuration keys [client_id, client_secret, redirect] for [Laravel\Socialite\Two\GithubProvider] OAuth provider.');
131+
132+
$factory = $this->app->make(Factory::class);
133+
134+
$this->app['config']->set('services.github', null);
135+
136+
$factory->driver('github');
137+
}
80138
}

0 commit comments

Comments
 (0)