forked from VeeamHub/vbo365-rest-self-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
microsoft.php
107 lines (90 loc) · 2.88 KB
/
microsoft.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
<?php
require_once('vendor/autoload.php');
set_time_limit(0);
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use GuzzleHttp\Exception\RequestException;
$scope = 'Directory.AccessAsUser.All User.ReadWrite.All offline_access';
function getDeviceCode($clientid, $tenantid, $scope) {
try {
$client = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/'.$tenantid.'/oauth2/v2.0/devicecode';
$response = $client->request('POST', $url, [
'form_params' => [
'client_id' => $clientid,
'scope' => $scope,
],
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'http_errors' => false,
'verify' => false
]);
if ($response->getStatusCode() === 200) {
$result = json_decode($response->getBody(), true);
} else {
$result = $response['error'];
}
return $result;
} catch (RequestException $e) {
if ($e->hasResponse()) {
$exception = (string) $e->getResponse()->getBody();
$exception = json_decode($exception, true);
echo 'Error: ' . $exception['error'] . '<br>' . $exception['error_description'];
} else {
echo $e->getMessage();
}
}
}
function getToken($clientid, $tenantid, $devicecode) {
try {
$client = new \GuzzleHttp\Client();
$url = 'https://login.microsoftonline.com/'.$tenantid.'/oauth2/v2.0/token';
$response = $client->request('POST', $url, [
'form_params' => [
'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code',
'client_id' => $clientid,
'device_code' => $devicecode,
],
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/x-www-form-urlencoded',
],
'verify' => false
]);
if ($response->getStatusCode() === 200) {
$result = json_decode($response->getBody(), true);
} else {
$result = $response->getStatusCode();
}
return $result;
} catch (RequestException $e) {
if ($e->hasResponse()) {
$exception = (string) $e->getResponse()->getBody();
$exception = json_decode($exception, true);
return $exception;
} else {
echo $e->getMessage();
}
}
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
if (isset($_POST['action'])) { $action = $_POST['action']; }
if (isset($_POST['assertion'])) { $assertion = $_POST['assertion']; }
if (isset($_POST['clientid'])) { $clientid = $_POST['clientid']; }
if (isset($_POST['tenantid'])) { $tenantid = $_POST['tenantid']; }
if (isset($_POST['devicecode'])) { $devicecode = $_POST['devicecode']; }
/* Microsoft Calls */
if ($action == 'getdevicecode') {
$code = getDeviceCode($clientid, $tenantid, $scope);
echo json_encode($code);
}
if ($action == 'gettoken') {
$token = getToken($clientid, $tenantid, $devicecode);
echo json_encode($token);
}
} else {
die();
}
?>