Skip to content

Commit

Permalink
merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
dholms committed Dec 1, 2023
2 parents 884772a + 691511b commit 21dcf63
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 33 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-swans-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@atproto/syntax': patch
---

prevent unnecessary throw/catch on uri syntax
42 changes: 23 additions & 19 deletions packages/bsky/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,41 @@ import { ServerConfig } from './config'
const BASIC = 'Basic '
const BEARER = 'Bearer '

export const authVerifier =
(idResolver: IdResolver, opts: { aud: string | null }) =>
async (reqCtx: { req: express.Request; res: express.Response }) => {
export const authVerifier = (
idResolver: IdResolver,
opts: { aud: string | null },
) => {
const getSigningKey = async (
did: string,
forceRefresh: boolean,
): Promise<string> => {
const atprotoData = await idResolver.did.resolveAtprotoData(
did,
forceRefresh,
)
return atprotoData.signingKey
}

return async (reqCtx: { req: express.Request; res: express.Response }) => {
const jwtStr = getJwtStrFromReq(reqCtx.req)
if (!jwtStr) {
throw new AuthRequiredError('missing jwt', 'MissingJwt')
}
const payload = await verifyJwt(
jwtStr,
opts.aud,
async (did, forceRefresh) => {
const atprotoData = await idResolver.did.resolveAtprotoData(
did,
forceRefresh,
)
return atprotoData.signingKey
},
)
const payload = await verifyJwt(jwtStr, opts.aud, getSigningKey)
return { credentials: { did: payload.iss }, artifacts: { aud: opts.aud } }
}
}

export const authOptionalVerifier = (
idResolver: IdResolver,
opts: { aud: string | null },
) => {
const verify = authVerifier(idResolver, opts)
const verifyAccess = authVerifier(idResolver, opts)
return async (reqCtx: { req: express.Request; res: express.Response }) => {
if (!reqCtx.req.headers.authorization) {
return { credentials: { did: null } }
}
return verify(reqCtx)
return verifyAccess(reqCtx)
}
}

Expand Down Expand Up @@ -131,9 +135,9 @@ export const buildBasicAuth = (username: string, password: string): string => {
}

export const getJwtStrFromReq = (req: express.Request): string | null => {
const { authorization = '' } = req.headers
if (!authorization.startsWith(BEARER)) {
const { authorization } = req.headers
if (!authorization?.startsWith(BEARER)) {
return null
}
return authorization.replace(BEARER, '').trim()
return authorization.slice(BEARER.length).trim()
}
4 changes: 2 additions & 2 deletions packages/bsky/src/util/debug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const debugCatch = <Func extends (...args: any[]) => any>(fn: Func) => {
return async (...args) => {
return async (...args: Parameters<Func>) => {
try {
return await fn(...args)
return (await fn(...args)) as Awaited<ReturnType<Func>>
} catch (err) {
console.error(err)
throw err
Expand Down
1 change: 1 addition & 0 deletions packages/pds/src/read-after-write/viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ export class LocalViewer {
const images = embed.images.map((img) => ({
thumb: this.getImageUrl('feed_thumbnail', img.image.ref.toString()),
fullsize: this.getImageUrl('feed_fullsize', img.image.ref.toString()),
aspectRatio: img.aspectRatio,
alt: img.alt,
}))
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/pds/src/util/debug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export const debugCatch = <Func extends (...args: any[]) => any>(fn: Func) => {
return async (...args) => {
return async (...args: Parameters<Func>) => {
try {
return await fn(...args)
return (await fn(...args)) as Awaited<ReturnType<Func>>
} catch (err) {
console.error(err)
throw err
Expand Down
3 changes: 3 additions & 0 deletions packages/pds/tests/proxied/read-after-write.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ describe('proxy read after write', () => {
images: [
{
image: img.image,
aspectRatio: { height: 2, width: 1 },
alt: 'alt text',
},
],
Expand Down Expand Up @@ -190,6 +191,8 @@ describe('proxy read after write', () => {
img.image.ref.toString(),
),
)
expect(imgs.images[0].aspectRatio).toEqual({ height: 2, width: 1 })
expect(imgs.images[0].alt).toBe('alt text')
expect(replies[0].replies?.length).toBe(1)
// @ts-ignore
expect(replies[0].replies[0].post.uri).toEqual(replyRes2.uri)
Expand Down
10 changes: 5 additions & 5 deletions packages/syntax/src/aturi_validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ export const ensureValidAtUri = (uri: string) => {
}

try {
ensureValidHandle(parts[2])
} catch {
try {
if (parts[2].startsWith('did:')) {
ensureValidDid(parts[2])
} catch {
throw new Error('ATURI authority must be a valid handle or DID')
} else {
ensureValidHandle(parts[2])
}
} catch {
throw new Error('ATURI authority must be a valid handle or DID')
}

if (parts.length >= 4) {
Expand Down
8 changes: 3 additions & 5 deletions packages/xrpc-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ export class Server {
}

// handle rate limits
if (consumeRateLimit) {
const result = await consumeRateLimit(reqCtx)
if (result instanceof RateLimitExceededError) {
return next(result)
}
const result = await consumeRateLimit(reqCtx)
if (result instanceof RateLimitExceededError) {
return next(result)
}

// run the handler
Expand Down

0 comments on commit 21dcf63

Please sign in to comment.