Skip to content

Commit 7644a50

Browse files
committed
feat: use SecureString for tokens
This is a breaking change, see the readme how tokens should be specified going forward.
1 parent 2db3c9d commit 7644a50

File tree

6 files changed

+38
-21
lines changed

6 files changed

+38
-21
lines changed

README.md

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,32 @@ Missing something? Please file an issue!
4141

4242
## Configuration
4343

44-
You can configure a default endpoint and token to be used by modifying `$PSDefaultParameterValues` in your `$PROFILE`:
44+
You can use a private instance by passing an instance URL with the `-Endpoint` parameter and an API token with the `-Token` parameter to any command.
45+
The token must be a `SecureString` for security.
46+
To configure a default endpoint and token to use, modify `$PSDefaultParameterValues` in your `$PROFILE`:
47+
48+
### On Windows
49+
50+
```powershell
51+
$PSDefaultParameterValues['*Sourcegraph*:Endpoint'] = 'https://sourcegraph.example.com'
52+
$PSDefaultParameterValues['*Sourcegraph*:Token'] = 'YOUR_ENCRYPTED_TOKEN' | ConvertTo-SecureString
53+
```
54+
55+
To get the value for `YOUR_ENCRYPTED_TOKEN`, run `Read-Host -AsSecureString | ConvertFrom-SecureString` once
56+
and paste in your token.
57+
58+
### On macOS/Linux
59+
60+
macOS and Linux do not have access to the Windows Data Protection API, so they cannot use
61+
`ConvertFrom-SecureString` to generate an encrypted plaintext version of the token without a custom encryption
62+
key.
63+
64+
If you are not concerned about storing the token in plain text in the `profile.ps1`, you can set it like this:
4565

4666
```powershell
47-
$PSDefaultParameterValues['*Sourcegraph*:Token'] = '5c01fd47a2b2187c2947f8a2eb76b358f3ed0e26'
4867
$PSDefaultParameterValues['*Sourcegraph*:Endpoint'] = 'https://sourcegraph.example.com'
68+
$PSDefaultParameterValues['*Sourcegraph*:Token'] = 'YOUR_PLAINTEXT_TOKEN' | ConvertTo-SecureString -AsPlainText -Force
4969
```
70+
71+
Alternatively, you could store the token in a password manager or the Keychain, then retrieve it in your
72+
profile and set it the same way.

src/api.psm1

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ function Invoke-SourcegraphApiRequest {
3434
$Variables = @{},
3535

3636
[Uri] $Endpoint = 'https://sourcegraph.com',
37-
38-
[string] $Token
37+
[SecureString] $Token
3938
)
4039

4140
$uri = [Uri]::new([Uri]::new($Endpoint), '/.api/graphql')
4241
$header = @{
4342
"User-Agent" = "Sourcegraph for PowerShell"
4443
}
45-
if (-not [string]::IsNullOrEmpty($Token)) {
46-
$header["Authorization"] = "token $Token"
44+
if ($Token) {
45+
$header["Authorization"] = "token $($Token | ConvertFrom-SecureString -AsPlainText)"
4746
}
4847
$body = @{
4948
query = $Query

src/codeintel.psm1

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ function Get-SourcegraphHover {
3030
#>
3131
[CmdletBinding()]
3232
param(
33-
[Uri] $Endpoint = 'https://sourcegraph.com',
34-
3533
[ValidateNotNullOrEmpty()]
3634
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
3735
[string] $RepositoryName,
@@ -52,8 +50,8 @@ function Get-SourcegraphHover {
5250
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
5351
[int[]] $CharacterNumber, # Supports multiple character numbers to support piping a LineMatch
5452

55-
[ValidateNotNullOrEmpty()]
56-
[string] $Token
53+
[Uri] $Endpoint = 'https://sourcegraph.com',
54+
[SecureString] $Token
5755
)
5856

5957
process {
@@ -143,7 +141,7 @@ function Get-SourcegraphDefinition {
143141
[int[]] $CharacterNumber, # Supports multiple character numbers to support piping a LineMatch
144142

145143
[ValidateNotNullOrEmpty()]
146-
[string] $Token
144+
[SecureString] $Token
147145
)
148146

149147
process {
@@ -238,7 +236,7 @@ function Get-SourcegraphReference {
238236
[int] $PageSize = 50,
239237

240238
[ValidateNotNullOrEmpty()]
241-
[string] $Token
239+
[SecureString] $Token
242240
)
243241

244242
process {

src/repos.psm1

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ function Get-SourcegraphRepository {
3939
[switch] $Descending,
4040

4141
[Uri] $Endpoint = 'https://sourcegraph.com',
42-
[ValidateNotNullOrEmpty()]
43-
[string] $Token
42+
[SecureString] $Token
4443
)
4544
process {
4645
if ($Id -or $CloneUrl) {

src/search.psm1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function Search-Sourcegraph {
3737
[switch] $Structural,
3838

3939
[Uri] $Endpoint = 'https://sourcegraph.com',
40-
[string] $Token
40+
[SecureString] $Token
4141
)
4242

4343
process {
@@ -119,7 +119,7 @@ function Get-SourcegraphSearchSuggestions {
119119
[switch] $Structural,
120120

121121
[Uri] $Endpoint = 'https://sourcegraph.com',
122-
[string] $Token
122+
[SecureString] $Token
123123
)
124124

125125
process {

src/users.psm1

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ function New-SourcegraphUser {
1717
#>
1818
[CmdletBinding(SupportsShouldProcess)]
1919
param(
20-
[Uri] $Endpoint = 'https://sourcegraph.com',
21-
2220
[ValidateNotNullOrEmpty()]
2321
[Parameter(Mandatory)]
2422
[string] $Username,
@@ -27,8 +25,8 @@ function New-SourcegraphUser {
2725
[Parameter(Mandatory)]
2826
[string] $Email,
2927

30-
[ValidateNotNullOrEmpty()]
31-
[string] $Token
28+
[Uri] $Endpoint = 'https://sourcegraph.com',
29+
[SecureString] $Token
3230
)
3331

3432
if ($PSCmdlet.ShouldProcess("Creating user $Username <$Email>", "Create user $Username <$Email>?", "Confirm")) {
@@ -62,7 +60,7 @@ function Get-SourcegraphUser {
6260
[ValidateSet('TODAY', 'THIS_WEEK', 'THIS_MONTH', 'ALL_TIME')]
6361
[string] $ActivePeriod,
6462

65-
[string] $Token
63+
[SecureString] $Token
6664
)
6765

6866
if ($Username) {

0 commit comments

Comments
 (0)