Skip to content

Instantly share code, notes, and snippets.

@joacar
Created September 28, 2021 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joacar/5e88bea068a0c0d7b093f783bcb57657 to your computer and use it in GitHub Desktop.
Save joacar/5e88bea068a0c0d7b093f783bcb57657 to your computer and use it in GitHub Desktop.
Create certificates from OpenID well known configurations meta data by following the 'jwks_uri' and iterate the keys result.
Param(
[Parameter(Mandatory = $True, Position=1)][string]$BaseUrl
)
function Get-Json {
Param([string]$json, [string]$key)
ConvertFrom-Json $json | Select-Object -expand $key
}
$configuration = Invoke-WebRequest -ContentType "application/json" -Uri "$BaseUrl/.well-known/openid-configuration"
$jwksUri = Get-Json $configuration.Content "jwks_uri"
$response = Invoke-WebRequest -ContentType "application/json" -Uri $jwksUri
$certificates = @{}
Get-Json $response.Content 'keys' | ForEach-Object { $certificates[$_.kid] = [string]$_.x5c }
$certificates.Keys | ForEach-Object {
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]([System.Convert]::FromBase64String($certificates[$_]))
$filePath = "${_}.crt"
Export-Certificate -Cert $cert -FilePath $filePath
Write-Host "Exported certificate '${cert}' to '${filePath}'" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment