oops, I'm sorry, I understood it:

def withActiveSession: Directive1[Session] = {
    optionalHeaderValueByName("session").flatMap {
      case Some(session) =>
        val s = Session(session)
        onSuccess(sessionRepository.validateSession(s)).flatMap { result =>
          if (result) {
            provide(s)
          } else {
            reject(MalformedHeaderRejection("session", "This session is not 
active"))
          }
        }
      case None =>
        reject(MissingHeaderRejection("session"))
    }
}




вторник, 24 апреля 2018 г., 8:09:20 UTC+3 пользователь Muthu Jayakumar 
написал:
>
> Hello there,
>
> I am trying to write a custom directive in akka http for something like 
> authorize (with a bit of difference from what's available OOTB).
>
> object AuthenticationUtils {
>   def generateUserRequest(implicit system: ActorSystem, executionContext: 
> ExecutionContext): Directive1[UserRequest] = {
>     optionalHeaderValueByName("Authorization").flatMap {
>       case Some(tokenValue) if Token.isValidTokenHeader(tokenValue) =>
>         val token = Token.fromHeader(tokenValue).get
>         val selection = system.actorSelection(
> "myauthenticator-actor-selection-here")
>         implicit val timeout: Timeout = Timeout(2 minutes) //may be 
> implicit timeout?
>         val future = (selection ? RedisTokenActor.GenerateUserRequest(
> token)).mapTo[GenerateUserRequestResponse]
>             .map { r => r.optUserRequest}
>         /*onSuccess(future) { //this does not work.
>           case Some(ur) => provide(ur)
>           case _ => reject(AuthorizationFailedRejection)
>         }*/
>         val res = Await.result(future, timeout.duration) //todo: make 
> this async.
>         res match {
>           case Some(ur) => provide(ur)
>           case _ => reject(AuthorizationFailedRejection)
>         }
>
>       case _ =>  reject(AuthorizationFailedRejection)
>     }
>   }
> }
>
> My question is that, how do I async the execution of the `future` within a 
> directive? When I use onSuccess, the return objects are not compatible.
> Please advice on how do I resolve this. When I use it synchronously, the 
> above code works as desired.
>
> Thanks
> Muthu
>

-- 
*****************************************************************************************************
** New discussion forum: https://discuss.akka.io/ replacing akka-user 
google-group soon.
** This group will soon be put into read-only mode, and replaced by 
discuss.akka.io
** More details: https://akka.io/blog/news/2018/03/13/discuss.akka.io-announced
*****************************************************************************************************
>>>>>>>>>> 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to