Overview
The Kin Conecta API uses session-based authentication to secure API endpoints. Authentication sessions are managed through the/api/auth_sessions endpoint.
The current implementation uses token hash authentication. Ensure you follow secure practices when generating and storing tokens.
Authentication Flow
The typical authentication flow involves:- User logs in with credentials (handled by your authentication logic)
- Server creates an authentication session with a token
- Client includes the token in subsequent requests
- Server validates the token and session status
- Session expires or is revoked when user logs out
Auth Sessions
Authentication sessions track active user sessions and contain important security metadata.Session Model
An authentication session includes:| Field | Type | Description |
|---|---|---|
sessionId | Long | Unique session identifier |
userId | Long | ID of the authenticated user |
tokenHash | String | Hashed authentication token |
expiresAt | DateTime | Token expiration timestamp |
revokedAt | DateTime | Revocation timestamp (null if active) |
ip | String | IP address of the client |
userAgent | String | Browser/client user agent |
createdAt | DateTime | Session creation timestamp |
Creating a Session
Create a new authentication session when a user logs in.Endpoint
Request Body
Example Request
Response
Retrieving Sessions
You can retrieve authentication sessions to verify active sessions or audit login activity.Get All Sessions
Get Specific Session
Response
Updating a Session
Update a session to revoke it or modify its expiration.Endpoint
Example: Revoking a Session
Deleting a Session
Permanently delete a session from the database.Endpoint
Example Request
Response
Token Management Best Practices
Generate Secure Tokens
Generate Secure Tokens
Use cryptographically secure random number generators to create tokens. Minimum length should be 32 characters.
Hash Tokens Before Storage
Hash Tokens Before Storage
Always hash tokens using a strong hashing algorithm (e.g., SHA-256) before storing in the database.
Set Appropriate Expiration
Set Appropriate Expiration
Sessions should expire after a reasonable time period:
- Short-lived: 1-2 hours for high-security operations
- Standard: 24 hours for regular user sessions
- Long-lived: 7-30 days for “remember me” functionality
Track Security Metadata
Track Security Metadata
Always capture and store:
- IP address for security auditing
- User agent for device tracking
- Creation timestamp for session history
Implement Session Validation
Implement Session Validation
Before processing requests, validate that:
- Session exists and matches the user
- Session has not expired (
expiresAt> current time) - Session has not been revoked (
revokedAtis null)
Revoke Sessions on Logout
Revoke Sessions on Logout
When users log out, update the session with a
revokedAt timestamp rather than deleting it immediately. This maintains an audit trail.Session Validation Example
Here’s a typical session validation flow:This is a simplified example. In production, implement additional security measures such as rate limiting, token rotation, and IP validation.
Common Authentication Errors
| Error | Status Code | Description | Solution |
|---|---|---|---|
| Invalid token | 401 | Token hash doesn’t match | Verify token is correct and properly hashed |
| Session expired | 401 | Current time > expiresAt | Require user to log in again |
| Session revoked | 401 | revokedAt is not null | Require user to log in again |
| Session not found | 404 | Session ID doesn’t exist | Verify session ID or create new session |
Next Steps
User Management
Learn about user creation and management
Error Handling
Understand API error responses