No title
Managing user sessions is a critical aspect of building secure web applications. In this guide, we will explore how to register and validate user sessions using JSON Web Tokens (JWT).
sequenceDiagram
participant Client
participant Authentication Server
participant Users Table
participant Registered Tokens Table
Client->>Authentication Server: Login Request
Note left of Authentication Server: email, password
Authentication Server->>Users Table: Get User Data
Users Table-->>Authentication Server: User Data
Authentication Server-->>Authentication Server: Validate password hash
Authentication Server-->>Authentication Server: Generate Access Token and Refresh Token
Authentication Server->>Registered Tokens Table: Save Tokens
Authentication Server-->>Client: Respond with Tokens
Client-->>Client: Store Tokens
The access_token is used for authenticating requests to resource servers, while the refresh_token is used to obtain a new access token without requiring the user to re-authenticate.
Storing tokens in a database allows you to keep track of active user sessions, revoke access, and manage token expiration. We will explore how these are used in the refreshing sessions section.
sequenceDiagram
participant Client
participant Resource Server
participant Authentication Server
participant Users Table
participant Blacklisted Tokens Table
Client->>Resource Server: Request Resource
Note right of Client: Authorization Bearer <access-token>
Resource Server->>Authentication Server: Verify Session
Note right of Resource Server: Authorization Bearer <access-token>
Authentication Server-->>Authentication Server: Verify Token Signature
Authentication Server->>Blacklisted Tokens Table: Check if token is blacklisted
Blacklisted Tokens Table-->>Authentication Server: Token if available
Authentication Server->>Users Table: Get User Data
Users Table-->>Authentication Server: User Data
Authentication Server-->>Resource Server: Respond with User Data
Note right of Resource Server: user_id, role, email, phone, ...
Resource Server-->>Resource Server: Authorize User based on Role, process request
Resource Server-->>Client: Response
This table tracks unregistered or revoked session tokens. This is crucial because when a user signs out, their access_token and refresh_token could still be valid. Blacklisting these tokens ensures that they can't be used for unauthorized access.
This table is vulnerable to table overflow attacks. To mitigate this risk, consider setting an expiration date for each token and regularly cleaning up expired tokens.
sequenceDiagram
participant Client
participant Authentication Server
participant Registered Tokens Table
participant Blacklisted Tokens Table
Client->>Authentication Server: Refresh Token Request
Note left of Authentication Server: refresh_token
Authentication Server-->>Authentication Server: Validate refresh_token
Authentication Server->>Blacklisted Tokens Table: Check if token is blacklisted
Blacklisted Tokens Table-->>Authentication Server: Token if available
Authentication Server->>Registered Tokens Table: Get refresh_token
Registered Tokens Table-->>Authentication Server: refresh_token
Authentication Server-->>Authentication Server: Check if refresh_token matches with the stored token
Authentication Server-->>Authentication Server: Generate Access Token
Authentication Server->>Registered Tokens Table: Save New Tokens
Authentication Server->>Blacklisted Tokens Table: Blacklist Old Tokens
Authentication Server-->>Client: Respond with New Token
Client-->>Client: Store New Token
sequenceDiagram
participant Client
participant Authentication Server
participant Registered Tokens Table
participant Blacklisted Tokens Table
Client->>Authentication Server: Logout Request
Note left of Authentication Server: access_token
Authentication Server-->>Authentication Server: Validate access_token
Authentication Server->>Blacklisted Tokens Table: Blacklist Tokens
Authentication Server->>Registered Tokens Table: Delete Tokens for uuid
Authentication Server-->>Client: Respond with Success
Client-->>Client: Remove Token
Rendering diagram...
The access_token is used for authenticating requests to resource servers, while the refresh_token is used to obtain a new access token without requiring the user to re-authenticate.
Storing tokens in a database allows you to keep track of active user sessions, revoke access, and manage token expiration. We will explore how these are used in the refreshing sessions section.
Rendering diagram...
The access_token is used for authenticating requests to resource servers, while the refresh_token is used to obtain a new access token without requiring the user to re-authenticate.
Rendering diagram...
This table tracks unregistered or revoked session tokens. This is crucial because when a user signs out, their access_token and refresh_token could still be valid. Blacklisting these tokens ensures that they can't be used for unauthorized access.
This table is vulnerable to table overflow attacks. To mitigate this risk, consider setting an expiration date for each token and regularly cleaning up expired tokens.
Rendering diagram...
This table tracks unregistered or revoked session tokens. This is crucial because when a user signs out, their access_token and refresh_token could still be valid. Blacklisting these tokens ensures that they can't be used for unauthorized access.
This table is vulnerable to table overflow attacks. To mitigate this risk, consider setting an expiration date for each token and regularly cleaning up expired tokens.
Rendering diagram...
Rendering diagram...
Rendering diagram...
Rendering diagram...
Rendering diagram...
The access_token is used for authenticating requests to resource servers, while the refresh_token is used to obtain a new access token without requiring the user to re-authenticate.
Storing tokens in a database allows you to keep track of active user sessions, revoke access, and manage token expiration. We will explore how these are used in the refreshing sessions section.
Rendering diagram...
The access_token is used for authenticating requests to resource servers, while the refresh_token is used to obtain a new access token without requiring the user to re-authenticate.
Rendering diagram...
This table tracks unregistered or revoked session tokens. This is crucial because when a user signs out, their access_token and refresh_token could still be valid. Blacklisting these tokens ensures that they can't be used for unauthorized access.
This table is vulnerable to table overflow attacks. To mitigate this risk, consider setting an expiration date for each token and regularly cleaning up expired tokens.
Rendering diagram...
This table tracks unregistered or revoked session tokens. This is crucial because when a user signs out, their access_token and refresh_token could still be valid. Blacklisting these tokens ensures that they can't be used for unauthorized access.
This table is vulnerable to table overflow attacks. To mitigate this risk, consider setting an expiration date for each token and regularly cleaning up expired tokens.
Rendering diagram...
Rendering diagram...
Rendering diagram...
Rendering diagram...