extractCredentials

extractCredentials

§Signature

§Description

Extracts the potentially present HttpCredentials provided with the request's Authorization header, which can be then used to implement some custom authentication or authorization logic.

See Credentials and password timing attacks for details about verifying the secret.

§Example

  1. val route =
  2. extractCredentials { creds =>
  3. complete {
  4. creds match {
  5. case Some(c) => "Credentials: " + c
  6. case _ => "No credentials"
  7. }
  8. }
  9. }
  10.  
  11. // tests:
  12. val johnsCred = BasicHttpCredentials("John", "p4ssw0rd")
  13. Get("/") ~> addCredentials(johnsCred) ~> // adds Authorization header
  14. route ~> check {
  15. responseAs[String] shouldEqual "Credentials: Basic Sm9objpwNHNzdzByZA=="
  16. }
  17.  
  18. Get("/") ~> route ~> check {
  19. responseAs[String] shouldEqual "No credentials"
  20. }