Skip to content

Commit

Permalink
Merge pull request #38 from microsoft/dev
Browse files Browse the repository at this point in the history
v0.8.1
  • Loading branch information
Ndiritu authored Jun 5, 2023
2 parents 92c6e8d + cae071d commit 17e7827
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto-merge-dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
steps:
- name: Dependabot metadata
id: metadata
uses: dependabot/fetch-metadata@v1.4.0
uses: dependabot/fetch-metadata@v1.5.1
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"

Expand Down
18 changes: 14 additions & 4 deletions src/Oauth/ProviderFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Microsoft\Kiota\Authentication\Oauth;

use InvalidArgumentException;
use League\OAuth2\Client\Grant\GrantFactory;
use League\OAuth2\Client\Provider\GenericProvider;

Expand All @@ -11,18 +12,27 @@ class ProviderFactory
* Initialises a PHP League provider for the Microsoft Identity platform
* @param TokenRequestContext $tokenRequestContext
* @param array<string, object> $collaborators
* @param string $tokenServiceBaseUrl Base URL for the token and authorize endpoint. Defaults to
* https://login.microsoftonline.com
* @param string $userInfoServiceBaseUrl Base URL for the user info endpoint. Defaults to
* https://graph.microsoft.com
* @return GenericProvider
*/
public static function create(TokenRequestContext $tokenRequestContext, array $collaborators = []): GenericProvider
public static function create(
TokenRequestContext $tokenRequestContext,
array $collaborators = [],
string $tokenServiceBaseUrl = 'https://login.microsoftonline.com',
string $userInfoServiceBaseUrl = 'https://graph.microsoft.com'
): GenericProvider
{
$grantFactory = new GrantFactory();
// Add our custom grant type to the registry
$grantFactory->setGrant('urn:ietf:params:Oauth:grant-type:jwt-bearer', new OnBehalfOfGrant());

return new GenericProvider([
'urlAccessToken' => "https://login.microsoftonline.com/{$tokenRequestContext->getTenantId()}/oauth2/v2.0/token",
'urlAuthorize' => "https://login.microsoftonline.com/{$tokenRequestContext->getTenantId()}/oauth2/v2.0/authorize",
'urlResourceOwnerDetails' => 'https://graph.microsoft.com/oidc/userinfo',
'urlAccessToken' => "$tokenServiceBaseUrl/{$tokenRequestContext->getTenantId()}/oauth2/v2.0/token",
'urlAuthorize' => "$tokenServiceBaseUrl/{$tokenRequestContext->getTenantId()}/oauth2/v2.0/authorize",
'urlResourceOwnerDetails' => "$userInfoServiceBaseUrl/oidc/userinfo",
'accessTokenResourceOwnerId' => 'id_token'
], $collaborators + [
'grantFactory' => $grantFactory
Expand Down
33 changes: 32 additions & 1 deletion tests/Oauth/ProviderFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
namespace Microsoft\Kiota\Authentication\Test\Oauth;

use GuzzleHttp\Client;
use InvalidArgumentException;
use League\OAuth2\Client\Provider\GenericProvider;
use League\OAuth2\Client\Token\AccessToken;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
use Microsoft\Kiota\Authentication\Oauth\ProviderFactory;
use PHPUnit\Framework\TestCase;

class ProviderFactoryTest extends TestCase
{
public function testCustomHttpClient()
public function testCustomHttpClient(): void
{
$httpClient = new Client();

Expand All @@ -22,4 +25,32 @@ public function testCustomHttpClient()

self::assertSame($httpClient, $provider->getHttpClient());
}

public function testDefaultConfiguration(): void
{
$oauthProvider = ProviderFactory::create(new ClientCredentialContext(
'1', 'client', 'secret'
));
$this->assertInstanceOf(GenericProvider::class, $oauthProvider);
$this->assertEquals("https://graph.microsoft.com/oidc/userinfo", $oauthProvider->getResourceOwnerDetailsUrl(
$this->createMock(AccessToken::class)
));
$this->assertEquals("https://login.microsoftonline.com/1/oauth2/v2.0/token", $oauthProvider->getBaseAccessTokenUrl([]));
$this->assertEquals("https://login.microsoftonline.com/1/oauth2/v2.0/authorize", $oauthProvider->getBaseAuthorizationUrl());
}

public function testUpdatingBaseURLs(): void
{
$chinaCloudUserInfo = 'https://microsoftgraph.chinacloudapi.cn';
$chinaCloudTokenService = 'https://login.chinacloudapi.cn';
$oauthProvider = ProviderFactory::create(new ClientCredentialContext(
'1', 'client', 'secret'
), [], $chinaCloudTokenService, $chinaCloudUserInfo);
$this->assertInstanceOf(GenericProvider::class, $oauthProvider);
$this->assertEquals("$chinaCloudUserInfo/oidc/userinfo",$oauthProvider->getResourceOwnerDetailsUrl(
$this->createMock(AccessToken::class)
));
$this->assertEquals("$chinaCloudTokenService/1/oauth2/v2.0/token", $oauthProvider->getBaseAccessTokenUrl([]));
$this->assertEquals("$chinaCloudTokenService/1/oauth2/v2.0/authorize", $oauthProvider->getBaseAuthorizationUrl());
}
}

0 comments on commit 17e7827

Please sign in to comment.