如何在服务器端获取身份验证令牌?

问题描述:

我试图实现与GRPC定制的基于令牌的身份验证方案,我必须在客户端下面的代码:如何在服务器端获取身份验证令牌?

with open('server.crt', 'rb') as f: 
    trusted_certs = f.read() 

credentials = grpc.ssl_channel_credentials(root_certificates=trusted_certs) 
composite_credentials = grpc.composite_channel_credentials(credentials, grpc.access_token_call_credentials("test_token")) 
channel = grpc.secure_channel('{}:{}'.format('localhost', 50051), composite_credentials) 
stub = helloworld_pb2_grpc.GreeterStub(channel) 
response = stub.SayHello(helloworld_pb2.HelloRequest(name='test')) 

此代码工作正常,似乎测试令牌传送到服务器。但是,我不能想出一个如何在服务器端获取令牌并检查它的解决方案。

以防万一,如果有人跑上去对同样的问题,正确的解决方法是使用:

context.invocation_metadata() 

在服务器端。这将返回表示在客户端传递的元数据的元组的集合(例如,“授权”头和它的值)。