OAuth 2.0 ও OpenID Connect
"Login with Google" — কীভাবে কাজ করে?
আপনি একটি নতুন app-এ "Sign in with Google" বাটনে চাপ দিলেন। Browser Google-এ গেল, login করলেন, allow চাপলেন — app-এ এসে আপনি logged in! কিন্তু — app আপনার Gmail password জানে না। এটাই OAuth-এর জাদু।
কোন সমস্যা সমাধান করে?
পুরাতন দিনে third-party app আপনার password চাইত (e.g., "Photo printing site needs Facebook password to access photos")। সমস্যা:
- Password share করা insecure।
- Limited permission impossible — সব access।
- Password বদলালে — সব broken।
- Revoke specific app impossible।
OAuth 2.0 কী?
OAuth 2.0 = একটি authorization framework যা third-party app-কে user-এর resources access করার অনুমতি দেয় — password share না করেই।
৪ Actor
- Resource Owner: User (আপনি)।
- Client: Third-party app যে access চায়।
- Authorization Server: User authenticate ও token issue করে (Google, Facebook)।
- Resource Server: Protected data hold করে (Gmail API)।
Authorization Code Flow (most common)
- User app-এ "Login with Google" চাপলেন।
- App user-কে Google authorization server-এ redirect — scope (permissions) সহ।
- User Google-এ login করলেন এবং consent দিলেন।
- Google authorization code (one-time) সহ app-এ redirect।
- App backend authorization code + client secret দিয়ে Google-কে call।
- Google access token + refresh token দিল।
- App access token দিয়ে user data fetch (Gmail, profile)।
OAuth Grant Types
Authorization Code (Recommended)
Web/mobile app — server-side secret। সবচেয়ে secure।
Authorization Code + PKCE
Mobile/SPA-এর জন্য — secret store-এর সমস্যা solve।
PKCE = Proof Key for Code Exchange — extra verification।
Client Credentials
Server-to-server, no user — internal API।
Device Code
Smart TV, IoT — input limited device। User mobile-এ code enter করেন।
Implicit Flow (Deprecated)
SPA-এর জন্য ছিল — security সমস্যা।
Resource Owner Password Credentials (Deprecated)
App password directly নেয় — OAuth-এর spirit-এর বিপরীত।
Tokens
Access Token
- Short-lived (১৫ minutes - ১ hour)।
- API call-এ ব্যবহৃত।
- JWT format common।
Refresh Token
- Long-lived (days/months)।
- New access token-এর জন্য।
- Rotate করা ভালো (refresh token rotation)।
OpenID Connect (OIDC)
OAuth 2.0 = authorization। OIDC = OAuth-এর উপর authentication layer।
OIDC কী যোগ করে?
- ID Token: JWT যা user identity প্রকাশ করে।
- UserInfo Endpoint: Standardized user profile API।
- Standardized scopes: openid, profile, email।
- Discovery: Provider configuration auto-discover।
ID Token vs Access Token
ID Token
- User-এর কথা বলে
- JWT — readable
- App authentication-এ
- API call-এ ব্যবহৃত না
Access Token
- API permission
- Format implementation-dependent
- Resource access-এ
- App-এ parse করা উচিত না
JWT (JSON Web Token)
JWT typical OAuth/OIDC token format:
Properties
- Self-contained — claim থাকে token-এ।
- Signed — tampering detect।
- Stateless — DB lookup নাই (typically)।
- Base64URL encoded।
Pitfalls
- Secret leak = catastrophic।
- Algorithm confusion attacks।
- Revocation কঠিন (stateless)।
- Token size grow — many claims।
জনপ্রিয় Providers
- Google Identity: Google login।
- Auth0 (Okta): Managed identity।
- AWS Cognito: AWS-এ।
- Microsoft Azure AD: Enterprise।
- Keycloak: Open-source self-hosted।
- Firebase Auth: Mobile-friendly।
বাস্তব উদাহরণ
- "Sign in with Google": OIDC + OAuth।
- "Connect to Slack/GitHub": OAuth।
- API platform (Stripe, Twitter): OAuth scope-based।
- Mobile app authentication: Auth Code + PKCE।
Security Best Practices
- HTTPS everywhere।
- State parameter (CSRF protection)।
- PKCE mobile/SPA-এ।
- Short-lived access token।
- Refresh token rotation।
- Scope minimum দিন।
- Validate JWT signature + expiration।
- Don't expose tokens in URL।
সাধারণ ভুল ধারণা
- "OAuth = authentication": না — authorization। OIDC authentication।
- "JWT always secure": Wrong implementation = disaster।
- "OAuth complex": Right flow choose হলে straightforward।
- "Implicit flow OK": Deprecated — Auth Code + PKCE use করুন।
📌 চ্যাপ্টার সারমর্ম
- OAuth 2.0 = third-party authorization (not authentication)।
- OIDC = OAuth + authentication layer (ID token)।
- Authorization Code Flow standard; PKCE mobile/SPA-এ।
- Access token short-lived; refresh token long-lived।
- JWT common token format — stateless ও self-contained।