How can I access header in a service?

1.1k views Asked by At

I'm trying to handle JWT-authentication in gRPC on my backend. I can extract the JWT in an interceptor but how do I access it in my service? I think it should be done with a CoroutineContextServerInterceptor but this doesn't work:

val jwtKey: Context.Key<String> = Context.key("jwtKey")

fun main() {
    ServerBuilder.forPort(8980).intercept(UserInjector).addService(MyService).build().start().awaitTermination()
}

object UserInjector : CoroutineContextServerInterceptor() {
    override fun coroutineContext(call: ServerCall<*, *>, headers: Metadata): CoroutineContext {
        val jwtString = headers.get(Metadata.Key.of("jwt", Metadata.ASCII_STRING_MARSHALLER))
        println("coroutineContext: $jwtString")
        return GrpcContextElement(Context.current().withValue(jwtKey, jwtString))
    }
}

object MyService : MyServiceGrpcKt.MyServiceCoroutineImplBase() {
    override suspend fun testingJWT(request: Test.MyRequest): Test.MyResponse {
        println("testingJWT: ${jwtKey.get()}")
        return Test.MyResponse.getDefaultInstance()
    }
}

Output:

coroutineContext: something
testingJWT: null
1

There are 1 answers

3
Louis Wasserman On BEST ANSWER

I think you'll need to propagate that in its own coroutine context element.

class JwtElement(val jwtString: String) : CoroutineContext.Element {
  companion object Key : CoroutineContext.Key<JwtElement>
  override val key: CoroutineContext.Key<JwtElement>
    get() = Key
}

object UserInjector : CoroutineContextServerInterceptor() {
  override fun coroutineContext(call: ServerCall<*, *>, headers: Metadata): CoroutineContext {
    val jwtString = headers.get(Metadata.Key.of("jwt", Metadata.ASCII_STRING_MARSHALLER))
    println("coroutineContext: $jwtString")
    return JwtElement(jwtString)
  }
}

object MyService : MyServiceGrpcKt.MyServiceCoroutineImplBase() {
  override suspend fun testingJWT(request: Test.MyRequest): Test.MyResponse {
    println("testingJWT: ${coroutineContext[JwtElement]}")
    return Test.MyResponse.getDefaultInstance()
  }
}