Skip to content

Commit

Permalink
[bsky did 0] Feature - Add appview DID to bsky (#3321)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelbsky authored Jan 7, 2025
1 parent 72eba67 commit 9690f11
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 25 deletions.
55 changes: 31 additions & 24 deletions packages/bsky/src/api/well-known.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,38 @@ import AppContext from '../context'
export const createRouter = (ctx: AppContext): express.Router => {
const router = express.Router()

router.get('/.well-known/did.json', (_req, res) => {
const hostname = ctx.cfg.publicUrl && new URL(ctx.cfg.publicUrl).hostname
if (!hostname || ctx.cfg.serverDid !== `did:web:${hostname}`) {
return res.sendStatus(404)
}
res.json({
'@context': ['https://www.w3.org/ns/did/v1'],
id: ctx.cfg.serverDid,
verificationMethod: [
{
id: `${ctx.cfg.serverDid}#atproto`,
type: 'Multikey',
controller: ctx.cfg.serverDid,
publicKeyMultibase: ctx.signingKey.did().replace('did:key:', ''),
},
],
service: [
{
id: '#bsky_notif',
type: 'BskyNotificationService',
serviceEndpoint: `https://${hostname}`,
},
],
const did = ctx.cfg.serverDid
if (did.startsWith('did:web:')) {
const hostname = did.slice('did:web:'.length)
const serviceEndpoint = `https://${hostname}`

router.get('/.well-known/did.json', (_req, res) => {
res.json({
'@context': ['https://www.w3.org/ns/did/v1'],
id: did,
verificationMethod: [
{
id: `${did}#atproto`,
type: 'Multikey',
controller: did,
publicKeyMultibase: ctx.signingKey.did().replace('did:key:', ''),
},
],
service: [
{
id: '#bsky_notif',
type: 'BskyNotificationService',
serviceEndpoint,
},
{
id: '#bsky_appview',
type: 'BskyAppView',
serviceEndpoint,
},
],
})
})
})
}

return router
}
14 changes: 14 additions & 0 deletions packages/dev-env/src/bsky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ export class TestBsky {
signer: serviceKeypair,
})

const endpoint = `http://localhost:${port}`

await plcClient.updateData(serverDid, serviceKeypair, (x) => {
x.services['bsky_notif'] = {
type: 'BskyNotificationService',
endpoint,
}
x.services['bsky_appview'] = {
type: 'BskyAppView',
endpoint,
}
return x
})

// shared across server, ingester, and indexer in order to share pool, avoid too many pg connections.
const db = new bsky.Database({
url: cfg.dbPostgresUrl,
Expand Down
10 changes: 9 additions & 1 deletion packages/pds/src/pipethrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export async function parseProxyInfo(

export const parseProxyHeader = async (
// Using subset of AppContext for testing purposes
ctx: Pick<AppContext, 'idResolver'>,
ctx: Pick<AppContext, 'cfg' | 'idResolver'>,
proxyTo: string,
): Promise<{ did: string; url: string }> => {
// /!\ Hot path
Expand Down Expand Up @@ -246,6 +246,14 @@ export const parseProxyHeader = async (
throw new InvalidRequestError('could not resolve proxy did service url')
}

// Special case a configured appview, while still proxying correctly any other appview
if (
ctx.cfg.bskyAppView &&
proxyTo === `${ctx.cfg.bskyAppView.did}#bsky_appview`
) {
return { did, url: ctx.cfg.bskyAppView.url }
}

return { did, url }
}

Expand Down

0 comments on commit 9690f11

Please sign in to comment.