Created
May 2, 2018 14:52
-
-
Save jmkoni/87850a359bc8a2e9c50f33e125b6826a to your computer and use it in GitHub Desktop.
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
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