You could abbreviate things a bit like this (assuming callbackFunc
returns a String
):
def validXsrf():String = {
val xsrf = for{
cookie <- request.cookies.get("XSRF-TOKEN")
header <- request.headers.get("X-XSRF-TOKEN")
if cookie.value == header
} yield callbackFunc()
xsrf.getOrElse(throw new XsrfException())
}
Now if you didn't want to throw the exception on the failure case, you could redefine the validXsrf
method around returning a Try
instead, something like this:
def validXsrf():Try[String] = {
val xsrf = for{
cookie <- request.cookies.get("XSRF-TOKEN")
header <- request.headers.get("X-XSRF-TOKEN")
if cookie.value == header
} yield callbackFunc()
xsrf.fold[Try[String]](Failure(new XsrfException()))(Success(_))
}