Authentication Architecture
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
| โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Applications โ
โ (Web, Mobile, Third-party) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ API Gateway โ
โ (Rate limiting, CORS, Security headers) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Authentication Layer โ
โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โ
โ โ JWT โ โ OAuth โ โ SSO โ โ
โ โ Validation โ โ Server โ โ (SAML/OIDC)โ โ
โ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Authorization Layer โ
โ (RBAC, Permissions, Tenant isolation) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Protected Resources โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
JWT Implementation
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
| from datetime import datetime, timedelta
from jose import jwt
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"])
class AuthService:
def create_access_token(
self,
user_id: str,
expires_delta: timedelta = timedelta(hours=1)
) -> str:
payload = {
"sub": user_id,
"exp": datetime.utcnow() + expires_delta,
"iat": datetime.utcnow(),
"type": "access"
}
return jwt.encode(payload, SECRET_KEY, algorithm="HS256")
def create_refresh_token(self, user_id: str) -> str:
payload = {
"sub": user_id,
"exp": datetime.utcnow() + timedelta(days=30),
"type": "refresh"
}
return jwt.encode(payload, REFRESH_SECRET, algorithm="HS256")
def verify_password(self, plain: str, hashed: str) -> bool:
return pwd_context.verify(plain, hashed)
def hash_password(self, password: str) -> str:
return pwd_context.hash(password)
|
Security Checklist
| Security Measure | Purpose | Implementation |
|---|
| HTTPS Only | Encrypt transit | TLS 1.3 |
| Password Hashing | Protect credentials | bcrypt/argon2 |
| Rate Limiting | Prevent brute force | Token bucket |
| Input Validation | Prevent injection | Pydantic/schemas |
| CORS | Control cross-origin | Allowlist |
| Security Headers | Browser protection | CSP, HSTS, etc. |
| Audit Logging | Track access | All auth events |
OAuth 2.0 Flows
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
| from authlib.integrations.starlette_client import OAuth
oauth = OAuth()
# Configure providers
oauth.register(
name='google',
client_id=GOOGLE_CLIENT_ID,
client_secret=GOOGLE_CLIENT_SECRET,
authorize_url='https://accounts.google.com/o/oauth2/auth',
access_token_url='https://oauth2.googleapis.com/token',
client_kwargs={'scope': 'openid email profile'}
)
@app.get('/auth/google')
async def google_login(request: Request):
redirect_uri = request.url_for('google_callback')
return await oauth.google.authorize_redirect(request, redirect_uri)
@app.get('/auth/google/callback')
async def google_callback(request: Request):
token = await oauth.google.authorize_access_token(request)
user_info = token.get('userinfo')
# Create or update user, issue JWT
return create_session(user_info)
|
Frequently Asked Questions
What is API security?
API security involves protecting APIs from unauthorized access, abuse, and attacks. This includes: authentication (JWT, OAuth), authorization, rate limiting, input validation, encryption, and protection against common attacks (injection, BOLA, etc.).
How much does API security consulting cost?
API security development typically costs $120-180 per hour. A security review starts around $5,000-10,000, while thorough security implementation with OAuth, rate limiting, and monitoring ranges from $20,000-50,000+.
JWT vs OAuth vs API keys: which should I use?
Use API keys for: simple machine-to-machine auth, internal services. Use JWT for: stateless authentication, mobile apps. Use OAuth 2.0 for: third-party integrations, delegated authorization. Many systems combine these for different use cases.
What are common API security vulnerabilities?
OWASP API Top 10 includes: Broken Object Level Authorization (BOLA), broken authentication, excessive data exposure, lack of rate limiting, and injection attacks. I audit for these and implement proper controls.
How do you implement rate limiting?
I implement: per-user and per-IP limits, sliding window algorithms, tiered limits for different user types, graceful degradation, and proper 429 responses. Rate limiting prevents abuse and protects infrastructure.
Experience:
Related Technologies: REST APIs, FastAPI, Python, SaaS Development