decentralized and customizable links page on top of atproto ligo.at
atproto link-in-bio python uv
at 1b7c0778f94de667d002f803fdb1fcc64b2efd23 57 lines 1.6 kB view raw
1from flask import current_app 2from flask.sessions import SessionMixin 3from typing import NamedTuple, TypeVar 4 5from .atproto.types import OAuthAuthRequest, OAuthSession 6 7 8def save_auth_request(session: SessionMixin, request: OAuthAuthRequest): 9 return _set_into_session(session, "oauth_auth_request", request) 10 11 12def save_auth_session(session: SessionMixin, auth_session: OAuthSession): 13 return _set_into_session(session, "oauth_auth_session", auth_session) 14 15 16def delete_auth_request(session: SessionMixin): 17 return _delete_from_session(session, "oauth_auth_request") 18 19 20def delete_auth_session(session: SessionMixin): 21 return _delete_from_session(session, "oauth_auth_session") 22 23 24def get_auth_request(session: SessionMixin) -> OAuthAuthRequest | None: 25 return _get_from_session(session, "oauth_auth_request", OAuthAuthRequest) 26 27 28def get_auth_session(session: SessionMixin) -> OAuthSession | None: 29 return _get_from_session(session, "oauth_auth_session", OAuthSession) 30 31 32def _set_into_session(session: SessionMixin, key: str, value: NamedTuple): 33 session[key] = value._asdict() 34 35 36def _delete_from_session(session: SessionMixin, key: str): 37 del session[key] 38 39 40OAuthClass = TypeVar("OAuthClass") 41 42 43def _get_from_session( 44 session: SessionMixin, 45 key: str, 46 Type: type[OAuthClass], 47) -> OAuthClass | None: 48 if key not in session: 49 return None 50 51 try: 52 return Type(**session[key]) 53 except TypeError as exception: 54 current_app.logger.debug(f"unable to load {key}") 55 current_app.logger.debug(exception) 56 del session[key] 57 return None