diff --git a/source/dlangbot/app.d b/source/dlangbot/app.d index d2aab60..e44d58b 100644 --- a/source/dlangbot/app.d +++ b/source/dlangbot/app.d @@ -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; @@ -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"]) diff --git a/source/dlangbot/github.d b/source/dlangbot/github.d index 825529d..274294d 100644 --- a/source/dlangbot/github.d +++ b/source/dlangbot/github.d @@ -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); +} diff --git a/test/utils.d b/test/utils.d index 1dff589..7035213 100644 --- a/test/utils.d +++ b/test/utils.d @@ -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);