Skip to content

Commit

Permalink
Update OAuth2::getAuthUrl method
Browse files Browse the repository at this point in the history
  • Loading branch information
ksvirkou-hubspot committed Jun 8, 2023
1 parent 819c4a9 commit 81404ac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/Utils/OAuth2.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Utils/OAuth2Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit 81404ac

Please sign in to comment.