Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default graph scopes and valid hosts #10

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 6 additions & 11 deletions src/PhpLeagueAccessTokenProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,9 @@ class PhpLeagueAccessTokenProvider implements AccessTokenProvider
public function __construct(TokenRequestContext $tokenRequestContext, array $scopes = [], array $allowedHosts = [])
{
$this->tokenRequestContext = $tokenRequestContext;
if (empty($scopes)) {
$scopes = ['https://graph.microsoft.com/.default'];
}
$this->scopes = $scopes;

$this->allowedHostsValidator = new AllowedHostsValidator();
if (empty($allowedHosts)) {
$this->allowedHostsValidator->setAllowedHosts(["graph.microsoft.com", "graph.microsoft.us", "dod-graph.microsoft.us", "graph.microsoft.de", "microsoftgraph.chinacloudapi.cn", "canary.graph.microsoft.com"]);
} else {
$this->allowedHostsValidator->setAllowedHosts($allowedHosts);
}

$this->allowedHostsValidator->setAllowedHosts($allowedHosts);
$this->initOauthProvider();
}

Expand All @@ -80,10 +71,14 @@ public function __construct(TokenRequestContext $tokenRequestContext, array $sco
*/
public function getAuthorizationTokenAsync(string $url): Promise
{
$scheme = parse_url($url, PHP_URL_SCHEME);
$parsedUrl = parse_url($url);
$scheme = $parsedUrl["scheme"] ?? null;
$host = $parsedUrl["host"] ?? null;

if ($scheme !== 'https' || !$this->getAllowedHostsValidator()->isUrlHostValid($url)) {
return new FulfilledPromise(null);
}
$this->scopes = $this->scopes ?: ["{$scheme}://{$host}/.default"];
try {
$params = array_merge($this->tokenRequestContext->getParams(), ['scope' => implode(' ', $this->scopes)]);
if ($this->cachedToken) {
Expand Down
2 changes: 1 addition & 1 deletion src/PhpLeagueAuthenticationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class PhpLeagueAuthenticationProvider extends BaseBearerTokenAuthenticationProvi
* @param array $scopes
* @param array $allowedHosts
*/
public function __construct(TokenRequestContext $tokenRequestContext, array $scopes, array $allowedHosts = [])
public function __construct(TokenRequestContext $tokenRequestContext, array $scopes = [], array $allowedHosts = [])
{
$this->accessTokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext, $scopes, $allowedHosts);
parent::__construct($this->accessTokenProvider);
Expand Down
16 changes: 5 additions & 11 deletions tests/PhpLeagueAccessTokenProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ protected function setUp(): void
);
}

public function testPassingEmptyScopesThrowsException(): void
{
$this->expectException(\InvalidArgumentException::class);
$tokenProvider = new PhpLeagueAccessTokenProvider(new ClientCredentialContext('', '', ''), []);
}

public function testPassingMultipleScopes(): void
{
$tokenProvider = new PhpLeagueAccessTokenProvider(new ClientCredentialContext(
Expand All @@ -55,7 +49,7 @@ public function testGetAuthorizationTokenWithSuccessfulTokenResponse(): void
{
$oauthContexts = $this->getOauthContexts();
foreach ($oauthContexts as $tokenRequestContext) {
$tokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext, ['https://graph.microsoft.com/.default']);
$tokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext);
$mockResponses = [
function (Request $request) use ($tokenRequestContext) {
$expectedUrl = 'https://login.microsoftonline.com/tenantId/oauth2/v2.0/token';
Expand All @@ -77,7 +71,7 @@ public function testGetAuthorizationTokenCachesInMemory(): void
{
$oauthContexts = $this->getOauthContexts();
foreach ($oauthContexts as $tokenRequestContext) {
$tokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext, ['https://graph.microsoft.com/.default']);
$tokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext);
$mockResponses = [
new Response(200, [], json_encode(['access_token' => 'abc', 'expires_in' => 5])),
new Response(200, [], json_encode(['access_token' => 'xyz', 'expires_in' => 5]))
Expand All @@ -89,17 +83,17 @@ public function testGetAuthorizationTokenCachesInMemory(): void
}
}

public function testGetAuthorizationTokenEmptyWhenNotHostAllowed(): void
public function testGetAuthorizationTokenWhenAllowedHostsNotDefined(): void
{
$oauthContexts = $this->getOauthContexts();
foreach ($oauthContexts as $tokenRequestContext) {
$tokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext, ['https://graph.microsoft.com/.default']);
$tokenProvider = new PhpLeagueAccessTokenProvider($tokenRequestContext);
$mockResponses = [
new Response(200, [], json_encode(['access_token' => 'abc', 'expires_in' => 5])),
new Response(200, [], json_encode(['access_token' => 'xyz', 'expires_in' => 5]))
];
$tokenProvider->getOauthProvider()->setHttpClient($this->getMockHttpClient($mockResponses));
$this->assertEquals(null, $tokenProvider->getAuthorizationTokenAsync('https://example.com')->wait());
$this->assertEquals('abc', $tokenProvider->getAuthorizationTokenAsync('https://example.com')->wait());
// Second call happens before token expires. We should get the existing access token
$this->assertEquals('abc', $tokenProvider->getAuthorizationTokenAsync('https://graph.microsoft.com')->wait());
}
Expand Down