From 67972786b4ec5b30daf551c3407cc3cbe5d05965 Mon Sep 17 00:00:00 2001 From: Klesh Wong Date: Wed, 2 Jun 2021 16:06:42 +0800 Subject: [PATCH] [feature] pass for windows powershell --- win/Modules/Pass/Pass.psm1 | 133 ++++++++++++++++++++++++++++++ win/WindowsTerminal/settings.json | 3 +- win/gpg4win.md | 4 + win/profile.ps1 | 19 ----- 4 files changed, 139 insertions(+), 20 deletions(-) create mode 100644 win/Modules/Pass/Pass.psm1 create mode 100644 win/gpg4win.md diff --git a/win/Modules/Pass/Pass.psm1 b/win/Modules/Pass/Pass.psm1 new file mode 100644 index 0000000..e9cde2e --- /dev/null +++ b/win/Modules/Pass/Pass.psm1 @@ -0,0 +1,133 @@ +$PASSWORD_STORE_DIR = Get-Item "~\.password-store" + +function GeneratePassword { + param( + [Int] $Size = 10, + [Char[]] $Charsets = "ULNS", + [Char[]] $Exclude + ) + + $Chars = @(); $TokenSet = @() + If (!$TokenSets) { + $Global:TokenSets = @{ + U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ' + L = [Char[]]'abcdefghijklmnopqrstuvwxyz' + N = [Char[]]'0123456789' + S = [Char[]]'!"#$%&''()*+,-./:;<=>?@[\]^_`{|}~' + } + } + $CharSets | ForEach { + $Tokens = $TokenSets."$_" | ForEach {If ($Exclude -cNotContains $_) {$_}} + If ($Tokens) { + $TokensSet += $Tokens + If ($_ -cle [Char]"Z") {$Chars += $Tokens | Get-Random} + } + } + While ($Chars.Count -lt $Size) {$Chars += $TokensSet | Get-Random} + return ($Chars | Sort-Object {Get-Random}) -Join "" +} + +function EnsurePath { + param( + [String] $Path + ) + + $dir = Split-Path $Path + if (!$dir) { + $dir = "." + } + if (!(Test-Path -PathType Container $dir)) { + New-Item -ItemType Directory -Path $dir + } + if (!$Path.EndsWith(".gpg")) { + $Path = $Path + ".gpg" + } + Join-Path $PASSWORD_STORE_DIR.FullName $Path +} + +function GetUid { + if ((gpg --list-secret-keys | findstr uid) -match '<(.*?)>') { + $matches[1] + } else { + throw "unable to find default uid" + } +} + +function Edit-Pass { + [Cmdletbinding()] + param( + [Parameter(Mandatory=$true)] [String] $Path + ) + + $Path = EnsurePath($Path) + echo $Path + + $tmpfile = (New-TemporaryFile).FullName + gpg --decrypt $Path > $tmpfile + nvim $tmpfile + + if ($?) { + gpg -r (GetUid) -o "$tmpfile.gpg" --encrypt $tmpfile + Move-Item -Path "$tmpfile.gpg" -Destination "$Path" -Force + Remove-Item $tmpfile -Force + } +} + +function New-Pass { + [Cmdletbinding()] + param( + [Parameter(Mandatory=$true)] [String] $Path + ) + + $Path = EnsurePath $Path + $pass = GeneratePassword + if (Test-Path -PathType Leaf $Path) { + $text = gpg --decrypt $Path + $text[0] = $pass + } else { + $text = @($pass) + } + Remove-Item $Path -Force + $text | gpg -r (GetUid) -o $Path --encrypt - + Set-Clipboard $pass + Write-Host $pass + Write-Host "new password is saved and copied to Clipboard" +} + +function Get-Pass { + [Cmdletbinding()] + param( + [Parameter(Mandatory=$true)] [String] $Path, + [Bool] $Clipboard=$true + ) + + $Path = EnsurePath $Path + if ($Clipboard) { + $pass = gpg --decrypt $Path | Select -First 1 + if ($pass) { + Set-Clipboard $pass + Write-Host "password is copied to Clipboard successfully" + #TODO clear clipboard after centain period + } else { + throw "password is empty" + } + } else { + Write-Host $pass + } +} + +function Find-Pass { + $selected = Get-ChildItem $PASSWORD_STORE_DIR -Recurse -Filter *.gpg | %{ + $_.FullName.SubString( + $PASSWORD_STORE_DIR.FullName.Length+1, + $_.FullName.Length-$PASSWORD_STORE_DIR.FullName.Length-5 + ) + } | Invoke-Fzf + + if ($selected) { + Get-Pass $selected + } +} + +Export-ModuleMember -Function Edit-Pass,New-Pass,Get-Pass,Find-Pass + diff --git a/win/WindowsTerminal/settings.json b/win/WindowsTerminal/settings.json index 08a8ec5..44187aa 100644 --- a/win/WindowsTerminal/settings.json +++ b/win/WindowsTerminal/settings.json @@ -34,7 +34,8 @@ "colorScheme": "One Half Dark", "cursorShape": "filledBox", // install the non-window-compatible version - "fontFace": "agave Nerd Font r", + // "fontFace": "agave Nerd Font r", + "fontFace": "Sarasa Mono SC", "fontSize": 12 }, "list": [ diff --git a/win/gpg4win.md b/win/gpg4win.md new file mode 100644 index 0000000..8b7bd75 --- /dev/null +++ b/win/gpg4win.md @@ -0,0 +1,4 @@ + +# auto start gpg-agent + +gpgconf --launch gpg-agent diff --git a/win/profile.ps1 b/win/profile.ps1 index 4cde92e..bda2ccc 100644 --- a/win/profile.ps1 +++ b/win/profile.ps1 @@ -95,25 +95,6 @@ function ssh-copy-id { Get-Content $IdentityFile | ssh $UserHost "umask 077; mkdir -p .ssh ; cat >> .ssh/authorized_keys" } -function pass-edit { - [Cmdletbinding()] - param ( - [Parameter()] - [String] - $Path - ) - - $tmpfile = New-TemporaryFile - gpg --decrypt $Path > $tmpfile.FullName - nvim $tmpfile.FullName - if ($? && ((gpg --list-secret-keys | findstr uid) -match '<(.*?)>')) { - $uid=$matches[1] - gpg -r $uid -o "${tmpfile.FullName}.gpg" --encrypt $tmpfile.FullName - Move-Item -Path "${tmpfile.FullName}.gpg" -Destination "$Path" -Force - Remove-Item $tmpfile.FullName -Force - } -} - function f { [Cmdletbinding()]