Skip to content

Commit 97f868d

Browse files
committed
Implement optional Bearer token auth w/ HASURA_SERVICE_TOKEN_SECRET env
1 parent 2ada8fb commit 97f868d

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

ndc-app/src/main/kotlin/io/hasura/ndc/app/application/Filters.kt

+29
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package io.hasura.ndc.app.application
33
import io.vertx.core.http.HttpServerRequest
44
import jakarta.inject.Inject
55
import jakarta.ws.rs.container.ContainerRequestContext
6+
import jakarta.ws.rs.core.HttpHeaders
7+
import jakarta.ws.rs.core.Response
68
import jakarta.ws.rs.core.UriInfo
79
import org.jboss.logging.Logger
810
import org.jboss.resteasy.reactive.server.ServerRequestFilter
@@ -19,4 +21,31 @@ class Filters {
1921
logger.debug(b.result())
2022
}
2123
}
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+
}
2251
}

0 commit comments

Comments
 (0)