-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
891c322
commit fc4d461
Showing
5 changed files
with
136 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using namespace System.Runtime.InteropServices | ||
|
||
function Get-MaskedKeyString { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory = $true)] | ||
[string]$Source, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[int]$First, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[int]$Last, | ||
|
||
[Parameter(Mandatory = $true)] | ||
[int]$MaxNumberOfAsterisks | ||
) | ||
|
||
if ($Source.Length -le ($First + $Last)) { | ||
return $Source | ||
} | ||
|
||
$numberOfAsterisks = $Source.Length - $First - $Last | ||
if ($numberOfAsterisks -gt $MaxNumberOfAsterisks) { | ||
$numberOfAsterisks = $MaxNumberOfAsterisks | ||
} | ||
|
||
$maskedString = $Source.Substring(0, $First) + ('*' * $numberOfAsterisks) + $Source.Substring($Source.Length - $Last, $Last) | ||
return $maskedString | ||
} | ||
|
||
# this is a modified function that surfaces the API key that PSOpenAI will use | ||
function Get-ApiKey { | ||
[CmdletBinding()] | ||
[OutputType([securestring])] | ||
Param( | ||
[Parameter(Position = 0)] | ||
[AllowNull()] | ||
[psobject]$ApiKey, | ||
[switch]$PlainText | ||
) | ||
|
||
# Search API key below priorities. | ||
|
||
if ($PSDefaultParameterValues.ApiKey) { | ||
$key = [string]$PSDefaultParameterValues.ApiKey | ||
Write-Verbose -Message 'API Key found in $PSDefaultParameterValues' | ||
} | ||
# 2. Global variable "OPENAI_API_KEY" | ||
elseif ($null -ne $global:OPENAI_API_KEY -and $global:OPENAI_API_KEY -is [string]) { | ||
$key = [string]$global:OPENAI_API_KEY | ||
Write-Verbose -Message 'API Key found in global variable "OPENAI_API_KEY".' | ||
} | ||
# 3. Environment variable "OPENAI_API_KEY" | ||
elseif ($null -ne $env:OPENAI_API_KEY -and $env:OPENAI_API_KEY -is [string]) { | ||
$key = [string]$env:OPENAI_API_KEY | ||
Write-Verbose -Message 'API Key found in environment variable "OPENAI_API_KEY".' | ||
} | ||
|
||
if ($key -is [securestring]) { | ||
$key = Get-DecryptedString $key | ||
} | ||
|
||
if ($PlainText) { | ||
return $key | ||
} | ||
|
||
if ($key) { | ||
if ($key.StartsWith('sk-', [StringComparison]::Ordinal)) { | ||
$first = 6 | ||
}else { | ||
$first = 3 | ||
} | ||
Get-MaskedKeyString -Source $key -First $first -Last 2 -MaxNumberOfAsterisks 45 | ||
} else { | ||
$null | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters