Skip to content

Commit

Permalink
allow use of OAuth without a redirect_uri
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmarspenneo committed Jan 31, 2024
1 parent 7fd9218 commit 4118fe6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 12 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ $oAuth = Penneo\SDK\OAuth\OAuthBuilder::start()
->setEnvironment('environmentHere')
->setClientId('clientIdHere')
->setClientSecret('clientSecretHere')
->setRedirectUri('redirectUriHere')
->setTokenStorage(new SessionTokenStorage())
->setApiKey('apiKeyHere')
->setApiSecret('apiSecretHere')
Expand Down
1 change: 0 additions & 1 deletion docs/programmatic_oauth_example.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
->setEnvironment(Environment::SANDBOX)
->setClientId('clientId') // <- the credentials provided by Penneo
->setClientSecret('clientSecret') // <-
->setRedirectUri('http://dev.php.local') // the exact URL you provided to Penneo
->setTokenStorage($tokenStorage)
->setApiKey('apiKey') // <- the api credentials found in your
->setApiKey('apiSecret') // <- penneo users settings
Expand Down
8 changes: 8 additions & 0 deletions src/OAuth/AuthorizeUrlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Penneo\SDK\OAuth\Config\Environment;
use Penneo\SDK\OAuth\Config\OAuthConfig;
use Penneo\SDK\OAuth\PKCE\CodeChallenge;
use Penneo\SDK\PenneoSdkRuntimeException;

class AuthorizeUrlBuilder
{
Expand All @@ -18,6 +19,13 @@ public function __construct(OAuthConfig $config)

public function build(array $scope, CodeChallenge $codeChallenge, string $state = ''): string
{
if (null === $this->config->getRedirectUri()) {
throw new PenneoSdkRuntimeException(
'Cannot build redirect URL! Please set the redirectUri with ->setRedirectUri() when building ' .
'the OAuth client!'
);
}

$queryParameters = [
'response_type' => 'code',
'client_id' => $this->config->getClientId(),
Expand Down
6 changes: 3 additions & 3 deletions src/OAuth/Config/OAuthConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class OAuthConfig
*/
private $clientSecret;
/**
* @var string
* @var string|null
*/
private $redirectUri;
/**
Expand All @@ -34,7 +34,7 @@ public function __construct(
string $environment,
string $clientId,
string $clientSecret,
string $redirectUri,
string $redirectUri = null,
string $apiKey = null,
string $apiSecret = null
) {
Expand All @@ -61,7 +61,7 @@ public function getClientSecret(): string
return $this->clientSecret;
}

public function getRedirectUri(): string
public function getRedirectUri(): ?string
{
return $this->redirectUri;
}
Expand Down
9 changes: 3 additions & 6 deletions src/OAuth/OAuthBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ final class OAuthBuilder
private $clientId;
/** @var string */
private $clientSecret;
/** @var string */
private $redirectUri;
/** @var string|null */
private $redirectUri = null;
/** @var TokenStorage */
private $tokenStorage;
/** @var string|null */
Expand Down Expand Up @@ -108,9 +108,6 @@ private function validateAllParametersPresent(): void
if (!$this->clientSecret) {
$this->throwMissingParameterError('clientSecret');
}
if (!$this->redirectUri) {
$this->throwMissingParameterError('redirectUri');
}
if (!$this->tokenStorage) {
$this->throwMissingParameterError('tokenStorage');
}
Expand Down Expand Up @@ -143,7 +140,7 @@ private function validateEnvironment(): void
/** @throws PenneoSdkRuntimeException */
private function validateRedirectUri(): void
{
if (!filter_var($this->redirectUri, FILTER_VALIDATE_URL)) {
if (null !== $this->redirectUri && !filter_var($this->redirectUri, FILTER_VALIDATE_URL)) {
throw new PenneoSdkRuntimeException('Cannot build! The supplied redirect URI is not a valid URL!');
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/OAuth/OAuth/RedirectUriTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Penneo\SDK\Tests\Unit\OAuth\OAuth;

use Penneo\SDK\OAuth\PKCE\CodeChallenge;
use Penneo\SDK\OAuth\PKCE\PKCE;
use Penneo\SDK\PenneoSdkRuntimeException;
use Penneo\SDK\Tests\Unit\OAuth\BuildsOAuth;
use Penneo\SDK\Tests\Unit\OAuth\TestsEnvironments;
use PHPUnit\Framework\TestCase;
Expand All @@ -12,6 +14,21 @@ class RedirectUriTest extends TestCase
use BuildsOAuth;
use TestsEnvironments;

public function testThrowsPenneoSDKExceptionWhenRedirectUriIsMissing()
{
$oauth = $this->build([
'redirectUri' => null,
]);

$this->expectException(PenneoSdkRuntimeException::class);
$this->expectExceptionMessage(
'Cannot build redirect URL! Please set the redirectUri with ->setRedirectUri() when building the OAuth ' .
'client!'
);

$oauth->buildRedirectUrl(['full_access'], $this->createMock(CodeChallenge::class), 'someState');
}

/** @dataProvider environmentProvider */
public function testSuccessfullyBuildsUri(string $environment, string $expectedDomain)
{
Expand Down
1 change: 0 additions & 1 deletion tests/unit/OAuth/OAuthBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public static function providerRequiredBuildParameters(): array
return [
['clientId'],
['clientSecret'],
['redirectUri'],
['tokenStorage'],
['environment'],
];
Expand Down

0 comments on commit 4118fe6

Please sign in to comment.