Created
March 10, 2022 16:58
-
-
Save zionyx/2271e955ac174b88f7b1ea2c42298557 to your computer and use it in GitHub Desktop.
This piece of PowerShell code helps to setup authentication info to Azure Artifact npm feed.
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
[CmdletBinding()] | |
param ( | |
[String]$Registry, | |
[SecureString]$Token | |
) | |
$SampleRegistry = 'For example: //pkgs.dev.azure.com/project/npm/registry/' | |
function Validate-Registry { | |
param ( | |
[String]$Registry | |
) | |
if (-Not($Registry.StartsWith('//'))) { | |
Write-Error "Registry config must start with //. `n$sampleRegistry" -ErrorAction Stop | |
} | |
If (-Not($Registry.EndsWith('/'))) { | |
Write-Error "Registry config must end with /. `n$sampleRegistry" -ErrorAction Stop | |
} | |
} | |
function Encode-ToBase64 { | |
param ( | |
[SecureString]$Token | |
) | |
if ($PSVersionTable.PSVersion.Major -gt 5) { | |
# PowerShell Core 7's method | |
$bytes = [System.Text.Encoding]::ASCII.GetBytes($($Token | ConvertFrom-SecureString -AsPlainText)) | |
$encodedToken = [Convert]::ToBase64String($bytes) | ConvertTo-SecureString -AsPlainText | |
} | |
else { | |
# PowerShell 5's method | |
$bytes = [System.Text.Encoding]::ASCII.GetBytes($(New-Object PSCredential "user", $Token).GetNetworkCredential().Password) | |
$encodedToken = [Convert]::ToBase64String($bytes) | ConvertTo-SecureString -AsPlainText -Force | |
} | |
$encodedToken | |
} | |
function Run-NpmCommands { | |
param ( | |
[String]$Registry, | |
[SecureString]$Base64Token | |
) | |
if (!(Get-Command npm)) { | |
Write-Error "npm command line is required to proceed. Please install npm cli: https://nodejs.org/en/download/" -ErrorAction Stop | |
} | |
npm config set "$Registry`:username" "bearer" | |
$OverallExitCode1 = 0 -bor $LASTEXITCODE | |
npm config set "$Registry`:email" "no-reply@one.com" | |
$OverallExitCode2 = $OverallExitCode1 -bor $LASTEXITCODE | |
if ($PSVersionTable.PSVersion.Major -gt 5) { | |
# PowerShell Core 7's method | |
npm config set "$Registry`:_password" $($Base64Token | ConvertFrom-SecureString -AsPlainText) | |
} | |
else { | |
# PowerShell 5's method | |
npm config set "$Registry`:_password" $(New-Object PSCredential "user", $Base64Token).GetNetworkCredential().Password | |
} | |
$OverallExitCode3 = $OverallExitCode2 -bor $LASTEXITCODE | |
if ($OverallExitCode3 -ne 0) { | |
Write-Error "npm config set commands did not exit correctly." -ErrorAction Stop | |
} | |
} | |
if (!$Registry) { | |
$RegistryInput = Read-Host -Prompt "Enter registry config. $SampleRegistry" | |
} | |
else { | |
$RegistryInput = $Registry | |
} | |
Validate-Registry -Registry $RegistryInput | |
if (!$Token) { | |
$TokenInput = Read-Host -Prompt "Enter non-base64 encoded token" -AsSecureString | |
} | |
$Base64SecureToken = Encode-ToBase64 -Token $TokenInput | |
Run-NpmCommands -Registry $RegistryInput -Base64Token $Base64SecureToken |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment