-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: add /api/me to get identity model
includes fields: - username: str - given_name: Optional[str] - permissions in the form {"resource": ["action", ],} where permissions are only populated _by request_, because the server cannot know what all resource/action combinations are available.
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 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
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,56 @@ | ||
"""Handlers related to authorization | ||
""" | ||
import json | ||
import sys | ||
from typing import Dict | ||
from typing import List | ||
from typing import Optional | ||
|
||
if sys.version_info >= (3, 8): | ||
from typing import TypedDict | ||
else: | ||
try: | ||
from typing_extensions import TypedDict | ||
except ImportError: | ||
TypedDict = Dict | ||
|
||
from tornado import web | ||
|
||
from ...base.handlers import APIHandler | ||
|
||
|
||
class IdentityModel(TypedDict): | ||
username: str | ||
given_name: Optional[str] | ||
permissions: Dict[str, List[str]] | ||
|
||
|
||
class IdentityHandler(APIHandler): | ||
"""Get the current user's identity model""" | ||
|
||
@web.authenticated | ||
def get(self): | ||
resources: List[str] = self.get_argument("resources") or [] | ||
actions: List[str] = self.get_argument("actions") or [ | ||
"read", | ||
"write", | ||
"execute", | ||
] | ||
permissions: Dict[str, List[str]] = {} | ||
user = self.current_user | ||
for resource in resources: | ||
allowed = permissions[resource] = [] | ||
for action in actions: | ||
if self.authorizer.is_authorized(self, user=user, resource=resource, action=action): | ||
allowed.append(action) | ||
user_model: IdentityModel = dict( | ||
permissions=permissions, | ||
**self.authorizer.user_model(user), | ||
) | ||
self.write(json.dumps(user_model)) | ||
|
||
|
||
default_handlers = [ | ||
(r"/api/me", IdentityHandler), | ||
] |
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