Skip to content

Instantly share code, notes, and snippets.

@h4de5
Last active July 20, 2021 15:35
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 h4de5/ef390dd5c0f41af8d14b1c013a9ceef7 to your computer and use it in GitHub Desktop.
Save h4de5/ef390dd5c0f41af8d14b1c013a9ceef7 to your computer and use it in GitHub Desktop.
Use basic authentification, ignore invalid and self-signed certificates and parse the request result
token=$(curl --insecure --request GET \
--url "https://localhost:1234/Token" \
--header "$(echo Authorization: Basic $(echo -n 'username:password' | base64))")
# echo $token
# open browser
# xdg-open "https://localhost:1234/?token=" + $token
sensible-browser "https://localhost:1234/?token=" + $token
$user = 'username'
$pass = 'password'
$url = 'https://localhost:1234/Token'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$Headers = @{
ContentType = "text/plain"
Authorization = "Basic $encodedCreds"
}
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
{
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
}
[ServerCertificateValidationCallback]::Ignore()
$result = Invoke-WebRequest -Uri $url -Headers $Headers -Method Get
$result.Headers.'Content-Type text/plain; charset=utf-8'
$token = [System.Text.Encoding]::UTF8.GetString($result.Content)
# open browser
Start-Process ("https://localhost:1234/?token=" + $token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment