Skip to content

Commit

Permalink
refactor auth in bsky
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 22, 2023
1 parent fc32b36 commit f208397
Show file tree
Hide file tree
Showing 78 changed files with 599 additions and 2,074 deletions.
18 changes: 8 additions & 10 deletions packages/bsky/src/api/app/bsky/actor/getProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ import { ModerationService } from '../../../../services/moderation'
export default function (server: Server, ctx: AppContext) {
const getProfile = createPipeline(skeleton, hydration, noRules, presentation)
server.app.bsky.actor.getProfile({
auth: ctx.authOptionalAccessOrRoleVerifier,
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ auth, params, res }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const modService = ctx.services.moderation(ctx.db.getPrimary())
const viewer = 'did' in auth.credentials ? auth.credentials.did : null
const canViewTakendownProfile =
auth.credentials.type === 'role' && auth.credentials.triage
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)

const [result, repoRev] = await Promise.allSettled([
getProfile(
{ ...params, viewer, canViewTakendownProfile },
{ ...params, viewer, canViewTakedowns },
{ db, actorService, modService },
),
actorService.getRepoRev(viewer),
Expand All @@ -53,12 +51,12 @@ const skeleton = async (
ctx: Context,
): Promise<SkeletonState> => {
const { actorService, modService } = ctx
const { canViewTakendownProfile } = params
const { canViewTakedowns } = params
const actor = await actorService.getActor(params.actor, true)
if (!actor) {
throw new InvalidRequestError('Profile not found')
}
if (!canViewTakendownProfile && softDeleted(actor)) {
if (!canViewTakedowns && softDeleted(actor)) {
const isSuspended = await modService.isSubjectSuspended(actor.did)
if (isSuspended) {
throw new InvalidRequestError(
Expand All @@ -78,10 +76,10 @@ const skeleton = async (
const hydration = async (state: SkeletonState, ctx: Context) => {
const { actorService } = ctx
const { params, actor } = state
const { viewer, canViewTakendownProfile } = params
const { viewer, canViewTakedowns } = params
const hydration = await actorService.views.profileDetailHydration(
[actor.did],
{ viewer, includeSoftDeleted: canViewTakendownProfile },
{ viewer, includeSoftDeleted: canViewTakedowns },
)
return { ...state, ...hydration }
}
Expand Down Expand Up @@ -110,7 +108,7 @@ type Context = {

type Params = QueryParams & {
viewer: string | null
canViewTakendownProfile: boolean
canViewTakedowns: boolean
}

type SkeletonState = { params: Params; actor: Actor }
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/actor/getProfiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import { createPipeline, noRules } from '../../../../pipeline'
export default function (server: Server, ctx: AppContext) {
const getProfile = createPipeline(skeleton, hydration, noRules, presentation)
server.app.bsky.actor.getProfiles({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ auth, params, res }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const [result, repoRev] = await Promise.all([
getProfile({ ...params, viewer }, { db, actorService }),
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/actor/getSuggestions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.actor.getSuggestions({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const graphService = ctx.services.graph(db)
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const result = await getSuggestions(
{ ...params, viewer },
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/actor/searchActors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import {

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.searchActors({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ auth, params }) => {
const { cursor, limit } = params
const requester = auth.credentials.did
const requester = auth.credentials.iss
const rawQuery = params.q ?? params.term
const query = cleanQuery(rawQuery || '')
const db = ctx.db.getReplica('search')
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/actor/searchActorsTypeahead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import {

export default function (server: Server, ctx: AppContext) {
server.app.bsky.actor.searchActorsTypeahead({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const { limit } = params
const requester = auth.credentials.did
const requester = auth.credentials.iss
const rawQuery = params.q ?? params.term
const query = cleanQuery(rawQuery || '')
const db = ctx.db.getReplica('search')
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getActorFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { TimeCidKeyset, paginate } from '../../../../db/pagination'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getActorFeeds({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ auth, params }) => {
const { actor, limit, cursor } = params
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getActorLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getActorLikes({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth, res }) => {
const viewer = auth.credentials.did
const viewer = auth.credentials.iss
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const feedService = ctx.services.feed(db)
Expand Down
5 changes: 2 additions & 3 deletions packages/bsky/src/api/app/bsky/feed/getAuthorFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getAuthorFeed({
auth: ctx.authOptionalAccessOrRoleVerifier,
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth, res }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const feedService = ctx.services.feed(db)
const graphService = ctx.services.graph(db)
const viewer =
auth.credentials.type === 'access' ? auth.credentials.did : null
const { viewer } = ctx.authVerifier.parseCreds(auth)

const [result, repoRev] = await Promise.all([
getAuthorFeed(
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getFeed({
auth: ctx.authOptionalVerifierAnyAudience,
auth: ctx.authVerifier.standardOptionalAnyAud,
handler: async ({ params, auth, req }) => {
const db = ctx.db.getReplica()
const feedService = ctx.services.feed(db)
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const { timerSkele, timerHydr, ...result } = await getFeed(
{ ...params, viewer },
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getFeedGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeedGenerator({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const { feed } = params
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const db = ctx.db.getReplica()
const feedService = ctx.services.feed(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getFeedGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getFeedGenerators({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const { feeds } = params
const viewer = auth.credentials.did
const viewer = auth.credentials.iss
const db = ctx.db.getReplica()
const feedService = ctx.services.feed(db)
const actorService = ctx.services.actor(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getFeedSkeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { toSkeletonItem } from '../../../../feed-gen/types'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getFeedSkeleton({
auth: ctx.authVerifierAnyAudience,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const { feed } = params
const viewer = auth.credentials.did
const viewer = auth.credentials.iss
const localAlgo = ctx.algos[feed]

if (!localAlgo) {
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getLikes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import { createPipeline } from '../../../../pipeline'
export default function (server: Server, ctx: AppContext) {
const getLikes = createPipeline(skeleton, hydration, noBlocks, presentation)
server.app.bsky.feed.getLikes({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const graphService = ctx.services.graph(db)
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const result = await getLikes(
{ ...params, viewer },
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getListFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getListFeed({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth, res }) => {
const viewer = auth.credentials.did
const viewer = auth.credentials.iss
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const feedService = ctx.services.feed(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getPostThread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getPostThread({
auth: ctx.authOptionalAccessOrRoleVerifier,
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth, res }) => {
const viewer = 'did' in auth.credentials ? auth.credentials.did : null
const { viewer } = ctx.authVerifier.parseCreds(auth)
const db = ctx.db.getReplica('thread')
const feedService = ctx.services.feed(db)
const actorService = ctx.services.actor(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ import { ActorService } from '../../../../services/actor'
export default function (server: Server, ctx: AppContext) {
const getPosts = createPipeline(skeleton, hydration, noBlocks, presentation)
server.app.bsky.feed.getPosts({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const db = ctx.db.getReplica()
const feedService = ctx.services.feed(db)
const actorService = ctx.services.actor(db)
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const results = await getPosts(
{ ...params, viewer },
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getRepostedBy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getRepostedBy({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ params, auth }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const graphService = ctx.services.graph(db)
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const result = await getRepostedBy(
{ ...params, viewer },
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getSuggestedFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import AppContext from '../../../../context'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.feed.getSuggestedFeeds({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ auth }) => {
const viewer = auth.credentials.did
const viewer = auth.credentials.iss

const db = ctx.db.getReplica()
const feedService = ctx.services.feed(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/getTimeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.getTimeline({
auth: ctx.authVerifier,
auth: ctx.authVerifier.standard,
handler: async ({ params, auth, res }) => {
const viewer = auth.credentials.did
const viewer = auth.credentials.iss
const db = ctx.db.getReplica('timeline')
const feedService = ctx.services.feed(db)
const actorService = ctx.services.actor(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/feed/searchPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.feed.searchPosts({
auth: ctx.authOptionalVerifier,
auth: ctx.authVerifier.standardOptional,
handler: async ({ auth, params }) => {
const viewer = auth.credentials.did
const viewer = auth.credentials.iss
const db = ctx.db.getReplica('search')
const feedService = ctx.services.feed(db)
const actorService = ctx.services.actor(db)
Expand Down
4 changes: 2 additions & 2 deletions packages/bsky/src/api/app/bsky/graph/getBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { notSoftDeletedClause } from '../../../../db/util'

export default function (server: Server, ctx: AppContext) {
server.app.bsky.graph.getBlocks({
auth: ctx.authVerifier,
auth: ctx.authVerifier.standard,
handler: async ({ params, auth }) => {
const { limit, cursor } = params
const requester = auth.credentials.did
const requester = auth.credentials.iss
const db = ctx.db.getReplica()
const { ref } = db.db.dynamic

Expand Down
16 changes: 7 additions & 9 deletions packages/bsky/src/api/app/bsky/graph/getFollowers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@ export default function (server: Server, ctx: AppContext) {
presentation,
)
server.app.bsky.graph.getFollowers({
auth: ctx.authOptionalAccessOrRoleVerifier,
auth: ctx.authVerifier.optionalStandardOrRole,
handler: async ({ params, auth }) => {
const db = ctx.db.getReplica()
const actorService = ctx.services.actor(db)
const graphService = ctx.services.graph(db)
const viewer = 'did' in auth.credentials ? auth.credentials.did : null
const canViewTakendownProfile =
auth.credentials.type === 'role' && auth.credentials.triage
const { viewer, canViewTakedowns } = ctx.authVerifier.parseCreds(auth)

const result = await getFollowers(
{ ...params, viewer, canViewTakendownProfile },
{ ...params, viewer, canViewTakedowns },
{ db, actorService, graphService },
)

Expand All @@ -46,10 +44,10 @@ const skeleton = async (
ctx: Context,
): Promise<SkeletonState> => {
const { db, actorService } = ctx
const { limit, cursor, actor, canViewTakendownProfile } = params
const { limit, cursor, actor, canViewTakedowns } = params
const { ref } = db.db.dynamic

const subject = await actorService.getActor(actor, canViewTakendownProfile)
const subject = await actorService.getActor(actor, canViewTakedowns)
if (!subject) {
throw new InvalidRequestError(`Actor not found: ${actor}`)
}
Expand All @@ -58,7 +56,7 @@ const skeleton = async (
.selectFrom('follow')
.where('follow.subjectDid', '=', subject.did)
.innerJoin('actor as creator', 'creator.did', 'follow.creator')
.if(!canViewTakendownProfile, (qb) =>
.if(!canViewTakedowns, (qb) =>
qb.where(notSoftDeletedClause(ref('creator'))),
)
.selectAll('creator')
Expand Down Expand Up @@ -130,7 +128,7 @@ type Context = {

type Params = QueryParams & {
viewer: string | null
canViewTakendownProfile: boolean
canViewTakedowns: boolean
}

type SkeletonState = {
Expand Down
Loading

0 comments on commit f208397

Please sign in to comment.