Hi, is there a way to do a Principal lookup in the...
# http4k
r
Hi, is there a way to do a Principal lookup in the
security
definition of a ContractRoute or have do be done apart ?
The answer is Yes, BearerAuthSecurity has a constructor that looks like is doing it:
Copy code
constructor(key: RequestContextLens<Any>, lookup: (String) -> Any?, name: String = "bearerAuth")
But the problem I got with that is that it requires a <Any> if I try to use a lens:
val credentials = RequestContextKey.required<Credentials>(contexts)
I get a:
Copy code
Type mismatch.
Required:
RequestContextLens<Any> /* = BiDiLens<Request, Any> */
Found:
RequestContextLens<Credentials> /* = BiDiLens<Request, Credentials> */
Declaring the Lens as
Any
and then doing an
as
when using the Lens works.
Copy code
val credentials = RequestContextKey.required<Any>(contexts)

...
val handler: HttpHandler = { request ->
    val user = credentials(request) as? Credentials
    ...
}
...

meta {
   ...
   security = BearerAuthSecurity(key = credentials, User::fromJwtClaims)
   ...
}
d
Hmmm. Try writing your own Security and seeing if it works. It may require changing.
You may need to override the securotyrenderer as well to get it to render correctly in the openapidocs