-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrosoft_graph.php
71 lines (59 loc) · 2.38 KB
/
microsoft_graph.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\Cache\CacheItemPoolInterface;
/**
* Provides support for acquiring Microsoft Graph access tokens using the confidential client application sign-in flow.
*
* @link https://learn.microsoft.com/en-us/dotnet/api/microsoft.identity.client.confidentialclientapplication?view=azure-dotnet-preview Original API which inspired this class.
*/
class ConfidentialClientApplication {
private string $tenantId;
private string $clientId;
private string $clientSecret;
private CacheItemPoolInterface $pool;
function __construct(string $tenantId, string $clientId, string $clientSecret,
CacheItemPoolInterface $pool) {
$this->tenantId = $tenantId;
$this->clientId = $clientId;
$this->clientSecret = $clientSecret;
$this->pool = $pool;
}
/**
* Obtains an access token for Microsoft Graph using the client credentials flow.
*
* @return string The access token
*
* @throws GuzzleException If there's an error with the network request.
*/
function acquireToken(): string {
$item = $this->pool->getItem('MS365_ConfidentialClientAccessToken');
if ($item->isHit()) {
$token = $item->get();
} else {
error_log('Acquiring access token from MS365 API');
$guzzle = new Client();
$url = 'https://login.microsoftonline.com/' . $this->tenantId . '/oauth2/v2.0/token';
$token = json_decode($guzzle->post($url, [
'form_params' => [
'client_id' => $this->clientId,
'client_secret' => $this->clientSecret,
'scope' => 'https://graph.microsoft.com/.default',
'grant_type' => 'client_credentials',
],
])->getBody()->getContents());
$accessToken = $token->access_token;
$parts = explode('.', $accessToken);
$payload = $parts[1];
$payload = base64_decode($payload);
$payload = json_decode($payload);
$expires = $payload->exp;
$expiresAt = new DateTimeImmutable("@$expires");
$item->set($accessToken);
$item->expiresAt($expiresAt);
$this->pool->save($item);
$token = $accessToken;
}
return $token;
}
}