Skip to content

Instantly share code, notes, and snippets.

@jmkoni
Created May 2, 2018 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmkoni/87850a359bc8a2e9c50f33e125b6826a to your computer and use it in GitHub Desktop.
Save jmkoni/87850a359bc8a2e9c50f33e125b6826a to your computer and use it in GitHub Desktop.
import javax.servlet.http.{Cookie, HttpServletRequest}
import play.twirl.api.{Html, HtmlFormat}
import scala.util.{Failure, Success}
object Authentication {
def authenticateCookie(
request: HttpServletRequest): Option[UserTokenData] = {
val token =
request.getCookies.find((c: Cookie) => c.getName == "application_cookie")
if (token.isEmpty) {
val authFailure = AuthenticationFailure(request.getHeader("User-Agent"),
request.getRequestURL.toString,
request.getRemoteAddr)
println("Error: application_cookie cookie not found")
println("More information:")
println(authFailure.toString)
return None
}
val userToken = JWT.parseUserJwt(token.get.getValue)
userToken match {
case Success(utd) => Some(utd)
case Failure(t) => {
println("Error while parsing application_cookie cookie: " + t.toString)
None
}
}
}
}
case class AuthenticationFailure(userAgent: String,
url: String,
remoteAddr: String) {
override def toString = {
"AuthenticationFailure(\n" +
" User-Agent: " + userAgent + "\n" +
" Request URL: " + url + "\n" +
" Remote Address: " + remoteAddr + "\n" +
")"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment