Imagine you want to enter a secret club, but the club has a special rule:
You need a special pass with your name, age, and the club's name on it.
Your friend, who is a member of the club, can create that pass for you.
They use a secret code to lock the pass so that no one can change the information on it.
This process is called signing.
They take the information on the pass (your name, age, club name) and use the secret code to create a unique signature.
They then attach the signature to the pass.
When you go to the club, the security has the same secret code that your friend used to create the pass.
The security checks your pass by taking the information on it and using the secret code to create a new signature.
They then compare this new signature to the one attached to your pass.
✅ If the new signature matches the one on your pass, it means the pass hasn't been tampered with, and the guard lets you in.
🚫 If the signatures don't match, the guard knows that the pass is fake or has been changed, and you won't be allowed to enter.
𝗡𝗼𝘄, 𝘁𝗵𝗲 𝗮𝗰𝘁𝘂𝗮𝗹 𝗱𝗲𝗳𝗶𝗻𝗶𝘁𝗶𝗼𝗻:
JSON Web Tokens (JWTs) are a compact and secure way to transmit information between two parties.
They are often used for authentication and authorization purposes in modern APIs.
The information in a JWT is stored as a JSON object, which is then encoded and signed with a secret key to ensure its integrity.
A user logs in to a website with their username and password.
The server checks the credentials, and if they are valid, it creates a JWT with the user's information (like their user ID) and signs it using a secret key.
The server sends the JWT to the user's browser, which stores it (usually in a cookie or local storage).
When the user requests protected resources from the server (like their profile page), the browser sends the JWT along with the request.
The server verifies the JWT using the secret key.
If the JWT is valid, the server allows access to the requested resources.
JWTs are significant in modern APIs because they provide a stateless, scalable, and secure way to authenticate and authorize users.
They enable servers to offload session management tasks and reduce the overhead associated with traditional session-based authentication methods.