Skip to content

Commit

Permalink
Pass headers to feed generator (#2030)
Browse files Browse the repository at this point in the history
* Pass headers to feed generator

* Allow-list headers rather than forward all by default
  • Loading branch information
CooperEdmunds authored Jan 9, 2024
1 parent 51fcba7 commit 50f7045
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
18 changes: 11 additions & 7 deletions packages/bsky/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,21 @@ export default function (server: Server, ctx: AppContext) {
const db = ctx.db.getReplica()
const feedService = ctx.services.feed(db)
const viewer = auth.credentials.iss

const headers: Record<string, string> = {}
const headersToForward = ['authorization', 'accept-language']
for (const header of headersToForward) {
const value = req.headers[header]
if (typeof value === 'string') {
headers[header] = value
}
}
const { timerSkele, timerHydr, ...result } = await getFeed(
{ ...params, viewer },
{
db,
feedService,
appCtx: ctx,
authorization: req.headers['authorization'],
headers,
},
)

Expand Down Expand Up @@ -127,7 +134,7 @@ type Context = {
db: Database
feedService: FeedService
appCtx: AppContext
authorization?: string
headers: Record<string, string>
}

type Params = GetFeedParams & { viewer: string | null }
Expand All @@ -147,7 +154,7 @@ const skeletonFromFeedGen = async (
ctx: Context,
params: GetFeedParams,
): Promise<AlgoResponse> => {
const { db, appCtx, authorization } = ctx
const { db, appCtx, headers } = ctx
const { feed } = params
// Resolve and fetch feed skeleton
const found = await db.db
Expand Down Expand Up @@ -185,9 +192,6 @@ const skeletonFromFeedGen = async (
let skeleton: SkeletonOutput
try {
// @TODO currently passthrough auth headers from pds
const headers: Record<string, string> = authorization
? { authorization: authorization }
: {}
const result = await agent.api.app.bsky.feed.getFeedSkeleton(params, {
headers,
})
Expand Down
19 changes: 17 additions & 2 deletions packages/pds/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,32 @@ import AppContext from '../../../../context'
export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeed({
auth: ctx.authVerifier.access,
handler: async ({ params, auth }) => {
handler: async ({ req, params, auth }) => {
const requester = auth.credentials.did

const { data: feed } =
await ctx.appViewAgent.api.app.bsky.feed.getFeedGenerator(
{ feed: params.feed },
await ctx.appviewAuthHeaders(requester),
)
const serviceAuthHeaders = await ctx.serviceAuthHeaders(
requester,
feed.view.did,
)
const headers: Record<string, string> = {}
const headersToForward = ['accept-language']
for (const header of headersToForward) {
const value = req.headers[header]
if (typeof value === 'string') {
headers[header] = value
}
}
const feedOpts = {
headers: { ...headers, ...serviceAuthHeaders.headers },
}
const res = await ctx.appViewAgent.api.app.bsky.feed.getFeed(
params,
await ctx.serviceAuthHeaders(requester, feed.view.did),
feedOpts,
)
return {
encoding: 'application/json',
Expand Down

0 comments on commit 50f7045

Please sign in to comment.