| 1 |
# Authentication Extension Example in gRPC Python |
| 2 |
|
| 3 |
## Check Our Guide First |
| 4 |
|
| 5 |
For most common usage of authentication in gRPC Python, please see our |
| 6 |
[Authentication](https://grpc.io/docs/guides/auth/) guide's Python section. The |
| 7 |
Guide includes following scenarios: |
| 8 |
|
| 9 |
1. Server SSL credential setup |
| 10 |
2. Client SSL credential setup |
| 11 |
3. Authenticate with Google using a JWT |
| 12 |
4. Authenticate with Google using an Oauth2 token |
| 13 |
|
| 14 |
Also, the guide talks about gRPC specific credential types. |
| 15 |
|
| 16 |
### Channel credentials |
| 17 |
|
| 18 |
Channel credentials are attached to a `Channel` object, the most common use case |
| 19 |
are SSL credentials. |
| 20 |
|
| 21 |
### Call credentials |
| 22 |
|
| 23 |
Call credentials are attached to a `Call` object (corresponding to an RPC). |
| 24 |
Under the hood, the call credentials is a function that takes in information of |
| 25 |
the RPC and modify metadata through callback. |
| 26 |
|
| 27 |
## About This Example |
| 28 |
|
| 29 |
This example focuses on extending gRPC authentication mechanism: |
| 30 |
1) Customize authentication plugin; |
| 31 |
2) Composite client side credentials; |
| 32 |
3) Validation through interceptor on server side. |
| 33 |
|
| 34 |
## AuthMetadataPlugin: Manipulate metadata for each call |
| 35 |
|
| 36 |
Unlike TLS/SSL based authentication, the authentication extension in gRPC Python |
| 37 |
lives at a much higher level of networking. It relies on the transmission of |
| 38 |
metadata (HTTP Header) between client and server, instead of alternating the |
| 39 |
transport protocol. |
| 40 |
|
| 41 |
gRPC Python provides a way to intercept an RPC and append authentication related |
| 42 |
metadata through |
| 43 |
[`AuthMetadataPlugin`](https://grpc.github.io/grpc/python/grpc.html#grpc.AuthMetadataPlugin). |
| 44 |
Those in need of a custom authentication method may simply provide a concrete |
| 45 |
implementation of the following interface: |
| 46 |
|
| 47 |
```Python |
| 48 |
class AuthMetadataPlugin: |
| 49 |
"""A specification for custom authentication.""" |
| 50 |
|
| 51 |
def __call__(self, context, callback): |
| 52 |
"""Implements authentication by passing metadata to a callback. |
| 53 |
|
| 54 |
Implementations of this method must not block. |
| 55 |
|
| 56 |
Args: |
| 57 |
context: An AuthMetadataContext providing information on the RPC that |
| 58 |
the plugin is being called to authenticate. |
| 59 |
callback: An AuthMetadataPluginCallback to be invoked either |
| 60 |
synchronously or asynchronously. |
| 61 |
""" |
| 62 |
``` |
| 63 |
|
| 64 |
Then pass the instance of the concrete implementation to |
| 65 |
`grpc.metadata_call_credentials` function to be converted into a |
| 66 |
`CallCredentials` object. Please NOTE that it is possible to pass a Python |
| 67 |
function object directly, but we recommend to inherit from the base class to |
| 68 |
ensure implementation correctness. |
| 69 |
|
| 70 |
|
| 71 |
```Python |
| 72 |
def metadata_call_credentials(metadata_plugin, name=None): |
| 73 |
"""Construct CallCredentials from an AuthMetadataPlugin. |
| 74 |
|
| 75 |
Args: |
| 76 |
metadata_plugin: An AuthMetadataPlugin to use for authentication. |
| 77 |
name: An optional name for the plugin. |
| 78 |
|
| 79 |
Returns: |
| 80 |
A CallCredentials. |
| 81 |
""" |
| 82 |
``` |
| 83 |
|
| 84 |
The `CallCredentials` object can be passed directly into an RPC like: |
| 85 |
|
| 86 |
```Python |
| 87 |
call_credentials = grpc.metadata_call_credentials(my_foo_plugin) |
| 88 |
stub.FooRpc(request, credentials=call_credentials) |
| 89 |
``` |
| 90 |
|
| 91 |
Or you can use `ChannelCredentials` and `CallCredentials` at the same time by |
| 92 |
combining them: |
| 93 |
|
| 94 |
```Python |
| 95 |
channel_credentials = ... |
| 96 |
call_credentials = ... |
| 97 |
composite_credentials = grpc.composite_channel_credentials( |
| 98 |
channel_credential, |
| 99 |
call_credentials) |
| 100 |
channel = grpc.secure_channel(server_address, composite_credentials) |
| 101 |
``` |
| 102 |
|
| 103 |
It is also possible to apply multiple `CallCredentials` to a single RPC: |
| 104 |
|
| 105 |
```Python |
| 106 |
call_credentials_foo = ... |
| 107 |
call_credentials_bar = ... |
| 108 |
call_credentials = grpc.composite_call_credentials( |
| 109 |
call_credentials_foo, |
| 110 |
call_credentials_bar) |
| 111 |
stub.FooRpc(request, credentials=call_credentials) |
| 112 |
``` |