@@ -41,6 +41,7 @@ import { APITeamsService as TeamsServiceAPI } from "./teams";
41
41
import { APIUserService as UserServiceAPI } from "./user" ;
42
42
import { WorkspaceServiceAPI } from "./workspace-service-api" ;
43
43
import { AuthProviderServiceAPI } from "./auth-provider-service-api" ;
44
+ import { Unauthenticated } from "./unauthenticated" ;
44
45
45
46
decorate ( injectable ( ) , PublicAPIConverter ) ;
46
47
@@ -214,9 +215,21 @@ export class API {
214
215
215
216
const apply = async < T > ( ) : Promise < T > => {
216
217
const subjectId = await self . verify ( context ) ;
217
- await rateLimit ( subjectId ) ;
218
- context . user = await self . ensureFgaMigration ( subjectId ) ;
218
+ const isAuthenticated = ! ! subjectId ;
219
+ const requiresAuthentication = ! Unauthenticated . get ( target , prop ) ;
219
220
221
+ if ( ! isAuthenticated && requiresAuthentication ) {
222
+ throw new ConnectError ( "unauthenticated" , Code . Unauthenticated ) ;
223
+ }
224
+
225
+ if ( isAuthenticated ) {
226
+ await rateLimit ( subjectId ) ;
227
+ context . user = await self . ensureFgaMigration ( subjectId ) ;
228
+ }
229
+
230
+ // TODO(at) if unauthenticated, we still need to apply enforece a rate limit
231
+
232
+ // actually call the RPC handler
220
233
return Reflect . apply ( target [ prop as any ] , target , args ) ;
221
234
} ;
222
235
if ( grpc_type === "unary" || grpc_type === "client_stream" ) {
@@ -250,14 +263,16 @@ export class API {
250
263
} ;
251
264
}
252
265
253
- private async verify ( context : HandlerContext ) : Promise < string > {
266
+ private async verify ( context : HandlerContext ) : Promise < string | undefined > {
254
267
const cookieHeader = ( context . requestHeader . get ( "cookie" ) || "" ) as string ;
255
- const claims = await this . sessionHandler . verifyJWTCookie ( cookieHeader ) ;
256
- const subjectId = claims ?. sub ;
257
- if ( ! subjectId ) {
258
- throw new ConnectError ( "unauthenticated" , Code . Unauthenticated ) ;
268
+ try {
269
+ const claims = await this . sessionHandler . verifyJWTCookie ( cookieHeader ) ;
270
+ const subjectId = claims ?. sub ;
271
+ return subjectId ;
272
+ } catch ( error ) {
273
+ log . warn ( "Failed to authenticate user with JWT Session" , error ) ;
274
+ return undefined ;
259
275
}
260
- return subjectId ;
261
276
}
262
277
263
278
private async ensureFgaMigration ( userId : string ) : Promise < User > {
0 commit comments