( input: QueueMessageInput, ctx: QueueMessageContext )
| 139 | * authority; the chat's authorization never stands in for the workspace's. |
| 140 | */ |
| 141 | export async function preflightRuntimeAuthorizationRecovery( |
| 142 | cloudAgentSessionId: string, |
| 143 | ctx: QueueMessageContext |
| 144 | ): Promise<void> { |
| 145 | const workspaceSessionId = await preflightChatRuntimeAuthorization(cloudAgentSessionId, ctx); |
| 146 | await renewWorkspaceAuthorityForUserSend({ |
| 147 | env: ctx.env, |
| 148 | userId: ctx.userId, |
| 149 | authToken: ctx.authToken, |
| 150 | workspaceSessionId, |
| 151 | }); |
| 152 | } |
| 153 | |
| 154 | /** Returns the chat's workspace when the send may proceed on its authorization. */ |
| 155 | async function preflightChatRuntimeAuthorization( |
| 156 | cloudAgentSessionId: string, |
| 157 | ctx: QueueMessageContext |
| 158 | ): Promise<string | undefined> { |
| 159 | if (!ctx.authToken) return; |
| 160 | const claims = jwt.decode(ctx.authToken); |
| 161 | if ( |
| 162 | !claims || |
| 163 | typeof claims !== 'object' || |
| 164 | !('aud' in claims || 'tokenPurpose' in claims || 'credentialExchange' in claims) |
| 165 | ) { |
| 166 | return; |
| 167 | } |
| 168 | const cloudAgentScoped = 'cloudAgentAuthority' in claims; |
| 169 | const sessionId = cloudAgentSessionId as SessionId; |
| 170 | const state = await withDORetry( |
| 171 | () => getSandboxSessionStub(ctx.env, ctx.userId, sessionId), |
| 172 | target => target.getRuntimeAuthorizationStatus(), |
| 173 | 'getRuntimeAuthorizationStatus' |
| 174 | ); |
| 175 | // `legacy` is a credential-compatibility status for a stored authorization, |
| 176 | // not the retired `agent_*` session plane. |
| 177 | if (state.state === 'legacy' || state.state === 'active') return state.workspaceSessionId; |
| 178 | if (state.state === 'revoked') { |
| 179 | logRuntimeAuthorizationDiagnostic( |
| 180 | cloudAgentSessionId, |
| 181 | 'preflight', |
| 182 | 'stored_authorization_revoked' |
| 183 | ); |
| 184 | throw new TRPCError({ code: 'FORBIDDEN', message: 'Runtime authorization denied' }); |
no test coverage detected