Skip to content

Instantly share code, notes, and snippets.

@infamousjoeg
Last active March 7, 2025 21:53
Show Gist options
  • Save infamousjoeg/6d2f225b07f0a523e46492c1e138bad6 to your computer and use it in GitHub Desktop.
Save infamousjoeg/6d2f225b07f0a523e46492c1e138bad6 to your computer and use it in GitHub Desktop.
getSAMLResponse-Interactive using Selenium
function New-SAMLInteractive {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true)]
[string] $LoginIDP
)
Begin {
# Regular expression to extract SAML Response
$RegEx = '(?i)name="SAMLResponse"(?: type="hidden")? value=\"(.*?)\"(?:.*)?\/>'
# Check if Selenium WebDriver assemblies are already loaded
if (-not ([AppDomain]::CurrentDomain.GetAssemblies() | Where-Object { $_.FullName -like "*WebDriver*" })) {
# Check if WebDriver is installed via NuGet
$seleniumPath = "$env:USERPROFILE\.nuget\packages\Selenium.WebDriver"
$seleniumSupport = "$env:USERPROFILE\.nuget\packages\Selenium.Support"
$webDriverManager = "$env:USERPROFILE\.nuget\packages\WebDriver.ChromeDriver.win32"
# If not found, attempt to install automatically
if (-not (Test-Path $seleniumPath)) {
Write-Host "Selenium WebDriver not found. Installing required packages..."
Install-Package Selenium.WebDriver -Scope CurrentUser -Force | Out-Null
Install-Package Selenium.Support -Scope CurrentUser -Force | Out-Null
Install-Package WebDriver.ChromeDriver.win32 -Scope CurrentUser -Force | Out-Null
}
# Get latest versions available
$seleniumVersion = (Get-ChildItem $seleniumPath | Sort-Object Name -Descending)[0].Name
$supportVersion = (Get-ChildItem $seleniumSupport | Sort-Object Name -Descending)[0].Name
$driverVersion = (Get-ChildItem $webDriverManager | Sort-Object Name -Descending)[0].Name
# Load Selenium assemblies
Add-Type -Path "$seleniumPath\$seleniumVersion\lib\net45\WebDriver.dll"
Add-Type -Path "$seleniumSupport\$supportVersion\lib\net45\WebDriver.Support.dll"
}
}
Process {
try {
# Set up Chrome options
$chromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$chromeOptions.AddArgument("--window-size=640,700")
$chromeOptions.AddArgument("--disable-extensions")
# Get the path to ChromeDriver
$chromeDriverPath = "$env:USERPROFILE\.nuget\packages\WebDriver.ChromeDriver.win32"
$driverVersion = (Get-ChildItem $chromeDriverPath | Sort-Object Name -Descending)[0].Name
$chromeDriverExe = "$chromeDriverPath\$driverVersion\driver\chromedriver.exe"
if (-not (Test-Path $chromeDriverExe)) {
throw "ChromeDriver not found. Please ensure WebDriver.ChromeDriver.win32 package is installed."
}
# Initialize Chrome WebDriver
$driver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeDriverExe, $chromeOptions)
# Navigate to the login page
$driver.Navigate().GoToUrl($LoginIDP)
# Wait for authentication process to complete
$SAMLResponse = $null
$maxWaitTime = 300 # 5 minutes maximum wait time
$startTime = Get-Date
while (((Get-Date) - $startTime).TotalSeconds -lt $maxWaitTime) {
$pageSource = $driver.PageSource
if ($pageSource -match "SAMLResponse") {
# Extract the SAML response using regex
if ($pageSource -match $RegEx) {
$SAMLResponse = ($Matches[1] -replace '+', '+') -replace '=', '='
break
}
}
# Small delay before checking again
Start-Sleep -Milliseconds 500
}
if ($null -eq $SAMLResponse) {
throw "SAMLResponse not matched"
}
return $SAMLResponse
}
finally {
# Clean up - close the browser window
if ($driver) {
$driver.Quit()
$driver.Dispose()
}
}
}
End {
# Nothing specific to clean up in End block
}
}
function Test-SeleniumInstallation {
[CmdletBinding()]
param()
try {
# Check if NuGet provider is available
if (-not (Get-PackageProvider -Name NuGet -ErrorAction SilentlyContinue)) {
Write-Host "Installing NuGet package provider..."
Install-PackageProvider -Name NuGet -Force -Scope CurrentUser | Out-Null
}
# Check if Selenium WebDriver is installed
$seleniumPackage = Get-Package -Name Selenium.WebDriver -ErrorAction SilentlyContinue
if (-not $seleniumPackage) {
Write-Host "Installing Selenium WebDriver..."
Install-Package Selenium.WebDriver -Force -Scope CurrentUser | Out-Null
}
# Check if Selenium Support is installed
$supportPackage = Get-Package -Name Selenium.Support -ErrorAction SilentlyContinue
if (-not $supportPackage) {
Write-Host "Installing Selenium Support..."
Install-Package Selenium.Support -Force -Scope CurrentUser | Out-Null
}
# Check if ChromeDriver is installed
$driverPackage = Get-Package -Name WebDriver.ChromeDriver.win32 -ErrorAction SilentlyContinue
if (-not $driverPackage) {
Write-Host "Installing ChromeDriver..."
Install-Package WebDriver.ChromeDriver.win32 -Force -Scope CurrentUser | Out-Null
}
return $true
}
catch {
Write-Error "Failed to check or install Selenium components: $_"
return $false
}
}
# Example usage:
# Test-SeleniumInstallation
# $samlResponse = New-SAMLInteractive -LoginIDP "https://your-idp-login-url.com"
# Write-Output $samlResponse
@infamousjoeg
Copy link
Author

First, ensure Selenium components are installed

Test-SeleniumInstallation

Then use the function as before

$samlResponse = New-SAMLInteractive -LoginIDP "https://your-idp-login-url.com"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment