Skip to content

Commit b2ce47b

Browse files
TheAladeenTheAladeen
and
TheAladeen
authored
[5.x] Adds support for configuring scope of providers from configuration. (#728)
Update `buildProvider` to initialize scopes from the configuration `services.{driver}.scopes` array if provided. Add tests to validate GitHub driver behaves correctly with and without `services.{driver}.scopes` array provided in configuration. Ensure scopes are returned as indexed arrays without missing indexes by using `array_values` to possibly prevent future implementation issues. Ensure backwards compatibility by wrapping provider instantiation in parenthesis when calling scopes(). Co-authored-by: TheAladeen <[email protected]>
1 parent c654af8 commit b2ce47b

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/SocialiteManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ protected function createSlackOpenidDriver()
223223
*/
224224
public function buildProvider($provider, $config)
225225
{
226-
return new $provider(
226+
return (new $provider(
227227
$this->container->make('request'), $config['client_id'],
228228
$config['client_secret'], $this->formatRedirectUrl($config),
229229
Arr::get($config, 'guzzle', [])
230-
);
230+
))->scopes($config['scopes'] ?? []);
231231
}
232232

233233
/**

src/Two/AbstractProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ protected function getCode()
395395
*/
396396
public function scopes($scopes)
397397
{
398-
$this->scopes = array_unique(array_merge($this->scopes, (array) $scopes));
398+
$this->scopes = array_values(array_unique(array_merge($this->scopes, (array) $scopes)));
399399

400400
return $this;
401401
}
@@ -408,7 +408,7 @@ public function scopes($scopes)
408408
*/
409409
public function setScopes($scopes)
410410
{
411-
$this->scopes = array_unique((array) $scopes);
411+
$this->scopes = array_values(array_unique((array) $scopes));
412412

413413
return $this;
414414
}

tests/SocialiteManagerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,24 @@ public function test_it_can_instantiate_the_github_driver()
3131

3232
$this->assertInstanceOf(GithubProvider::class, $provider);
3333
}
34+
35+
public function test_it_can_instantiate_the_github_driver_with_scopes_from_config_array()
36+
{
37+
$factory = $this->app->make(Factory::class);
38+
$this->app['config']->set('services.github', [
39+
'client_id' => 'github-client-id',
40+
'client_secret' => 'github-client-secret',
41+
'redirect' => 'http://your-callback-url',
42+
'scopes' => ['user:email', 'read:user'],
43+
]);
44+
$provider = $factory->driver('github');
45+
$this->assertSame(['user:email', 'read:user'], $provider->getScopes());
46+
}
47+
48+
public function test_it_can_instantiate_the_github_driver_with_scopes_without_array_from_config()
49+
{
50+
$factory = $this->app->make(Factory::class);
51+
$provider = $factory->driver('github');
52+
$this->assertSame(['user:email'], $provider->getScopes());
53+
}
3454
}

0 commit comments

Comments
 (0)