Last active
July 17, 2018 22:20
-
-
Save or-shachar/18d3563abbaca6e89d35aa92bb697a62 to your computer and use it in GitHub Desktop.
Github webhook handler digest handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.codec.digest.{HmacAlgorithms, HmacUtils} | |
class WebHookHandler(webhookSecret:String) { | |
private val hmacDigest = new HmacUtils(HmacAlgorithms.HMAC_SHA_1, webhookSecret) | |
private val xHubSignaturePattern = "sha1=(.+)".r | |
private def validate(xHubSignature: String, body: String): Boolean = { | |
xHubSignature match { | |
case xHubSignaturePattern(sha1) => sha1 == hmacDigest.hmacHex(body) | |
case _ => false | |
} | |
} | |
private def handle(headers:Map[String,String], body:String):Unit = { | |
val validated = headers.get("x-hub-signature").map(xHubSignature=>validate(xHubSignature, body)).getOrElse(false) | |
if (validated){ | |
headers.get("X-GitHub-Event") match { | |
case Some("pull_request") => ... //parse pullRequest structure | |
case Some("push") => ... //parse push event structure | |
case _ => Unit | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment