Skip to content

Commit

Permalink
allow setting state as a string for direct setup
Browse files Browse the repository at this point in the history
  • Loading branch information
frets1700 committed Jan 11, 2024
1 parent a67d291 commit 464157a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Interfaces/TokenInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function refresh(string $refreshToken, string $redirectUrl) : TokenEntity

/**
* Get AuthorizationUrl.
* @param array<string, string> $state
* @param array<string, string> | string $state
* @param string $redirectUrl
* @return string
*/
public function getAuthorizationUrl(array $state, string $redirectUrl) : string;
public function getAuthorizationUrl(array|string $state, string $redirectUrl) : string;
}
11 changes: 8 additions & 3 deletions src/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public function refresh(string $refreshToken, string $redirectUrl, ?AbstractProv
->setTokenReceivedOn();
}

/** @param array<string, string> $state */
public function getAuthorizationUrl(array $state, string $redirectUrl): string
/**
* @param array<string, string>|string $state
*/
public function getAuthorizationUrl(array|string $state, string $redirectUrl): string
{
$tokenAuthorizationProvider = new GenericProvider([
'clientId' => $this->clientId,
Expand All @@ -116,7 +118,10 @@ public function getAuthorizationUrl(array $state, string $redirectUrl): string
'urlResourceOwnerDetails' => static::OAUTH_USER_INFO_URL
]);

$state = \json_encode($state);
if (\is_array($state)) {
$state = \json_encode($state);
}

return $tokenAuthorizationProvider->getAuthorizationUrl([
'state' => $state,
'scope' => $this->scopes
Expand Down
3 changes: 3 additions & 0 deletions tests/TokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public function testAuthorizationUrl()

$authUrl = $this->tokenHandler->getAuthorizationUrl(['123'], 'test.com');
$this->assertEquals('https://login.microsoftonline.com/common/oauth2/v2.0/authorize?state=%5B%22123%22%5D&scope=openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fcalendars.readwrite&response_type=code&approval_prompt=auto&redirect_uri=test.com&client_id=foo', $authUrl);

$authUrl = $this->tokenHandler->getAuthorizationUrl('abc123333', 'test.com');
$this->assertEquals('https://login.microsoftonline.com/common/oauth2/v2.0/authorize?state=abc123333&scope=openid%20offline_access%20https%3A%2F%2Fgraph.microsoft.com%2Fcalendars.readwrite&response_type=code&approval_prompt=auto&redirect_uri=test.com&client_id=foo', $authUrl);
}

private function getProvider(MockHandler $mock): AbstractProvider
Expand Down

0 comments on commit 464157a

Please sign in to comment.