generated from ministryofjustice/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3912c25
commit de750ab
Showing
4 changed files
with
30 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...rc/main/kotlin/uk/gov/justice/digital/hmpps/config/telemetry/ClientTrackingInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package uk.gov.justice.digital.hmpps.config.telemetry | ||
|
||
import com.nimbusds.jwt.SignedJWT | ||
import io.opentelemetry.api.trace.Span | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.context.annotation.Configuration | ||
import org.springframework.http.HttpHeaders | ||
import org.springframework.web.servlet.HandlerInterceptor | ||
|
||
@Configuration | ||
class ClientTrackingInterceptor : HandlerInterceptor { | ||
override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, handler: Any): Boolean { | ||
val token = request.getHeader(HttpHeaders.AUTHORIZATION) | ||
if (token.startsWith(BEARER)) { | ||
try { | ||
val jwtBody = SignedJWT.parse(token.replace(BEARER, "")).jwtClaimsSet | ||
Span.current().setAttribute("clientId", jwtBody.getClaim("client_id").toString()) | ||
} catch (ignored: Exception) { | ||
// Do nothing - don't create client id span | ||
} | ||
} | ||
return true | ||
} | ||
|
||
companion object { | ||
private const val BEARER = "Bearer " | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters