Skip to content

Commit

Permalink
Fix defaults after logging on first time
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusStorhaug committed Sep 20, 2023
1 parent 3a6ab21 commit 447cfcb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 30 deletions.
17 changes: 16 additions & 1 deletion src/GitHub/private/common.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

46 changes: 30 additions & 16 deletions src/GitHub/public/Core/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
49 changes: 49 additions & 0 deletions src/GitHub/public/Core/Set-GitHubConfig.ps1
Original file line number Diff line number Diff line change
@@ -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
}
}
13 changes: 0 additions & 13 deletions src/GitHub/scripts/Initialize.ps1

This file was deleted.

0 comments on commit 447cfcb

Please sign in to comment.