Skip to content

Commit 9d59275

Browse files
committed
Secure the user secret prompt
1 parent ee39730 commit 9d59275

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

backup_github_repositories.ps1

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,26 @@ Overrides the default backup directory.
2929
.\backup_github_repositories.ps1 -backupDirectory "C:\myBackupDirectory"
3030
#>
3131

32-
[CmdletBinding()]
32+
[CmdletBinding(
33+
DefaultParameterSetName = 'SecureSecret'
34+
)]
3335
Param (
3436

3537
[Parameter(
36-
Mandatory=$true,
38+
Mandatory=$True,
3739
HelpMessage="The name of a GitHub user that has access to the GitHub API."
3840
)]
39-
[string]$userName,
41+
[string]$username,
4042

4143
[Parameter(
42-
Mandatory=$true,
43-
HelpMessage="The password or personal access token of the GitHub user."
44+
Mandatory=$True,
45+
HelpMessage="The password or personal access token of the GitHub user.",
46+
ParameterSetName = 'SecureSecret'
47+
)]
48+
[Security.SecureString]${user password or personal access token},
49+
[Parameter(
50+
Mandatory = $True,
51+
ParameterSetName = 'PlainTextSecret'
4452
)]
4553
[string]$userSecret,
4654

@@ -49,6 +57,18 @@ Param (
4957
[string]$backupDirectory
5058
)
5159

60+
# Consolidate the user secret, either from the argument or the prompt, in a secure string format.
61+
if ($userSecret) {
62+
$secureStringUserSecret = $userSecret | ConvertTo-SecureString -AsPlainText -Force
63+
} else {
64+
$secureStringUserSecret = ${user password or personal access token}
65+
}
66+
67+
# Convert the secure user secret string into a plain text representation.
68+
$plainTextUserSecret = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
69+
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureStringUserSecret)
70+
)
71+
5272
# Default the backup directory to './YYYY-MM-DD'. This can
5373
# not be done in the Param section because $PSScriptRoot
5474
# will not be resolved if this script gets invoked from cmd.
@@ -107,7 +127,7 @@ if($organisationName) {
107127
#
108128
# @see https://developer.github.com/v3/auth/#basic-authentication
109129
#
110-
$basicAuthenticationCredentials = "${userName}:${userSecret}"
130+
$basicAuthenticationCredentials = "${username}:${plainTextUserSecret}"
111131
$encodedBasicAuthenticationCredentials = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($basicAuthenticationCredentials))
112132
$requestHeaders = @{
113133
Authorization = "Basic $encodedBasicAuthenticationCredentials"

0 commit comments

Comments
 (0)