Skip to content

Commit 614ea0b

Browse files
[+] Added Passphrase support
1 parent dd75124 commit 614ea0b

6 files changed

Lines changed: 41 additions & 13 deletions

File tree

PSGithubUtils/PSGithubUtils.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PSGithubUtils.psm1'
1313

1414
# Version number of this module.
15-
ModuleVersion = '0.1.0'
15+
ModuleVersion = '0.2.0'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -94,7 +94,7 @@
9494

9595
PSData = @{
9696
# Tags applied to this module. These help with module discovery in online galleries.
97-
Tags = @("Github", "API Client")
97+
Tags = @("Github", "JWT", "Invitation")
9898

9999
# A URL to the license for this module.
100100
LicenseUri = 'https://github.com/RootITUp/PSGithubUtils/LICENSE.md'

PSGithubUtils/PSGithubUtils.psm1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@ if ($PSVersionTable.PSVersion.Major -lt 7){
2424
throw [System.NotImplementedException]::new($PSVersionTable.PSVersion)
2525
}
2626

27-
Add-Type -Path $securityLibPath
27+
Add-Type -Path $securityLibPath
28+
Add-Type -TypeDefinition (Get-Content -Path ".\lib\Private\BasicPasswordFinder.cs" -Raw) -ReferencedAssemblies @($securityLibPath)

PSGithubUtils/Private/Get-JWTToken.ps1

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
function Get-JWTToken {
22
param(
3-
[Parameter(Mandatory, ParameterSetName="Data")]
3+
[Parameter(Mandatory, ParameterSetName = "Data")]
44
[string]
55
$KeyData,
6-
[Parameter(Mandatory, ParameterSetName="Path")]
6+
[Parameter(Mandatory, ParameterSetName = "Path")]
77
[string]
88
$KeyPath,
9-
[Parameter(Mandatory, ParameterSetName="Data")]
10-
[Parameter(Mandatory, ParameterSetName="Path")]
9+
[Parameter(ParameterSetName = "Path")]
10+
[string]
11+
$Passphrase,
12+
[Parameter(Mandatory, ParameterSetName = "Data")]
13+
[Parameter(Mandatory, ParameterSetName = "Path")]
1114
[int]
1215
$AppId,
1316
[ValidateScript({ $_ -gt 30 })]
@@ -37,17 +40,19 @@ function Get-JWTToken {
3740
# D) Create Signature
3841

3942
## 1.) Load PEM
40-
if ($PSCmdlet.ParameterSetName -eq "Path"){
43+
if ($PSCmdlet.ParameterSetName -eq "Path") {
4144
$KeyData = Get-Content -Path $KeyPath -Raw
4245
}
43-
$reader = [System.IO.StringReader]::new($keyData)
46+
47+
$reader = [System.IO.StringReader]::new($KeyData)
4448
try{
45-
$pemReader = New-Object Org.BouncyCastle.OpenSsl.PemReader $reader
49+
$pemReader = New-Object Org.BouncyCastle.OpenSsl.PemReader $reader, ([BasicPasswordFinder]::new($Passphrase))
4650
$keyPair = $pemReader.ReadObject()
47-
}finally{
51+
}
52+
finally {
4853
$reader.Close()
4954
}
50-
55+
5156
## 2.) Prepare Signer
5257
$signer = [Org.BouncyCastle.Security.SignerUtilities]::GetSigner("SHA256withRSA")
5358
$signer.Init($true, $keyPair.Private)

PSGithubUtils/Public/Get-GithubToken.ps1

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ function Get-GithubToken {
2929
[Parameter(Mandatory, ParameterSetName = "Path")]
3030
[string]
3131
$KeyPath,
32+
[Parameter(ParameterSetName = "Path")]
33+
[string]
34+
$Passphrase,
3235
[Parameter(Mandatory, ParameterSetName = "Data")]
3336
[Parameter(Mandatory, ParameterSetName = "Path")]
3437
[int]
@@ -44,7 +47,7 @@ function Get-GithubToken {
4447
$jwt = Get-JWTToken -AppId $AppId -KeyData $KeyData
4548
}
4649
"Path" {
47-
$jwt = Get-JWTToken -AppId $AppId -KeyPath $KeyPath
50+
$jwt = Get-JWTToken -AppId $AppId -KeyPath $KeyPath -Passphrase $Passphrase
4851
}
4952
default {
5053
throw [System.NotImplementedException]::new($PSCmdlet.ParameterSetName)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Org.BouncyCastle.OpenSsl;
2+
3+
public class BasicPasswordFinder : IPasswordFinder
4+
{
5+
private readonly string _password;
6+
7+
public BasicPasswordFinder(string password){
8+
this._password = password;
9+
}
10+
11+
public char[] GetPassword() {
12+
return this._password.ToCharArray();
13+
}
14+
}

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ $token = Get-GithubToken -KeyPath C:\secret.pem -AppId 2131234 -InstallationId 5
1111
New-OrganizationMember -Token $token -Organization "Organization123" -Email "asdasd@test.de"
1212
```
1313

14+
```powershell
15+
# Given a private key encrypted via "openssl rsa -in secret.pem -aes256 -out encrypted-secret.pem"
16+
$token = Get-GithubToken -KeyPath C:\encrypted-secret.pem -Passphrase $passphrase -AppId 2131234 -InstallationId 5342523356
17+
```
18+
1419
## Using PowerShellForGithub
1520

1621
```powershell

0 commit comments

Comments
 (0)