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

Add example for auth client

parent 82e53a7a
No related branches found
No related tags found
1 merge request!5Добавлены примеры использования, обновлены proto, произведен апгрейд зависимостей
import logging
import grpc
from oauthlib.oauth2 import BackendApplicationClient
from perxis.auth import OAuth2Plugin
from perxis.collections_perxis.collections_pb2 import ListRequest
from perxis.collections_perxis.collections_pb2_grpc import CollectionsStub
def main():
client_id = 'YGPQSAUPgco4iTQxNejtXDjFVwPmYT3n'
client_secret = '_d6F-VlQGmcsU25AXIoCU4EU3v56-28P6jwUHkutg_hSPseConphyLs3F-Buuq1h'
token_url = 'https://adwz.auth0.com/oauth/token'
audience = 'some'
oauth2_plugin = OAuth2Plugin(
client=BackendApplicationClient(client_id=client_id),
client_secret=client_secret,
token_url=token_url,
audience=audience
)
call_credentials = grpc.metadata_call_credentials(oauth2_plugin, name='auth')
channel_credentials = grpc.ssl_channel_credentials()
composite_credentials = grpc.composite_channel_credentials(
channel_credentials, call_credentials
)
with grpc.secure_channel('envoy.perxis.pt.perx.ru:443', composite_credentials) as channel:
stub = CollectionsStub(channel)
collections = stub.List(ListRequest(space_id="c1mrvup3e6litup7tf0g", env_id="master"))
print(collections)
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
main()
......@@ -11,7 +11,7 @@ class OAuth2Plugin(grpc.AuthMetadataPlugin):
_token = None
def __init__(self, client: Type[Client], client_secret: str, token_url: str, audience: str,
signature_header_key: str = 'Authorization', token_type: str = 'Bearer') -> None:
signature_header_key: str = 'authorization', token_type: str = 'Bearer') -> None:
self._client_secret = client_secret
self._token_url = token_url
......@@ -44,7 +44,7 @@ class OAuth2Plugin(grpc.AuthMetadataPlugin):
if self._token is None:
self._token = fetch_token()
if self._token.expires_at and self._token.expires_at < time.time():
if self._token['expires_at'] and self._token['expires_at'] < time.time():
if 'refresh_token' in self._token:
self._token = refresh_token()
else:
......
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