From 447cfcbd37bf10e49ac1e8b6b888be34cec6ddfa Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Wed, 20 Sep 2023 02:47:11 +0200 Subject: [PATCH] Fix defaults after logging on first time --- src/GitHub/private/common.ps1 | 17 ++++++- .../public/Core/Connect-GitHubAccount.ps1 | 46 +++++++++++------ src/GitHub/public/Core/Set-GitHubConfig.ps1 | 49 +++++++++++++++++++ src/GitHub/scripts/Initialize.ps1 | 13 ----- 4 files changed, 95 insertions(+), 30 deletions(-) create mode 100644 src/GitHub/public/Core/Set-GitHubConfig.ps1 delete mode 100644 src/GitHub/scripts/Initialize.ps1 diff --git a/src/GitHub/private/common.ps1 b/src/GitHub/private/common.ps1 index 873fbc3ce..959bf8d5b 100644 --- a/src/GitHub/private/common.ps1 +++ b/src/GitHub/private/common.ps1 @@ -2,5 +2,20 @@ $script:Owner = '' $script:Repo = '' $script:Token = '' -$Script:Version = '' +$Script:Version = '2022-11-28' $script:ContentType = 'application/vnd.github+json' + +if ([string]::IsNullOrEmpty($script:Token)) { + $script:Token = $env:GH_TOKEN +} +if ([string]::IsNullOrEmpty($script:Token)) { + $script:Token = $env:GITHUB_TOKEN +} +if ([string]::IsNullOrEmpty($Script:Token)) { + Initialize-SecretVault + $secrets = Get-SecretInfo -Vault SecretStore + if ('GitHub.Token' -in $secrets.name) { + $script:Token = Get-Secret -Name 'GitHub.Token' -AsPlainText + } +} + diff --git a/src/GitHub/public/Core/Connect-GitHubAccount.ps1 b/src/GitHub/public/Core/Connect-GitHubAccount.ps1 index fb3747dfa..9e5e1b962 100644 --- a/src/GitHub/public/Core/Connect-GitHubAccount.ps1 +++ b/src/GitHub/public/Core/Connect-GitHubAccount.ps1 @@ -36,32 +36,46 @@ [String] $Token, [Parameter()] - [String] $APIBaseURI = 'https://api.github.com', - - [Parameter()] - [string] $Version = '2022-11-28' + [String] $APIBaseURI = 'https://api.github.com' ) + $Vault = Get-SecretVault | Where-Object -Property ModuleName -EQ 'Microsoft.PowerShell.SecretStore' + if ($PSBoundParameters.ContainsKey('Token')) { - $script:Token = $Token + Set-GithubConfig -Token $Token # $tokenType = 'token' } else { $tokenResponse = Invoke-GitHubDeviceCodeLogin - $script:Token = $tokenResponse.access_token # ghu_#### - # $tokenExporation = $tokenResponse.expires_in # 28800 = 8 hours - # $refreshToken = $tokenResponse.refresh_token # ghr_######## - # $refreshTokenExpiresIn = $tokenResponse.refresh_token_expires_in # 15724800 = 6 months - # $tokenType = $tokenResponse.token_type # bearer + $script:Token = $tokenResponse.access_token # ghu_#### + $tokenExpiresIn = $tokenResponse.expires_in # 28800 = 8 hours + $tokenExpirationDate = (Get-Date).AddSeconds($tokenExpiresIn) # 2021-09-28T21:00:00.0000000-04:00 + $refreshToken = $tokenResponse.refresh_token # ghr_######## + $refreshTokenExpiresIn = $tokenResponse.refresh_token_expires_in # 15724800 = 6 months + $refreshTokenExpirationDate = (Get-Date).AddSeconds($refreshTokenExpiresIn) # 2022-03-28T21:00:00.0000000-04:00 + # $tokenType = $tokenResponse.token_type # bearer # $tokenScope = $tokenResponse.scope + Set-GithubConfig -Token $Token + Set-Secret -Name 'GitHub.access_token' -Secret $script:Token -Vault $Vault.Name + Set-Secret -Name 'GitHub.access_token.expirationDate' -Secret $tokenExpirationDate -Vault $Vault.Name + Set-Secret -Name 'GitHub.refresh_token' -Secret $refreshToken -Vault $Vault.Name + Set-Secret -Name 'GitHub.refresh_token.expirationDate' -Secret $refreshTokenExpirationDate -Vault $Vault.Name } - $Vault = Get-SecretVault | Where-Object -Property ModuleName -EQ 'Microsoft.PowerShell.SecretStore' - Set-Secret -Name 'GitHub.Token' -Secret $script:Token -Vault $Vault.Name + if ($PSBoundParameters.ContainsKey('Owner')) { + Set-GitHubConfig -Owner $Owner + } - $script:APIBaseURI = $APIBaseURI - $script:Owner = $Owner - $script:Repo = $Repo - $script:Version = $Version + if ($PSBoundParameters.ContainsKey('Repo')) { + Set-GitHubConfig -Repo $Repo + } + + if ($PSBoundParameters.ContainsKey('APIBaseURI')) { + Set-GitHubConfig -APIBaseURI $APIBaseURI + } + + if ($PSBoundParameters.ContainsKey('Version')) { + Set-GitHubConfig -Version $Version + } $user = Get-GitHubUser diff --git a/src/GitHub/public/Core/Set-GitHubConfig.ps1 b/src/GitHub/public/Core/Set-GitHubConfig.ps1 new file mode 100644 index 000000000..703219952 --- /dev/null +++ b/src/GitHub/public/Core/Set-GitHubConfig.ps1 @@ -0,0 +1,49 @@ +function Set-GitHubConfig { + [CmdletBinding()] + param ( + [Parameter()] + [String] $Owner, + + [Parameter()] + [String] $Repo, + + [Parameter()] + [String] $Token, + + [Parameter()] + [String] $APIBaseURI = 'https://api.github.com', + + [Parameter()] + [string] $Version = '2022-11-28' + ) + + $Vault = Get-SecretVault | Where-Object -Property ModuleName -EQ 'Microsoft.PowerShell.SecretStore' + + if ($PSBoundParameters.ContainsKey('Token')) { + $script:Token = $Token + Set-Secret -Name 'GitHub.access_token' -Secret $script:Token -Vault $Vault.Name + Remove-Secret -Name 'GitHub.access_token.expirationDate' -Vault $Vault.Name + Remove-Secret -Name 'GitHub.refresh_token' -Vault $Vault.Name + Remove-Secret -Name 'GitHub.refresh_token.expirationDate' -Vault $Vault.Name + } + + if ($PSBoundParameters.ContainsKey('Owner')) { + $script:Owner = $Owner + Set-Secret -Name 'GitHub.Owner' -Secret $script:Owner -Vault $Vault.Name + } + + if ($PSBoundParameters.ContainsKey('Repo')) { + $script:Repo = $Repo + Set-Secret -Name 'GitHub.Repo' -Secret $script:Repo -Vault $Vault.Name + } + + if ($PSBoundParameters.ContainsKey('APIBaseURI')) { + $script:APIBaseURI = $APIBaseURI + Set-Secret -Name 'GitHub.APIBaseURI' -Secret $script:APIBaseURI -Vault $Vault.Name + } + + if ($PSBoundParameters.ContainsKey('Version')) { + $script:Version = $Version + Set-Secret -Name 'GitHub.Version' -Secret $script:Version -Vault $Vault.Name + } +} diff --git a/src/GitHub/scripts/Initialize.ps1 b/src/GitHub/scripts/Initialize.ps1 deleted file mode 100644 index 696c3f6b6..000000000 --- a/src/GitHub/scripts/Initialize.ps1 +++ /dev/null @@ -1,13 +0,0 @@ -if ([string]::IsNullOrEmpty($Script:Token)) { - $script:Token = $env:GH_TOKEN -} -if ([string]::IsNullOrEmpty($Script:Token)) { - $script:Token = $env:GITHUB_TOKEN -} -if ([string]::IsNullOrEmpty($Script:Token)) { - Initialize-SecretVault - $secrets = Get-SecretInfo -Vault SecretStore - if ('GitHub.Token' -in $secrets.name) { - $script:Token = Get-Secret -Name 'GitHub.Token' -AsPlainText - } -}