Skip to content

Commit

Permalink
[1.x] Allow scopes to be defined in config and merge with defaults (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelbutcher authored Jun 14, 2022
1 parent 8bd7924 commit c0c2382
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/FacebookServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ protected function registerFacebook(): void
'app_id' => $app['config']->get('facebook.app_id'),
'app_secret' => $app['config']->get('facebook.app_secret'),
'redirect_uri' => $app['config']->get('facebook.redirect_uri'),
'scopes' => $app['config']->get('facebook.scopes', []),
'default_graph_version' => $app['config']->get('facebook.graph_version'),
'enable_beta_mode' => $app['config']->get('facebook.beta_mode'),
'persistent_data_handler' => $app[PersistentDataInterface::class],
Expand Down
4 changes: 3 additions & 1 deletion src/Traits/HandlesAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public function getRedirect(?string $redirectUrl = null, array $scopes = []): st
throw new \InvalidArgumentException('A valid redirect URL is required');
}

$scopes = ! empty($scopes) ? $scopes : ($this->config['scopes'] ?? ['email', 'public_profile']);
$scopes = array_merge($this->config['scopes'] ?? [], [
'email', 'public_profile',
]);

return $this->getLoginHelper()->getLoginUrl($url, $scopes);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/FacebookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@
$facebook->getRedirect();
});

it('builds default scopes', function () {
$redirect = $this->getFacebookMock()->getRedirect();
$this->assertStringContainsStringIgnoringCase('scope='.urlencode('email,public_profile'), $redirect);
});

it('builds replaces duplicate scopes with defaults', function () {
$redirect = $this->getFacebookMock(['scopes' => ['email', 'public_profile']])->getRedirect();
$this->assertStringContainsStringIgnoringCase('scope='.urlencode('email,public_profile'), $redirect);
});

it('builds appends default scopes to requested', function () {
$redirect = $this->getFacebookMock(['scopes' => ['publish_actions']])->getRedirect();
$this->assertStringContainsStringIgnoringCase('scope='.urlencode('publish_actions,email,public_profile'), $redirect);
});

it('returns a valid redirect login helper instance', function () {
$helper = $this->getFacebookMock()->getLoginHelper();
$dataHandler = $helper->getPersistentDataHandler();
Expand Down

0 comments on commit c0c2382

Please sign in to comment.