Accessing the '/PasswordVault/API/auth/LDAP/Logon/' CyberArk LDAP REST endpoint (on-premises PVWA)

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

Welcome Idira to LIVEcommunity!

Our community just got stronger! Explore new identity security resources, connect with experts, and discover more ways to learn and collaborate.

Meet Idira

Accessing the '/PasswordVault/API/auth/LDAP/Logon/' CyberArk LDAP REST endpoint (on-premises PVWA)

L1 Bithead

I’m expecting to get a JSON response according to the documentation, but appear to be getting a quoted encoded base64 string. When the Base64 string is decoded, there are three values that are separated by semicolons.

 

Questions:

- Should I expect my endpoint response to be JSON?

- Is there a way to force a JSON response?

- What are the three values currently being returned in the Base64 encoded string, and which one should I use as the session token?

 

PowerShell Logic Being Used (For Testing):

 

# Set TLS Version

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

 

# Temporarily Skip Certificate Validation

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

 

# Define your credentials

$username = "accountname"

$password = "accountpassword"

 

# Define the server IP

$pvwa = "ipaddress"

 

# Define the URI for the authentication

$uri_auth = "https://$pvwa/PasswordVault/API/auth/LDAP/Logon/"

 

# Create a hashtable with the credentials

$credentials = @{

   "username" = $username

   "password" = $password

}

 

# Convert credentials to JSON

$body = ConvertTo-Json $credentials

 

# Make the POST request

$response = Invoke-WebRequest -Method Post -Uri $uri_auth -Body $body -ContentType "application/json"

 

# Print the status code

Write-Output "Status code: $($response.StatusCode)"

 

# Print the status description

Write-Output "Status description: $($response.StatusDescription)"

 

# Print the response content

Write-Output "Response content: $($response.Content)"

 

# Assume $response.Content contains Base64 encoded string

$base64EncodedResponse = $response.Content.Trim('"')

 

# Decode the response

$base64DecodedBytes = [System.Convert]::FromBase64String($base64EncodedResponse)

$decodedResponse = [System.Text.Encoding]::UTF8.GetString($base64DecodedBytes)

 

# Print the decoded response

$decodedResponse

 

1 accepted solution

Accepted Solutions

try

$headers = @{   "Authorization" = $base64EncodedResponse   }

 

View solution in original post

8 REPLIES 8

L7 Applicator

Should I expect my endpoint response to be JSON?

No.

 

Is there a way to force a JSON response?

Probably no.

 

What are the three values currently being returned in the Base64 encoded string, and which one should I use as the session token?

The string in the response *is* the session token. No need to decode it... just store it in a variable and use that as the value for the "Authorization" header for subsequent requests.

L1 Bithead

@Bernhard Brückler​ 

 

Thank you for the reply / insights. Do you happen to know how long a session token is valid for? 

 

The doc I was using does not seem to highlight that info. 

 

https://docs.cyberark.com/Product-Doc/OnlineHelp/PrivCloud/Latest/en/Content/SDK/CyberArk%20Authentication%20-%20Logon_v10.htm

I'm guessing it is the same as the general inactivity timeout for the PVWA webpage (20 minutes by default, but can be changed), but I haven't actually tested that.

L1 Bithead

I’m having no luck taking the base64 encoded response content (session token) and using that in my http Bearer Authorization header. Can someone provide working code showing how they handle their "PasswordVault/API/auth/LDAP/Logon/" content response and utilize it as a session token?

 

A PowerShell example is preferred, but any code example would be appreciated.

L6 Presenter

Hi @lawrence christensen​,

 

I didn't understand exactly if the problem you are having is to get the token or how to use it for subsequent calls.

 

I recommend you to use Invoke-RestMethod instead of Invoke-WebRequest as below.

$response = Invoke-RestMethod -Method Post -Uri $uri_auth -Body $body -ContentType "application/json"

 

$response contain the token (is equivalent of your $response.Content)

 

Field_Content_versions_oct20__068Ht00000RAeWpIAL.pngNino

L1 Bithead

 

@Antonino Bambino​ 

 

I'm receiving what looks to be a valid session token back when I use the PowerShell code posted with my initial question in this thread. I get the same base64 encoded string back when I use the “Invoke-RestMethod” or “Invoke-WebRequest” command. At the moment the benefit of the “Invoke-WebRequest” is that I can print the response status code (200) and status description (ok) when the code executes cleanly.      

 

At the moment I have not been able to use that session token successfully to access the following endpoint. 

 

Get users:

 

https://docs.cyberark.com/Product-Doc/OnlineHelp/PAS/12.6/en/Content/SDK/get-users-api.htm?tocpath=D...

 

After you authenticate and receive a valid session token, how are you handling that session token to access other rest endpoints? 

 

When I try to use the session token (base64 encoded) to access another rest endpoint I receive the following error message:   

 

status code: 401 

Status description: An error has occurred please contact your administrator. 

 

At the moment I think this is likely happening because I’m not properly creating the session token header (see code). I was hoping to see how others were using it in their code so I could figure out where my deficiencies lie.

 

Generic PowerShell Code:  

 

# Define SSL Protocol Used

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

 

# Skip Certificate Check 

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

 

# Define your credentials

$username = "username"

$password = "password"

 

# Define the server IP

$pvwa = "ip"

 

# Define the URI for the authentication

$uri_auth = "https://$pvwa/PasswordVault/API/auth/LDAP/Logon/"

 

# Create a hashtable with the credentials

$credentials = @{

  "username" = $username

  "password" = $password

}

 

# Convert credentials to JSON

$body = ConvertTo-Json $credentials

 

# Make the POST request

$response = Invoke-WebRequest -Method Post -Uri $uri_auth -Body $body -ContentType "application/json"

 

# Print the status code

Write-Output "Status code: $($response.StatusCode)"

 

# Print the status description

Write-Output "Status description: $($response.StatusDescription)"

 

# Print the response content

Write-Output "Response content: $($response.Content)"

 

# Assume $response.Content contains Base64 encoded string

$base64EncodedResponse = $response.Content.Trim('"')

 

# Decode the response

$base64DecodedBytes = [System.Convert]::FromBase64String($base64EncodedResponse)

$decodedResponse = [System.Text.Encoding]::UTF8.GetString($base64DecodedBytes)

 

# Print the decoded response

Write-Output "decodedResponse: $decodedResponse"

 

# URI for the CyberArk get user rest endpoint

$getUserAPI = "https://$pvwa/PasswordVault/API/Users/"

 

# Add bearer token to headers

$headers = @{

  "Authorization" = "Bearer $base64EncodedResponse"

}

 

# Make the GET request to the CyberArk rest endpoint

#$response_getUserAPI = Invoke-WebRequest -Method Get -Uri $getUserAPI -Headers $headers

 

try {

  $response_getUserAPI = Invoke-WebRequest -Method Get -Uri $getUserAPI -Headers $headers

} catch {

  Write-Host ("Request failed with status code: {0}" -f $_.Exception.Response.StatusCode.value__)

  Write-Host ("Status description: {0}" -f $_.Exception.Response.StatusDescription)

   

  # Print the response body which might contain more detailed error information

  $streamReader = New-Object System.IO.StreamReader $_.Exception.Response.GetResponseStream()

  $errorResponseBody = $streamReader.ReadToEnd()

  Write-Host ("Response body: {0}" -f $errorResponseBody)

}

 

# Print the status code

Write-Output "Status Code: $($response_getUserAPI.StatusCode)"

 

# Print the status description

Write-Output "Status Description: $($response_getUserAPI.StatusDescription)"

 

# Print the CyberArk response content

Write-Output "Response Content: $($response_getUserAPI.Content)"

try

$headers = @{   "Authorization" = $base64EncodedResponse   }

 

L1 Bithead

@Pete Maan​ 

 

That worked!

 

Thank you for pointing me in the right direction.

  • 1 accepted solution
  • 1055 Views
  • 8 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!