LampSettingLib.psm1

#Region '.\Public\LampSettingLib.ps1' 0
# --------------------------------------------------------------------
# Settings Lib
# --------------------------------------------------------------------
#
# Purpose: manage settings and authentication



# Reads and returns the setting
Function Get-LampSetting($settingName, $defaultValue) {
  $settingValue = [Environment]::GetEnvironmentVariable($settingName, 'User')
  if ([string]::IsNullOrEmpty($settingValue)) {
    return $defaultValue
  } else {
    return $settingValue
  }
}

# Saves a setting. NOTE: ONLY WORKS WITH ADMINISTRATIVE PRIVILEGES
Function Save-LampSetting([String] $settingName, $settingValue) {
  [Environment]::SetEnvironmentVariable($settingName, $settingValue, 'User')
}

# Resets all the settings. NOTE: ONLY WORKS WITH ADMINISTRATIVE PRIVILEGES
Function Reset-LampSettings() {
  Save-LampSetting -settingName 'lampUser' -settingValue $null
  Save-LampSetting -settingName 'lampPassword' -settingValue $null
  #Save-LampSetting -settingName 'lampActiveLanguage' -settingValue $null
  Save-LampSetting -settingName 'lampHost' -settingValue $null
  Save-LampSetting -settingName 'OpenAiApiKey' -settingValue $null
}

# log in to LaMP according to the settings. NOTE: ONLY WORKS WITH ADMINISTRATIVE PRIVILEGES
Function Login-Lamp() {
  $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding # in case it's not ASCII

  $global:lampHost = Get-LampSetting -settingName 'lampHost' -defaultValue 'https://lampws.tisane.ai:443'
  $user = Get-LampSetting -settingName 'lampUser' -defaultValue ''
  $encryptedPassword = Get-LampSetting -settingName 'lampPassword' -defaultValue ''
  $languageId = Get-LampSetting -settingName 'lampActiveLanguage' -defaultValue ''
  $saveIfSuccessful = $false
  if ([string]::IsNullOrEmpty($user)) {
    $cred = Get-Credential -Message "Log on to LaMP"
    $user = $cred.UserName
    $securePassword = $cred.Password
    $saveIfSuccessful = $true
  } else {
    $securePassword = ConvertTo-SecureString -String $encryptedPassword
  }
  
  $password = [System.Net.NetworkCredential]::new("", $securePassword).Password # plain unencrypted


  
## overcome the HTTPS error: https://stackoverflow.com/questions/11696944/powershell-v3-invoke-webrequest-https-error
# do NOT change the indentation, it causes errors
If (-not ("TrustAllCertsPolicy" -as [type])) {
  Add-Type @"
      using System.Net;
      using System.Web;
      using System.Security.Cryptography.X509Certificates;
      public class TrustAllCertsPolicy : ICertificatePolicy {
          public bool CheckValidationResult(
              ServicePoint srvPoint, X509Certificate certificate,
              WebRequest request, int certificateProblem) {
              return true;
          }
      }
"@

}
  $global:authorizationToken = @{}
  try {
    [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

    Write-Progress -Activity "Logging '$user' on to $global:lampHost" -Status "Normally takes a couple of seconds"

    $authenticationBody = '["' + $user + '", "' + $password + '"]'

    $authenticationResponse = Invoke-WebRequest -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing
    #$authenticationResponse
    $inJson = ConvertFrom-Json -InputObject $authenticationResponse.Content
    
    if ($saveIfSuccessful) {
      Save-LampSetting -settingName 'lampUser' -settingValue $user
      $encryptedPassword = ConvertFrom-SecureString -SecureString $securePassword
      Save-LampSetting -settingName 'lampPassword' -settingValue $encryptedPassword
    }

    $global:authorizationToken.Add('Authorization', $inJson.token)
    
    Write-Host "$user is logged on successfully" -ForegroundColor Green

  } catch {
    Write-Host "Unable to log on to $global:lampHost: $_" -ForegroundColor Red
    Reset-LampSettings
  }
  
}
Function Gpt-Key(){
  $global:OpenAiApiKey = Get-LampSetting -settingName 'OpenAiApiKey' -defaultValue ""
  if ([string]::IsNullOrEmpty($global:OpenAiApiKey)){
    $apiKey = Read-Host -Prompt "Enter open ai api key: "
    Save-LampSetting -settingName 'OpenAiApiKey' -settingValue $apiKey
  }

}  

# sets active language
Function Set-LampLanguage($languageId) {
  try {
    $whatever = Invoke-WebRequest -Uri "$global:lampHost/setLanguage?language=$languageId" -Method POST -Headers $global:authorizationToken -Body ' ' -UseBasicParsing
     Write-Host "Language set to $languageId" -ForegroundColor Green
 } catch {
    Write-Host "Unable to set language to $languageId on $global:lampHost: $_" -ForegroundColor Red
  }
}
#EndRegion '.\Public\LampSettingLib.ps1' 117