Authentication
The IBW Backend Service supports multiple authentication methods depending on the endpoint and user type.
Authentication Methods
1. Telegram Authentication
Used for user-facing endpoints. Users authenticate via Telegram bot tokens.
Header:
X-Telegram-Auth: <telegram_bot_token>How it works:
- User authenticates with Telegram bot
- Telegram provides authentication data
- Client sends
X-Telegram-Authheader with Telegram auth data - Server validates the Telegram authentication
- Server extracts user information from Telegram data
Middleware: TelegramAuthMiddlewareFunc
Usage:
app.Get("/api/v1/users/me",
middleware.TelegramAuthMiddlewareFunc(cfg),
handler.GetMe)2. Optional Telegram Authentication
Some endpoints support optional authentication for personalized data.
Middleware: OptionalTelegramAuthMiddlewareFunc
Behavior:
- If auth header is present, user is authenticated
- If auth header is missing, request proceeds without user context
- Response may vary based on authentication status
Usage:
app.Get("/api/v1/users",
middleware.OptionalTelegramAuthMiddlewareFunc(cfg),
handler.ListUsers)3. JWT Authentication
Used for API access tokens.
Header:
Authorization: Bearer <jwt_token>Token Generation:
- Tokens are generated via
/api/v1/auth/login - Tokens contain user information
- Tokens have expiration time
Middleware: JWT validation in handlers
4. Admin API Key Authentication
Used for admin endpoints.
Header:
X-API-Key: <admin_api_key>Configuration:
- Set
ADMIN_API_KEYin environment variables - Admin API key is validated on each admin request
Middleware: AdminAPIKeyAuth
Usage:
admin := v1.Group("/admin", middleware.AdminAPIKeyAuth(cfg))5. Admin JWT Authentication
Alternative admin authentication using JWT tokens.
Header:
Authorization: Bearer <admin_jwt_token>Middleware: AdminAuthMiddleware
Usage:
admin := v1.Group("/admin", middleware.AdminAuthMiddleware(cfg, jwtService))Authentication Flow
User Authentication Flow
- User interacts with Telegram bot
- Bot provides authentication data
- Client includes auth data in
X-Telegram-Authheader - Server validates Telegram auth data
- Server extracts
telegram_idfrom auth data - Server looks up or creates user in database
- User context is attached to request
Admin Authentication Flow
- Admin provides API key or JWT token
- Server validates credentials
- Admin context is attached to request
- Admin endpoints become accessible
Protected Endpoints
User Protected Endpoints
Require Telegram authentication:
/api/v1/users/me- Get current user/api/v1/users/socials- Update socials/api/v1/events/attend- Register for event/api/v1/quests/:id/start- Start quest/api/v1/connections/*- Connection management/api/v1/notifications/*- Notifications
Admin Protected Endpoints
Require admin authentication:
/api/v1/admin/*- All admin endpoints
Public Endpoints
No authentication required:
/healthz- Health check/api/v1/events- List events (public data)/api/v1/conferences- List conferences/api/v1/speakers- List speakers/api/v1/quests- List quests (public data)/api/v1/feedback- Submit feedback
Error Responses
Unauthorized (401)
{
"error": "Unauthorized",
"code": "UNAUTHORIZED"
}Forbidden (403)
{
"error": "Forbidden",
"code": "FORBIDDEN"
}Invalid Token (401)
{
"error": "Invalid token",
"code": "INVALID_TOKEN"
}Security Best Practices
- Always use HTTPS in production
- Validate tokens on every request
- Set token expiration times
- Rotate API keys regularly
- Use secure storage for credentials
- Implement rate limiting on auth endpoints
- Log authentication failures for security monitoring
- Never expose API keys or tokens in client code
Token Management
JWT Token Structure
{
"user_id": 123,
"telegram_id": 456789,
"exp": 1234567890,
"iat": 1234567890
}Token Expiration
- Tokens have expiration times
- Expired tokens return 401 Unauthorized
- Clients should refresh tokens before expiration
Rate Limiting
Authentication endpoints are rate-limited to prevent abuse:
- Login attempts: Limited per IP
- API key validation: Limited per key
- Telegram auth: Limited per user
Testing Authentication
Test Telegram Auth
curl -X GET http://localhost:8080/api/v1/users/me \
-H "X-Telegram-Auth: <telegram_auth_data>"Test Admin API Key
curl -X GET http://localhost:8080/api/v1/admin/users \
-H "X-API-Key: <admin_api_key>"Test JWT Token
curl -X GET http://localhost:8080/api/v1/users/me \
-H "Authorization: Bearer <jwt_token>"