PowerShell SAML Rest API auth

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PowerShell SAML Rest API auth

L1 Bithead

Ok, so I am trying to follow this doc.

https://docs.cyberark.com/EPM/Latest/en/Content/WebServices/SAMLAuthentication.htm

 

Import-Module PS-SAML-Interactive   $LoginURL = "Okta embedded app link"   $Value = New-SAMLInteractive -LoginIDP $LoginURL -Verbose   $Header = @{ 'Content-Type' = 'application/x-www-form-urlencoded'; }   $Body = @{ 'Key' = 'SAMLResponse'; 'Value' = "$Value"; }   $JsonBody = ConvertTo-JSON $Body   Invoke-RestMethod -Uri "https://login.epm.cyberark.com/SAML/Logon" -Method Post -Headers $Header -Body $JsonBody

This is the code I am trying to use but it just keeps failing and I have no idea why. I found PS-SAML-Interactive module from a post in the community for a different cyberark product but it doesn't seem to work. I even tried manually grabbing the token from SAML tracer and that doesn't work either.

 

Does anyone have a working PowerShell script for SAML auth?

1 accepted solution

Accepted Solutions

L3 Networker
function New-SAMLInteractive{   [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $LoginIDP )   Begin{   $RegEx = '(?i)name="SAMLResponse"(?: type="hidden")? value=\"(.*?)\"(?:.*)?\/>'   Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Web   }   Process{   # create window for embedded browser $form = New-Object Windows.Forms.Form $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen; $form.Width = 640 $form.Height = 700 $form.showIcon = $false $form.TopMost = $true $web = New-Object Windows.Forms.WebBrowser $web.Size = $form.ClientSize $web.Anchor = "Left,Top,Right,Bottom" $web.ScriptErrorsSuppressed = $true   $form.Controls.Add($web)   $web.Navigate($LoginIDP) $web.add_Navigating({ if ($web.DocumentText -match "SAMLResponse"){   $_.cancel = $true   if ($web.DocumentText -match $RegEx){ $form.Close() $Script:SAMLResponse = $(($Matches[1] -replace '+', '+') -replace '=', '=')   }   }   }) # show browser window, waits for window to close if([system.windows.forms.application]::run($form) -ne "OK") { if ($null -ne $Script:SAMLResponse){   Write-Output $Script:SAMLResponse $form.Close() Remove-Variable -Name SAMLResponse -Scope Script -ErrorAction SilentlyContinue   } Else{ throw "SAMLResponse not matched" } } }   End{ $form.Dispose() }   } #Logon $SAMlResponse=New-SAMLInteractive -LoginIDP 'IDP_URL_HERE'       $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json")     $url = "https://PVWA.COM/PasswordVault/api/auth/saml/logon"   $body = @{   concurrentSession='true'   apiUse='true'   SAMLResponse=$SAMlResponse   }   $token = Invoke-RestMethod -Uri $url -Method Post -body $body -ContentType 'application/x-www-form-urlencoded'   $headers.Add("Authorization", "$token") #Logon End

Credits for the original PS Script: https://github.com/allynl93/getSAMLResponse-Interactive

View solution in original post

6 REPLIES 6

L3 Networker
function New-SAMLInteractive{   [CmdletBinding()] param( [Parameter(Mandatory=$true)] [string] $LoginIDP )   Begin{   $RegEx = '(?i)name="SAMLResponse"(?: type="hidden")? value=\"(.*?)\"(?:.*)?\/>'   Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Web   }   Process{   # create window for embedded browser $form = New-Object Windows.Forms.Form $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen; $form.Width = 640 $form.Height = 700 $form.showIcon = $false $form.TopMost = $true $web = New-Object Windows.Forms.WebBrowser $web.Size = $form.ClientSize $web.Anchor = "Left,Top,Right,Bottom" $web.ScriptErrorsSuppressed = $true   $form.Controls.Add($web)   $web.Navigate($LoginIDP) $web.add_Navigating({ if ($web.DocumentText -match "SAMLResponse"){   $_.cancel = $true   if ($web.DocumentText -match $RegEx){ $form.Close() $Script:SAMLResponse = $(($Matches[1] -replace '+', '+') -replace '=', '=')   }   }   }) # show browser window, waits for window to close if([system.windows.forms.application]::run($form) -ne "OK") { if ($null -ne $Script:SAMLResponse){   Write-Output $Script:SAMLResponse $form.Close() Remove-Variable -Name SAMLResponse -Scope Script -ErrorAction SilentlyContinue   } Else{ throw "SAMLResponse not matched" } } }   End{ $form.Dispose() }   } #Logon $SAMlResponse=New-SAMLInteractive -LoginIDP 'IDP_URL_HERE'       $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Content-Type", "application/json")     $url = "https://PVWA.COM/PasswordVault/api/auth/saml/logon"   $body = @{   concurrentSession='true'   apiUse='true'   SAMLResponse=$SAMlResponse   }   $token = Invoke-RestMethod -Uri $url -Method Post -body $body -ContentType 'application/x-www-form-urlencoded'   $headers.Add("Authorization", "$token") #Logon End

Credits for the original PS Script: https://github.com/allynl93/getSAMLResponse-Interactive

Your $body parameter is what fixed it for me. I just replaced that and removed the convert json and it is now working.

 

You're awesome thank you very much for the help!!!

Hi @Derek Hartman​ 

 

Can you please share your powershell code(after masking the environment URLs) using which you are able to authenticate to CyberArk via SAML? I am unable to do it getting error "Sorry, but we’re having trouble signing you in.

AADSTS750054: SAMLRequest or SAMLResponse must be present as query string parameters in HTTP request for SAML Redirect binding"😞

@Sabyasachi Patnaik​ 

Sure, here is my code.

Import-Module PS-SAML-Interactive   $LoginURL = "Okta embedded app link"   $Value = New-SAMLInteractive -LoginIDP $LoginURL -Verbose   $Header = @{ 'Content-Type' = 'application/x-www-form-urlencoded'; }   $Body = @{ concurrentSession='true' apiUse='true' SAMLResponse=$Value }   Invoke-RestMethod -Uri "https://login.epm.cyberark.com/SAML/Logon" -Method Post -Headers $Header -Body $Body

 

 

Thank you @Derek Hartman​ 

 

Let me validate.

In your case, you did for IDP initiated not for SP Initiated right?

  • 1 accepted solution
  • 1771 Views
  • 6 replies
  • 0 Likes
Like what you see?

Show your appreciation!

Click Like if a post is helpful to you or if you just want to show your support.

Click Accept as Solution to acknowledge that the answer to your question has been provided.

The button appears next to the replies on topics you’ve started. The member who gave the solution and all future visitors to this topic will appreciate it!

These simple actions take just seconds of your time, but go a long way in showing appreciation for community members and the LIVEcommunity as a whole!

The LIVEcommunity thanks you for your participation!