Implementing the /authorize/jwt/verify-token API 

Similar to the issueToken() function, we create a function called verifyToken() with the @Path annotation in the AuthenticationController class. This maps the URI with /authorize/jwt/verify-token to the verifyToken() function:

@POST
@Path("/jwt/verify-token")
@Produces(MediaType.APPLICATION_JSON)
fun verifyToken(@Context httpServletRequest: HttpServletRequest,
@Context httpServletResponse: HttpServletResponse): Response {

var header: String = httpServletRequest.getHeader("Authorization")
var token: String = extractJwtToken(header);

val expiresIn:Long = verifyJwt(token)
return Response.status(200).entity(JSONObject("{"token":"active"," +
""expiresInSeconds":$expiresIn }")).build()
}

The verifyToken() function takes servlet request and response objects. We extract the Authorization header from the request and verify the validity of the token using the verifyJwt() function. verifyJwt() will have the logic to check the token's validity.

Let's invoke the /authorize/jwt/verify-token API using cURL:

curl -X POST 
http://localhost:8080/authentication-service/authorize/jwt/verify-token
-H 'Accept: application/json'
-H 'Content-Type: application/json'
-H 'Authorization: Bearer {{jwt}}'

This gives the following output:

We implemented two APIs, one for issuing a JWT token, and another to verify.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset