Skip to content
Snippets Groups Projects
Commit 82e9747a authored by Georgiy Eterevskiy's avatar Georgiy Eterevskiy
Browse files

Add docs

parent 7893a4f6
No related branches found
No related tags found
1 merge request!3Добавлен плагин аутентификации
# perxis-python
## Аутентификация
gRPC Python предоставляет способ перехвата RPC и добавления метаданных,
связанных с аутентификацией, через AuthMetadataPlugin.
Те, кому нужен специальный метод аутентификации, могут просто предоставить конкретную реализацию следующего интерфейса:
```python
class AuthMetadataPlugin:
"""A specification for custom authentication."""
def __call__(self, context, callback):
"""Implements authentication by passing metadata to a callback.
Implementations of this method must not block.
Args:
context: An AuthMetadataContext providing information on the RPC that
the plugin is being called to authenticate.
callback: An AuthMetadataPluginCallback to be invoked either
synchronously or asynchronously.
"""
```
Затем передайте экземпляр конкретной реализации в функцию grpc.metadata_call_credentials,
которая будет преобразована в объект CallCredentials.
ОБРАТИТЕ ВНИМАНИЕ, что можно передать объект функции Python напрямую, но мы рекомендуем наследовать от базового класса,
чтобы гарантировать правильность реализации.
```python
def metadata_call_credentials(metadata_plugin, name=None):
"""Construct CallCredentials from an AuthMetadataPlugin.
Args:
metadata_plugin: An AuthMetadataPlugin to use for authentication.
name: An optional name for the plugin.
Returns:
A CallCredentials.
"""
```
Объект CallCredentials можно передать непосредственно в RPC, например:
```python
call_credentials = grpc.metadata_call_credentials(my_foo_plugin)
stub.FooRpc(request, credentials=call_credentials)
```
### Пример авторизации и аутентификации OAuth2
```python
import grpc
from auth import OAuth2Gateway
from users.users_pb2 import GetRequest
from users.users_pb2_grpc import UsersStub
oauth2_plugin = OAuth2Gateway(
client_id='client_id', client_secret='client_secret',
token_url='https://example.com/oauth/token', audience='audience'
)
call_credentials = grpc.metadata_call_credentials(oauth2_plugin)
channel = grpc.aio.insecure_channel('https://example.com/api/')
request = GetRequest(user_id=1)
stub = UsersStub(channel)
stub.Get(request, credentials=call_credentials)
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment