Skip to content

Commit ff3498a

Browse files
authored
Merge pull request #167 from microsoftgraph/dev
Release 2.1.0
2 parents 34f0482 + 93ca783 commit ff3498a

File tree

7 files changed

+74
-9
lines changed

7 files changed

+74
-9
lines changed

CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
### Changed
13+
14+
## [2.1.0]
15+
16+
### Added
17+
18+
- Enables initialising `GraphPhpLeagueAccessTokenProvider` with custom cache.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ To install the `microsoft-graph-core` library with Composer, either run `compose
88
```
99
{
1010
"require": {
11-
"microsoft/microsoft-graph-core": "^2.0.3"
11+
"microsoft/microsoft-graph-core": "^2.1.0"
1212
}
1313
}
1414
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
],
1414
"require": {
1515
"php": "^8.0 || ^7.4",
16-
"microsoft/kiota-authentication-phpleague": "^1.0.1",
16+
"microsoft/kiota-authentication-phpleague": "^1.1.0",
1717
"microsoft/kiota-http-guzzle": "^1.1.0",
1818
"microsoft/kiota-serialization-json": "^1.0.1",
1919
"microsoft/kiota-serialization-text": "^1.0.1",

src/Authentication/GraphPhpLeagueAccessTokenProvider.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace Microsoft\Graph\Core\Authentication;
1010

1111

12-
use InvalidArgumentException;
1312
use Microsoft\Graph\Core\NationalCloud;
13+
use Microsoft\Kiota\Authentication\Cache\AccessTokenCache;
1414
use Microsoft\Kiota\Authentication\Oauth\ProviderFactory;
1515
use Microsoft\Kiota\Authentication\Oauth\TokenRequestContext;
1616
use Microsoft\Kiota\Authentication\PhpLeagueAccessTokenProvider;
@@ -38,13 +38,16 @@ class GraphPhpLeagueAccessTokenProvider extends PhpLeagueAccessTokenProvider
3838
* @param array<string> $scopes if left empty, it's set to ["https://[graph national cloud host]/.default"] scope
3939
* @param string $nationalCloud Defaults to https://graph.microsoft.com. See
4040
* https://learn.microsoft.com/en-us/graph/deployments
41+
* @param AccessTokenCache|null $accessTokenCache Defaults to an in-memory cache if null
4142
*/
4243
public function __construct(
4344
TokenRequestContext $tokenRequestContext,
4445
array $scopes = [],
45-
string $nationalCloud = NationalCloud::GLOBAL
46+
string $nationalCloud = NationalCloud::GLOBAL,
47+
?AccessTokenCache $accessTokenCache = null
4648
)
4749
{
50+
$nationalCloud = empty($nationalCloud) ? NationalCloud::GLOBAL : $nationalCloud;
4851
$allowedHosts = [
4952
"graph.microsoft.com",
5053
"graph.microsoft.us",
@@ -61,6 +64,24 @@ public function __construct(
6164
$tokenBaseServiceUrl,
6265
$nationalCloud
6366
);
64-
parent::__construct($tokenRequestContext, $scopes, $allowedHosts, $oauthProvider);
67+
parent::__construct($tokenRequestContext, $scopes, $allowedHosts, $oauthProvider, $accessTokenCache);
68+
}
69+
70+
/**
71+
* Get an instance of GraphPhpLeagueAccessTokenProvider with a custom cache
72+
*
73+
* @param AccessTokenCache $accessTokenCache
74+
* @param TokenRequestContext $tokenRequestContext
75+
* @param array<string> $scopes
76+
* @return GraphPhpLeagueAccessTokenProvider
77+
*/
78+
public static function createWithCache(
79+
AccessTokenCache $accessTokenCache,
80+
TokenRequestContext $tokenRequestContext,
81+
array $scopes = []
82+
): self
83+
{
84+
return new GraphPhpLeagueAccessTokenProvider(
85+
$tokenRequestContext, $scopes, NationalCloud::GLOBAL, $accessTokenCache);
6586
}
6687
}

src/Authentication/GraphPhpLeagueAuthenticationProvider.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
namespace Microsoft\Graph\Core\Authentication;
1010

1111
use Microsoft\Graph\Core\NationalCloud;
12-
use Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider;
1312
use Microsoft\Kiota\Authentication\Oauth\TokenRequestContext;
13+
use Microsoft\Kiota\Authentication\PhpLeagueAuthenticationProvider;
1414

1515
/**
1616
* Class GraphPhpLeagueAuthenticationProvider
@@ -19,7 +19,7 @@
1919
* @license https://opensource.org/licenses/MIT MIT License
2020
* @link https://developer.microsoft.com/graph
2121
*/
22-
class GraphPhpLeagueAuthenticationProvider extends BaseBearerTokenAuthenticationProvider
22+
class GraphPhpLeagueAuthenticationProvider extends PhpLeagueAuthenticationProvider
2323
{
2424
/**
2525
* @param TokenRequestContext $tokenRequestContext
@@ -34,6 +34,7 @@ public function __construct(
3434
)
3535
{
3636
$accessTokenProvider = new GraphPhpLeagueAccessTokenProvider($tokenRequestContext, $scopes, $nationalCloud);
37-
parent::__construct($accessTokenProvider);
37+
parent::__construct($tokenRequestContext, $scopes, $accessTokenProvider->getAllowedHosts());
3838
}
39+
3940
}

src/GraphConstants.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ final class GraphConstants
2525
const REST_ENDPOINT = "https://graph.microsoft.com/";
2626

2727
// Define HTTP request constants
28-
const SDK_VERSION = "2.0.3";
28+
const SDK_VERSION = "2.1.0";
2929

3030
// Define error constants
3131
const MAX_PAGE_SIZE = 999;

tests/Authentication/GraphPhpLeagueAccessTokenProviderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@
22

33
namespace Microsoft\Graph\Core\Test\Authentication;
44

5+
use GuzzleHttp\Client;
6+
use GuzzleHttp\Handler\MockHandler;
7+
use GuzzleHttp\Psr7\Request;
8+
use GuzzleHttp\Psr7\Response;
59
use League\OAuth2\Client\Token\AccessToken;
610
use Microsoft\Graph\Core\Authentication\GraphPhpLeagueAccessTokenProvider;
711
use Microsoft\Graph\Core\NationalCloud;
12+
use Microsoft\Kiota\Authentication\Cache\InMemoryAccessTokenCache;
813
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
914
use PHPUnit\Framework\TestCase;
1015

@@ -44,4 +49,24 @@ public function testCorrectOAuthEndpointsSet(): void
4449
$this->assertEquals("$baseUrl/tenant/oauth2/v2.0/authorize", $tokenProvider->getOauthProvider()->getBaseAuthorizationUrl());
4550
}
4651

52+
public function testCreateWithCache(): void
53+
{
54+
$tokenRequestContext = new ClientCredentialContext('tenant', 'clientId', 'secret');
55+
$cache = new InMemoryAccessTokenCache();
56+
$tokenProvider = GraphPhpLeagueAccessTokenProvider::createWithCache($cache, $tokenRequestContext, ['https://graph.microsoft.com/.default']);
57+
$mockResponses = [
58+
function (Request $request) use ($tokenRequestContext) {
59+
parse_str($request->getBody()->getContents(), $requestBodyMap);
60+
$expectedBody = array_merge($tokenRequestContext->getParams(), [
61+
'scope' => 'https://graph.microsoft.com/.default'
62+
]);
63+
$this->assertEquals($expectedBody, $requestBodyMap);
64+
return new Response(200, [], json_encode(['access_token' => 'xyz', 'expires_in' => 1]));
65+
},
66+
];
67+
$tokenProvider->getOauthProvider()->setHttpClient(new Client(['handler' => new MockHandler($mockResponses)]));
68+
$tokenProvider->getAuthorizationTokenAsync('https://graph.microsoft.com/me');
69+
$this->assertEquals('xyz', $cache->getTokenWithContext($tokenRequestContext)->getToken());
70+
}
71+
4772
}

0 commit comments

Comments
 (0)