|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Metrogistics\AzureSocialite; |
|
4
|
|
|
|
|
5
|
|
|
class GraphAPI |
|
6
|
|
|
{ |
|
7
|
|
|
// Our AAD instance with all its useful information |
|
8
|
|
|
protected $azureActiveDirectory; |
|
9
|
|
|
protected $accessToken; |
|
10
|
|
|
protected $graphApiBaseUrl = 'https://graph.microsoft.com'; |
|
11
|
|
|
protected $graphApiVersion = 'v1.0'; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($azureActiveDirectory) |
|
14
|
|
|
{ |
|
15
|
|
|
// IF they didnt pre-populate all our AAD stuff then make a new one for the common tenant |
|
16
|
|
|
if (! $azureActiveDirectory) { |
|
17
|
|
|
$azureActiveDirectory = new AzureActiveDirectory(); |
|
18
|
|
|
} |
|
19
|
|
|
$this->azureActiveDirectory = $azureActiveDirectory; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
// Users can set an access token or let the app default to its own |
|
23
|
|
|
public function setAccessToken($token) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->accessToken = $token; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
protected function getAccessToken() |
|
29
|
|
|
{ |
|
30
|
|
|
if (! $this->accessToken) { |
|
31
|
|
|
$this->authenticateAsApplication(); |
|
32
|
|
|
} |
|
33
|
|
|
return $this->accessToken; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
protected function getGuzzleClientParameters() |
|
37
|
|
|
{ |
|
38
|
|
|
$parameters = [ |
|
39
|
|
|
'headers' => [ |
|
40
|
|
|
'Authorization' => 'Bearer '.$this->getAccessToken(), |
|
41
|
|
|
], |
|
42
|
|
|
]; |
|
43
|
|
|
|
|
44
|
|
|
return $parameters; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
protected function authenticateAsApplication() |
|
48
|
|
|
{ |
|
49
|
|
|
$guzzle = new \GuzzleHttp\Client(); |
|
50
|
|
|
$url = $this->azureActiveDirectory->tokenEndpoint; |
|
51
|
|
|
$parameters = [ |
|
52
|
|
|
'form_params' => [ |
|
53
|
|
|
'scope' => $this->graphApiBaseUrl.'/.default', |
|
54
|
|
|
'grant_type' => 'client_credentials', |
|
55
|
|
|
'client_id' => env('AZURE_AD_CLIENT_ID'), |
|
56
|
|
|
'client_secret' => env('AZURE_AD_CLIENT_SECRET'), |
|
57
|
|
|
] |
|
58
|
|
|
]; |
|
59
|
|
|
$response = $guzzle->post($url, $parameters); |
|
60
|
|
|
$responseObject = json_decode($response->getBody()); |
|
61
|
|
|
$this->setAccessToken($responseObject->access_token); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
protected function getUrl($url) |
|
65
|
|
|
{ |
|
66
|
|
|
$guzzle = new \GuzzleHttp\Client(); |
|
67
|
|
|
$response = $guzzle->get($url, $this->getGuzzleClientParameters()); |
|
68
|
|
|
$json = $response->getBody(); |
|
69
|
|
|
$data = json_decode($json, true); |
|
70
|
|
|
return $data; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
protected function buildUrl($pieces = []) |
|
74
|
|
|
{ |
|
75
|
|
|
$url = $this->graphApiBaseUrl; |
|
76
|
|
|
// Include version before any pieces of the url |
|
77
|
|
|
array_unshift($pieces, $this->graphApiVersion); |
|
78
|
|
|
// Build the url |
|
79
|
|
|
foreach($pieces as $piece) { |
|
80
|
|
|
$url .= '/'.$piece; |
|
81
|
|
|
} |
|
82
|
|
|
return $url; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function getMe() |
|
86
|
|
|
{ |
|
87
|
|
|
$pieces = ['me']; |
|
88
|
|
|
return $this->getUrl($this->buildUrl($pieces)); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function listUsers() |
|
92
|
|
|
{ |
|
93
|
|
|
$pieces = ['users']; |
|
94
|
|
|
return $this->getUrl($this->buildUrl($pieces)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
public function getUser($user) |
|
98
|
|
|
{ |
|
99
|
|
|
$pieces = ['users', $user]; |
|
100
|
|
|
return $this->getUrl($this->buildUrl($pieces)); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function getUserGroups($user) |
|
104
|
|
|
{ |
|
105
|
|
|
$pieces = ['users', $user, 'memberOf']; |
|
106
|
|
|
return $this->getUrl($this->buildUrl($pieces)); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function listGroups() |
|
110
|
|
|
{ |
|
111
|
|
|
$pieces = ['groups']; |
|
112
|
|
|
return $this->getUrl($this->buildUrl($pieces)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getGroup($group) |
|
116
|
|
|
{ |
|
117
|
|
|
$pieces = ['groups', $group]; |
|
118
|
|
|
return $this->getUrl($this->buildUrl($pieces)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
// generalized function to parse out graph odata response values and index them by property |
|
122
|
|
|
public function parseGraphDataKeyByProperty($graphData, $key = 'value', $property = 'id') |
|
123
|
|
|
{ |
|
124
|
|
|
$parsed = []; |
|
125
|
|
|
if (isset($graphData[$key]) && is_array($graphData[$key])) { |
|
126
|
|
|
foreach($graphData[$key] as $value) { |
|
127
|
|
|
if (isset($value[$property]) && $value[$property]) { |
|
128
|
|
|
$parsed[$value[$property]] = $value; |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
return $parsed; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
// specific example to parse group list |
|
136
|
|
|
public function parseGroupListByName($graphData) |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->parseGraphDataKeyByProperty($graphData, 'value', 'displayName'); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|