Skip to content
This repository has been archived by the owner on Jan 8, 2023. It is now read-only.

Commit

Permalink
Add check signature config option
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaiR committed Nov 7, 2018
1 parent fa8125b commit 5fb9ba7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/main/kotlin/io/github/spair/repoxbot/EntryPointVerticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import io.vertx.core.http.HttpMethod
import io.vertx.core.http.HttpServerRequest
import io.vertx.core.json.JsonObject
import io.vertx.core.logging.LoggerFactory
import java.net.HttpURLConnection
import com.fasterxml.jackson.core.JsonParseException
import java.net.HttpURLConnection

class EntryPointVerticle : AbstractVerticle() {

Expand Down Expand Up @@ -46,7 +46,9 @@ class EntryPointVerticle : AbstractVerticle() {
val secretKey = getSharedConfig(GITHUB_SECRET)
val payload = body.toJsonObject()

if (Signature.isEqualSignature(signature, secretKey, payload.toString())) {
val shouldCheckSign = getSharedConfig(CHECK_SIGN).toBoolean()

if (!shouldCheckSign || (shouldCheckSign && Signature.isCorrectSignature(signature, secretKey, payload.toString()))) {
val response = processPayload(request.headers()[EVENT_HEADER], payload)
request.response().setStatusCode(HttpURLConnection.HTTP_OK).end(response)
} else {
Expand All @@ -66,9 +68,9 @@ class EntryPointVerticle : AbstractVerticle() {
PING_EVENT -> {
val zen = payload.getString("zen")
logger.info("Ping event caught. Zen: $zen")
zen
"Pong! Zen was: '$zen'"
}
else -> "Unknown event caught. Event: $event"
} ?: "Empty response"
}
}
}
1 change: 1 addition & 0 deletions src/main/kotlin/io/github/spair/repoxbot/MainVerticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class MainVerticle : AbstractVerticle() {

setConfigOrDefault(PORT, DEFAULT_PORT)
setConfigOrDefault(ENTRY_POINT, DEFAULT_ENTRY_POINT)
setConfigOrDefault(CHECK_SIGN, DEFAULT_CHECK_SIGN)

logger.info("Configuration initialized! " +
"RepoXBot now works with next GitHub repository: ${sharedConfig[GITHUB_ORG]}/${sharedConfig[GITHUB_REPO]}; " +
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/github/spair/repoxbot/constant/config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const val GITHUB_SECRET = "github_secret"

const val PORT = "port"
const val ENTRY_POINT = "entry_point"
const val CHECK_SIGN = "check_sign"

const val DEFAULT_PORT = "8080"
const val DEFAULT_ENTRY_POINT = "/handle"
const val DEFAULT_CHECK_SIGN = "true"
2 changes: 1 addition & 1 deletion src/main/kotlin/io/github/spair/repoxbot/util/Signature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Signature {
private const val HMAC_SHA1_ALGORITHM = "HmacSHA1"
private val hexArray = "0123456789ABCDEF".toCharArray()

fun isEqualSignature(signature: String, secretKey: String, xData: String): Boolean {
fun isCorrectSignature(signature: String, secretKey: String, xData: String): Boolean {
val signingKey = SecretKeySpec(secretKey.toByteArray(), HMAC_SHA1_ALGORITHM)
val mac = Mac.getInstance(HMAC_SHA1_ALGORITHM).apply { init(signingKey) }
return signature.equals(toHextString(mac.doFinal(xData.toByteArray())), ignoreCase = true)
Expand Down

0 comments on commit 5fb9ba7

Please sign in to comment.