LampSettingLib.psm1
#Region '.\Public\LampSettingLib.ps1' 0 # -------------------------------------------------------------------- # Settings Lib # -------------------------------------------------------------------- # # Purpose: manage settings and authentication # Reads and returns the setting # 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 = [System.Text.UTF8Encoding]::new() # 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 $global:authorizationToken = @{} try { Write-Progress -Activity "Logging '$user' on to $global:lampHost" -Status "Normally takes a couple of seconds" $authenticationBody = '["' + $user + '", "' + $password + '"]' $authenticationResponse = Invoke-RestMethod -Uri "$global:lampHost/authenticate" -Method POST -Body $authenticationBody -UseBasicParsing -SkipCertificateCheck #$authenticationResponse #$inJson = $authenticationResponse | ConvertFrom-Json if ($saveIfSuccessful) { Save-LampSetting -settingName 'lampUser' -settingValue $user $encryptedPassword = ConvertFrom-SecureString -SecureString $securePassword Save-LampSetting -settingName 'lampPassword' -settingValue $encryptedPassword } $global:authorizationToken.Add('Authorization', $authenticationResponse.token) Write-Host "$user is logged on successfully" -ForegroundColor Green } catch { Write-Host "Unable to log on to $global:lampHost: $_" -ForegroundColor Red Reset-LampSettings } } # 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' 89 |