|
1 | | -import { describePrincipalAuth, type WorkflowExecutionPrincipal } from '@sim/auth/principal' |
2 | | -import { createLogger, setRequestAuth } from '@sim/logger' |
| 1 | +import type { WorkflowExecutionPrincipal } from '@sim/auth/principal' |
| 2 | +import { createLogger } from '@sim/logger' |
3 | 3 | import type { NextRequest } from 'next/server' |
4 | 4 | import { API_KEY_HEADER, BEARER_PREFIX } from '@/lib/api/server/credential-headers' |
5 | 5 | import { authenticateApiKeyFromHeader, updateApiKeyLastUsed } from '@/lib/api-key/service' |
@@ -83,7 +83,7 @@ function resolveUserFromJwt( |
83 | 83 | * @param options - Optional configuration |
84 | 84 | * @param options.requireWorkflowId - Whether workflowId/userId is required (default: true) |
85 | 85 | */ |
86 | | -async function resolveInternalAuth( |
| 86 | +export async function checkInternalAuth( |
87 | 87 | request: NextRequest, |
88 | 88 | options: { requireWorkflowId?: boolean } = {} |
89 | 89 | ): Promise<AuthResult> { |
@@ -131,7 +131,7 @@ async function resolveInternalAuth( |
131 | 131 | * @param options - Optional configuration |
132 | 132 | * @param options.requireWorkflowId - Whether workflowId/userId is required for JWT (default: true) |
133 | 133 | */ |
134 | | -async function resolveSessionOrInternalAuth( |
| 134 | +export async function checkSessionOrInternalAuth( |
135 | 135 | request: NextRequest, |
136 | 136 | options: { requireWorkflowId?: boolean } = {} |
137 | 137 | ): Promise<AuthResult> { |
@@ -195,7 +195,7 @@ async function resolveSessionOrInternalAuth( |
195 | 195 | * |
196 | 196 | * For internal JWT calls, requires workflowId to determine user context |
197 | 197 | */ |
198 | | -async function resolveHybridAuth( |
| 198 | +export async function checkHybridAuth( |
199 | 199 | request: NextRequest, |
200 | 200 | options: { requireWorkflowId?: boolean } = {} |
201 | 201 | ): Promise<AuthResult> { |
@@ -277,36 +277,3 @@ async function resolveHybridAuth( |
277 | 277 | } |
278 | 278 | } |
279 | 279 | } |
280 | | - |
281 | | -type AuthCheck = ( |
282 | | - request: NextRequest, |
283 | | - options?: { requireWorkflowId?: boolean } |
284 | | -) => Promise<AuthResult> |
285 | | - |
286 | | -/** |
287 | | - * Records how a request authenticated on the request context, so the logs and |
288 | | - * analytics of a route that authenticates through these helpers rather than a |
289 | | - * route builder carry the same `auth` attribution. A principal describes |
290 | | - * itself; an internal JWT that produced none is recorded by its auth type. |
291 | | - */ |
292 | | -function recordingAuth(resolve: AuthCheck): AuthCheck { |
293 | | - return async (request, options) => { |
294 | | - const result = await resolve(request, options) |
295 | | - if (!result.success) return result |
296 | | - if (result.principal) { |
297 | | - setRequestAuth(describePrincipalAuth(result.principal)) |
298 | | - } else if (result.authType) { |
299 | | - setRequestAuth({ kind: result.authType }) |
300 | | - } |
301 | | - return result |
302 | | - } |
303 | | -} |
304 | | - |
305 | | -/** Internal JWT authentication only. See {@link resolveInternalAuth}. */ |
306 | | -export const checkInternalAuth = recordingAuth(resolveInternalAuth) |
307 | | - |
308 | | -/** Session or internal JWT authentication, never an API key. See {@link resolveSessionOrInternalAuth}. */ |
309 | | -export const checkSessionOrInternalAuth = recordingAuth(resolveSessionOrInternalAuth) |
310 | | - |
311 | | -/** Any of the three supported credentials. See {@link resolveHybridAuth}. */ |
312 | | -export const checkHybridAuth = recordingAuth(resolveHybridAuth) |
0 commit comments