This repository has been archived by the owner on Jan 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWestco.Security.psm1
81 lines (60 loc) · 2.12 KB
/
Westco.Security.psm1
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
$ErrorActionPreference = 'Stop'
$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
$MicroCHAP = $ScriptPath + '\MicroCHAP.dll'
if(-not(Test-Path -Path $MicroCHAP)) {
$MicroCHAP = Get-ChildItem -Path $ScriptPath -Filter "MicroCHAP.dll" -Recurse | Select-Object -First 1 -ExpandProperty FullName
}
Add-Type -Path $MicroCHAP
function Get-SscChallenge {
param(
[Parameter(Mandatory=$true)]
[string]$InstanceUrl
)
$url = "$($InstanceUrl)/sitecore/api/ssc/chapauth/challenge"
$result = Invoke-WebRequest -Uri $url -TimeoutSec 360 -UseBasicParsing
$result.Content | ConvertFrom-Json | Select-Object -ExpandProperty challenge
}
function Get-SscResult {
param(
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$true)]
[string]$SharedSecret,
[Parameter(Mandatory=$true)]
[string]$Challenge
)
$signatureService = New-Object MicroCHAP.SignatureService -ArgumentList $SharedSecret
$signature = $signatureService.CreateSignature($challenge, $Url, $null)
$headers = @{ "X-MC-MAC" = $signature.SignatureHash; "X-MC-Nonce" = $challenge; }
$result = Invoke-WebRequest -Uri $Url -Headers $headers -TimeoutSec 10800 -UseBasicParsing
$result.Content
}
function Invoke-SscRequest {
param(
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$true)]
[string]$SharedSecret,
[Parameter(Mandatory=$true)]
[string]$Challenge,
[string]$Payload,
[string]$ContentType = "application/json",
[string]$Method = "Post"
)
$signatureService = New-Object MicroCHAP.SignatureService -ArgumentList $SharedSecret
$signature = $signatureService.CreateSignature($challenge, $Url, $null)
$headers = @{ "X-MC-MAC" = $signature.SignatureHash; "X-MC-Nonce" = $challenge; }
$requestProps = @{
Uri = $Url
Headers = $headers
ContentType = $ContentType
TimeoutSec = 10800
UseBasicParsing = $true
Method = $Method
}
if($Method -eq "Post") {
$requestProps["Body"] = $Payload
}
$result = Invoke-WebRequest @requestProps
$result.Content
}