Overview
The DonaSF API uses JSON Web Tokens (JWT) for secure authentication. After successfully logging in, you’ll receive a JWT token that must be included in subsequent API requests to access protected endpoints.Authentication Flow
- Submit credentials to the login endpoint
- Receive a JWT token in the response
- Include the token in the
Authorizationheader for authenticated requests - Token expires after 7 days
Obtaining a JWT Token
Login Endpoint
Request Parameters
User identifier - can be either email address or phone number
User password
Response Fields
The JWT token string to use for authenticated requests
The user’s identifier (email or phone)
Token expiration timestamp (UTC)
Unique client/user identifier
User’s display name
Account active status
Response Example
Alternative Login: Identity Provider
For users authenticating via external identity providers (Google, Facebook, etc.):Token Structure
The JWT token contains the following claims:| Claim | Description | Source |
|---|---|---|
IdCliente | Unique client ID | Database |
Identificador | User identifier (email/phone) | Login request |
correo | User’s email address | Database |
telefono | User’s phone number | Database |
nombre | User’s full name | Database |
Contrasena | Password hash (standard login only) | Login request |
IdProvider | Identity provider ID (OAuth login only) | Login request |
exp | Token expiration timestamp | Generated |
Token Validation Parameters
The API validates JWT tokens using the following parameters (configured inProgram.cs:22-29):
| Parameter | Value | Description |
|---|---|---|
ValidateIssuer | false | Issuer validation is disabled |
ValidateAudience | false | Audience validation is disabled |
ValidateLifetime | true | Token expiration is checked |
ValidateIssuerSigningKey | true | Signature verification is required |
IssuerSigningKey | From appsettings.json | 256-bit symmetric key |
The signing key is stored in
appsettings.json under Jwt:Key. In production, this should be stored securely using environment variables or a secrets manager.Using the Token in Requests
Include the JWT token in theAuthorization header with the Bearer scheme:
Token Expiration
- Standard Login: Tokens expire 7 days after issuance
- Configured in:
ClienteController.cs:128andClienteController.cs:166 - Expiration Format: UTC datetime in ISO 8601 format
- Configuration Setting:
appsettings.jsoncontainsJwt:Expires: 3600(in seconds, equals 1 hour) but the actual implementation uses 7 days
There’s a discrepancy between the configuration file (1 hour) and the actual implementation (7 days). The code implementation takes precedence.
Error Responses
Invalid Credentials
400 Bad Request
Missing Parameters
400 Bad Request
Server Error
400 Bad Request
Security Best Practices
- Token Storage: Store tokens in secure, httpOnly cookies or secure storage mechanisms
- Token Transmission: Always use HTTPS to prevent token interception
- Token Expiration: Monitor token expiration and implement refresh logic
- Signing Key: Use strong, randomly generated keys (minimum 256 bits)
- Environment-Specific Keys: Use different signing keys for development, staging, and production
Implementation Details
JWT Configuration Location
- Configuration:
Program.cs:19-30 - Token Generation:
ClienteController.cs:110-146(standard login) - Token Generation (OAuth):
ClienteController.cs:149-185(identity provider) - Settings:
appsettings.json:9-12
Algorithm
- Signing Algorithm: HMAC-SHA256 (HS256)
- Key Type: Symmetric (shared secret)
- Key Source: Configuration file (
appsettings.json)
Next Steps
Donations API
Explore donation management endpoints
Architecture
Learn about the system architecture