Skip to content

Commit

Permalink
🪲 [Fix]: Fix the token pattern matching (#153)
Browse files Browse the repository at this point in the history
## Description

- Fix an issue where the token pattern matched parts of the actual token
instead of just getting the prefix. Specifically this was an issue for
the `github_pat` token matching.

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [x] 🪲 [Fix]
- [ ] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [ ] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug authored Nov 10, 2024
1 parent 41b43b7 commit 68f62b5
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/functions/public/Auth/Connect-GitHubAccount.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
$HostName = $HostName -replace '^https?://'
$ApiBaseUri = "https://api.$HostName"
$authType = $PSCmdlet.ParameterSetName
$tokenPrefixPattern = '(?<=^(ghu|gho|ghs|github_pat|ghp)).*'

# If running on GitHub Actions and no access token is provided, use the GitHub token.
if ($env:GITHUB_ACTIONS -eq 'true') {
Expand Down Expand Up @@ -218,7 +219,7 @@
$context += @{
Secret = ConvertTo-SecureString -AsPlainText $tokenResponse.access_token
SecretExpirationDate = (Get-Date).AddSeconds($tokenResponse.expires_in)
SecretType = $tokenResponse.access_token -replace '_[^_]+$'
SecretType = $tokenResponse.access_token -replace $tokenPrefixPattern
AuthClientID = $authClientID
DeviceFlowType = $Mode
RefreshToken = ConvertTo-SecureString -AsPlainText $tokenResponse.refresh_token
Expand All @@ -229,7 +230,7 @@
'OAuthApp' {
$context += @{
Secret = ConvertTo-SecureString -AsPlainText $tokenResponse.access_token
SecretType = $tokenResponse.access_token -replace '_[^_]+$'
SecretType = $tokenResponse.access_token -replace $tokenPrefixPattern
AuthClientID = $authClientID
DeviceFlowType = $Mode
Scope = $tokenResponse.scope
Expand Down Expand Up @@ -257,14 +258,14 @@
Start-Process "https://$HostName/settings/tokens"
$accessTokenValue = Read-Host -Prompt 'Enter your personal access token' -AsSecureString
$Token = ConvertFrom-SecureString $accessTokenValue -AsPlainText
$secretType = $Token -replace '_[^_]+$'
$secretType = $Token -replace $tokenPrefixPattern
$context += @{
Secret = ConvertTo-SecureString -AsPlainText $Token
SecretType = $secretType
}
}
'Token' {
$secretType = $Token -replace '_[^_]+$'
$secretType = $Token -replace $tokenPrefixPattern
switch -Regex ($secretType) {
'ghp|github_pat' {
$context += @{
Expand Down

0 comments on commit 68f62b5

Please sign in to comment.