Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 1 addition & 20 deletions source/dlangbot/app.d
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,6 @@ void startServer(HTTPServerSettings settings)
// Github hook
//==============================================================================

auto getSignature(string data)
{
import std.digest.digest, std.digest.hmac, std.digest.sha;
import std.string : representation;

auto hmac = HMAC!SHA1(hookSecret.representation);
hmac.put(data.representation);
return hmac.finish.toHexString!(LetterCase.lower);
}

Json verifyRequest(string signature, string data)
{
import std.exception : enforce;
import std.string : chompPrefix;

enforce(getSignature(data) == signature.chompPrefix("sha1="),
"Hook signature mismatch");
return parseJsonString(data);
}

void trelloHook(HTTPServerRequest req, HTTPServerResponse res)
{
import std.array : array;
Expand All @@ -130,6 +110,7 @@ void trelloHook(HTTPServerRequest req, HTTPServerResponse res)
void githubHook(HTTPServerRequest req, HTTPServerResponse res)
{
import std.functional : toDelegate;
import dlangbot.github : verifyRequest;

auto json = verifyRequest(req.headers["X-Hub-Signature"], req.bodyReader.readAllUTF8);
switch (req.headers["X-GitHub-Event"])
Expand Down
24 changes: 24 additions & 0 deletions source/dlangbot/github.d
Original file line number Diff line number Diff line change
Expand Up @@ -562,3 +562,27 @@ struct GHMerge
string sha;
@name("merge_method") @byName MergeMethod mergeMethod;
}

//==============================================================================
// Github hook signature
//==============================================================================

auto getSignature(string data)
{
import std.digest.digest, std.digest.hmac, std.digest.sha;
import std.string : representation;

auto hmac = HMAC!SHA1(hookSecret.representation);
hmac.put(data.representation);
return hmac.finish.toHexString!(LetterCase.lower);
}

Json verifyRequest(string signature, string data)
{
import std.exception : enforce;
import std.string : chompPrefix;

enforce(getSignature(data) == signature.chompPrefix("sha1="),
"Hook signature mismatch");
return parseJsonString(data);
}
1 change: 1 addition & 0 deletions test/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ void postGitHubHook(string payload, string eventType = "pull_request",
{
import std.file : readText;
import std.path : buildPath;
import dlangbot.github : getSignature;

logInfo("Starting test in %s:%d with payload: %s", file, line, payload);

Expand Down