I have the following Akka directive structure
cors(){
post {
extractCredentials {
credentials:Option[HttpCredentials] => {
// I can successfully inspect the credentials here
}
}
}
}
Now I am trying to use authenticateBasic like
private def foo(credentials: Credentials):Option[FineAuthenticationResult] = {
credentials match
case Credentials.Provided(token) =>
println(token)
Some(ValidCredentials("foo"))
case _ => Some(InvalidToken("foo"))
}
cors(){
post {
authenticateBasic(
realm = "Secure",
foo
) { authenticationResult =>
// Keep track of authorization results per channel and EVENT type
val additionalTags = Map(
"eventType" -> event.customEventType,
"channel" -> event.channel
)
authenticationResult match
case InvalidToken(message) =>
reject(CredentialsRejection(s"invalid token $message", additionalTags))
case InvalidCredentials(message) =>
reject(CredentialsRejection(s"invalid token $message", additionalTags))
case ValidCredentials(_) => complete(StatusCode.int2StatusCode(200))
}
}
}
The foo authenticator always ends up in the Missing credentials case.
I am testing like
Postman.post(uri = "/path/path",
channel = Some("a_channel"),
eventType = Some("a-request")) ~>
addCredentials(getValidToken) ~>
routes ~>
check {
status shouldBe StatusCodes.OK
}
Why am I always getting missing credentials when using authenticateBasic while using extractCredentials always gives me the right credentials.
tldr
The problem is really simple but I had to go through the internals of
authenticateBasicto understand what is going wrong. We cannot useauthenticateBasicwith anOAuth2Token. We can only useauthenticateOAuth2.More details
authenticateBasicis usingunder the hood. And
authenticateBasicAsyncis usingHere we can see that
Cis in principleBasicHttpCredentials. But withaddCredentials- in the case ofOAuth0-Cwill beOAuth0Token. Consequently, thiscollectis going to drop the
Credentialsobject.