-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwkaggregationproxy.ps1
55 lines (43 loc) · 1.78 KB
/
jwkaggregationproxy.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
using namespace System
using namespace System.Net
using namespace System.Security.Cryptography.X509Certificates
#requires -Module PSJsonWebToken
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
[string]$body = @{keys = $null } | ConvertTo-Json;
[HttpStatusCode]$statusCode = [HttpStatusCode]::OK;
# Get JKU and public key:
$jwkUri = $env:JWK_URI;
$base64Cert = $env:JWT_PEM;
# Write to the Azure Functions log stream.
Write-Host "GetJwkSet function processed a request.";
$jwkCollection = $null;
try {
# Get the existing JWK collection we wish to add to:
$jwkCollection = Get-JwkCollection -Uri $jwkUri -ErrorAction Stop;
# Convert base 64 string to byte array and ultimately to X509Certfifcate2 object:
[byte[]]$certificateBytes = [Convert]::FromBase64String($base64Cert);
$certificate = [X509Certificate2]::new($certificateBytes, $null);
# Convert the X509Certificate2 into a JWK object:
$jwk = $certificate | New-JsonWebKey;
# Add our JWK to the collection:
$jwkCollection += $jwk;
# Serialize and assign to the $body variable to return:
$jwkSet = @{keys = $jwkCollection };
$body = $jwkSet | ConvertTo-Json;
}
catch {
$statusCode = [HttpStatusCode]::Forbidden;
}
$responseHeaders = @{'Content-Type' = 'application/json; charset=utf-8';
'Strict-Transport-Security' = 'max-age=31536000; includeSubDomains';
'Content-Security-Policy' = "default-src 'self'";
'X-Content-Type-Options' = 'nosniff';
'X-Frame-Options' = "SAMEORIGIN";
'Referrer-Policy' = 'no-referrer-when-downgrade'
}
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = $statusCode;
Body = $body;
Headers = $responseHeaders;
})