kurt_steiner
01/16/2024, 11:14 AMcom.auth0.jwt.exceptions.TokenExpiredException: The Token has expired on 2024-01-16T08:48:32Z.
I want to know how can I return a json response instead of throwing an errorThomas Urbanitsch
01/16/2024, 11:18 AMkurt_steiner
01/16/2024, 11:19 AMkurt_steiner
01/16/2024, 11:25 AM2024-01-16 19:22:57.999 [DefaultDispatcher-worker-5] TRACE io.ktor.server.auth.Authentication - Trying to authenticate / with auth-jwt
2024-01-16 19:22:58.050 [DefaultDispatcher-worker-5] TRACE io.ktor.auth.jwt - Token verification failed
com.auth0.jwt.exceptions.TokenExpiredException: The Token has expired on 2024-01-16T08:48:32Z.
at com.auth0.jwt.JWTVerifier$BaseVerification.assertValidInstantClaim(JWTVerifier.java:346)
at com.auth0.jwt.JWTVerifier$BaseVerification.lambda$addMandatoryClaimChecks$17(JWTVerifier.java:308)
and this is the code
install(StatusPages) {
exception(TokenExpiredException::class) { call, _ ->
call.respond(Response.Err("token expired"))
}
}
where is the error of my code ?Thomas Urbanitsch
01/16/2024, 11:45 AMkurt_steiner
01/16/2024, 11:52 AMThomas Urbanitsch
01/16/2024, 12:33 PMkurt_steiner
01/16/2024, 12:35 PMkurt_steiner
01/16/2024, 12:39 PMauthenticate("auth-jwt") {
get("/") {
val principal = call.principal<UserIdPrincipal>()
if (principal == null) {
call.respond(HttpStatusCode.Unauthorized, Response.Err("not login"))
} else {
val username = principal.name
call.respond(Response.Ok("get ok", "you are $username"))
}
}
}
but I can't catch the error by StatusPagesThomas Urbanitsch
01/16/2024, 12:41 PMkurt_steiner
01/16/2024, 12:44 PMinstall(StatusPages) {
exception<Throwable> { call, cause ->
call.respond(Response.Err("Fuck"))
}
status(HttpStatusCode.NotFound) { call, status ->
call.respondText(text = "404: Page Not Found", status = status)
}
exception<TokenExpiredException> { call, cause ->
call.respond(HttpStatusCode.Unauthorized, Response.Err(cause.message ?: "Fuck"))
}
}
but still not workkurt_steiner
01/16/2024, 12:52 PMThomas Urbanitsch
01/16/2024, 12:52 PMkurt_steiner
01/16/2024, 12:53 PMfun Application.configureException() {
install(StatusPages) {
exception<Throwable> { call, cause ->
call.respond(Response.Err("Fuck"))
}
status(HttpStatusCode.NotFound) { call, status ->
call.respondText(text = "404: Page Not Found", status = status)
}
exception<TokenExpiredException> { call, cause ->
call.respond(HttpStatusCode.Unauthorized, Response.Err(cause.message ?: "Fuck"))
}
}
routing {
get("/error/") {
call.respondText("Hello, world!")
}
get("/error/internal-error") {
throw Exception("Internal Server Error")
}
get("/error/authorization-error") {
throw Exception("Forbidden Error")
}
get("/error/authentication-error") {
call.respond(HttpStatusCode.Unauthorized)
}
get("/error/payment-error") {
call.respond(HttpStatusCode.PaymentRequired)
}
}
}
Thomas Urbanitsch
01/16/2024, 12:54 PMkurt_steiner
01/16/2024, 12:55 PMThomas Urbanitsch
01/16/2024, 2:44 PMstatus(HttpStatusCode.Unauthorized) { call, status ->
call.respondText(text = "401: Unauthorized!", status = status)
}
If you need more control i think using challenge in the authentication and throwing the exceptions yourself would be an option, as seen herekurt_steiner
01/16/2024, 2:51 PMkurt_steiner
01/16/2024, 2:54 PMkurt_steiner
01/16/2024, 3:07 PMThomas Urbanitsch
01/16/2024, 3:08 PMkurt_steiner
01/16/2024, 3:09 PMThomas Urbanitsch
01/16/2024, 3:13 PMkurt_steiner
01/16/2024, 3:32 PM