Web Authentification Overview
up:: Web Development TK
- User visits the site and enters username and password.
- On submission, a request is sent to the server over HTTPS with the credentials in the body.
- Server compares credentials to stored credentials (using a secure comparison). If invalid, return
403 Forbidden
. - If valid, the server encrypts an access key into a JWT, sets it as an
HttpOnly
cookie withSecure
,SameSite=Strict
, and an expiration time, and sends it to the client. - Browser stores the cookie, automatically including it in subsequent requests to the server.
- User makes API calls, and the JWT is sent in the cookie (or manually in the
Authorization
header if preferred). - Server validates the JWT on each request by verifying its signature and expiration.
- Once the cookie or JWT expires, the user needs to log in again.
Other things to implement:
- Rate-limit login attempts to mitigate brute-force attacks.
- Encrypt sensitive data in transit by enforcing HTTPS using HSTS (HTTP Strict Transport Security).
- Use secure environment variables to store server credentials and JWT secret keys.
- Set a short expiration time for the JWT (e.g., 15 minutes) to limit the impact of a compromised token.
- Consider adding refresh tokens for seamless reauthentication without re-entering credentials.
- Validate user inputs on the server to prevent injection attacks.
- Implement CSRF protection if using cookies for authentication (e.g., SameSite, CSRF tokens).
- Log authentication events (e.g., logins, failed attempts, and token use) for audit and monitoring.
- Regularly rotate JWT secrets and revoke old tokens when secrets are updated.
- Ensure proper error handling to avoid leaking sensitive information in responses.
- Test the implementation for vulnerabilities, such as XSS, CSRF, and session fixation attacks.