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

时间:2017-05-27 22:01:51

标签: python grpc

我正在尝试使用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'))

此代码工作正常,似乎将测试令牌传输到服务器。但是,我无法想出如何在服务器端获取令牌并进行检查的解决方案。

1 个答案:

答案 0 :(得分:2)

如果有人遇到同样的问题,那么正确的解决方案就是使用:

context.invocation_metadata()

在服务器端。这将返回一组元组,这些元组表示在客户端传递的元数据(例如,“authorization”标题及其值)。