-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEmbeddedBrowser.ps1
More file actions
61 lines (57 loc) · 2.39 KB
/
EmbeddedBrowser.ps1
File metadata and controls
61 lines (57 loc) · 2.39 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
function New-BrowserRequest {
[CmdletBinding()]
param()
begin {
Add-Type -Path "$PSScriptRoot\Helper.cs" -ReferencedAssemblies "System.Web","PresentationCore","PresentationFramework","WindowsBase","System.Xaml" -ErrorAction Stop
}
process {
[Helper.EmbeddedBrowserRequest]::new()
}
}
function StartEmbeddedBrowserRequest {
[CmdletBinding()]
param(
[parameter(Mandatory=$true,Position=0)]
[Uri]
$Authority,
[parameter(Mandatory=$true,Position=1)]
[PSTypeName("OAuth2.ClientConfig")]
$Client,
[Parameter()]
[System.Collections.IDictionary]
$QueryString
)
begin {
$local:metadata = Get-Metadata -Authority $Authority -ErrorAction Stop
$local:redirect_uri = $Client.RedirectUri
}
process {
$local:query = $QueryString |
Add-QueryString "redirect_uri" $local:redirect_uri
if([string]::IsNullOrWhiteSpace($Client.Credential.Password)) {
$local:verifier = $null
$local:challenge = New-CodeChallenge ([ref]$verifier)
$local:query = $QueryString |
Add-QueryString "code_challenge" $local:challenge |
Add-QueryString "code_challenge_method" "S256"
} else {
$local:verifier = $null
}
$local:authorizationRequest = [UriBuilder]::new($local:metadata.authorization_endpoint)
$local:authorizationRequest.Query = (ConvertTo-QueryString $local:query)
Write-Verbose "StartEmbeddedBrowserRequest GET $local:authorizationRequest"
$local:response = (New-BrowserRequest -ErrorAction Stop).AuthorizationRequest($local:authorizationRequest.Uri)
if($local:response) {
Write-Verbose "StartEmbeddedBrowserRequest $($local:response)"
$local:response.Query | ConvertFrom-QueryString | Select-QueryString "code" | ? { $_ } | % {
[PSCustomObject]@{
"PSTypeName" = "OAuth2.Code"
"Credential" = [pscredential]::new("code", (ConvertTo-SecureString -AsPlainText -Force -String $_)).GetNetworkCredential()
"RedirectUri" = $local:redirect_uri
"Verifier" = $local:verifier
}
}
}
}
}
Export-ModuleMember -Function "*"