Skip to content

Commit 4d73312

Browse files
refactor: create shared @codebuff/internal package for authentication utilities
- Create new @codebuff/internal package with centralized auth functions - Move reusable authentication logic from backend and web packages - Consolidate admin email list into single source of truth - Update all imports from @codebuff/integrations to @codebuff/internal - Fix build configuration and project references - Improve error handling with structured AuthResult interface 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent e381236 commit 4d73312

File tree

27 files changed

+268
-101
lines changed

27 files changed

+268
-101
lines changed

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
"@ai-sdk/openai": "1.3.22",
1818
"@anthropic-ai/sdk": "^0.39.0",
1919
"@codebuff/billing": "workspace:*",
20+
"@codebuff/internal": "workspace:*",
2021
"@google-cloud/vertexai": "1.10.0",
2122
"@google/generative-ai": "0.24.1",
2223
"@t3-oss/env-core": "0.11.1",
23-
"@types/cors": "^2.8.17",
2424
"ai": "4.3.16",
2525
"common": "workspace:*",
2626
"cors": "^2.8.5",

backend/src/util/check-auth.ts

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ import { ServerAction } from 'common/actions'
77
import { logger } from '@/util/logger'
88
import { triggerMonthlyResetAndGrant } from '@codebuff/billing'
99

10-
// List of admin user emails
11-
const ADMIN_USER_EMAILS = [
12-
'venkateshrameshkumar+1@gmail.com',
13-
'brandonchenjiacheng@gmail.com',
14-
'jahooma@gmail.com',
15-
'charleslien97@gmail.com',
16-
]
10+
import * as internal from '@codebuff/internal'
1711

1812
export const checkAuth = async ({
1913
fingerprintId,
@@ -24,44 +18,31 @@ export const checkAuth = async ({
2418
authToken?: string
2519
clientSessionId: string
2620
}): Promise<void | ServerAction> => {
27-
if (!authToken) {
28-
if (!fingerprintId) {
29-
logger.error(
30-
{ clientSessionId },
31-
'Auth token and fingerprint ID are missing'
32-
)
33-
return {
34-
type: 'action-error',
35-
message: 'Auth token and fingerprint ID are missing',
36-
}
37-
}
38-
return
39-
}
40-
41-
const user = await db
42-
.select({
43-
id: schema.user.id,
44-
email: schema.user.email,
45-
discord_id: schema.user.discord_id,
46-
})
47-
.from(schema.user)
48-
.innerJoin(schema.session, eq(schema.user.id, schema.session.userId))
49-
.where(eq(schema.session.sessionToken, authToken))
50-
.then((users) => {
51-
if (users.length === 1) {
52-
return users[0]
53-
}
54-
return undefined
55-
})
21+
// Use shared auth check functionality
22+
const authResult = await internal.utils.checkAuthToken({
23+
fingerprintId,
24+
authToken,
25+
})
5626

57-
if (!user) {
58-
logger.warn({ clientSessionId }, 'Invalid auth token')
27+
if (!authResult.success) {
28+
logger.error(
29+
{ clientSessionId, error: authResult.error },
30+
authResult.error?.message || 'Authentication failed'
31+
)
5932
return {
6033
type: 'action-error',
61-
message: 'Invalid auth token',
34+
message: authResult.error?.message || 'Authentication failed',
6235
}
6336
}
6437

38+
if (authResult.user) {
39+
// Log successful authentication if we have a user
40+
logger.debug(
41+
{ clientSessionId, userId: authResult.user.id },
42+
'Authentication successful'
43+
)
44+
}
45+
6546
return
6647
}
6748

@@ -98,7 +79,7 @@ export const checkAdmin = async (
9879
return res.status(401).json({ error: errorMessage })
9980
}
10081

101-
// Get the user email associated with this session token
82+
// Get the user ID associated with this session token
10283
const user = await db
10384
.select({
10485
id: schema.user.id,
@@ -109,17 +90,22 @@ export const checkAdmin = async (
10990
.where(eq(schema.session.sessionToken, authToken))
11091
.then((users) => users[0])
11192

112-
// Check if user has admin access
113-
if (!user?.email || !ADMIN_USER_EMAILS.includes(user.email)) {
93+
if (!user) {
94+
return res.status(401).json({ error: 'Invalid session' })
95+
}
96+
97+
// Check if user has admin access using shared utility
98+
const adminUser = await internal.utils.checkUserIsCodebuffAdmin(user.id)
99+
if (!adminUser) {
114100
logger.warn(
115-
{ userId: user?.id, email: user?.email, clientSessionId },
101+
{ userId: user.id, email: user.email, clientSessionId },
116102
'Unauthorized access attempt to admin endpoint'
117103
)
118104
return res.status(403).json({ error: 'Forbidden' })
119105
}
120106

121107
// Store user info in request for handlers to use if needed
122-
// req.user = user // TODO: ensure type check passes
108+
// req.user = adminUser // TODO: ensure type check passes
123109

124110
// Auth passed and user is admin, proceed to next middleware
125111
next()

backend/tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"common/*": ["./src/common/*", "../common/src/*"],
2020
"@/*": ["./src/*"],
2121
"@codebuff/billing": ["../packages/billing/src"],
22-
"@codebuff/bigquery": ["../packages/bigquery/src"]
22+
"@codebuff/bigquery": ["../packages/bigquery/src"],
23+
"@codebuff/internal": ["../packages/internal/src"]
2324
},
2425
"typeRoots": [
2526
"./node_modules/@types",
@@ -33,7 +34,8 @@
3334
"references": [
3435
{ "path": "../common" },
3536
{ "path": "../packages/billing" },
36-
{ "path": "../packages/bigquery" }
37+
{ "path": "../packages/bigquery" },
38+
{ "path": "../packages/internal" }
3739
],
3840
"ts-node": {
3941
"require": ["tsconfig-paths/register"]

bun.lock

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

evals/git-evals/email-eval-results.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { sendBasicEmail } from '../../packages/integrations/src'
1+
import { sendBasicEmail } from '../../packages/internal/src/loops'
22
import { FullEvalLog } from './types'
33
import { PostEvalAnalysis } from './post-eval-analysis'
44

evals/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@
1313
}
1414
},
1515
"tags": [],
16-
"implicitDependencies": ["@codebuff/integrations", "codebuff"]
16+
"implicitDependencies": ["@codebuff/internal", "codebuff"]
1717
}

evals/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
"common/*": ["../common/src/*"],
88
"backend/*": ["../backend/src/*"],
99
"npm-app/*": ["../npm-app/src/*"],
10-
"@codebuff/integrations/*": ["../packages/integrations/src/*"]
10+
"@codebuff/internal/*": ["../packages/internal/src/*"]
1111
},
1212
"typeRoots": ["./node_modules/@types", "../node_modules/@types"]
1313
},
1414
"references": [
1515
{ "path": "../common" },
1616
{ "path": "../backend" },
1717
{ "path": "../npm-app" },
18-
{ "path": "../packages/integrations" }
18+
{ "path": "../packages/internal" }
1919
],
2020
"exclude": ["test-repos"],
2121
"include": ["**/*.ts"]

packages/integrations/src/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@codebuff/integrations",
2+
"name": "@codebuff/internal",
33
"version": "1.0.0",
44
"license": "UNLICENSED",
55
"main": "dist/index.js",
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"common": "workspace:*",
14+
"drizzle-orm": "*",
1415
"loops": "^5.0.1"
1516
},
1617
"devDependencies": {

packages/internal/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Export internal utilities
2+
export * as utils from './utils/auth';
3+
4+
// Export loops functionality
5+
export * from './loops';

0 commit comments

Comments
 (0)