Skip to content

Commit bf338e3

Browse files
[Feature] Auth - Login in with PowerShell (#18)
Adds a component for Auth: - Supports login with PAT using the `-AccessToken` parameter. - Fixes #8 - Supports login with PAT from environment vars `GH_TOKEN` and `GITHUB_TOKEN` - Fixes #9 - Supports login using Device Flow authorization: - with a GitHub App, the preferred way (default) Fixes #10 - with a OAuth app, the less preferred way. Use parameter `-Mode OAuthApp` - Fixes #11 - refresh the access token using a refresh token by providing the `-Refresh` param. - Fixes #17
1 parent a37e646 commit bf338e3

26 files changed

+842
-89
lines changed

.github/workflows/Test.GitHub.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Test [GitHub]
2+
3+
on:
4+
workflow_dispatch:
5+
6+
permissions: write-all
7+
8+
jobs:
9+
TestGitHub:
10+
name: Test GitHub
11+
if: always()
12+
strategy:
13+
matrix:
14+
os: [ubuntu-latest, windows-latest, macos-latest]
15+
runs-on: ${{ matrix.os }}
16+
env:
17+
GH_TOKEN: ${{ github.token }} # Used for GitHub CLI authentication
18+
steps:
19+
- name: Test Authentication using auto PAT
20+
if: always()
21+
shell: pwsh
22+
run: |
23+
Write-Output '::group::[Debug info] - Environment variables'
24+
$env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY.Split('/')[1]
25+
Get-ChildItem Env:
26+
Write-Output '::endgroup::'
27+
28+
Write-Output '::group::[Debug info] - File structure'
29+
Write-Verbose "Current directory: $((Get-Location).Path)" -Verbose
30+
Write-Verbose "------------------------------------" -Verbose
31+
Write-Verbose "Current directory content:" -Verbose
32+
Get-ChildItem -Path . -Recurse | Select-Object -ExpandProperty FullName | Sort-Object
33+
Write-Output '::endgroup::'
34+
35+
Write-Output '::group::Install-Module -Name GitHub -Verbose -Force'
36+
Install-Module -Name GitHub -Verbose -Force
37+
Write-Output '::endgroup::'
38+
39+
Write-Output '::group::Get-GitHubConfig'
40+
Get-GitHubConfig
41+
Write-Output '::endgroup::'
42+
43+
Write-Output '::group::Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose'
44+
Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose
45+
Write-Output '::endgroup::'
46+
47+
- name: Test Authentication using Specific PAT
48+
if: always()
49+
shell: pwsh
50+
run: |
51+
Write-Output '::group::[Debug info] - Environment variables'
52+
$env:GITHUB_REPOSITORY_NAME = $env:GITHUB_REPOSITORY.Split('/')[1]
53+
Get-ChildItem Env:
54+
Write-Output '::endgroup::'
55+
56+
Write-Output '::group::[Debug info] - File structure'
57+
Write-Verbose "Current directory: $((Get-Location).Path)" -Verbose
58+
Write-Verbose "------------------------------------" -Verbose
59+
Write-Verbose "Current directory content:" -Verbose
60+
Get-ChildItem -Path . -Recurse | Select-Object -ExpandProperty FullName | Sort-Object
61+
Write-Output '::endgroup::'
62+
63+
Write-Output '::group::Install-Module -Name GitHub -Verbose -Force'
64+
Install-Module -Name GitHub -Verbose -Force
65+
Write-Output '::endgroup::'
66+
67+
Write-Output '::group::Get-GitHubConfig'
68+
Get-GitHubConfig
69+
Write-Output '::endgroup::'
70+
71+
Write-Output '::group::Connect-GitHubAccount -AccessToken $env:GH_TOKEN -Verbose'
72+
Connect-GitHubAccount -AccessToken $env:GH_TOKEN -Verbose
73+
Write-Output '::endgroup::'
74+
75+
Write-Output '::group::Get-GitHubConfig'
76+
Get-GitHubConfig
77+
Write-Output '::endgroup::'
78+
79+
Write-Output '::group::Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose'
80+
Get-GitHubWorkflow -Repo $env:GITHUB_REPOSITORY_NAME -Owner $env:GITHUB_REPOSITORY_OWNER -Verbose
81+
Write-Output '::endgroup::'

README.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,34 @@ To dive into the world of GitHub automation with PowerShell, follow these steps:
2626

2727
1. **Installation**: Download and install the GitHub PowerShell module from the provided link or the PowerShell Gallery.
2828

29-
2. **Authentication**: Authenticate using your GitHub credentials or access tokens to begin executing commands.
29+
```powershell
30+
Install-Module -Name GitHub -Force -AllowClobber
31+
```
3032
31-
3. **Command Exploration**: Familiarize yourself with the available cmdlets using the module's comprehensive documentation or inline help.
33+
1. **Authentication**: Authenticate using your GitHub credentials or access tokens to begin executing commands.
3234
33-
4. **Sample Scripts**: Check out sample scripts and usage patterns to jumpstart your automation tasks on GitHub.
35+
Logging in using device flow:
36+
```powershell
37+
Connect-GitHubAccount
38+
39+
Please visit: https://github.com/login/device
40+
and enter code: ABCD-1234
41+
Successfully authenticated!
42+
```
43+
44+
Logging in using PAT token:
45+
```powershell
46+
>_ Connect-GitHubAccount -AccessToken 'ghp_abcdefghklmnopqrstuvwxyz123456789123'
47+
>_
48+
```
49+
50+
2. **Command Exploration**: Familiarize yourself with the available cmdlets using the module's comprehensive documentation or inline help.
51+
52+
```powershell
53+
Get-Command -Module GitHub
54+
```
55+
56+
3. **Sample Scripts**: Check out sample scripts and usage patterns to jumpstart your automation tasks on GitHub.
3457
3558
## More Information & Resources
3659

media/github.png

22.4 KB
Loading

src/GitHub/GitHub.ps1

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Write-Verbose "Initializing GitHub module..." -Verbose
2+
3+
$script:Config = $script:ConfigTemplate | ConvertTo-Json -Depth 100 | ConvertFrom-Json
4+
Initialize-SecretVault -Name $script:SecretVault.Name -Type $script:SecretVault.Type
5+
Restore-GitHubConfig
6+
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
14+
}

src/GitHub/GitHub.psm1

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
11
[Cmdletbinding()]
22
param()
33

4-
$sciptName = $MyInvocation.MyCommand.Name
4+
$scriptName = $MyInvocation.MyCommand.Name
5+
Write-Verbose "[$scriptName] - Importing module"
56

6-
Write-Verbose "[$sciptName] Importing subcomponents"
7-
$folders = 'classes', 'private', 'public'
8-
# Import everything in these folders
7+
#region - Importing data files
8+
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 {
12+
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Importing data file"
13+
New-Variable -Name $_.BaseName -Value (Import-PowerShellDataFile -Path $_.FullName) -Force
14+
Write-Verbose "[$scriptName] - [data] - [$($_.Name)] - Done"
15+
}
16+
Write-Verbose "[$scriptName] - [data] - Done"
17+
#endregion - Importing datas
18+
19+
#region - Importing script files
20+
$folders = 'init', 'classes', 'private', 'public'
921
foreach ($folder in $folders) {
10-
Write-Verbose "[$sciptName] - Processing folder [$folder]"
22+
Write-Verbose "[$scriptName] - [$folder] - Processing folder"
1123
$folderPath = Join-Path -Path $PSScriptRoot -ChildPath $folder
12-
Write-Verbose "[$sciptName] - [$folderPath]"
1324
if (Test-Path -Path $folderPath) {
14-
Write-Verbose "[$sciptName] - [$folderPath] - Getting all files"
15-
$files = $null
16-
$files = Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse
17-
# dot source each file
25+
$files = Get-ChildItem -Path $folderPath -Include '*.ps1', '*.psm1' -Recurse | Sort-Object -Property FullName
1826
foreach ($file in $files) {
19-
Write-Verbose "[$sciptName] - [$folderPath] - [$($file.Name)] - Importing"
20-
Import-Module $file
21-
Write-Verbose "[$sciptName] - [$folderPath] - [$($file.Name)] - Done"
27+
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Importing script file"
28+
Import-Module $file -Verbose:$false
29+
Write-Verbose "[$scriptName] - [$folder] - [$($file.Name)] - Done"
2230
}
2331
}
32+
Write-Verbose "[$scriptName] - [$folder] - Done"
2433
}
34+
#endregion - Importing script files
2535

36+
#region - Importing root script files
37+
Write-Verbose "[$scriptName] - [PSModuleRoot] - Processing folder"
38+
Get-ChildItem -Path $PSScriptRoot -Filter '*.ps1' | ForEach-Object {
39+
Write-Verbose "[$scriptName] - [PSModuleRoot] - [$($_.Name)] - Importing root script files"
40+
Import-Module $_ -Verbose:$false
41+
Write-Verbose "[$scriptName] - [PSModuleRoot] - [$($_.Name)] - Done"
42+
}
43+
Write-Verbose "[$scriptName] - [Root] - Done"
44+
#endregion - Importing root script files
45+
46+
#region Export module members
2647
$foldersToProcess = Get-ChildItem -Path $PSScriptRoot -Directory | Where-Object -Property Name -In $folders
2748
$moduleFiles = $foldersToProcess | Get-ChildItem -Include '*.ps1' -Recurse -File -Force
2849
$functions = $moduleFiles.BaseName
29-
$Param = @{
50+
$param = @{
3051
Function = $functions
3152
Variable = ''
3253
Cmdlet = ''
@@ -35,4 +56,7 @@ $Param = @{
3556

3657
Write-Verbose 'Exporting module members'
3758

38-
Export-ModuleMember @Param
59+
Export-ModuleMember @param
60+
#endregion Export module members
61+
62+
Write-Verbose "[$scriptName] - Done"

src/GitHub/classes/Data/Config.ps1

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

src/GitHub/classes/Data/SecretVault.ps1

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

src/GitHub/data/Auth.psd1

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@{
2+
GitHubApp = @{
3+
ClientID = 'Iv1.f26b61bc99e69405' # $script:Auth.GitHubApp.ClientID
4+
}
5+
OAuthApp = @{
6+
ClientID = '7204ae9b0580f2cb8288' # $script:Auth.OAuthApp.ClientID
7+
}
8+
}

src/GitHub/data/SecretVault.psd1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@{
2+
Name = 'GitHub' # $script:SecretVault.Name
3+
Type = 'Microsoft.PowerShell.SecretStore' # $script:SecretVault.Type
4+
Secret = @{
5+
Name = 'Config' # $script:SecretVault.Secret.Name
6+
}
7+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
TOPIC
2+
about_Auth
3+
4+
SHORT DESCRIPTION
5+
Describes the authentication methods provided in the PowerShell module for interacting with GitHub's REST API.
6+
7+
LONG DESCRIPTION
8+
This module provides several functions to manage authentication for GitHub's REST API. There are primarily two ways to authenticate:
9+
10+
1. GitHub Device Flow: This method prompts the user to visit a specific URL on GitHub where they must enter a user verification code. Once this is done, the module retrieves the necessary access tokens to make authenticated API requests.
11+
12+
2. Personal Access Token: The user can provide a Personal Access Token (PAT) to authenticate. This PAT allows the module to interact with the API on the user's behalf. The module can automatically use environment variables `GH_TOKEN` or `GITHUB_TOKEN` if they are present.
13+
14+
The module also provides functionalities to refresh the access token and to disconnect or logout from the GitHub account.
15+
16+
EXAMPLES
17+
Example 1:
18+
Connect-GitHubAccount
19+
Connects to GitHub using the device flow login. You'll be prompted to visit a specific URL on GitHub and enter the provided user verification code.
20+
21+
Example 2:
22+
Connect-GitHubAccount -AccessToken 'ghp_####'
23+
Connects to GitHub using a provided personal access token (PAT).
24+
25+
Example 3:
26+
Connect-GitHubAccount -Refresh
27+
Refreshes the access token for continued session validity.
28+
29+
Example 4:
30+
Disconnect-GitHubAccount
31+
Disconnects from GitHub and removes the current GitHub configuration.
32+
33+
Example 5 (Automatic login using environment variables):
34+
If either the `GH_TOKEN` or `GITHUB_TOKEN` environment variables are set, the module will automatically use them for authentication during module initialization.
35+
36+
KEYWORDS
37+
GitHub, Authentication, Device Flow, Personal Access Token, PowerShell, REST API
38+
39+
SEE ALSO
40+
For more information on the Device Flow visit:
41+
- https://docs.github.com/en/apps/creating-github-apps/writing-code-for-a-github-app/building-a-cli-with-a-github-app
42+
43+
For information about scopes and other authentication methods on GitHub:
44+
- https://docs.github.com/en/apps/oauth-apps/building-oauth-apps/scopes-for-oauth-apps
45+
- https://docs.github.com/en/rest/overview/other-authentication-methods#authenticating-for-saml-sso

0 commit comments

Comments
 (0)