-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGetTokens.ps1
More file actions
78 lines (70 loc) · 2.69 KB
/
GetTokens.ps1
File metadata and controls
78 lines (70 loc) · 2.69 KB
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
<#
Logs in to Microsoft using the Device code method
It will save the refresh token and access token as variables if it works
#>
function Get-MicrosoftTokens {
param(
[string]$ClientId = "1950a258-227b-4e31-a9cf-717495945fc2",
[string]$UserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36",
[int]$PollingInterval = 10,
[int]$PollingTimeout = 300
)
# Get device code
$body = @{
"client_id" = $ClientId
"resource" = "https://graph.microsoft.com"
}
$headers = @{
"User-Agent" = $UserAgent
}
$authResponse = Invoke-RestMethod `
-UseBasicParsing `
-Method Post `
-Uri "https://login.microsoftonline.com/common/oauth2/devicecode?api-version=1.0" `
-Headers $headers `
-Body $body
Write-Host "Enter this code in your browser" -ForegroundColor Cyan
Write-Host "`t[+] Code: " $authResponse.user_code -ForegroundColor Green
Start-Process $authResponse.verification_url
$startTime = Get-Date
$endTime = $startTime.AddSeconds($PollingTimeout)
$accessToken = $null
$refreshToken = $null
do {
# Suppress error messages from the polling request
$Tokens = $null
try {
$body=@{
"client_id" = $ClientId
"grant_type" = "urn:ietf:params:oauth:grant-type:device_code"
"code" = $authResponse.device_code
}
$Tokens = Invoke-RestMethod `
-UseBasicParsing `
-Method Post `
-Uri "https://login.microsoftonline.com/Common/oauth2/token?api-version=1.0" `
-Headers $headers `
-Body $body -ErrorAction SilentlyContinue
} catch {
# Suppress error messages from the polling request
}
if ($Tokens -ne $null) {
$accessToken = $Tokens.access_token
$refreshToken = $Tokens.refresh_token
} else {
Start-Sleep -Seconds $PollingInterval
}
} until (($accessToken -ne $null) -or (Get-Date) -gt $endTime)
if ($accessToken -ne $null) {
$secret = ConvertTo-SecureString $refreshToken -AsPlainText -Force
Set-Secret -Name "MyRefreshToken" -Secret $secret
$env:ACCESSTOKEN = $accessToken
$env:REFRESHTOKEN = $refreshToken
Write-Host "Got some tokens" -ForegroundColor Cyan
Write-Host "`t[+] Access token: $accessToken"
Write-Host "`t[+] Refresh token: $refreshToken"
} else {
Write-Warning "`t[-] Polling timed out before user signed in" -ForegroundColor Red
}
}
Get-MicrosoftTokens