The before code shows centralized auth where each token validation requires a network call to the auth service, causing latency and dependency. The after code uses distributed auth by locally verifying the JWT token signature and expiry, eliminating network calls and improving scalability.
Before (Centralized Auth - naive token check with network call):
import requests
def is_token_valid(token):
response = requests.post('https://auth.service/validate', json={'token': token})
return response.status_code == 200
After (Distributed Auth - local JWT verification):
import jwt
SECRET_KEY = 'secret'
def is_token_valid(token):
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=['HS256'])
return True
except jwt.ExpiredSignatureError:
return False
except jwt.InvalidTokenError:
return False