Skip to content

Commit

Permalink
Update scopes when regenerating a token
Browse files Browse the repository at this point in the history
  • Loading branch information
cbaconnier committed Mar 4, 2022
1 parent d05ac2f commit 011edce
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/Auth/ZohoAuthProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use League\OAuth2\Client\Token\AccessToken;
use League\OAuth2\Client\Token\AccessTokenInterface;
use MelbaCh\LaravelZoho\Clients\ZohoUrlFactory;
use MelbaCh\LaravelZoho\Repositories\ConfigRepository;
use Psr\Http\Message\ResponseInterface;

class ZohoAuthProvider extends AbstractProvider
Expand Down Expand Up @@ -62,7 +61,7 @@ public function getResourceOwnerDetailsUrl(AccessToken $token): string
*/
protected function getDefaultScopes(): array
{
return app(ConfigRepository::class)->scopes();
return config('zoho.scopes', []);
}

/**
Expand Down
16 changes: 11 additions & 5 deletions src/Controllers/ZohoAuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\OAuth2\Client\Token\AccessTokenInterface;
use MelbaCh\LaravelZoho\Auth\ZohoAuthProvider;
use MelbaCh\LaravelZoho\Repositories\AccessTokenRepository;
use MelbaCh\LaravelZoho\Repositories\ConfigRepository;

class ZohoAuthController extends Controller
{
Expand All @@ -24,15 +25,20 @@ public function __construct()
*
* @throws IdentityProviderException
*/
public function requestToken(AccessTokenRepository $accessTokenRepository)
{
if (! request()->get('code')) {
public function requestToken(
AccessTokenRepository $accessTokenRepository,
ConfigRepository $configRepository,
) {
if (!request()->get('code')) {
return $this->redirectToZoho();
}

$this->verifyState();

$token = $this->getAccessToken($accessTokenRepository);

$configRepository->setScopes(config('zoho.scopes', []));

if ($token instanceof RedirectResponse) {
return $token;
}
Expand Down Expand Up @@ -80,8 +86,8 @@ private function getAccessToken(
]);
} catch (IdentityProviderException $e) {
request()->session()->flash('zoho.access_token_error', [
'code' => $e->getCode(),
'message' => $e->getMessage(),
'code' => $e->getCode(),
'message' => $e->getMessage(),
]);

return redirect(config('zoho.on_error_url', '/'));
Expand Down
2 changes: 2 additions & 0 deletions src/Repositories/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public function region(): string;

public function scopes(): array;

public function setScopes(array $scopes): self;

public function secret(): string;

public function clientId(): string;
Expand Down
8 changes: 8 additions & 0 deletions src/Repositories/DatabaseConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function scopes(): array
return $this->get()['parameters']['scopes'] ?? [];
}

public function setScopes(array $scopes): self
{
$config = $this->get();
$config['parameters']['scopes'] = $scopes;
$this->store($config);
return $this;
}

public function secret(): string
{
return $this->get()['secrets']['secret'] ?? '';
Expand Down
6 changes: 6 additions & 0 deletions src/Repositories/DefaultConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public function scopes(): array
return $this->config['parameters']['scopes'];
}

public function setScopes(array $scopes): ConfigRepository
{
// Not supported
return $this;
}

public function secret(): string
{
return $this->config['secrets']['secret'];
Expand Down
3 changes: 2 additions & 1 deletion tests/Controllers/ZohoAuthControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected function setUp(): void
$repository->shouldReceive('secret')->andReturn('123-789');
$repository->shouldReceive('region')->andReturn('EU');
$repository->shouldReceive('scopes')->andReturn(['my-scope', 'my-another-scope']);
$repository->shouldReceive('setScopes')->andReturnSelf();
});
}

Expand All @@ -41,7 +42,7 @@ public function it_redirect_the_user_to_zoho_when_code_is_not_provided(): void
'access_type' => 'offline',
'prompt' => 'consent',
'state' => session('oauth2state'),
'scope' => 'my-scope,my-another-scope',
'scope' => implode(',', config('zoho.scopes')),
'response_type' => 'code',
'approval_prompt' => 'auto',
'client_id' => 'abc-xyz',
Expand Down

0 comments on commit 011edce

Please sign in to comment.