In this post we’re going to figure out how to get a browser cookie using Selenium and decode a JWT token in a cookie.
Note: There is a lot of sources on the Internet that say why using JWT tokens in session management is a bad idea but since JWT tokens are still used by many sites we should be able to exctract, decode and validate them.
Get browser cookie with Selenium
Let’s say we need to get a cookie named “SESSION_ID”. We can do it easily with Selenium
Here encodedToken is our encoded JWT token.
Decode JWT token
Now let’s decode the JWT token and get the payload.
Usually JWT tokens look like this
The payload is the part between .’s. We need to extract it and decode
We will get
You can check yourself if you decoded JWT token correctly - jwt.io
Decode JWT token - complete example
Let’s implement it a more elegant way.
DecodedJWT
DecodedJWT allows us to use a JWT token as an object with access to all data as its fields. We just need to call decode(..) to decode an encoded JWT token and instantiate it as an object.