-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmicrosoft_connect.php
82 lines (81 loc) · 3.04 KB
/
microsoft_connect.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
<?php
require_once ('CurlWrapper.php');
function microsoft_connect ($cookie, $login, $password) {
$wrapper = new CurlWrapper($cookie);
$body = $wrapper->get('https://intra.epitech.eu');
if (!$body) {
return 401;
}
$parts = [];
preg_match('#"https://login.microsoftonline([^"]+)"#', $body, $parts);
$url = 'https://login.microsoftonline' . $parts[1];
$ms1 = $wrapper->get($url);
preg_match("/Constants.CONTEXT = '([^']+)';/", $ms1, $parts);
if (!$ms1 || !isset($parts[1])) {
return 401;
}
$sts = $parts[1];
$urlRealm = 'https://login.microsoftonline.com/common/userrealm/?user=' . $login . '&api-version=2.1&stsRequest=' . $sts . '&checkForMicrosoftAccount=true';
$realmDatas = json_decode($wrapper->get($urlRealm));
if (!$realmDatas || !isset($realmDatas->AuthURL)) {
return 401;
}
$realmUrl = $realmDatas->AuthURL;
$epitechLoginPage = $wrapper->get($realmUrl);
preg_match('#action="/adfs([^"]+)"#', $epitechLoginPage, $parts);
if (!$epitechLoginPage || !isset($parts[1])) {
return 401;
}
$loginUrl = 'https://sts.epitech.eu/adfs' . $parts[1];
$formResult = $wrapper->post($loginUrl, [
'UserName' => $login,
'Password' => $password,
'Kmsi' => 'true',
'AuthMethod' => 'FormsAuthentication'
]);
preg_match('/action="([^"]+)"/', $formResult, $parts);
if (!$formResult || !isset($parts[1])) {
return 401;
}
$microsoftLoginUrl = $parts[1];
preg_match('/name="wa" value="([^"]+)"/', $formResult, $parts);
if (!$formResult || !isset($parts[1])) {
return 401;
}
$wa = $parts[1];
preg_match('/name="wresult" value="([^"]+)"/', $formResult, $parts);
if (!$formResult || !isset($parts[1])) {
return 401;
}
$wresult = html_entity_decode($parts[1]);
preg_match('/name="wctx" value="([^"]+)"/', $formResult, $parts);
if (!$formResult || !isset($parts[1])) {
return 401;
}
$wctx = html_entity_decode($parts[1]);
$microsoftLoginResult = $wrapper->post($microsoftLoginUrl, [
'wa' => $wa,
'wresult' => $wresult,
'wctx' => $wctx
]);
if (!$microsoftLoginResult) {
return 401;
}
if (strstr($microsoftLoginResult, "consent_accept_form") !== false)
{
preg_match('/name="ctx" value="([^"]+)"/', $microsoftLoginResult, $parts);
$ctx = $parts[1];
preg_match('/name="flowToken" value="([^"]+)"/', $microsoftLoginResult, $parts);
$flowToken = $parts[1];
preg_match('/name="canary" value="([^"]+)"/', $microsoftLoginResult, $parts);
$canary = $parts[1];
$urlParts = explode('/', str_replace('//', '/', $microsoftLoginUrl));
$targetUrl = $urlParts[0] . '//' . $urlParts[1] . '/common/Consent/Grant';
$wrapper->post($targetUrl, [
'ctx' => $ctx,
'flowToken' => $flowToken,
'canary' => $canary
]);
}
return $wrapper->getHttpResponseCode();
}