-
Notifications
You must be signed in to change notification settings - Fork 14
Migrate frontpage identity operations to use atcute #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+156
−126
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
19b5da0
Migrate frontpage identity operations to use atcute
tom-sherman 5c0e0e7
Return our branded type
tom-sherman e6f1ac6
Add timeout back in
tom-sherman aa64911
Add missing accept header
tom-sherman fbabe98
Add back bidrectional verficiation that was accidentally removed
tom-sherman 735493e
Comment typo
tom-sherman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| export const FRONTPAGE_ATPROTO_HANDLE = "frontpage.fyi"; | ||
| export const FRONTPAGE_APPVIEW_USER_AGENT = | ||
| "appview/@frontpage.fyi (@frontpage.fyi, @tom.sherman.is)"; | ||
tom-sherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,121 +1,89 @@ | ||
| import { cache } from "react"; | ||
| import { type DID, getDidDoc, isDid, parseDid } from "./did"; | ||
| import { unstable_cache } from "next/cache"; | ||
| import { z } from "zod"; | ||
|
|
||
| export const getVerifiedDid = cache(async (handle: string) => { | ||
| const [dnsDid, httpDid] = await Promise.all([ | ||
| getAtprotoDidFromDns(handle).catch((_) => { | ||
| return null; | ||
| import { type DID, getDidDoc, isDid } from "./did"; | ||
| import { | ||
| CompositeHandleResolver, | ||
| DohJsonHandleResolver, | ||
| WellKnownHandleResolver, | ||
| } from "@atcute/identity-resolver"; | ||
| import { FRONTPAGE_APPVIEW_USER_AGENT } from "@/lib/constants"; | ||
| import { isValidHandle } from "@atproto/syntax"; | ||
|
|
||
| const handleResolver = new CompositeHandleResolver({ | ||
| strategy: "both", | ||
| methods: { | ||
| dns: new DohJsonHandleResolver({ | ||
| dohUrl: "https://cloudflare-dns.com/dns-query", | ||
| fetch: (request) => | ||
| fetch(request, { | ||
| headers: { | ||
| "User-Agent": FRONTPAGE_APPVIEW_USER_AGENT, | ||
| Accept: "application/dns-json", | ||
| }, | ||
| next: { | ||
| revalidate: 60 * 60 * 24, // 24 hours | ||
| }, | ||
| }), | ||
| }), | ||
| getAtprotoFromHttps(handle).catch((_) => { | ||
| return null; | ||
| http: new WellKnownHandleResolver({ | ||
| fetch: (request) => { | ||
| const signal = AbortSignal.timeout(1500); | ||
| return fetch(request, { | ||
| signal, | ||
| headers: { | ||
| "User-Agent": FRONTPAGE_APPVIEW_USER_AGENT, | ||
| }, | ||
| next: { | ||
| revalidate: 60 * 60 * 24, // 24 hours | ||
| }, | ||
| }); | ||
| }, | ||
| }), | ||
| ]); | ||
| }, | ||
| }); | ||
|
|
||
| if (dnsDid && httpDid && dnsDid !== httpDid) { | ||
| const getVerifiedDidFromHandle = cache(async (handle: string) => { | ||
| if (!isValidHandle(handle)) { | ||
| return null; | ||
| } | ||
| const did = await handleResolver.resolve(handle).catch(() => null); | ||
tom-sherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if (!did) return null; | ||
|
|
||
| const did = dnsDid ?? (httpDid ? parseDid(httpDid) : null); | ||
| if (!did) { | ||
| if (!isDid(did)) { | ||
| return null; | ||
| } | ||
|
|
||
tom-sherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const plcDoc = await getDidDoc(did); | ||
| const plcHandle = plcDoc.alsoKnownAs | ||
| .find((handle) => handle.startsWith("at://")) | ||
| const didDoc = await getDidDoc(did); | ||
| const didDocHandle = didDoc.alsoKnownAs | ||
| ?.find((handle) => handle.startsWith("at://")) | ||
| ?.replace("at://", ""); | ||
|
|
||
| if (!plcHandle) return null; | ||
| if (!didDocHandle) return null; | ||
|
|
||
| return plcHandle.toLowerCase() === handle.toLowerCase() ? did : null; | ||
| return didDocHandle.toLowerCase() === handle.toLowerCase() ? did : null; | ||
| }); | ||
|
|
||
| const DnsQueryResponse = z.object({ | ||
| Answer: z.array( | ||
| z.object({ | ||
| name: z.string(), | ||
| type: z.number(), | ||
| TTL: z.number(), | ||
| data: z.string(), | ||
| }), | ||
| ), | ||
| }); | ||
|
|
||
| // See https://developers.cloudflare.com/1.1.1.1/encryption/dns-over-https/make-api-requests/dns-json/ | ||
| async function getAtprotoDidFromDns(handle: string) { | ||
| const url = new URL("https://cloudflare-dns.com/dns-query"); | ||
| url.searchParams.set("type", "TXT"); | ||
| url.searchParams.set("name", `_atproto.${handle}`); | ||
|
|
||
| const response = await fetch(url, { | ||
| headers: { | ||
| Accept: "application/dns-json", | ||
| }, | ||
| next: { | ||
| revalidate: 60 * 60 * 24, // 24 hours | ||
| }, | ||
| }); | ||
|
|
||
| const { Answer } = DnsQueryResponse.parse(await response.json()); | ||
| // Answer[0].data is "\"did=...\"" (with quotes) | ||
| const val = Answer[0]?.data | ||
| ? // eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access | ||
| JSON.parse(Answer[0]?.data).split("did=")[1] | ||
| : null; | ||
|
|
||
| return val | ||
| ? // eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
| parseDid(val) | ||
| : null; | ||
| } | ||
|
|
||
| const getAtprotoFromHttps = unstable_cache( | ||
| async (handle: string) => { | ||
| let res; | ||
| const timeoutSignal = AbortSignal.timeout(1500); | ||
| try { | ||
| res = await fetch(`https://${handle}/.well-known/atproto-did`, { | ||
| signal: timeoutSignal, | ||
| }); | ||
| } catch (_e) { | ||
| // We're caching failures here, we should at some point invalidate the cache by listening to handle changes on the network | ||
| return null; | ||
| } | ||
|
|
||
| if (!res.ok) { | ||
| return null; | ||
| } | ||
| return parseDid((await res.text()).trim()); | ||
| }, | ||
| ["did", "https"], | ||
| { | ||
| revalidate: 60 * 60 * 24, // 24 hours | ||
| }, | ||
| ); | ||
|
|
||
| /** | ||
| * Returns the DID of the the handle or the DID itself if it's a DID. Or null if the handle doesn't resolve to a DID. | ||
| * Returns the DID of the handle or the DID itself if it's a DID. Or null if the handle doesn't resolve to a DID. | ||
| */ | ||
| export const getDidFromHandleOrDid = cache(async (handleOrDid: string) => { | ||
| const decodedHandleOrDid = decodeURIComponent(handleOrDid); | ||
| if (isDid(decodedHandleOrDid)) { | ||
| return decodedHandleOrDid; | ||
| } | ||
|
|
||
tom-sherman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return getVerifiedDid(decodedHandleOrDid); | ||
| return getVerifiedDidFromHandle(decodedHandleOrDid); | ||
| }); | ||
|
|
||
| export const getVerifiedHandle = cache(async (did: DID) => { | ||
| const plcDoc = await getDidDoc(did); | ||
| const plcHandle = plcDoc.alsoKnownAs | ||
| .find((handle) => handle.startsWith("at://")) | ||
| const didDoc = await getDidDoc(did); | ||
| const didDocHandle = didDoc.alsoKnownAs | ||
| ?.find((handle) => handle.startsWith("at://")) | ||
| ?.replace("at://", ""); | ||
|
|
||
| if (!plcHandle) return null; | ||
| if (!didDocHandle) return null; | ||
|
|
||
| const resolvedDid = await getVerifiedDid(plcHandle); | ||
| const resolvedDid = await getVerifiedDidFromHandle(didDocHandle); | ||
|
|
||
| return resolvedDid ? plcHandle : null; | ||
| return resolvedDid ? didDocHandle : null; | ||
| }); | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.