···1010 resolve_pds_from_did,
1111)
1212from .atproto.oauth import pds_authed_req
1313-from .db import close_db_connection, init_db
1313+from .db import Keyval, close_db_connection, init_db
1414from .oauth import get_auth_session, oauth, save_auth_session
1515from .types import OAuthSession
1616···1818_ = app.config.from_prefixed_env()
1919app.register_blueprint(oauth)
2020init_db(app)
2121-2222-links: dict[str, list[dict[str, str]]] = {}
2323-profiles: dict[str, tuple[str, str]] = {}
24212522SCHEMA = "at.ligo"
2623···5552@app.get("/@<string:handle>")
5653def page_profile_with_handle(handle: str):
5754 reload = request.args.get("reload") is not None
5858-5959- did = resolve_did_from_handle(handle, reload=reload)
5555+ kv = Keyval(app, "did_from_handle")
5656+ did = resolve_did_from_handle(handle, kv, reload=reload)
6057 if did is None:
6158 return "did not found", 404
6259 return page_profile(did, reload=reload)
636064616562def page_profile(did: str, reload: bool = False):
6666- pds = resolve_pds_from_did(did, reload=reload)
6363+ kv = Keyval(app, "pds_from_did")
6464+ pds = resolve_pds_from_did(did, kv, reload=reload)
6765 if pds is None:
6866 return "pds not found", 404
6967 profile, _ = load_profile(pds, did, reload=reload)
···195193196194197195def load_links(pds: str, did: str, reload: bool = False) -> list[dict[str, str]] | None:
198198- if did in links and not reload:
196196+ kv = Keyval(app, "links_from_did")
197197+ links = kv.get(did)
198198+199199+ if links is not None and not reload:
199200 app.logger.debug(f"returning cached links for {did}")
200200- return links[did]
201201+ return json.loads(links)
201202202202- response = get_record(pds, did, f"{SCHEMA}.actor.links", "self")
203203- if response is None:
203203+ record = get_record(pds, did, f"{SCHEMA}.actor.links", "self")
204204+ if record is None:
204205 return None
205206206206- record = json.loads(response)
207207- links_ = record["value"]["links"]
207207+ links = record["links"]
208208 app.logger.debug(f"caching links for {did}")
209209- links[did] = links_
210210- return links_
209209+ kv.set(did, value=json.dumps(links))
210210+ return links
211211212212213213def load_profile(
214214- pds: str, did: str, reload: bool = False
214214+ pds: str,
215215+ did: str,
216216+ reload: bool = False,
215217) -> tuple[tuple[str, str] | None, bool]:
216216- if did in profiles and not reload:
218218+ kv = Keyval(app, "profile_from_did")
219219+ profile = kv.get(did)
220220+221221+ if profile is not None and not reload:
217222 app.logger.debug(f"returning cached profile for {did}")
218218- return profiles[did], False
223223+ return json.loads(profile), False
219224220225 from_bluesky = False
221221- response = get_record(pds, did, f"{SCHEMA}.actor.profile", "self")
222222- if response is None:
223223- response = get_record(pds, did, "app.bsky.actor.profile", "self")
226226+ record = get_record(pds, did, f"{SCHEMA}.actor.profile", "self")
227227+ if record is None:
228228+ record = get_record(pds, did, "app.bsky.actor.profile", "self")
224229 from_bluesky = True
225225- if response is None:
230230+ if record is None:
226231 return None, False
227232228228- record = json.loads(response)
229229- value: dict[str, str] = record["value"]
230230- profile = (value["displayName"], value["description"])
233233+ profile = (record["displayName"], record["description"])
231234 app.logger.debug(f"caching profile for {did}")
232232- profiles[did] = profile
235235+ kv.set(did, value=json.dumps(profile))
233236 return profile, from_bluesky
234237235238
+13-5
src/oauth.py
···6677import json
8899+from .db import Keyval
1010+911from .atproto import (
1012 is_valid_did,
1113 is_valid_handle,
···2830 if not username:
2931 return redirect(url_for("page_login"), 303)
30323333+ pdskv = Keyval(current_app, "authserver_from_pds")
3434+3135 if is_valid_handle(username) or is_valid_did(username):
3236 login_hint = username
3333- identity = resolve_identity(username)
3737+ kv = Keyval(current_app, "did_from_handle")
3838+ identity = resolve_identity(username, didkv=kv)
3439 if identity is None:
3540 return "couldnt resolve identity", 500
3641 did, handle, doc = identity
···3843 if not pds_url:
3944 return "pds not found", 404
4045 current_app.logger.debug(f"account PDS: {pds_url}")
4141- authserver_url = resolve_authserver_from_pds(pds_url)
4646+ authserver_url = resolve_authserver_from_pds(pds_url, pdskv)
4247 if not authserver_url:
4348 return "authserver not found", 404
44494550 elif username.startswith("https://") and is_safe_url(username):
4651 did, handle, pds_url = None, None, None
4752 login_hint = None
4848- authserver_url = resolve_authserver_from_pds(username) or username
5353+ authserver_url = resolve_authserver_from_pds(username, pdskv) or username
49545055 else:
5156 return "not a valid handle, did or auth server", 400
···134139135140 row = auth_request
136141142142+ didkv = Keyval(current_app, "did_from_handle")
143143+ authserverkv = Keyval(current_app, "authserver_from_pds")
144144+137145 if row.did:
138146 # If we started with an account identifier, this is simple
139147 did, handle, pds_url = row.did, row.handle, row.pds_url
···141149 else:
142150 did = tokens.sub
143151 assert is_valid_did(did)
144144- identity = resolve_identity(did)
152152+ identity = resolve_identity(did, didkv=didkv)
145153 if not identity:
146154 return "could not resolve identity", 500
147155 did, handle, did_doc = identity
148156 pds_url = pds_endpoint_from_doc(did_doc)
149157 if not pds_url:
150158 return "could not resolve pds", 500
151151- authserver_url = resolve_authserver_from_pds(pds_url)
159159+ authserver_url = resolve_authserver_from_pds(pds_url, authserverkv)
152160 assert authserver_url == authserver_iss
153161154162 assert row.scope == tokens.scope
+7-1
src/schema.sql
···11--- empty for now
11+drop table if exists keyval;
22+create table if not exists keyval (
33+ prefix text not null,
44+ key text not null,
55+ value text,
66+ primary key (prefix, key)
77+) strict, without rowid;