File tree 1 file changed +29
-0
lines changed
ndc-app/src/main/kotlin/io/hasura/ndc/app/application
1 file changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,8 @@ package io.hasura.ndc.app.application
3
3
import io.vertx.core.http.HttpServerRequest
4
4
import jakarta.inject.Inject
5
5
import jakarta.ws.rs.container.ContainerRequestContext
6
+ import jakarta.ws.rs.core.HttpHeaders
7
+ import jakarta.ws.rs.core.Response
6
8
import jakarta.ws.rs.core.UriInfo
7
9
import org.jboss.logging.Logger
8
10
import org.jboss.resteasy.reactive.server.ServerRequestFilter
@@ -19,4 +21,31 @@ class Filters {
19
21
logger.debug(b.result())
20
22
}
21
23
}
24
+
25
+ @ServerRequestFilter
26
+ fun tokenFilter (ctx : ContainerRequestContext ): Response ? {
27
+ val secret = System .getenv(" HASURA_SERVICE_TOKEN_SECRET" )
28
+ if (secret.isNullOrEmpty()) {
29
+ logger.warn(" Environment variable HASURA_SERVICE_TOKEN_SECRET not set. Token validation is bypassed." )
30
+ return null
31
+ }
32
+
33
+ val authHeader = ctx.getHeaderString(HttpHeaders .AUTHORIZATION )
34
+ if (authHeader == null || ! authHeader.startsWith(" Bearer " )) {
35
+ logger.error(" Authorization header missing or not in Bearer format" )
36
+ return Response .status(Response .Status .UNAUTHORIZED ).build()
37
+ }
38
+
39
+ val token = authHeader.substringAfter(" Bearer " )
40
+ if (token.isEmpty()) {
41
+ logger.error(" Token is empty" )
42
+ return Response .status(Response .Status .UNAUTHORIZED ).build()
43
+ }
44
+ if (token != secret) {
45
+ logger.error(" Token is invalid" )
46
+ return Response .status(Response .Status .UNAUTHORIZED ).build()
47
+ }
48
+
49
+ return null
50
+ }
22
51
}
You can’t perform that action at this time.
0 commit comments