Skip to content

Commit

Permalink
Added token generation compatibility for Linux & MacOS
Browse files Browse the repository at this point in the history
Linux and MacOS use UTF-8 rather than Windows Powershell UTF-16.  Due to the extra null bytes from UTF-16, password conversion from SecureString to plaintext was terminating after the first character on Linux.  This commit adds a check to determine if the host system is Windows or Linux/MacOS.  If Windows, the original logic runs.  If Linux or MacOS, a different method for converting a SecureString to plaintext is performed.
  • Loading branch information
agreenbhm authored Nov 29, 2023
1 parent b70e349 commit 4f8ae95
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions PSGuacamole/Public/APIConnection/New-GuacToken.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ Function New-GuacToken()
if ($Null -eq $Password -or $Password.Length -eq 0)
{
$SecurePassword = Read-Host "Enter password" -AsSecureString

# Decode SecureString to Plain text for Guacamole (https://www.powershelladmin.com/wiki/Powershell_prompt_for_password_convert_securestring_to_plain_text)
# Create a "password pointer"
$PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)

# Get the plain text version of the password
$Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)

# Free the pointer
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)
# If running on Windows
if ($IsWindows)
{
# Decode SecureString to Plain text for Guacamole (https://www.powershelladmin.com/wiki/Powershell_prompt_for_password_convert_securestring_to_plain_text)
# Create a "password pointer"
$PasswordPointer = [Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePassword)

# Get the plain text version of the password
$Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto($PasswordPointer)

# Free the pointer
[Runtime.InteropServices.Marshal]::ZeroFreeBSTR($PasswordPointer)
# If running on Linux of MacOS
} else {
$Password = ConvertFrom-SecureString -SecureString $SecurePassword -AsPlaintext
}
}

$Body = @{
Expand Down

0 comments on commit 4f8ae95

Please sign in to comment.