Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
perxis-python
Manage
Activity
Members
Labels
Plan
Issues
3
Issue boards
Milestones
Wiki
Custom issue tracker
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
perxis
perxis-python
Commits
82e9747a
Commit
82e9747a
authored
4 years ago
by
Georgiy Eterevskiy
Browse files
Options
Downloads
Patches
Plain Diff
Add docs
parent
7893a4f6
No related branches found
No related tags found
1 merge request
!3
Добавлен плагин аутентификации
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
README.md
+69
-0
69 additions, 0 deletions
README.md
with
69 additions
and
0 deletions
README.md
+
69
−
0
View file @
82e9747a
# 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
)
```
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment