Skip to content

Commit df42207

Browse files
[Feature] Meta - Add the Meta functions (#22)
- Fix Authentication issues with DeviceFlow - Fixes #20 - Fixes #19 - The Auth module now supports Device Flow for both GitHubApp, OAuthApp, PAT and System PAT. - If running Connect-GitHubAccount the flow will do the following: - If system tokens are available, use those to log in. - Else trigger device flow. Check if access token is valid. if token is valid for less than 4 hours, refresh the token using the device flow refresh token process. Same if access token is expired. - If refresh token is expired, trigger new device flow auth process. - If type of auth changes due to switchs, trigger new device flow auth process. - If running Connect-GitHubAccount -AccessToken is run, an interactive process is assumed. A prompt will ask the user to provide a PAT token, which can be both Fine grained or classic. - Changed so tokens are treated as SecureStrings from its stored until its put in the function/command that uses it, and converts it there. - Fixes #23 - Configuration is now stored with a prefix that is configured in the SecretVault.psd1 file in the data folder. Default this is "GHPS_". All settings written to the secret store uses this. - Adds an API call method `Invoke-GitHubAPI` to manage all the calls to the GitHub API - Includes default usage of settings, like ApiBaseUri, AccessToken (now as a SecureString) and Version for the api version. - Supports pagination, in line with [Using pagination in the REST API](https://docs.github.com/en/rest/guides/using-pagination-in-the-rest-api?apiVersion=2022-11-28) - Fixes #12 - Adds functions under the Meta namespace.
1 parent bf338e3 commit df42207

27 files changed

+635
-308
lines changed

src/GitHub/GitHub.ps1

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
Write-Verbose "Initializing GitHub module..." -Verbose
1+
$scriptFilePath = $MyInvocation.MyCommand.Path
2+
3+
Write-Verbose "[$scriptFilePath] - Initializing GitHub module..." -Verbose
24

3-
$script:Config = $script:ConfigTemplate | ConvertTo-Json -Depth 100 | ConvertFrom-Json
45
Initialize-SecretVault -Name $script:SecretVault.Name -Type $script:SecretVault.Type
5-
Restore-GitHubConfig
66

7-
if (-not [string]::IsNullOrEmpty($env:GH_TOKEN)) {
8-
Write-Verbose 'Logging on using GH_TOKEN'
9-
Connect-GitHubAccount -AccessToken $env:GH_TOKEN
10-
}
11-
if (-not [string]::IsNullOrEmpty($env:GITHUB_TOKEN)) {
12-
Write-Verbose 'Logging on using GITHUB_TOKEN'
13-
Connect-GitHubAccount -AccessToken $env:GITHUB_TOKEN
7+
# Autologon if a token is present in environment variables
8+
$envVar = Get-ChildItem -Path 'Env:' | Where-Object Name -In 'GH_TOKEN', 'GITHUB_TOKEN' | Select-Object -First 1
9+
$envVarPresent = $envVar.count -gt 0
10+
if ($envVarPresent) {
11+
Connect-GitHubAccount
1412
}

src/GitHub/GitHub.psm1

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,27 @@ Write-Verbose "[$scriptName] - Importing module"
66

77
#region - Importing data files
88
Write-Verbose "[$scriptName] - [data] - Processing folder"
9-
$dataFolder = (Join-Path $PSScriptRoot 'data')
10-
Write-Verbose "[$scriptName] - [data] - [$dataFolder]"
11-
Get-ChildItem -Path "$dataFolder" -Recurse -Force -Include '*.psd1' | ForEach-Object {
9+
$dataFolder = Join-Path $PSScriptRoot 'data'
10+
Get-ChildItem -Path $dataFolder -Recurse -Force -Include '*.psd1' -ErrorAction SilentlyContinue | ForEach-Object {
1211
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Importing data file"
1312
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
1413
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Done"
1514
}
1615
Write-Verbose "[$scriptName] - [data] - Done"
17-
#endregion - Importing datas
16+
#endregion - Importing data files
1817

1918
#region - Importing script files
2019
$folders = 'init', 'classes', 'private', 'public'
2120
foreach ($folder in $folders) {
2221
Write-Verbose "[$scriptName] - [$folder] - Processing folder"
23-
$folderPath = Join-Path -Path $PSScriptRoot -ChildPath $folder
22+
$folderPath = Join-Path $PSScriptRoot $folder
2423
if (Test-Path -Path $folderPath) {
25-
$files = Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse | Sort-Object -Property FullName
26-
foreach ($file in $files) {
27-
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Importing script file"
28-
Import-Module $file -Verbose:$false
29-
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Done"
30-
}
24+
Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse |
25+
Sort-Object -Property FullName | ForEach-Object {
26+
Write-Verbose "[$scriptName] - [$folder] - [$($_.Name)] - Importing script file"
27+
Import-Module $_ -Verbose:$false
28+
Write-Verbose "[$scriptName] - [$folder] - [$($_.Name)] - Done"
29+
}
3130
}
3231
Write-Verbose "[$scriptName] - [$folder] - Done"
3332
}
@@ -44,7 +43,7 @@ Write-Verbose "[$scriptName] - [Root] - Done"
4443
#endregion - Importing root script files
4544

4645
#region Export module members
47-
$foldersToProcess = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object -Property Name -In $folders
46+
$foldersToProcess = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object Name -In $folders
4847
$moduleFiles = $foldersToProcess | Get-ChildItem -Include '*.ps1' -Recurse -File -Force
4948
$functions = $moduleFiles.BaseName
5049
$param = @{
@@ -54,9 +53,9 @@ $param = @{
5453
Alias = '*'
5554
}
5655

57-
Write-Verbose 'Exporting module members'
58-
56+
Write-Verbose "[$scriptName] - Exporting module members"
5957
Export-ModuleMember @param
58+
Write-Verbose "[$scriptName] - Exporting module members - Done"
6059
#endregion Export module members
6160

6261
Write-Verbose "[$scriptName] - Done"

src/GitHub/data/SecretVault.psd1

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
@{
2-
Name = 'GitHub' # $script:SecretVault.Name
2+
Name = 'SecretStore' # $script:SecretVault.Name
33
Type = 'Microsoft.PowerShell.SecretStore' # $script:SecretVault.Type
4-
Secret = @{
5-
Name = 'Config' # $script:SecretVault.Secret.Name
6-
}
4+
Prefix = 'GHPS_' # $script:SecretVault.Prefix
75
}

src/GitHub/en_US/about_Config.help.txt

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ FUNCTIONS
5353

5454
- Get-GitHubConfig: Fetches the current module configuration.
5555
- Reset-GitHubConfig: Resets all or specific sections to its default values.
56-
- Restore-GitHubConfig: Restores the configuration from the secret vault.
57-
- Save-GitHubConfig: Saves the current configuration to the secret vault.
5856
- Set-GitHubConfig: Allows setting specific elements of the configuration.
5957

6058
CONFIGURATION
@@ -87,21 +85,10 @@ EXAMPLES
8785

8886
-------------------------- EXAMPLE 3 --------------------------
8987

90-
Restore-GitHubConfig
88+
Reset-GitHubConfig -Scope Auth
9189

92-
This command restores the GitHub configuration from the secret vault.
90+
This command resets the Auth related settings of the GitHub configuration to its default values.
9391

94-
-------------------------- EXAMPLE 4 --------------------------
95-
96-
Reset-GitHubConfig -Scope 'App.API'
97-
98-
This command resets the 'App.API' section of the GitHub configuration to its default values.
99-
100-
-------------------------- EXAMPLE 5 --------------------------
101-
102-
Save-GitHubConfig
103-
104-
This command saves the current GitHub configuration to the secret vault.
10592

10693
KEYWORDS
10794

src/GitHub/private/Auth/DeviceFlow/Check-GitHubAccessToken.ps1

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/GitHub/private/Auth/DeviceFlow/Invoke-GitHubDeviceFlowLogin.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
# The refresh token to use for re-authentication.
3535
[Parameter()]
36-
[string] $RefreshToken
36+
[securestring] $RefreshToken
3737
)
3838

3939
do {

src/GitHub/private/Auth/DeviceFlow/Request-GitHubAccessToken.ps1

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Mandatory,
3636
ParameterSetName = 'RefreshToken'
3737
)]
38-
[string] $RefreshToken
38+
[securestring] $RefreshToken
3939
)
4040

4141
$body = @{
@@ -44,7 +44,7 @@
4444

4545
if ($PSBoundParameters.ContainsKey('RefreshToken')) {
4646
$body += @{
47-
'refresh_token' = $RefreshToken
47+
'refresh_token' = (ConvertFrom-SecureString $RefreshToken -AsPlainText)
4848
'grant_type' = 'refresh_token'
4949
}
5050
}
@@ -67,6 +67,8 @@
6767
Write-Verbose ($RESTParams.GetEnumerator() | Out-String)
6868

6969
$tokenResponse = Invoke-RestMethod @RESTParams -Verbose:$false
70+
71+
Write-Verbose ($tokenResponse | ConvertTo-Json | Out-String)
7072
return $tokenResponse
7173
} catch {
7274
Write-Error $_

src/GitHub/private/Auth/DeviceFlow/Request-GitHubDeviceCode.ps1

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,3 @@
5656
throw $_
5757
}
5858
}
59-

src/GitHub/private/Auth/DeviceFlow/Wait-GitHubAccessToken.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
Mandatory,
3939
ParameterSetName = 'RefreshToken'
4040
)]
41-
[string] $RefreshToken,
41+
[securestring] $RefreshToken,
4242

4343
# The interval to wait between polling for the token.
4444
[Parameter()]

src/GitHub/private/Config/Config.ps1

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)