Skip to content
Snippets Groups Projects
Commit 795f4f29 authored by Felipe D'Abrantes's avatar Felipe D'Abrantes
Browse files

Include JWT in authentication request

parent 5dd1c420
No related branches found
No related tags found
1 merge request!14Add endpoints to manage Dailies
...@@ -21,7 +21,7 @@ class AuthenticationFilter @Inject() (implicit val executionContext: ExecutionCo ...@@ -21,7 +21,7 @@ class AuthenticationFilter @Inject() (implicit val executionContext: ExecutionCo
* @return An optional Forbidden Result with which to abort the request. * @return An optional Forbidden Result with which to abort the request.
*/ */
override def filter[A](request: AuthenticationRequest[A]): Future[Option[Result]] = Future.successful { override def filter[A](request: AuthenticationRequest[A]): Future[Option[Result]] = Future.successful {
if (!request.userId.isDefined) if (!request.requesterId.isDefined)
Some(Results.Forbidden("Invalid JWT Token")) Some(Results.Forbidden("Invalid JWT Token"))
else else
None None
......
...@@ -14,7 +14,8 @@ import play.api.libs.json.Json ...@@ -14,7 +14,8 @@ import play.api.libs.json.Json
import javax.inject.Inject import javax.inject.Inject
class AuthenticationRequest[A](val userId: Option[ObjectId], request: Request[A]) extends WrappedRequest[A](request) class AuthenticationRequest[A](val jwt: String, val requesterId: Option[ObjectId], request: Request[A])
extends WrappedRequest[A](request)
/** /**
...@@ -29,9 +30,8 @@ class AuthenticationTransformer @Inject() (implicit val executionContext: Execut ...@@ -29,9 +30,8 @@ class AuthenticationTransformer @Inject() (implicit val executionContext: Execut
* @return The new parameter to pass to the Action block. * @return The new parameter to pass to the Action block.
*/ */
override def transform[A](request: Request[A]) = Future.successful { override def transform[A](request: Request[A]) = Future.successful {
println(request) val (jwt: String, requesterId: Option[ObjectId]) = processJWT(request)
val userId: Option[ObjectId] = processJWT(request) new AuthenticationRequest(jwt, requesterId, request)
new AuthenticationRequest(userId, request)
} }
/** /**
...@@ -40,7 +40,7 @@ class AuthenticationTransformer @Inject() (implicit val executionContext: Execut ...@@ -40,7 +40,7 @@ class AuthenticationTransformer @Inject() (implicit val executionContext: Execut
* @param request The incoming request. * @param request The incoming request.
* @return The user ID specified in the JWT's payload. * @return The user ID specified in the JWT's payload.
*/ */
def processJWT[A](request: Request[A]): Option[ObjectId] = { def processJWT[A](request: Request[A]): (String, Option[ObjectId]) = {
val privateKey = ConfigFactory.load().getString("jwt.privateKey") val privateKey = ConfigFactory.load().getString("jwt.privateKey")
try { try {
...@@ -52,14 +52,14 @@ class AuthenticationTransformer @Inject() (implicit val executionContext: Execut ...@@ -52,14 +52,14 @@ class AuthenticationTransformer @Inject() (implicit val executionContext: Execut
val content = payload.get.content val content = payload.get.content
val jsonContent = Json.parse(content) val jsonContent = Json.parse(content)
val userId = (jsonContent \ "userId").as[String] val requesterId = (jsonContent \ "userId").as[String]
Some(new ObjectId(userId)) (token, Some(new ObjectId(requesterId)))
} }
catch { catch {
case ex: Throwable => { case ex: Throwable => {
println(s"JWT Error: $ex") println(s"JWT Error: $ex")
None ("", None)
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment