class ServerAuthInterceptor extends ServerInterceptor {
private val AUTHORIZATION: Metadata.Key[String] = Metadata.Key.of("Authorization",
Metadata.ASCII_STRING_MARSHALLER)
override def interceptCall[RequestT, ResponseT](
methodName: String, serverCall: ServerCall[ResponseT], headers: Headers,
serverCallHandler: ServerCallHandler[RequestT, ResponseT]): Listener[RequestT] = {
val authHeader = headers.get(AUTHORIZATION)
// todo: Hook this up to a real auth provider
assert(authHeader == "Token: abc") // == User 1
serverCallHandler.startCall(methodName, serverCall, headers)
}
}
--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/bab04e06-86c5-4176-aea0-cfacea14d466%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I quite work out how to override onPayload to set/clear the threadlocal. I've been trying to return a class that extends ServerInterceptors.ForwardingListener but I can't work out how to get a `ServerCall.Listener<RespT>` to delegate to. I can only work out how to get a ServerCall.Listener<RequestT>.
Please can you give me some pointers?
package com.example.api.server
import javax.inject.Provider
import io.grpc.Metadata.Headers
import io.grpc.ServerCall.Listener
import io.grpc.ServerInterceptors.ForwardingListener
import io.grpc._
/**
* Validate auth credentials.
*/
class ServerAuthInterceptor extends ServerInterceptor {
private val AUTHORIZATION: Metadata.Key[String] = Metadata.Key.of("Authorization",
Metadata.ASCII_STRING_MARSHALLER)
private val threadLocalUserId: ThreadLocal[Int] = new ThreadLocal[Int]()
override def interceptCall[RequestT, ResponseT](
method: String, call: ServerCall[ResponseT], headers: Headers,
next: ServerCallHandler[RequestT, ResponseT]): Listener[RequestT] = {
val authHeader = headers.get(AUTHORIZATION)
// todo: Hook this up to a real auth provider
assert(authHeader == "UserId: 1")
val userId = 1
new ForwardingListener[RequestT](next.startCall(method, call, headers)) {
override def onPayload(payload: RequestT): Unit = {
// set the threadlocal before the call
threadLocalUserId.set(userId)
super.onPayload(payload)
// now clear it
threadLocalUserId.remove()
}
}
}
/**
* @return A Provider to allow access to the User ID.
*/
def userIdProvider(): Provider[Int] = {
new Provider[Int] {
override def get(): Int = {
threadLocalUserId.get()
}
}
}
}
val authInterceptor = new ServerAuthInterceptor()
val serviceDefinition = MyGrpc.bindService(
new MyApiService(dataStore = new DbDataStore(),
userIdProvider = authInterceptor.userIdProvider()))
val gRpcServerWithAuth = ServerInterceptors.intercept(serviceDefinition,
authInterceptor)
gRpcServer = NettyServerBuilder.forPort(port)
.addService(gRpcServerWithAuth)
.build()
Sorry to be a pain, but I'm struggling with this.
new ForwardingListener[RequestT](next.startCall(method, call, headers)) {
override def onPayload(payload: RequestT): Unit = {
val interceptor = new ClientAuthInterceptor(userId)
val channelWithAuth = ClientInterceptors.intercept(channel, interceptor)
blockingStub = SponventGrpc.newBlockingStub(channelWithAuth)
asyncStub = SponventGrpc.newStub(channelWithAuth) // doesn't forward the user ID - the default value of 0 is sent to the server when this stub is used
rpc sendMessage(stream Message) returns (Empty) {}
new ForwardingListener[RequestT](next.startCall(method, call, headers)) {
override def onPayload(payload: RequestT): Unit = {
// set the threadlocal before the call
threadLocalUserId.set(userId)
super.onPayload(payload)
// now clear it
threadLocalUserId.remove()
}
override def onHalfClose(): Unit = {
// set the threadlocal before the call
threadLocalUserId.set(userId)
super.onHalfClose()
// now clear it
threadLocalUserId.remove()
}
}
--
You received this message because you are subscribed to the Google Groups "grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grpc-io+u...@googlegroups.com.
To post to this group, send email to grp...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/grpc-io/d8d02a1f-c20b-43df-8de5-e9c101b54ea0%40googlegroups.com.