-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthorizationCodeFlow.ps1
More file actions
120 lines (99 loc) · 3.89 KB
/
Copy pathAuthorizationCodeFlow.ps1
File metadata and controls
120 lines (99 loc) · 3.89 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
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
# From https://github.com/globalsign/OAuth-2.0-client-examples/blob/master/PowerShell/Powershell-example.ps1
# Thank you for a great example!!
# configuration
# enable verbose output
$VerbosePreference = "Continue"
function AuthorizationCodeFlow([string] $domain, [PSCustomObject] $clientreq,[PSCustomObject] $clientres)
{
# authorization server metadata
$metadata = Invoke-RestMethod -Uri $("https://" + $domain.Trim() + "/.well-known/openid-configuration")
Write-Verbose "metadata.json: $($metadata)"
# windows forms dependencies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Web
# create window for embedded browser
$form = New-Object Windows.Forms.Form
$form.Width = 640
$form.Height = 480
$web = New-Object Windows.Forms.WebBrowser
$web.Size = $form.ClientSize
$web.Anchor = "Left,Top,Right,Bottom"
$form.Controls.Add($web)
# global for collecting authorization code response
$Global:redirect_uri = $null;
$Global:clientreq = $clientreq;
# add handler for the embedded browser's Navigating event
$web.add_Navigating({
Write-Verbose "Navigating $($_.Url)"
# detect when browser is about to fetch redirect_uri
$uri = [uri] $Global:clientreq.redirect_uris[0];
if($_.Url.Authority -eq $uri.Authority) {
# collect authorization response in a global
$Global:redirect_uri = $_.Url
# cancel event and close browser window
$form.DialogResult = "OK"
$form.Close()
$_.Cancel = $true
}
})
$scope = ""
$web.Navigate("$($metadata.authorization_endpoint)?scope=$($scope)&response_type=code&redirect_uri=$($clientreq.redirect_uris[0])&client_id=$($clientres.client_id)&audience=$($clientres.audience)")
# show browser window, waits for window to close
if($form.ShowDialog() -ne "OK") {
Write-Verbose "WebBrowser: Canceled"
return
}
if(-not $Global:redirect_uri) {
Write-Verbose "WebBrowser: redirect_uri is null"
return
}
# decode query string of authorization code response
$response = [Web.HttpUtility]::ParseQueryString($Global:redirect_uri.Query)
if(-not $response.Get("code")) {
Write-Verbose "WebBrowser: authorization code is null"
return
}
return $response.Get("code");
}
function LogoutUser([string] $domain, [PSCustomObject] $clientreq,[PSCustomObject] $clientres)
{
# windows forms dependencies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Web
# create window for embedded browser
$form = New-Object Windows.Forms.Form
$form.Width = 640
$form.Height = 480
$web = New-Object Windows.Forms.WebBrowser
$web.Size = $form.ClientSize
$web.Anchor = "Left,Top,Right,Bottom"
$form.Controls.Add($web)
# global for collecting authorization code response
$Global:redirect_uri = $null;
$Global:clientreq = $clientreq;
# add handler for the embedded browser's Navigating event
$web.add_Navigating({
Write-Verbose "Navigating $($_.Url)"
# detect when browser is about to fetch redirect_uri
$uri = [uri] $Global:clientreq.redirect_uris[0];
if($_.Url.Authority -eq $uri.Authority) {
# collect authorization response in a global
$Global:redirect_uri = $_.Url
# cancel event and close browser window
$form.DialogResult = "OK"
$form.Close()
$_.Cancel = $true
}
})
$web.Navigate("$($domain)/v2/logout?returnTo=$($clientreq.redirect_uris[0])&client_id=$($clientres.client_id)")
# show browser window, waits for window to close
if($form.ShowDialog() -ne "OK") {
Write-Verbose "WebBrowser: Canceled"
return
}
if(-not $Global:redirect_uri) {
Write-Verbose "WebBrowser: redirect_uri is null"
return
}
}