This repository has been archived by the owner on Feb 10, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
Signature:
header verification for Travis
This theoretically addresses #43, though ideally we should fetch Travis' public key ourselves rather than requiring the user to copy it into the settings file themself.
- Loading branch information
Showing
11 changed files
with
106 additions
and
88 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
53 changes: 0 additions & 53 deletions
53
src/main/scala/com/getbootstrap/savage/server/TravisAuthDirectives.scala
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
src/main/scala/com/getbootstrap/savage/server/TravisSignatureDirectives.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.getbootstrap.savage.server | ||
|
||
import java.util.Base64 | ||
import scala.util.Try | ||
import akka.event.LoggingAdapter | ||
import spray.http.FormData | ||
import spray.routing.{Directive1, MalformedHeaderRejection, MalformedRequestContentRejection, ValidationRejection} | ||
import spray.routing.directives.{BasicDirectives, HeaderDirectives, RouteDirectives, MarshallingDirectives} | ||
import com.getbootstrap.savage.crypto.{RsaPublicKey, Sha1WithRsa, SuccessfullyVerified} | ||
import com.getbootstrap.savage.util.Utf8String | ||
|
||
trait TravisSignatureDirectives { | ||
import BasicDirectives.provide | ||
import HeaderDirectives.headerValueByName | ||
import RouteDirectives.reject | ||
import MarshallingDirectives.{entity, as} | ||
|
||
private val signatureHeaderName = "Signature" | ||
private val signatureHeaderValue = headerValueByName(signatureHeaderName) | ||
|
||
def travisSignature(log: LoggingAdapter): Directive1[Array[Byte]] = signatureHeaderValue.flatMap { base64 => | ||
Try{ Base64.getDecoder.decode(base64) }.toOption match { | ||
case Some(bytesFromBase64) => provide(bytesFromBase64) | ||
case None => { | ||
log.error(s"Received Travis request with malformed Base64 value in ${signatureHeaderName} header!") | ||
reject(MalformedHeaderRejection(signatureHeaderName, "Malformed Base64 value")) | ||
} | ||
} | ||
} | ||
|
||
private val formDataEntity = entity(as[FormData]) | ||
|
||
def stringEntityIfTravisSignatureValid(travisPublicKey: RsaPublicKey, log: LoggingAdapter): Directive1[String] = travisSignature(log).flatMap { signature => | ||
formDataEntity.flatMap { formData => | ||
formData.fields.toMap.get("payload") match { | ||
case Some(payload:String) => { | ||
Sha1WithRsa.verifySignature(signature = signature, publicKey = travisPublicKey, signedData = payload.utf8Bytes) match { | ||
case SuccessfullyVerified => provide(payload) | ||
case _ => { | ||
log.warning("Received Travis request with incorrect signature! Signature={} Payload={}", signature, payload) | ||
reject(ValidationRejection("Incorrect SHA-1+RSA signature")) | ||
} | ||
} | ||
} | ||
case None => { | ||
log.error("Received Travis request that was missing the `payload` field!") | ||
reject(MalformedRequestContentRejection("Request body form data lacked required `payload` field")) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
object TravisSignatureDirectives extends TravisSignatureDirectives |
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.