-
Notifications
You must be signed in to change notification settings - Fork 108
/
Azure.php
108 lines (91 loc) · 3.57 KB
/
Azure.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
namespace TheNetworg\OAuth2\Client\Provider;
use League\OAuth2\Client\Provider\AbstractProvider;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
use League\OAuth2\Client\Token\AccessToken;
use Psr\Http\Message\ResponseInterface;
class Azure extends AbstractProvider {
use BearerAuthorizationTrait;
public $urlLogin = "https://login.microsoftonline.com/";
public $tenant = "common";
public $urlAPI = "https://graph.windows.net/";
public $API_VERSION = "1.6";
public function getBaseAuthorizationUrl() {
return $this->urlLogin.$this->tenant."/oauth2/authorize";
}
public function getBaseAccessTokenUrl(array $params) {
return $this->urlLogin.$this->tenant."/oauth2/token";
}
protected function checkResponse(ResponseInterface $response, $data) {
if(isset($data->{'odata.error'})) {
throw new IdentityProviderException(
(isset($data->{'odata.error'}->message) ? $data->{'odata.error'}->message : $response->getReasonPhrase()),
$response->getStatusCode(),
$response
);
}
}
protected function getDefaultScopes() {
return [];
}
protected function createResourceOwner(array $response, AccessToken $token) {
return new AzureResourceOwner($response);
}
public function getResourceOwnerDetailsUrl(AccessToken $token) {
return "me";
}
public function getObjects($tenant, $ref, $objects = [], $accessToken) {
$response = $this->request('GET', $tenant."/".$ref, $accessToken, []);
if($response) {
$values = $response->value;
foreach($values as $value) {
$objects[] = $value;
}
if(isset($response->{'odata.nextLink'})) {
$nextLink = $response->{'odata.nextLink'};
return $this->getObjects($tenant, $nextLink, $objects, $accessToken);
}
else {
return $objects;
}
}
}
public function get($ref, $accessToken) {
$response = $this->request('get', $ref, $accessToken);
return $this->wrapResponse($response);
}
public function post($ref, $body, $accessToken) {
$response = $this->request('post', $ref, $accessToken, ['body' => $body]);
return $this->wrapResponse($response);
}
public function put($ref, $body, $accessToken) {
$response = $this->request('put', $ref, $accessToken, ['body' => $body]);
return $this->wrapResponse($response);
}
public function delete($ref, $accessToken) {
$response = $this->request('delete', $ref, $accessToken);
return $this->wrapResponse($response);
}
public function patch($ref, $body, $accessToken) {
$response = $this->request('patch', $ref, $accessToken, ['body' => $body]);
return $this->wrapResponse($response);
}
private function request($method, $ref, $accessToken, $options = []) {
$url = $this->urlAPI.$ref;
$url .= (strrpos($url, "?") === FALSE) ? "?" : "&";
$url .= "api-version=".$this->API_VERSION;
$request = $this->getAuthenticatedRequest($method, $url, $accessToken, $options);
$response = $this->getResponse($request);
$response = json_decode(json_encode($response));
return $response;
}
private function wrapResponse($response) {
if(empty($response)) return null;
else if(isset($response->value)) return $response->value;
else return $response;
}
public function getClientId() {
return $this->clientId;
}
}