Best practices untuk implementasi authentication yang aman — dari JWT hingga OAuth

Panduan ringkas tapi komprehensif: desain, implementasi, dan operational security untuk sistem autentikasi modern.
Ringkasan

Autentikasi aman tidak hanya soal memilih JWT atau OAuth — melainkan kombinasi desain protokol, manajemen kunci, proteksi terhadap XSS/CSRF, penggunaan TLS, & lifecycle token (expiration, revocation, rotation). Dokumen ini merangkum praktik terbaik dan trade-off untuk skenario web, mobile, dan API.

Sumber utama: OWASP Authentication & Authorization Cheat Sheets, NIST SP 800-63B, OAuth Security Best Current Practice, serta panduan JWT/Auth0/industry. :contentReference[oaicite:0]{index=0}
Daftar isi
  1. Prinsip umum (passwords, MFA, TLS)
  2. Sessions vs Tokens (JWT) — kapan pakai apa
  3. JWT: praktik aman (signing, claims, storage, revocation)
  4. OAuth: flow yang disarankan (Auth Code + PKCE, refresh tokens)
  5. Implementasi praktis & snippets
  6. Operational security: logging, rotation, monitoring
  7. Checklist deploy

1. Prinsip dasar (gak bisa diabaikan)

2. Sessions vs Tokens (stateful vs stateless)

Pilihan antara session cookies (server-side sessions) dan token (mis. JWT) bergantung pada kebutuhan:

Tip: untuk web apps modern, kombinasi yang sering dipakai adalah short-lived access token + HttpOnly Secure refresh cookie (server rotates refresh tokens) untuk keseimbangan keamanan & skalabilitas. Sumber praktik token: Auth0 & industry best practices. :contentReference[oaicite:4]{index=4}

3. JWT — praktik aman

a) Gunakan JWT untuk tujuan tepat

JWT bagus untuk membawa claims yang perlu diverifikasi tanpa lookup. Jangan gunakan JWT untuk menyimpan data sensitif (PII) tanpa enkripsi.

b) Pilih algoritma signing yang kuat

c) Claims: minimal & eksplisit

d) Penyimpanan token di client

Penyimpanan token adalah sumber kesalahan terbesar:

e) Revocation & rotation

f) Protect against replay & misuse

4. OAuth — best practices & flows

OAuth adalah framework otorisasi (bukan autentikasi—walaupun sering dipakai untuk login). Gunakan flow yang sesuai:

a) Authorization Code + PKCE (recommended)

Untuk semua aplikasi publik (browser SPAs, mobile), gunakan Authorization Code Flow with PKCE (Proof Key for Code Exchange). Ini mitigasi code interception dan adalah praktik standar terbaru (OAuth 2.1 / Security BCP / RFCs). :contentReference[oaicite:8]{index=8}

b) Hindari Implicit Flow

Implicit flow (token langsung di URL fragment) sudah dianggap usang/unsafe. Gunakan authorization code + PKCE sebagai gantinya. :contentReference[oaicite:9]{index=9}

c) Refresh token handling

d) Client authentication & secrets

Client secrets tidak boleh disimpan di aplikasi publik (SPA, mobile). Gunakan PKCE dan treat clients as public unless server-side confidential client.

5. Proteksi XSS, CSRF, dan header security

Prinsip: jika token dapat diakses oleh JavaScript, asumsi bahwa XSS bisa mengancam token itu — minimalkan surface attack. Sumber OWASP & Auth0. :contentReference[oaicite:11]{index=11}

6. Implementasi praktis & contoh

a) Contoh: akses token pendek + refresh cookie (pseudocode)

// Login endpoint (server)
POST /login
  // verify credentials
  issueAccessToken = jwtSign({ sub: userId }, { expiresIn: '10m' })
  issueRefreshId = generateRandomId()
  storeRefreshInDB(userId, issueRefreshId, expiry=30d)
  // set refresh cookie (HttpOnly, Secure, SameSite=Strict)
  Set-Cookie: refresh=issueRefreshId; HttpOnly; Secure; SameSite=Strict; Path=/auth/refresh
  return { accessToken: issueAccessToken }

b) Refresh endpoint

// /auth/refresh
  read cookie refresh
  if not found -> 401
  validate refreshId in DB and not revoked
  if valid:
     rotateRefresh = generateRandomId()
     update DB (replace old refreshId with new)
     issue new accessToken (short-lived)
     set new refresh cookie
     return accessToken
  else revoke session -> 401

c) JWT validation checklist (server)

7. Operational security — logging, rotation, incident response

8. Checklist sebelum deploy

9. FAQ singkat

Q: Apakah JWT "berbahaya"?

A: Bukan intrinsically; risiko muncul jika JWT disalahgunakan — mis. lifetime terlalu panjang, storage di localStorage tanpa proteksi XSS, atau tidak ada mekanisme revocation. Gunakan best practices: short-lived tokens + secure refresh flow + algoritma dan signature yang benar. :contentReference[oaicite:12]{index=12}

Q: SPA + OAuth — bagaimana aman?

A: Gunakan Authorization Code + PKCE, hindari implicit flow; simpan tokens di cookie HttpOnly bila memungkinkan (atau gunakan BFF pattern). :contentReference[oaicite:13]{index=13}