Skip to content

Commit 367374f

Browse files
committed
GH-9: Implement the skeleton of claim declarator
1 parent fe95111 commit 367374f

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/fastapi_oauth2/claims.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from typing import Any
2+
from typing import Callable
3+
from typing import Union
4+
5+
6+
class Claims(dict):
7+
"""Claims configuration for a single provider."""
8+
9+
display_name: Union[str, Callable[[dict], Any]]
10+
identity: Union[str, Callable[[dict], Any]]
11+
picture: Union[str, Callable[[dict], Any]]
12+
email: Union[str, Callable[[dict], Any]]
13+
14+
def __init__(self, seq=None, **kwargs) -> None:
15+
super().__init__(seq or {}, **kwargs)
16+
self.display_name = self.get("display_name", "name")
17+
self.identity = self.get("identity", "sub")
18+
self.picture = self.get("picture", "picture")
19+
self.email = self.get("email", "email")
20+
21+
# @property
22+
# def display_name(self) -> str:
23+
# return self.get("display_name", "")
24+
#
25+
# @display_name.setter
26+
# def display_name(self, value: Union[Any, Callable[[dict], Any]]) -> None:
27+
# self["display_name"] = self._get_value(value)
28+
#
29+
# @property
30+
# def identity(self) -> str:
31+
# return self.get("identity", "")
32+
#
33+
# @identity.setter
34+
# def identity(self, value: Union[str, Callable[[dict], Any]]) -> None:
35+
# self["identity"] = self._get_value(value)
36+
#
37+
# @property
38+
# def picture(self) -> str:
39+
# return self.get("picture", "")
40+
#
41+
# @picture.setter
42+
# def picture(self, value: Union[str, Callable[[dict], Any]]) -> None:
43+
# self["picture"] = self._get_value(value)
44+
#
45+
# @property
46+
# def email(self) -> str:
47+
# return self.get("email", "")
48+
#
49+
# @email.setter
50+
# def email(self, value: Union[str, Callable[[dict], Any]]) -> None:
51+
# self["email"] = self._get_value(value)
52+
53+
def __getattr__(self, item):
54+
attr = super().get(item)
55+
if callable(attr):
56+
return attr(self)
57+
return self.get(attr)
58+
59+
# def _get_value(self, value: Union[str, Callable[[dict], Any]]) -> Any:
60+
# if callable(value):
61+
# return value(self)
62+
# return self.get(value, "")

0 commit comments

Comments
 (0)