Skip to content

Commit

Permalink
Merge pull request #251 from HubSpot/feature/updateOAuth
Browse files Browse the repository at this point in the history
Update OAuth2::getAuthUrl method
  • Loading branch information
ksvirkou-hubspot authored Jun 8, 2023
2 parents 819c4a9 + 81404ac commit 6fed8cb
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 6fed8cb

Please sign in to comment.