Spaces:
Sleeping
Sleeping
File size: 10,354 Bytes
9eafd9f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | # Research: Authentication & API Security
**Feature**: 001-auth-security
**Date**: 2026-01-09
**Phase**: 0 - Research & Technical Decisions
## Overview
This document captures research findings and technical decisions for implementing authentication and API security using Better Auth (frontend) and JWT verification (backend).
## Research Questions & Resolutions
### 1. Token Expiry Duration
**Question**: Spec says 1 hour, user input says 7 days - which should we use?
**Decision**: **7 days**
**Rationale**:
- The spec explicitly excludes "Token refresh mechanism and refresh tokens" from scope
- Without refresh tokens, 1-hour expiry creates poor UX (users logged out every hour)
- This is a hackathon/MVP project where simplicity is prioritized
- 7 days balances security with usability for the initial release
- Industry standard for web apps *with refresh tokens* is 1 hour access + long-lived refresh
- Industry standard for web apps *without refresh tokens* is 7-30 days
**Alternatives Considered**:
- 1 hour: Too short without refresh mechanism, poor UX
- 24 hours: Reasonable middle ground, but 7 days is acceptable for MVP
- 30 days: Too long, increases security risk unnecessarily
**Implementation**: Set `exp` claim in JWT to 7 days (604800 seconds) from issuance
---
### 2. Better Auth Integration Pattern
**Question**: How should Better Auth be integrated in Next.js 16 App Router?
**Decision**: Use Better Auth with email/password provider and JWT plugin
**Research Findings**:
- Better Auth supports Next.js App Router with server-side session management
- JWT plugin allows issuing tokens that can be verified by external backends
- Configuration file: `lib/auth.ts` with email provider and JWT plugin
- Session management via Better Auth's built-in session handling
- Token accessible via `auth()` helper in server components
**Implementation Pattern**:
```typescript
// lib/auth.ts
import { betterAuth } from "better-auth"
import { jwt } from "better-auth/plugins"
export const auth = betterAuth({
database: {
// Database connection for Better Auth's session storage
},
emailAndPassword: {
enabled: true,
},
plugins: [
jwt({
secret: process.env.BETTER_AUTH_SECRET!,
expiresIn: "7d",
})
],
})
```
**Alternatives Considered**:
- NextAuth.js: More popular but heavier, Better Auth is simpler for JWT use case
- Custom JWT implementation: Reinventing the wheel, Better Auth handles edge cases
- Auth0/Clerk: Third-party services, adds external dependency and cost
---
### 3. Backend JWT Verification Strategy
**Question**: How should FastAPI verify JWT tokens from Better Auth?
**Decision**: Use PyJWT library with FastAPI dependency injection
**Research Findings**:
- PyJWT is the standard Python library for JWT handling
- FastAPI's dependency injection system is ideal for auth middleware
- Better Auth uses HS256 (HMAC-SHA256) by default with shared secret
- Token verification should happen in a reusable dependency
**Implementation Pattern**:
```python
# src/core/security.py
import jwt
from fastapi import HTTPException, status
from src.core.config import settings
def verify_jwt_token(token: str) -> dict:
try:
payload = jwt.decode(
token,
settings.BETTER_AUTH_SECRET,
algorithms=["HS256"]
)
return payload
except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has expired"
)
except jwt.InvalidTokenError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token"
)
# src/api/deps.py
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
security = HTTPBearer()
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security)
) -> int:
token = credentials.credentials
payload = verify_jwt_token(token)
user_id = payload.get("sub") # Better Auth uses 'sub' for user ID
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token payload"
)
return int(user_id)
```
**Alternatives Considered**:
- python-jose: Older library, PyJWT is more actively maintained
- Middleware approach: Less flexible than dependency injection
- Manual token parsing: Error-prone, PyJWT handles edge cases
---
### 4. Password Hashing Strategy
**Question**: How should passwords be hashed and verified?
**Decision**: Use passlib with bcrypt algorithm
**Research Findings**:
- Better Auth handles password hashing on the frontend side
- Backend needs to verify passwords for custom auth endpoints (if any)
- passlib is the standard Python library for password hashing
- bcrypt is industry-standard, resistant to rainbow table attacks
- Cost factor of 12 provides good security/performance balance
**Implementation Pattern**:
```python
# src/core/security.py
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def hash_password(password: str) -> str:
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
return pwd_context.verify(plain_password, hashed_password)
```
**Note**: Since Better Auth handles authentication, backend password hashing may only be needed for:
- Admin user creation scripts
- Testing utilities
- Future direct authentication endpoints
**Alternatives Considered**:
- argon2: More modern but requires C dependencies, complicates deployment
- scrypt: Good but bcrypt is more widely supported
- Plain SHA256: Insecure, vulnerable to rainbow tables
---
### 5. Frontend Token Storage
**Question**: Where should JWT tokens be stored in the frontend?
**Decision**: Use Better Auth's built-in session management (httpOnly cookies)
**Research Findings**:
- Better Auth stores session tokens in httpOnly cookies by default
- This prevents XSS attacks (JavaScript cannot access the token)
- Better Auth's `auth()` helper automatically includes token in requests
- For API calls to backend, extract token from Better Auth session
**Implementation Pattern**:
```typescript
// lib/api.ts
import { auth } from './auth'
async function fetchAPI<T>(endpoint: string, options: RequestInit = {}): Promise<T> {
const session = await auth()
const token = session?.token // Better Auth provides token in session
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
...options,
headers: {
'Content-Type': 'application/json',
...(token && { 'Authorization': `Bearer ${token}` }),
...options.headers,
},
})
// Handle 401 responses
if (response.status === 401) {
// Redirect to login
window.location.href = '/auth/signin'
}
return response.json()
}
```
**Alternatives Considered**:
- localStorage: Vulnerable to XSS attacks
- sessionStorage: Same XSS vulnerability as localStorage
- Memory only: Lost on page refresh, poor UX
---
### 6. Error Handling for Authentication Failures
**Question**: How should authentication errors be communicated to users?
**Decision**: Use standardized error responses with appropriate HTTP status codes
**Research Findings**:
- 401 Unauthorized: Authentication required or failed
- 403 Forbidden: Authenticated but not authorized (not used in this spec)
- Generic error messages prevent information leakage
- Specific errors only in development mode
**Implementation Pattern**:
```python
# Backend error responses
{
"detail": "Invalid credentials", # Generic, doesn't reveal if email or password wrong
"error_code": "AUTH_FAILED"
}
{
"detail": "Token has expired",
"error_code": "TOKEN_EXPIRED"
}
{
"detail": "Invalid token",
"error_code": "TOKEN_INVALID"
}
```
**Security Considerations**:
- Never reveal whether email exists in database
- Never reveal which field (email/password) was incorrect
- Log detailed errors server-side for debugging
- Return generic errors to client
---
### 7. Database Schema Changes
**Question**: What changes are needed to the existing User model?
**Decision**: Add `password_hash` field to users table
**Research Findings**:
- Current User model has: id, email, name, created_at, updated_at
- Need to add: password_hash (string, nullable=False)
- Better Auth may also need its own tables for session management
- Migration should be reversible
**Implementation**:
```python
# alembic/versions/002_add_user_password.py
def upgrade():
op.add_column('users', sa.Column('password_hash', sa.String(255), nullable=False))
def downgrade():
op.drop_column('users', 'password_hash')
```
**Note**: Better Auth may create its own tables (sessions, accounts, etc.) - these should be in a separate migration or handled by Better Auth's migration system.
---
## Dependencies to Add
### Backend
- `PyJWT==2.8.0` - JWT encoding/decoding
- `passlib[bcrypt]==1.7.4` - Password hashing
- `python-multipart==0.0.6` - Form data parsing (for login forms)
### Frontend
- `better-auth` - Authentication library
- `@better-auth/react` - React hooks for Better Auth
---
## Environment Variables
### Backend (.env)
```
BETTER_AUTH_SECRET=<shared-secret-min-32-chars>
DATABASE_URL=<neon-postgres-url>
```
### Frontend (.env.local)
```
BETTER_AUTH_SECRET=<same-shared-secret>
NEXT_PUBLIC_API_URL=http://localhost:8000
```
**Critical**: BETTER_AUTH_SECRET must be identical in both frontend and backend.
---
## Security Checklist
- [x] Passwords hashed with bcrypt (cost factor 12)
- [x] JWT tokens signed with HS256 and shared secret
- [x] Tokens expire after 7 days
- [x] httpOnly cookies prevent XSS attacks
- [x] Generic error messages prevent information leakage
- [x] HTTPS required in production (documented in assumptions)
- [x] User ID extracted from validated token, not request parameters
- [x] All task endpoints require authentication
- [x] Database queries filtered by authenticated user ID
---
## Next Steps
Phase 1 will use these research findings to:
1. Create data-model.md with User entity updates
2. Generate API contracts for auth endpoints
3. Create quickstart.md with setup instructions
4. Update agent context files with new dependencies
|