Overview
The Short-URL API uses JWT (JSON Web Token) based authentication to secure password-protected short URLs. Only URLs created with a password require authentication for management operations.URLs without passwords do not require authentication and cannot be managed after creation. They are public and permanent.
Authentication Flow
Create a Password-Protected URL
When creating a short URL, provide a password (3-20 characters). The password is hashed using PBKDF2-SHA256 before storage.
main.py:85-86
Login to Get Access Token
Use the
/login endpoint with your URL code and password to receive a JWT access token.Token Generation
Tokens are generated using thecreate_access_token function from auth_utils.py:
auth_utils.py:20-25
Token Structure
Tokens contain the following payload:- url_id: MongoDB ObjectId of the URL entry (used to identify the URL)
- exp: Expiration timestamp (Unix timestamp)
Token Expiration
By default, tokens expire after 5 minutes, configured via theTOKEN_EXPIRE environment variable:
auth_utils.py:8
Login Endpoint
The/login endpoint authenticates users and returns an access token:
Request Example
Response Example
Implementation Details
main.py:97-106
You cannot login to URLs without passwords - attempting to do so returns a 400 error.
Using Bearer Tokens
All protected endpoints require a valid Bearer token in theAuthorization header.
Example Request
Authentication Process
The API uses FastAPI’sHTTPBearer security scheme:
main.py:63-70
Token Validation
Use the/validate_token endpoint to check if a token is valid:
Token Refresh
Before your token expires, use the/refresh_token endpoint to obtain a new token:
Implementation
main.py:204-212
Password Security
Password Hashing
Passwords are hashed using PBKDF2-SHA256 via thepasslib library:
auth_utils.py:10-15
Password Verification
auth_utils.py:17-18
Security Best Practices
Use Strong Passwords
Use Strong Passwords
While the API allows passwords as short as 3 characters, use longer, complex passwords for better security.
Store Tokens Securely
Store Tokens Securely
- Never expose tokens in URLs or logs
- Store tokens in secure storage (e.g., environment variables, secure cookie storage)
- Clear tokens from memory when no longer needed
Handle Token Expiration
Handle Token Expiration
- Implement automatic token refresh before expiration
- Handle 403 errors gracefully by redirecting to login
- Don’t retry failed requests indefinitely
Protect Your SECRET_KEY
Protect Your SECRET_KEY
The
SECRET_KEY environment variable is critical for JWT security:- Use a cryptographically secure random string
- Never commit SECRET_KEY to version control
- Rotate SECRET_KEY periodically (invalidates all tokens)
Use HTTPS Only
Use HTTPS Only
Always use HTTPS in production to prevent token interception during transmission.
Error Responses
| Status Code | Error | Description |
|---|---|---|
| 400 | Invalid request | Attempting to login to a URL without a password |
| 401 | Invalid credentials | Wrong password or URL code |
| 403 | Unauthorized | Missing credentials header |
| 403 | Forbidden | Invalid or expired token |
| 404 | Invalid or expired token | Token validation failed or URL deleted |
Protected Endpoints
The following endpoints require authentication:POST /change_password- Change URL passwordDELETE /delete- Delete short URLPATCH /pause- Pause URL redirectionPATCH /resume- Resume URL redirectionPATCH /reset_hits- Reset hit counterPATCH /change_url- Change destination URLGET /details- Get URL details and statisticsGET /validate_token- Validate current tokenGET /refresh_token- Get new access token
All protected endpoints use the
Depends(security) dependency to enforce authentication.