Skip to content

Commit

Permalink
consolidated code to userId.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexErrant committed Sep 1, 2023
1 parent 7dc352e commit 087c538
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 106 deletions.
12 changes: 0 additions & 12 deletions api-ugc/src/env.ts

This file was deleted.

9 changes: 7 additions & 2 deletions api-ugc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@

import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { type Env, type ApiUgcContext, getUserId } from './util'
import { type Env, type ApiUgcContext } from './util'
import { hstsName, hstsValue, type Base64, parsePublicToken } from 'shared'
import { setKysely, lookupMediaHash, binary16fromBase64URL } from 'shared-edge'
import {
setKysely,
lookupMediaHash,
binary16fromBase64URL,
getUserId,
} from 'shared-edge'
import { appRouter } from './router'
import { fetchRequestHandler } from '@trpc/server/adapters/fetch'
import { createContext } from './trpc'
Expand Down
44 changes: 0 additions & 44 deletions api-ugc/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,4 @@
import { type Context } from 'hono'
import { type JWTVerifyResult, jwtVerify } from 'jose'
import {
type Result,
type UserId,
csrfHeaderName,
toError,
hubSessionCookieName,
toOk,
} from 'shared'
import { getHubSessionSecret } from './env'

export type ApiUgcContext = Context<{
// eslint-disable-next-line @typescript-eslint/naming-convention
Expand All @@ -31,37 +21,3 @@ export type Env = {
hubOrigin: string
hubSessionSecret: string
}

// changes to this should be copied to CB799051-C477-4F6A-9251-AAF63C347F3A
export async function getUserId(
c: ApiUgcContext,
): Promise<Result<UserId, Response>> {
// https://github.com/honojs/hono/pull/884
if (c.req.header(csrfHeaderName) == null) {
return toError(c.text(`Missing '${csrfHeaderName}' header`, 401))
}
const hubSession = c.req.cookie(hubSessionCookieName)
if (hubSession == null) {
return toError(c.text(`Missing '${hubSessionCookieName}' cookie.`, 401))
} else {
let verifyResult: JWTVerifyResult
try {
verifyResult = await jwtVerify(
hubSession,
getHubSessionSecret(c.env.hubSessionSecret),
)
} catch {
return toError(
c.text(
`Failed to verify JWT in '${hubSessionCookieName}' cookie.`,
401,
),
)
}
if (verifyResult.payload.sub == null) {
return toError(c.text("There's no sub claim, ya goof.", 401))
} else {
return toOk(verifyResult.payload.sub as UserId)
}
}
}
12 changes: 0 additions & 12 deletions cwa/src/env.ts

This file was deleted.

3 changes: 2 additions & 1 deletion cwa/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { type Env, getUserId, type MediaHash, type CwaContext } from './util'
import { type Env, type MediaHash, type CwaContext } from './util'
import {
hstsName,
hstsValue,
Expand All @@ -29,6 +29,7 @@ import {
fromBase64,
userOwnsNoteAndHasMedia,
userOwnsTemplateAndHasMedia,
getUserId,
} from 'shared-edge'
import { connect } from '@planetscale/database'
import { buildPrivateToken } from './privateToken'
Expand Down
35 changes: 0 additions & 35 deletions cwa/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
toError,
toOk,
} from 'shared'
import { getHubSessionSecret } from './env'
import { type MediaTokenSecretBase64 } from './privateToken'

// https://gist.github.com/72lions/4528834
Expand Down Expand Up @@ -46,37 +45,3 @@ export type Env = {
peerSyncPublicKey: string
peerSyncPrivateKey: string
}

// changes to this should be copied to CB799051-C477-4F6A-9251-AAF63C347F3A
export async function getUserId(
c: CwaContext,
): Promise<Result<UserId, Response>> {
// https://github.com/honojs/hono/pull/884
if (c.req.header(csrfHeaderName) == null) {
return toError(c.text(`Missing '${csrfHeaderName}' header`, 401))
}
const hubSession = c.req.cookie(hubSessionCookieName)
if (hubSession == null) {
return toError(c.text(`Missing '${hubSessionCookieName}' cookie.`, 401))
} else {
let verifyResult: JWTVerifyResult
try {
verifyResult = await jwtVerify(
hubSession,
getHubSessionSecret(c.env.hubSessionSecret),
)
} catch {
return toError(
c.text(
`Failed to verify JWT in '${hubSessionCookieName}' cookie.`,
401,
),
)
}
if (verifyResult.payload.sub == null) {
return toError(c.text("There's no sub claim, ya goof.", 401))
} else {
return toOk(verifyResult.payload.sub as UserId)
}
}
}
1 change: 1 addition & 0 deletions shared-edge/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from './kysely.js'
export * from './nook.js'
export * from './utility.js'
export * from './user.js'
export * from './userId.js'
56 changes: 56 additions & 0 deletions shared-edge/src/userId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { type JWTVerifyResult, jwtVerify } from 'jose'
import {
type Result,
type UserId,
csrfHeaderName,
toError,
hubSessionCookieName,
toOk,
} from 'shared'
import { type Context } from 'hono'
import { base64ToArray } from './utility'

export async function getUserId<T extends { hubSessionSecret: string }>(
c: Context<{
// eslint-disable-next-line @typescript-eslint/naming-convention
Bindings: T
}>,
): Promise<Result<UserId, Response>> {
// https://github.com/honojs/hono/pull/884
if (c.req.header(csrfHeaderName) == null) {
return toError(c.text(`Missing '${csrfHeaderName}' header`, 401))
}
const hubSession = c.req.cookie(hubSessionCookieName)
if (hubSession == null) {
return toError(c.text(`Missing '${hubSessionCookieName}' cookie.`, 401))
} else {
let verifyResult: JWTVerifyResult
try {
verifyResult = await jwtVerify(
hubSession,
getHubSessionSecret(c.env.hubSessionSecret),
)
} catch {
return toError(
c.text(
`Failed to verify JWT in '${hubSessionCookieName}' cookie.`,
401,
),
)
}
if (verifyResult.payload.sub == null) {
return toError(c.text("There's no sub claim, ya goof.", 401))
} else {
return toOk(verifyResult.payload.sub as UserId)
}
}
}

let hubSessionSecret: null | Uint8Array = null

function getHubSessionSecret(hubSessionSecretString: string): Uint8Array {
if (hubSessionSecret === null) {
hubSessionSecret = base64ToArray(hubSessionSecretString)
}
return hubSessionSecret
}

0 comments on commit 087c538

Please sign in to comment.