Changeset View
Changeset View
Standalone View
Standalone View
profiles/blender_id.py
| Show All 15 Lines | class BIDSession: | ||||
| """Wrap up interactions with Blender ID, such as fetching user info and avatar.""" | """Wrap up interactions with Blender ID, such as fetching user info and avatar.""" | ||||
| _anonymous_session = None | _anonymous_session = None | ||||
| def __init__(self): | def __init__(self): | ||||
| """Initialise Blender ID client settings.""" | """Initialise Blender ID client settings.""" | ||||
| self.settings = blender_id_oauth_settings() | self.settings = blender_id_oauth_settings() | ||||
| def _make_session(self, oauth_user_id: str = None) -> OAuth2Session: | def _make_session(self, access_token: str = None) -> OAuth2Session: | ||||
| """Return a new OAuth2 session, optionally authenticated with a user token.""" | """Return a new OAuth2 session, optionally authenticated with an access token.""" | ||||
| if oauth_user_id: | if access_token: | ||||
| token = self.get_oauth_token(oauth_user_id) | |||||
| return OAuth2Session( | return OAuth2Session( | ||||
| self.settings.client, | self.settings.client, | ||||
| token={ | token={ | ||||
| 'access_token': token.access_token, | 'access_token': access_token, | ||||
| 'refresh_token': token.refresh_token, | |||||
| }, | }, | ||||
| ) | ) | ||||
| return OAuth2Session(self.settings.client) | return OAuth2Session(self.settings.client) | ||||
| @property | @property | ||||
| def session(self): | def session(self): | ||||
| """Return a reusable "anonymous" OAuth2Session for fetching avatars from Blender ID. | """Return a reusable "anonymous" OAuth2Session for fetching avatars from Blender ID. | ||||
| Show All 28 Lines | def get_user_info(self, oauth_user_id: str) -> Dict[str, Any]: | ||||
| { | { | ||||
| "id": 2, | "id": 2, | ||||
| "full_name": "Jane Doe", | "full_name": "Jane Doe", | ||||
| "email": "jane@example.com", | "email": "jane@example.com", | ||||
| "nickname": "janedoe", | "nickname": "janedoe", | ||||
| "roles": {"dev_core": True}, | "roles": {"dev_core": True}, | ||||
| } | } | ||||
| """ | """ | ||||
| session = self._make_session(oauth_user_id) | token = self.get_oauth_token(oauth_user_id) | ||||
| if not token: | |||||
| raise Exception(f'No access token found for {oauth_user_id}') | |||||
| session = self._make_session(access_token=token.access_token) | |||||
| resp = session.get(self.settings.url_userinfo) | resp = session.get(self.settings.url_userinfo) | ||||
| resp.raise_for_status() | resp.raise_for_status() | ||||
| payload = resp.json() | payload = resp.json() | ||||
| assert isinstance(payload, dict) | assert isinstance(payload, dict) | ||||
| return payload | return payload | ||||
| def get_avatar(self, oauth_user_id: str) -> Tuple[str, io.BytesIO]: | def get_avatar(self, oauth_user_id: str) -> Tuple[str, io.BytesIO]: | ||||
| Show All 9 Lines | |||||