Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚀[Feature]: Add functionality to ease the use of this for Function Apps #160

Open
1 task done
MariusStorhaug opened this issue Nov 20, 2024 · 0 comments
Open
1 task done
Labels
feature New feature

Comments

@MariusStorhaug
Copy link
Member

MariusStorhaug commented Nov 20, 2024

Description

Stuff like:

  • Connect using GitHub App -> Connect-GitHubAccount -ClientID ... -PrivateKey ...
  • Gather info from a request (as a bot). Map all events into objects.
  • Check signature from WebHook
$secret = '1231456789564321654987654321654987' # This should be from a KeyVault

function Test-Signature {
    param (
        [string]$PayloadBody,
        [string]$Secret,
        [string]$ReceivedSignature
    )

    $HMAC = New-Object System.Security.Cryptography.HMACSHA256
    $HMAC.Key = [Text.Encoding]::UTF8.GetBytes($Secret)
    $GeneratedSignatureBytes = $HMAC.ComputeHash([Text.Encoding]::UTF8.GetBytes($PayloadBody))
    $GeneratedSignature = 'sha256=' + ( -join ($GeneratedSignatureBytes | ForEach-Object { $_.ToString('x2') }))

    Write-Verbose "Received signature:  $ReceivedSignature"
    Write-Verbose "Generated signature: $GeneratedSignature"

    return Compare-SecureString -String1 $ReceivedSignature -String2 $GeneratedSignature
}

function Compare-SecureString {
    param (
        [string]$String1,
        [string]$String2
    )

    Write-Information 'Comparing:'
    Write-Information " - $String1"
    Write-Information " - $String2"

    if ($String1.Length -ne $String2.Length) {
        return $false
    }

    $Diff = 0
    for ($i = 0; $i -lt $String1.Length; $i++) {
        $Diff += [int][char]$String1[$i] -bxor [int][char]$String2[$i]
    }

    Write-Information "Diff: $Diff"
    return $Diff -eq 0
}
# Compare the signatures
$requestBody = $Request.Body | ConvertTo-Json -Depth 100 -Compress
if (Test-Signature -PayloadBody $requestBody -SecretToken $key -ReceivedSignature $Request.Headers['X-Hub-Signature-256']) {
    Write-Information 'Signatures match.'
    $response = [HttpResponseContext]@{
        StatusCode = [HttpStatusCode]::OK
    }
} else {
    Write-Information 'Signatures do not match.'
    $response = [HttpResponseContext]@{
        StatusCode = [System.Net.HttpStatusCode]::Unauthorized
    }
}

Push-OutputBinding -Name Response -Value (
    $response
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature
Projects
Status: Todo
Development

No branches or pull requests

1 participant