-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathauth.ps1
71 lines (58 loc) · 2.35 KB
/
auth.ps1
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
$ErrorActionPreference = "Stop"
Write-Output "choose the account area
1> Global
2> United States government
3> Germany
4> China (operated by 21Vianet)
"
$account_area = Read-Host 'account type'
switch ($account_area) {
1 { $auth_host = "login.microsoftonline.com" }
2 { $auth_host = "login.microsoftonline.us" }
3 { $auth_host = "login.microsoftonline.de" }
4 { $auth_host = "login.chinacloudapi.cn" }
Default { exit }
}
$client_id = Read-Host 'client_id'
$client_secret = Read-Host 'client_secret'
$auth_url = "https://${auth_host}/common/oauth2/v2.0/authorize?client_id=${client_id}&response_type=code&redirect_uri=http://localhost:53682/&response_mode=query&scope=offline_access%20Files.ReadWrite.All"
$auth_code = ""
$http = [System.Net.HttpListener]::new()
$http.Prefixes.Add("http://localhost:53682/")
$http.Start()
Start-Process $auth_url
while ($http.IsListening) {
Start-Sleep -Seconds 0.1
$context = $http.GetContext()
$code = $context.Request.QueryString.Get("code")
if ($code) {
$auth_code = $code
[string]$html = "<p>success, now you can close this window</p>"
$buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
$context.Response.ContentLength64 = $buffer.Length
$context.Response.OutputStream.Write($buffer, 0, $buffer.Length)
$context.Response.OutputStream.Close()
break
}
else {
[string]$html = "<p>error, please continue <a href='${auth_url}'>here</a></p>"
$buffer = [System.Text.Encoding]::UTF8.GetBytes($html)
$context.Response.ContentLength64 = $buffer.Length
$context.Response.OutputStream.Write($buffer, 0, $buffer.Length)
$context.Response.OutputStream.Close()
}
}
Write-Output "code received, start fetching token"
$reqdata = "client_id=${client_id}&client_secret=${client_secret}&grant_type=authorization_code&code=${auth_code}&redirect_uri=http://localhost:53682/&scope=offline_access%20Files.ReadWrite.All"
$res = Invoke-RestMethod "https://${auth_host}/common/oauth2/v2.0/token" -Method "POST" -Body $reqdata
$refresh_token = $res.refresh_token
$desktop = [Environment]::GetFolderPath("Desktop")
New-Item -Path $desktop\token.txt -ItemType File -Value $refresh_token
Write-Output @"
==========
${refresh_token}
==========
this is your refresh_token, keep it safe
it has been saved on your desktop
"@
cmd /C PAUSE