1- import type { WorkflowExecutionDelegatedPrincipal } from '@sim/auth/principal'
1+ import {
2+ type BoundWorkflowExecutionDelegatedPrincipal ,
3+ requirePrincipalSubjectUserId ,
4+ } from '@sim/auth/principal'
25import { createLogger } from '@sim/logger'
36import { getErrorMessage } from '@sim/utils/errors'
47import { type NextRequest , NextResponse } from 'next/server'
@@ -53,16 +56,16 @@ const windchillSessionOrExecutorAuth = createInternalSessionOrExecutorAuth({
5356
5457async function authenticateWindchillExecutor (
5558 request : NextRequest
56- ) : Promise < WorkflowExecutionDelegatedPrincipal > {
59+ ) : Promise < BoundWorkflowExecutionDelegatedPrincipal > {
5760 const principal = await windchillSessionOrExecutorAuth . authenticate ( request , { } )
5861 if (
5962 principal . kind !== 'delegated' ||
6063 principal . serviceId !== 'executor' ||
61- ! ( 'delegationContext' in principal )
64+ ! principal . delegationContext
6265 ) {
6366 throw new InternalUnauthenticatedError ( 'Authentication required' )
6467 }
65- return principal
68+ return { ... principal , delegationContext : principal . delegationContext }
6669}
6770
6871type WindchillRouteOutput = Extract < WindchillOperationResponse , { success : true } > [ 'output' ]
@@ -485,7 +488,7 @@ async function storeDownloadedFile({
485488 fileName,
486489 contentType,
487490} : {
488- principal : WorkflowExecutionDelegatedPrincipal
491+ principal : BoundWorkflowExecutionDelegatedPrincipal
489492 buffer : Buffer
490493 fileName : string
491494 contentType : string
@@ -501,14 +504,14 @@ async function storeDownloadedFile({
501504 buffer ,
502505 fileName ,
503506 contentType ,
504- principal . subjectUserId
507+ requirePrincipalSubjectUserId ( principal )
505508 )
506509 }
507510 return uploadCopilotFile ( {
508511 buffer,
509512 fileName,
510513 contentType,
511- userId : principal . subjectUserId ,
514+ userId : requirePrincipalSubjectUserId ( principal ) ,
512515 } )
513516}
514517
@@ -518,7 +521,7 @@ async function executeDownload(
518521 | { operation : 'windchill_download_primary_content' }
519522 | { operation : 'windchill_download_attachment' }
520523 > ,
521- principal : WorkflowExecutionDelegatedPrincipal ,
524+ principal : BoundWorkflowExecutionDelegatedPrincipal ,
522525 signal : AbortSignal
523526) : Promise < WindchillRouteOutput > {
524527 const documentUrl = windchillDocumentUrl ( body . baseUrl , body . documentOid )
@@ -562,7 +565,7 @@ async function executeDownload(
562565export const POST = withRouteHandler (
563566 async ( request : NextRequest ) => {
564567 const requestId = generateRequestId ( )
565- let principal : WorkflowExecutionDelegatedPrincipal
568+ let principal : BoundWorkflowExecutionDelegatedPrincipal
566569 try {
567570 principal = await authenticateWindchillExecutor ( request )
568571 } catch ( error ) {
@@ -603,7 +606,11 @@ export const POST = withRouteHandler(
603606 body . operation === 'windchill_upload_primary_content'
604607 ? [ body . primaryFile ]
605608 : body . attachmentFiles
606- const files = await loadUploadFiles ( inputs , principal . subjectUserId , requestId )
609+ const files = await loadUploadFiles (
610+ inputs ,
611+ requirePrincipalSubjectUserId ( principal ) ,
612+ requestId
613+ )
607614 if ( files instanceof NextResponse ) return files
608615 const uploadedFileNames = await uploadWindchillContent ( {
609616 params : body ,
0 commit comments