-
Notifications
You must be signed in to change notification settings - Fork 140
/
utils.ps1
66 lines (52 loc) · 1.58 KB
/
utils.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
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license. See LICENSE file in the project root for full license information.
#Requires -RunAsAdministrator
#Requires -Version 4.0
Param(
[parameter(Mandatory=$true , Position=0)]
[ValidateSet("Generate-AccessToken")]
[string]
$Command,
[parameter()]
[string]
$url
)
function Usage {
Write-Host "Commands:"
Write-Host "`tGenerate-AccessToken"
}
function Generate-AccessToken-Usage {
'Generate-AccessToken -url <apiUrl>'
Write-Host "-url:`t The url of the api that the key should be generated for."
}
function Generate-AccessToken-ParameterCheck {
if ([system.string]::IsNullOrEmpty($url)) {
Generate-AccessToken-Usage
Exit
}
}
function Generate-AccessToken($apiUrl) {
$res = Invoke-WebRequest "$apiUrl/security/api-keys" -UseBasicParsing -UseDefaultCredentials -SessionVariable sess;
$hTok = $res.headers."XSRF-TOKEN";
if ($hTok -is [array]) {
$hTok = $hTok[0]
}
$h = @{};
$h."XSRF-TOKEN" = $htok;
$res2 = Invoke-WebRequest "$apiUrl/security/api-keys" -UseBasicParsing -Headers $h -Method Post -UseDefaultCredentials -ContentType "application/json" -WebSession $sess -Body '{"expires_on": ""}';
$jObj = ConvertFrom-Json ([System.Text.Encoding]::UTF8.GetString($res2.content))
return $jObj.access_token;
}
switch($Command)
{
"Generate-AccessToken"
{
Generate-AccessToken-ParameterCheck
$key = Generate-AccessToken $url
return $key
}
default
{
Usage
}
}