Part 4 · Reliability ও Security 📖 ১৪ মিনিট পড়া 📝 ২০টি কুইজ

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 না করেই।

⚠️ মনে রাখুন: OAuth = authorization (কী করতে পারবে); not authentication (কে আপনি)। Authentication-এর জন্য OIDC দরকার।

৪ 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)

  1. User app-এ "Login with Google" চাপলেন।
  2. App user-কে Google authorization server-এ redirect — scope (permissions) সহ।
  3. User Google-এ login করলেন এবং consent দিলেন।
  4. Google authorization code (one-time) সহ app-এ redirect।
  5. App backend authorization code + client secret দিয়ে Google-কে call।
  6. Google access token + refresh token দিল।
  7. App access token দিয়ে user data fetch (Gmail, profile)।
User → App: Login App → Google: redirect with scope User → Google: authenticate + consent Google → App: authorization code App → Google (back-channel): code + secret Google → App: access token + refresh token App → Resource Server: Authorization: Bearer {token}

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:

eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0..._signature [Header].[Payload].[Signature] Header: {"alg":"HS256","typ":"JWT"} Payload: {"sub":"123","name":"Mahfuz","exp":1234} Signature: HMAC(header.payload, secret)

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।
  • 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।

সাধারণ ভুল ধারণা

  1. "OAuth = authentication": না — authorization। OIDC authentication।
  2. "JWT always secure": Wrong implementation = disaster।
  3. "OAuth complex": Right flow choose হলে straightforward।
  4. "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।