Add optional auth debug response header
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
All checks were successful
build-and-deploy / build-deploy (push) Successful in 27s
This commit is contained in:
@@ -30,9 +30,17 @@ class FirebaseAuthFilter(
|
|||||||
response: HttpServletResponse,
|
response: HttpServletResponse,
|
||||||
filterChain: FilterChain
|
filterChain: FilterChain
|
||||||
) {
|
) {
|
||||||
|
val debug = request.getHeader("X-Debug-Auth") == "1"
|
||||||
|
fun setDebug(value: String) {
|
||||||
|
if (debug) {
|
||||||
|
response.setHeader("X-Auth-Debug", value)
|
||||||
|
logger.info("Auth debug: {} {} -> {}", request.method, request.requestURI, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
val header = request.getHeader(HttpHeaders.AUTHORIZATION)
|
val header = request.getHeader(HttpHeaders.AUTHORIZATION)
|
||||||
if (header.isNullOrBlank() || !header.startsWith("Bearer ")) {
|
if (header.isNullOrBlank() || !header.startsWith("Bearer ")) {
|
||||||
logger.debug("Auth missing/invalid header for {}", request.requestURI)
|
logger.debug("Auth missing/invalid header for {}", request.requestURI)
|
||||||
|
setDebug(if (header.isNullOrBlank()) "missing_authorization" else "invalid_authorization")
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Missing Authorization token")
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Missing Authorization token")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -41,7 +49,10 @@ class FirebaseAuthFilter(
|
|||||||
val decoded = FirebaseAuth.getInstance().verifyIdToken(token)
|
val decoded = FirebaseAuth.getInstance().verifyIdToken(token)
|
||||||
val firebaseUid = decoded.uid
|
val firebaseUid = decoded.uid
|
||||||
val user = appUserRepo.findByFirebaseUid(firebaseUid)
|
val user = appUserRepo.findByFirebaseUid(firebaseUid)
|
||||||
?: throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found")
|
?: run {
|
||||||
|
setDebug("user_not_found")
|
||||||
|
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "User not found")
|
||||||
|
}
|
||||||
logger.debug("Auth verified uid={}, userId={}", firebaseUid, user.id)
|
logger.debug("Auth verified uid={}, userId={}", firebaseUid, user.id)
|
||||||
|
|
||||||
val principal = MyPrincipal(
|
val principal = MyPrincipal(
|
||||||
@@ -50,9 +61,11 @@ class FirebaseAuthFilter(
|
|||||||
)
|
)
|
||||||
val auth = UsernamePasswordAuthenticationToken(principal, token, emptyList())
|
val auth = UsernamePasswordAuthenticationToken(principal, token, emptyList())
|
||||||
SecurityContextHolder.getContext().authentication = auth
|
SecurityContextHolder.getContext().authentication = auth
|
||||||
|
setDebug("ok:userId=${principal.userId},superAdmin=${user.superAdmin}")
|
||||||
filterChain.doFilter(request, response)
|
filterChain.doFilter(request, response)
|
||||||
} catch (ex: Exception) {
|
} catch (ex: Exception) {
|
||||||
logger.debug("Auth failed for {}: {}", request.requestURI, ex.message)
|
logger.debug("Auth failed for {}: {}", request.requestURI, ex.message)
|
||||||
|
setDebug("verify_failed")
|
||||||
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid token")
|
response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid token")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user