Skip to content

Commit a39a59d

Browse files
authored
Merge pull request #318 from wandesnet/fix/remove-empty-parameter
Fix: remove empty parameters from authorization url
2 parents 4537d83 + c04fc37 commit a39a59d

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/Traits/OAuth2/AuthorizationCodeGrant.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,14 @@ public function getAuthorizationUrl(array $scopes = [], string $state = null, st
4646

4747
$this->state = $state ?? StringHelpers::random(32);
4848

49-
$queryParameters = [
49+
$queryParameters = array_filter([
5050
'response_type' => 'code',
51-
'scope' => implode($scopeSeparator, array_merge($defaultScopes, $scopes)),
51+
'scope' => implode($scopeSeparator, array_filter(array_merge($defaultScopes, $scopes))),
5252
'client_id' => $clientId,
5353
'redirect_uri' => $redirectUri,
5454
'state' => $this->state,
5555
...$additionalQueryParameters,
56-
];
56+
]);
5757

5858
$query = http_build_query($queryParameters, '', '&', PHP_QUERY_RFC3986);
5959
$query = trim($query, '?&');

tests/Feature/Oauth2/AuthCodeFlowConnectorTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@
5050
);
5151
});
5252

53+
test('you can get authorization url without setting valid scopes', function () {
54+
$connector = new OAuth2Connector;
55+
56+
$connector->oauthConfig()->setDefaultScopes(['', null]);
57+
58+
$url = $connector->getAuthorizationUrl([], 'my-state');
59+
60+
expect($url)->toEqual(
61+
'https://oauth.saloon.dev/authorize?response_type=code&client_id=client-id&redirect_uri=https%3A%2F%2Fmy-app.saloon.dev%2Fauth%2Fcallback&state=my-state'
62+
);
63+
});
64+
5365
test('default state is generated automatically with every authorization url if state is not defined', function () {
5466
$connector = new OAuth2Connector;
5567

0 commit comments

Comments
 (0)