-
-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathAADTokenProvider.php
221 lines (186 loc) · 6.01 KB
/
AADTokenProvider.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
namespace Office365\Runtime\Auth;
use Exception;
use Firebase\JWT\JWT;
use Office365\Runtime\Http\HttpMethod;
use Office365\Runtime\Http\RequestOptions;
use Office365\Runtime\Http\Requests;
/**
* OAuth2 provider to acquire the access token from AAD
*/
class AADTokenProvider extends BaseTokenProvider
{
/**
* @var string
*/
private static $TokenEndpoint = '/oauth2/token';
/**
* @var string
*/
private static $TokenEndpointV2 = '/oauth2/v2.0/token';
/**
* @var string
*/
public static $AuthorityUrl = "https://login.microsoftonline.com/";
/**
* @var string
*/
private static $AuthorizeEndpoint = '/oauth2/authorize';
/**
* @var string
*/
private $authorityUrl;
/**
* @param string $tenant
*/
public function __construct($tenant)
{
$this->authorityUrl = self::$AuthorityUrl . $tenant;
}
public function getTokenUrl($useV2){
return $this->authorityUrl . ($useV2 ? self::$TokenEndpointV2: self::$TokenEndpoint);
}
/**
* @param string $resource
* @param string $clientId
* @param string $clientSecret
* @param string $refreshToken
* @param string $redirectUri
* @throws Exception
*/
public function acquireRefreshToken($resource, $clientId, $clientSecret, $refreshToken, $redirectUri)
{
$parameters = array(
'grant_type' => 'refresh_token',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'resource' => $resource,
'redirect_uri' => $redirectUri,
'refresh_token' => $refreshToken
);
return $this->acquireToken($parameters);
}
/**
* @param string $resource
* @param ClientCredential $clientCredentials
* @throws Exception
*/
public function acquireTokenForClientCredential($resource, $clientCredentials, $scopes)
{
$parameters = array(
'grant_type' => 'client_credentials',
'client_id' => $clientCredentials->ClientId,
'client_secret' => $clientCredentials->ClientSecret,
'scope' => implode(" ", $scopes),
'resource' => $resource
);
return $this->acquireToken($parameters);
}
/**
* @param CertificateCredentials $credentials
* @throws Exception
*/
public function acquireTokenForClientCertificate($credentials){
$header = [
'x5t' => base64_encode(hex2bin($credentials->Thumbprint)),
];
$now = time();
$payload = [
'aud' => $this->getTokenUrl(true),
'exp' => $now + 360,
'iat' => $now,
'iss' => $credentials->ClientId,
'jti' => bin2hex(random_bytes(20)),
'nbf' => $now,
'sub' => $credentials->ClientId,
];
$jwt = JWT::encode($payload, str_replace('\n', "\n", $credentials->PrivateKey), 'RS256', null, $header);
$params['client_assertion_type'] = 'urn:ietf:params:oauth:client-assertion-type:jwt-bearer';
$params['client_assertion'] = $jwt;
$params['grant_type'] = "client_credentials";
$params['scope'] = implode(" ", $credentials->Scope);
return $this->acquireToken($params, true);
}
/**
* @param string $resource
* @param string $clientId
* @param UserCredentials $userCredentials
*
* @param FALSE|string $clientSecret
* Use $clientSecret in case your app is a confidential client.
*
* @throws Exception Resource owner password credential (ROPC) grant
* (https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc)
*/
public function acquireTokenForPassword($resource, $clientId, $userCredentials, $clientSecret = FALSE)
{
$parameters = array(
'grant_type' => 'password',
'client_id' => $clientId,
'username' => $userCredentials->Username,
'password' => $userCredentials->Password,
'resource' => $resource
);
if($clientSecret) {
$parameters += array('client_secret' => $clientSecret);
}
return $this->acquireToken($parameters);
}
/**
* @param string $resource
* @param string $clientId
* @param string $clientSecret
* @param string $code
* @param string $redirectUrl
* @throws Exception
*/
public function acquireTokenByAuthorizationCode($resource, $clientId, $clientSecret, $code, $redirectUrl)
{
$parameters = array(
'grant_type' => 'authorization_code',
'client_id' => $clientId,
'client_secret' => $clientSecret,
'code' => $code,
'resource' => $resource,
"redirect_uri" => $redirectUrl
);
return $this->acquireToken($parameters);
}
/**
* Acquires the access token
* @param array $parameters
* @param bool $useV2
* @return mixed
* @throws Exception
*/
public function acquireToken($parameters, $useV2=false)
{
$request = $this->prepareTokenRequest($parameters, $useV2);
$response = Requests::execute($request);
$response->validate();
return $this->normalizeToken($response->getContent());
}
/**
* @param array $parameters
* @param bool $useV2
* @return RequestOptions
*/
private function prepareTokenRequest($parameters, $useV2)
{
$tokenUrl = $this->getTokenUrl($useV2);
$request = new RequestOptions($tokenUrl);
$request->ensureHeader('content-Type', 'application/x-www-form-urlencoded');
$request->Method = HttpMethod::Post;
$request->Data = http_build_query($parameters);
return $request;
}
/**
* Parse the id token that represents a JWT token that contains information about the user
* @param string $tokenValue
* @return mixed
*/
private function normalizeToken($tokenValue)
{
return json_decode($tokenValue,true);
}
}