Skip to content

Commit 0f681fc

Browse files
[Enhancement] Auth - Move metadata to the tokens metadata in the secret store (#25)
- Fixes #24 - Moved all variables/data and "profile data" to the metadata of the SecretStore secret. - Access and Refresh tokens get stored as separate secrets in the vault. - Expiration dates for the two gets stored as metadata on the respective secret. - All other data like token type, repo, org, api version etc are stored on the metadata of the accesstoken as its similar to a profile.
1 parent df42207 commit 0f681fc

File tree

9 files changed

+235
-85
lines changed

9 files changed

+235
-85
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function ConvertFrom-HashTable {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(
5+
Mandatory,
6+
ValueFromPipeline
7+
)]
8+
[object]$InputObject
9+
)
10+
([pscustomobject](@{} + $InputObject))
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function ConvertTo-HashTable {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(
5+
Mandatory,
6+
ValueFromPipeline
7+
)]
8+
[pscustomobject]$InputObject
9+
)
10+
[hashtable]$hashtable = @{}
11+
12+
foreach ($item in $InputObject.PSobject.Properties) {
13+
Write-Verbose "$($item.Name) : $($item.Value) : $($item.TypeNameOfValue)"
14+
$hashtable.$($item.Name) = $item.Value
15+
}
16+
$hashtable
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function Join-Hashtable {
2+
[OutputType([void])]
3+
[Alias('Merge-HashTable')]
4+
[CmdletBinding()]
5+
param (
6+
[hashtable] $Main,
7+
[hashtable] $Overrides
8+
)
9+
$hashtable = @{}
10+
$Main.Keys | ForEach-Object {
11+
$hashtable[$_] = $Main[$_]
12+
}
13+
$Overrides.Keys | ForEach-Object {
14+
$hashtable[$_] = $Overrides[$_]
15+
}
16+
$hashtable
17+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function Remove-HashtableEntries {
2+
[OutputType([void])]
3+
[CmdletBinding()]
4+
param (
5+
[Parameter(
6+
Mandatory,
7+
ValueFromPipeline
8+
)]
9+
[hashtable] $Hashtable,
10+
[Parameter()]
11+
[switch] $NullOrEmptyValues,
12+
[Parameter()]
13+
[string[]] $RemoveTypes,
14+
[Parameter()]
15+
[string[]] $RemoveNames,
16+
[Parameter()]
17+
[string[]] $KeepTypes,
18+
[Parameter()]
19+
[string[]] $KeepNames
20+
21+
)
22+
if ($NullOrEmptyValues) {
23+
Write-Verbose 'Remove keys with null or empty values'
24+
($Hashtable.GetEnumerator() | Where-Object { -not $_.Value }) | ForEach-Object {
25+
Write-Verbose " - [$($_.Name)] - Value: [$($_.Value)] - Remove"
26+
$Hashtable.Remove($_.Name)
27+
}
28+
}
29+
if ($RemoveTypes) {
30+
Write-Verbose "Remove keys of type: [$RemoveTypes]"
31+
($Hashtable.GetEnumerator() | Where-Object { ($_.Value.GetType().Name -in $RemoveTypes) }) | ForEach-Object {
32+
Write-Verbose " - [$($_.Name)] - Type: [$($_.Value.GetType().Name)] - Remove"
33+
$Hashtable.Remove($_.Name)
34+
}
35+
}
36+
if ($KeepTypes) {
37+
Write-Verbose "Remove keys NOT of type: [$KeepTypes]"
38+
($Hashtable.GetEnumerator() | Where-Object { ($_.Value.GetType().Name -notin $KeepTypes) }) | ForEach-Object {
39+
Write-Verbose " - [$($_.Name)] - Type: [$($_.Value.GetType().Name)] - Remove"
40+
$Hashtable.Remove($_.Name)
41+
}
42+
}
43+
if ($RemoveNames) {
44+
Write-Verbose "Remove keys named: [$RemoveNames]"
45+
($Hashtable.GetEnumerator() | Where-Object { $_.Name -in $RemoveNames }) | ForEach-Object {
46+
Write-Verbose " - [$($_.Name)] - Remove"
47+
$Hashtable.Remove($_.Name)
48+
}
49+
}
50+
if ($KeepNames) {
51+
Write-Verbose "Remove keys NOT named: [$KeepNames]"
52+
($Hashtable.GetEnumerator() | Where-Object { $_.Name -notin $KeepNames }) | ForEach-Object {
53+
Write-Verbose " - [$($_.Name)] - Remove"
54+
$Hashtable.Remove($_.Name)
55+
}
56+
}
57+
}

src/GitHub/public/API/Invoke-GitHubAPI.ps1

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
# The base URI for the GitHub API. This is usually 'https://api.github.com', but can be adjusted if necessary.
3232
[Parameter()]
33-
[string] $ApiBaseUri = (Get-GitHubConfig -Name ApiBaseUri -AsPlainText),
33+
[string] $ApiBaseUri = (Get-GitHubConfig -Name ApiBaseUri),
3434

3535
# The specific endpoint for the API call, e.g., '/repos/user/repo/pulls'.
3636
[Parameter(Mandatory)]
@@ -62,7 +62,7 @@
6262

6363
# The GitHub API version to be used. By default, it pulls from a configuration script variable.
6464
[Parameter()]
65-
[string] $Version = (Get-GitHubConfig -Name ApiVersion -AsPlainText)
65+
[string] $Version = (Get-GitHubConfig -Name ApiVersion)
6666
)
6767

6868
$functionName = $MyInvocation.MyCommand.Name
@@ -72,7 +72,7 @@
7272
'X-GitHub-Api-Version' = $Version
7373
}
7474

75-
($headers.GetEnumerator() | Where-Object { -not $_.Value }) | ForEach-Object { $headers.Remove($_.Name) }
75+
Remove-HashTableEntries -Hashtable $headers -NullOrEmptyValues
7676

7777
$URI = ("$ApiBaseUri/" -replace '/$', '') + ("/$ApiEndpoint" -replace '^/', '')
7878

@@ -88,6 +88,7 @@
8888
StatusCodeVariable = 'StatusCode'
8989
ResponseHeadersVariable = 'ResponseHeaders'
9090
}
91+
Remove-HashTableEntries -Hashtable $APICall -NullOrEmptyValues
9192

9293
if ($Body) {
9394
if ($Body -is [string]) {

src/GitHub/public/Auth/Connect-GitHubAccount.ps1

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@
7373
'DeviceFlow' {
7474
Write-Verbose 'Logging in using device flow...'
7575
$clientID = $script:Auth.$Mode.ClientID
76-
if ($Mode -ne (Get-GitHubConfig -Name DeviceFlowType -AsPlainText -ea SilentlyContinue)) {
76+
if ($Mode -ne (Get-GitHubConfig -Name DeviceFlowType -ErrorAction SilentlyContinue)) {
7777
Write-Verbose "Using $Mode authentication..."
7878
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $clientID -Scope $Scope
7979
} else {
80-
$accessTokenValidity = [datetime](Get-GitHubConfig -Name 'AccessTokenExpirationDate' -AsPlainText) - (Get-Date)
80+
$accessTokenValidity = [datetime](Get-GitHubConfig -Name 'AccessTokenExpirationDate') - (Get-Date)
8181
$accessTokenIsValid = $accessTokenValidity.Seconds -gt 0
8282
$accessTokenValidityText = "$($accessTokenValidity.Hours):$($accessTokenValidity.Minutes):$($accessTokenValidity.Seconds)"
8383
if ($accessTokenIsValid) {
@@ -91,7 +91,7 @@
9191
$tokenResponse = Invoke-GitHubDeviceFlowLogin -ClientID $clientID -RefreshToken (Get-GitHubConfig -Name RefreshToken)
9292
}
9393
} else {
94-
$refreshTokenValidity = [datetime](Get-GitHubConfig -Name 'RefreshTokenExpirationDate' -AsPlainText) - (Get-Date)
94+
$refreshTokenValidity = [datetime](Get-GitHubConfig -Name 'RefreshTokenExpirationDate') - (Get-Date)
9595
$refreshTokenIsValid = $refreshTokenValidity.Seconds -gt 0
9696
if ($refreshTokenIsValid) {
9797
Write-Host '' -ForegroundColor Yellow -NoNewline
@@ -140,14 +140,14 @@
140140
Write-Host '! ' -ForegroundColor DarkYellow -NoNewline
141141
Start-Process 'https://github.com/settings/tokens'
142142
$accessTokenValue = Read-Host -Prompt 'Enter your personal access token' -AsSecureString
143-
$prefix = (ConvertFrom-SecureString $accessTokenValue -AsPlainText) -replace '_.*$', '_*'
144-
if ($prefix -notmatch '^ghp_|^github_pat_') {
143+
$accessTokenType = (ConvertFrom-SecureString $accessTokenValue -AsPlainText) -replace '_.*$', '_*'
144+
if ($accessTokenType -notmatch '^ghp_|^github_pat_') {
145145
Write-Host '' -ForegroundColor Yellow -NoNewline
146-
Write-Host "Unexpected access token format: $prefix"
146+
Write-Host "Unexpected access token format: $accessTokenType"
147147
}
148148
$settings = @{
149149
AccessToken = $accessTokenValue
150-
AccessTokenType = $prefix
150+
AccessTokenType = $accessTokenType
151151
ApiBaseUri = 'https://api.github.com'
152152
ApiVersion = '2022-11-28'
153153
AuthType = $AuthType
Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
function Get-GitHubConfig {
22
<#
33
.SYNOPSIS
4-
Get the current GitHub configuration.
4+
Get configuration value.
55
66
.DESCRIPTION
7-
Get the current GitHub configuration.
8-
The configuration is first loaded from the configuration file.
7+
Get a named configuration value from the GitHub configuration file.
98
109
.EXAMPLE
11-
Get-GitHubConfig
12-
13-
Returns the current GitHub configuration.
10+
Get-GitHubConfig -Name ApiBaseUri
1411
12+
Get the current GitHub configuration for the ApiBaseUri.
1513
#>
1614
[Alias('Get-GHConfig')]
1715
[Alias('GGHC')]
1816
[OutputType([object])]
1917
[CmdletBinding()]
2018
param (
21-
[string] $Name,
22-
[switch] $AsPlainText
19+
# Choose a configuration name to get.
20+
[Parameter()]
21+
[string] $Name
2322
)
23+
2424
$prefix = $script:SecretVault.Prefix
25-
if ($Name) {
26-
$Name = "$prefix$Name"
27-
Get-Secret -Name $Name -Vault $script:SecretVault.Name -AsPlainText:$AsPlainText
28-
} else {
29-
Get-SecretInfo | Where-Object Name -like "$prefix*" | ForEach-Object {
30-
Get-Secret -Name $_.Name -Vault $script:SecretVault.Name -AsPlainText:$AsPlainText
25+
26+
switch($Name) {
27+
'AccessToken' {
28+
Get-Secret -Name "$prefix`AccessToken"
29+
}
30+
'RefreshToken' {
31+
Get-Secret -Name "$prefix`RefreshToken"
32+
}
33+
'RefreshTokenExpirationDate' {
34+
$RefreshTokenData = Get-SecretInfo -Name "$prefix`RefreshToken"
35+
$RefreshTokenData.Metadata | ConvertFrom-HashTable | ConvertTo-HashTable | Select-Object -ExpandProperty $Name
36+
}
37+
default {
38+
$AccessTokenData = Get-SecretInfo -Name "$prefix`AccessToken"
39+
$AccessTokenData.Metadata | ConvertFrom-HashTable | ConvertTo-HashTable | Select-Object -ExpandProperty $Name
3140
}
3241
}
3342
}

0 commit comments

Comments
 (0)