Obtaining Identity in a BodyParser (or just from a RequestHeader)

28 views
Skip to first unread message

Chad Retz

unread,
Jul 11, 2016, 7:03:47 PM7/11/16
to Silhouette
What is the recommended way, with 3.0.4, to obtain an Identity (or optional identity) from just a request header? I need it in a body parser.

Christian Kaps

unread,
Jul 12, 2016, 5:37:00 AM7/12/16
to Silhouette
Hi Chad,

this isn't possible, and I have explained this in detail in this issue.

Best regards,
Christian

Chad Retz

unread,
Jul 12, 2016, 12:09:39 PM7/12/16
to Silhouette
I have the RequestHeader object in my BodyParser, surely I can use that to obtain an identity the same way that the action does? Maybe I should just inject the authenticator service and invoke retrieve? I just have to make sure to work the future into the iteratee  (yes, on old Play version because of no stable version on latest Play version).

Christian Kaps

unread,
Jul 12, 2016, 12:12:42 PM7/12/16
to Silhouette
The `RequestHeader` isn't enough. You need the complete `Request` containing the body. If you have the complete request then you can use the request handlers to get the identity. 

Chad Retz

unread,
Jul 13, 2016, 12:26:03 PM7/13/16
to Silhouette
The following seemed to work for me:

  def auth(request: RequestHeader): Future[Option[Application]] = {
    env.authenticatorService.retrieve(request).flatMap {
      case None => Future.successful(None)
      case Some(jwtAuth) => env.identityService.retrieve(jwtAuth.loginInfo)
    }
  }

In this case, env is the injected Environmnt. Granted it doesn't store the identity for other use such as in a SecuredAction, but that's ok for my purposes.
Reply all
Reply to author
Forward
0 new messages