diff --git a/lib/Utils/OAuth2.php b/lib/Utils/OAuth2.php index 6823a4fa1..9d8c33748 100644 --- a/lib/Utils/OAuth2.php +++ b/lib/Utils/OAuth2.php @@ -18,11 +18,19 @@ class OAuth2 */ public static function getAuthUrl(string $clientId, string $redirectURI, array $scopesArray = [], array $optionalScopesArray = []) { - return static::AUTHORIZE_URL.'?'.http_build_query([ + $queryParams = [ 'client_id' => $clientId, 'redirect_uri' => $redirectURI, - 'scope' => implode(' ', $scopesArray), - 'optional_scope' => implode(' ', $optionalScopesArray), - ], '', '&', PHP_QUERY_RFC3986); + ]; + + if (!empty($scopesArray)) { + $queryParams['scope'] = implode(' ', $scopesArray); + } + + if (!empty($optionalScopesArray)) { + $queryParams['optional_scope'] = implode(' ', $optionalScopesArray); + } + + return static::AUTHORIZE_URL.'?'.http_build_query($queryParams, '', '&', PHP_QUERY_RFC3986); } } diff --git a/tests/Unit/Utils/OAuth2Test.php b/tests/Unit/Utils/OAuth2Test.php index 13967dc15..12cacc67f 100644 --- a/tests/Unit/Utils/OAuth2Test.php +++ b/tests/Unit/Utils/OAuth2Test.php @@ -25,4 +25,18 @@ public function buildAuthorizationUrl() $authUrl ); } + + /** @test */ + public function buildAuthorizationUrlWithEmptyOptionalScope() + { + $authUrl = OAuth2::getAuthUrl( + 'clientid', + 'http://localhost', + ['contacts', 'timeline'] + ); + $this->assertSame( + 'https://app.hubspot.com/oauth/authorize?client_id=clientid&redirect_uri=http%3A%2F%2Flocalhost&scope=contacts%20timeline', + $authUrl + ); + } }