Skip to content

Commit

Permalink
[5.x] Adds support for configuring scope of providers from configurat…
Browse files Browse the repository at this point in the history
…ion. (#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]>
  • Loading branch information
TheAladeen and TheAladeen authored Jan 16, 2025
1 parent c654af8 commit b2ce47b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/SocialiteManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ protected function createSlackOpenidDriver()
*/
public function buildProvider($provider, $config)
{
return new $provider(
return (new $provider(
$this->container->make('request'), $config['client_id'],
$config['client_secret'], $this->formatRedirectUrl($config),
Arr::get($config, 'guzzle', [])
);
))->scopes($config['scopes'] ?? []);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Two/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ protected function getCode()
*/
public function scopes($scopes)
{
$this->scopes = array_unique(array_merge($this->scopes, (array) $scopes));
$this->scopes = array_values(array_unique(array_merge($this->scopes, (array) $scopes)));

return $this;
}
Expand All @@ -408,7 +408,7 @@ public function scopes($scopes)
*/
public function setScopes($scopes)
{
$this->scopes = array_unique((array) $scopes);
$this->scopes = array_values(array_unique((array) $scopes));

return $this;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/SocialiteManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,24 @@ public function test_it_can_instantiate_the_github_driver()

$this->assertInstanceOf(GithubProvider::class, $provider);
}

public function test_it_can_instantiate_the_github_driver_with_scopes_from_config_array()
{
$factory = $this->app->make(Factory::class);
$this->app['config']->set('services.github', [
'client_id' => 'github-client-id',
'client_secret' => 'github-client-secret',
'redirect' => 'http://your-callback-url',
'scopes' => ['user:email', 'read:user'],
]);
$provider = $factory->driver('github');
$this->assertSame(['user:email', 'read:user'], $provider->getScopes());
}

public function test_it_can_instantiate_the_github_driver_with_scopes_without_array_from_config()
{
$factory = $this->app->make(Factory::class);
$provider = $factory->driver('github');
$this->assertSame(['user:email'], $provider->getScopes());
}
}

0 comments on commit b2ce47b

Please sign in to comment.