Hi,

  In Spray, I can validate a http header X-Token using the following code. 


  def routes: Route = post {

    headerValueByName("X-Token") {
      token =>
        authenticate(validate(token)) {
          user => {
            respondWithMediaType(`application/json`) {
              complete {
                """"msg":"successful""""
              }
            }
          }
        }
    }
  }

  def validate(token: String): Future[Authentication[String]] = {
    Future {
      token match {
        case "foo" => Right("foo")
        case _ => Left(AuthenticationFailedRejection(CredentialsRejected, 
List()))
      }
    }


How to do the above in Akka http (1.0 RC4) ?

Below is my attempt which works. Is there a more elegant solution ?


trait AuthDemoApp {
  implicit val system = ActorSystem("authDemo")
  sys.addShutdownHook({
    system.shutdown()
  })

  implicit val materializer = ActorMaterializer()
  import system.dispatcher


  def check(requestCxt: RequestContext): Boolean = {
    val tokenOpt: Option[HttpHeader] = 
requestCxt.request.getHeader("X-Token")
    println("Token=" + tokenOpt)
    tokenOpt.map(_.value == "foo").getOrElse(false)
  }

  val route: Route =
    post {
      authorize(check _) {
        headerValueByName("X-Token") {
          token =>
            complete {
              s""""msg":"successful. token=$token""""
            }
        }

      }
    }
  // Starts a server listening at http://localhost:8080
  val binding: Future[ServerBinding] = Http().bindAndHandle(handler = 
route, interface = "localhost", port = 8080)

  binding.onSuccess {
    case _ => println("Started server ----")
  }

}



Thanks in advance for any suggestions !


Shing





-- 
>>>>>>>>>>      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 http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to