Skip to content

Commit 99034ac

Browse files
committed
move GH hook verification to github.d
1 parent af9586c commit 99034ac

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

source/dlangbot/app.d

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,6 @@ void startServer(HTTPServerSettings settings)
8787
// Github hook
8888
//==============================================================================
8989

90-
auto getSignature(string data)
91-
{
92-
import std.digest.digest, std.digest.hmac, std.digest.sha;
93-
import std.string : representation;
94-
95-
auto hmac = HMAC!SHA1(hookSecret.representation);
96-
hmac.put(data.representation);
97-
return hmac.finish.toHexString!(LetterCase.lower);
98-
}
99-
100-
Json verifyRequest(string signature, string data)
101-
{
102-
import std.exception : enforce;
103-
import std.string : chompPrefix;
104-
105-
enforce(getSignature(data) == signature.chompPrefix("sha1="),
106-
"Hook signature mismatch");
107-
return parseJsonString(data);
108-
}
109-
11090
void trelloHook(HTTPServerRequest req, HTTPServerResponse res)
11191
{
11292
import std.array : array;
@@ -130,6 +110,7 @@ void trelloHook(HTTPServerRequest req, HTTPServerResponse res)
130110
void githubHook(HTTPServerRequest req, HTTPServerResponse res)
131111
{
132112
import std.functional : toDelegate;
113+
import dlangbot.github : verifyRequest;
133114

134115
auto json = verifyRequest(req.headers["X-Hub-Signature"], req.bodyReader.readAllUTF8);
135116
switch (req.headers["X-GitHub-Event"])

source/dlangbot/github.d

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,3 +562,27 @@ struct GHMerge
562562
string sha;
563563
@name("merge_method") @byName MergeMethod mergeMethod;
564564
}
565+
566+
//==============================================================================
567+
// Github hook signature
568+
//==============================================================================
569+
570+
auto getSignature(string data)
571+
{
572+
import std.digest.digest, std.digest.hmac, std.digest.sha;
573+
import std.string : representation;
574+
575+
auto hmac = HMAC!SHA1(hookSecret.representation);
576+
hmac.put(data.representation);
577+
return hmac.finish.toHexString!(LetterCase.lower);
578+
}
579+
580+
Json verifyRequest(string signature, string data)
581+
{
582+
import std.exception : enforce;
583+
import std.string : chompPrefix;
584+
585+
enforce(getSignature(data) == signature.chompPrefix("sha1="),
586+
"Hook signature mismatch");
587+
return parseJsonString(data);
588+
}

test/utils.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ void postGitHubHook(string payload, string eventType = "pull_request",
209209
{
210210
import std.file : readText;
211211
import std.path : buildPath;
212+
import dlangbot.github : getSignature;
212213

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

0 commit comments

Comments
 (0)