Skip to content

Commit

Permalink
#1850 Add authentication in webhook
Browse files Browse the repository at this point in the history
  • Loading branch information
To-om committed Mar 18, 2021
1 parent 283d79e commit bed9acb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ object Authentication {
} yield PasswordAuthentication(username, password)
case "bearer" => (json \ "key").validate[String].map(KeyAuthentication(_, "Bearer "))
case "key" => (json \ "key").validate[String].map(KeyAuthentication(_, ""))
case "none" => JsSuccess(NoAuthentication)
case other => JsError(s"Unknown authentication type: $other")
}
}

val writes: Writes[Authentication] = Writes[Authentication] {
case PasswordAuthentication(username, password) => Json.obj("type" -> "basic", "username" -> username, "password" -> password)
case KeyAuthentication(key, "") => Json.obj("type" -> "key", "key" -> key)
case KeyAuthentication(key, "Bearer ") => Json.obj("type" -> "bearer", "key" -> key)
case KeyAuthentication(key, "") => Json.obj("type" -> "key", "key" -> key)
case KeyAuthentication(key, "Bearer ") => Json.obj("type" -> "bearer", "key" -> key)
}
implicit val format: Format[Authentication] = Format(reads, writes)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package org.thp.thehive.services.notification.notifiers

import akka.stream.Materializer
import org.apache.tinkerpop.gremlin.structure.Vertex
import org.thp.client.{ProxyWS, ProxyWSConfig}
import org.thp.client.{Authentication, ProxyWS, ProxyWSConfig}
import org.thp.scalligraph.models.{Entity, UMapping}
import org.thp.scalligraph.services.config.{ApplicationConfig, ConfigItem}
import org.thp.scalligraph.traversal.TraversalOps._
Expand Down Expand Up @@ -31,6 +31,7 @@ case class WebhookNotification(
name: String,
url: String,
version: Int = 0,
auth: Authentication,
wsConfig: ProxyWSConfig = ProxyWSConfig(),
includedTheHiveOrganisations: Seq[String] = Seq("*"),
excludedTheHiveOrganisations: Seq[String] = Nil
Expand Down Expand Up @@ -259,7 +260,7 @@ class Webhook(
val async = for {
message <- Future.fromTry(buildMessage(config.version, audit))
_ = logger.debug(s"Request webhook with message $message")
resp <- ws.url(config.url).post(message)
resp <- config.auth(ws.url(config.url)).post(message)
} yield if (resp.status >= 400) logger.warn(s"Webhook call on ${config.url} returns ${resp.status} ${resp.statusText}") else ()
async.andThen { case _ => ws.close() }
}
Expand Down

0 comments on commit bed9acb

Please sign in to comment.