Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for Bearer token on Webhook class #1784

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 22 additions & 8 deletions thehive-backend/app/services/WebHook.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,29 @@ import scala.util.{Failure, Success, Try}
import play.api.{Configuration, Logger}
import play.api.libs.json.JsObject
import play.api.libs.ws.WSRequest
import play.api.http.HeaderNames

import org.elastic4play.services.AuxSrv

case class WebHook(name: String, ws: WSRequest)(implicit ec: ExecutionContext) {
case class WebHook(name: String, ws: WSRequest, wsToken: Option[String] = None, wsTokenName: Option[String])(implicit ec: ExecutionContext) {
private[WebHook] lazy val logger = Logger(getClass.getName + "." + name)

def send(obj: JsObject): Unit = ws.post(obj).onComplete {
case Success(resp) if resp.status / 100 != 2 ⇒ logger.error(s"WebHook returns status ${resp.status} ${resp.statusText}")
case Failure(_: ConnectException) ⇒ logger.error(s"Connection to WebHook $name error")
case Failure(error) ⇒ logger.error("WebHook call error", error)
case _ ⇒
def send(obj: JsObject): Unit = {
if (wsToken != None) {
ws.withHttpHeaders(HeaderNames.AUTHORIZATION -> s"${wsTokenName.getOrElse("Bearer")} ${wsToken.get}").post(obj).onComplete {
case Success(resp) if resp.status / 100 != 2 ⇒ logger.error(s"WebHook returns status ${resp.status} ${resp.statusText}")
case Failure(_: ConnectException) ⇒ logger.error(s"Connection to WebHook $name error")
case Failure(error) ⇒ logger.error("WebHook call error", error)
case _ ⇒
}
} else {
ws.post(obj).onComplete {
case Success(resp) if resp.status / 100 != 2 ⇒ logger.error(s"WebHook returns status ${resp.status} ${resp.statusText}")
case Failure(_: ConnectException) ⇒ logger.error(s"Connection to WebHook $name error")
case Failure(error) ⇒ logger.error("WebHook call error", error)
case _ ⇒
}
}
}
}

Expand All @@ -31,8 +43,10 @@ class WebHooks(webhooks: Seq[WebHook], auxSrv: AuxSrv, implicit val ec: Executio
name ← cfg.subKeys
whConfig ← Try(cfg.get[Configuration](name)).toOption
url ← whConfig.getOptional[String]("url")
token ← Some(whConfig.getOptional[String]("token"))
tokenName ← Some(whConfig.getOptional[String]("tokenName"))
instanceWS = whWS.withConfig(whConfig).url(url)
} yield WebHook(name, instanceWS)(ec), auxSrv, ec)
} yield WebHook(name, instanceWS, token, tokenName)(ec), auxSrv, ec)
}

def send(obj: JsObject): Unit =
Expand All @@ -44,4 +58,4 @@ class WebHooks(webhooks: Seq[WebHook], auxSrv: AuxSrv, implicit val ec: Executio
.map(o ⇒ obj + ("object" → o))
.fallbackTo(Future.successful(obj))
.foreach(o ⇒ webhooks.foreach(_.send(o)))
}
}