-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from getdozer/feat-Authorization
feat: Authorization
- Loading branch information
Showing
7 changed files
with
118 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from pydozer.auth import AuthClient | ||
from pydozer.api import ApiClient | ||
|
||
master_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiJjYWNoZV91c2VyIiwic3ViIjoiYXBpQGRvemVyLmNvbSIsImV4cCI6MTY4MTI4NzcwNDAyNywiYWNjZXNzIjoiQWxsIn0.aD5L6XURIr3hrUp0LzfUQWKHairjK6DDkMlzvnVvzHA' | ||
|
||
client = AuthClient(token=master_token, url='0.0.0.0:50051') | ||
|
||
restricted_token = client.get_auth_token() | ||
|
||
print(f"Token: {restricted_token}") | ||
|
||
api = ApiClient("pickup", url='0.0.0.0:50051', token=restricted_token) | ||
|
||
count = api.count() | ||
|
||
print(f"Count: {count}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import json | ||
import os | ||
import grpc | ||
from pydozer.helper import describe_services | ||
from pydozer.auth_pb2_grpc import AuthGrpcServiceStub | ||
import pydozer.auth_pb2 as auth__pb2 | ||
|
||
|
||
DOZER_API_URL = os.getenv("DOZER_API_URL", "0.0.0.0:50051") | ||
|
||
|
||
class AuthClient: | ||
"""Common Client for Dozer Authorization Service | ||
Args: | ||
token: A valid token to generate restricted tokens | ||
url (str, optional): Dozer gRPC URL. Defaults to Env variable DOZER_API_URL or `0.0.0.0:50051`. | ||
""" | ||
def __init__(self, token, url=DOZER_API_URL, secure=False): | ||
|
||
self.metadata = [('authorization', f'Bearer {token}')] | ||
|
||
if secure: | ||
channel = grpc.secure_channel(url) | ||
else: | ||
channel = grpc.insecure_channel(url) | ||
|
||
self.channel = channel | ||
|
||
self.client = AuthGrpcServiceStub(channel) | ||
|
||
def describe(self) -> dict: | ||
"""Describe the available gRPC services and methods | ||
Returns: | ||
dict: dictionary of available services and methods | ||
""" | ||
|
||
return describe_services(self.channel) | ||
|
||
def get_auth_token(self, query: dict = None) -> auth__pb2.GetAuthTokenResponse: | ||
"""Get a token for restricted access. Response is in the common format. | ||
Args: | ||
query (dict, optional): Accepts a filter | ||
to query only a subset of records. | ||
Keys could be | ||
`$filter`: `dict` eg: `{"name": "John"}` or `{"id": { "$gt": 1}}` | ||
`$limit`: `int` | ||
`$skip`: `int`, | ||
'$after`: `int`, cursor to start from. `$skip` and `$after` cannot be used in the same query | ||
`$order_by`: `dict` eg: `{"name": "asc"}` or `{"id": "desc"}` | ||
Defaults to {}. | ||
Returns: | ||
GetAuthTokenResponse: {"token": token} | ||
token: A token with restricted access | ||
""" | ||
|
||
if query is None or len(query) == 0: | ||
query_str = '"All"' | ||
else: | ||
data = {} | ||
for key, value in query.items(): | ||
data[key] = value | ||
query_str = json.dumps(data) | ||
|
||
req = auth__pb2.GetAuthTokenRequest(access_filter=query_str) | ||
response = self.client.getAuthToken(req, metadata=self.metadata) | ||
return response.token | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters