Praktik Keamanan Terbaik
Checklist keamanan saat mengintegrasikan SSO Tabalong.
1. Selalu Gunakan HTTPS
Semua komunikasi dengan OAuth server HARUS menggunakan HTTPS. Jangan pernah mengirim token atau credentials melalui HTTP.
2. Simpan Client Secret dengan Aman
# JANGAN simpan di kode
client_secret = "rahasia123" # SALAH
# Gunakan environment variable
client_secret = os.environ.get('OAUTH_CLIENT_SECRET') # BENAR3. Gunakan State Parameter
State parameter mencegah CSRF attacks. Selalu:
- Generate random state sebelum redirect
- Simpan state di session
- Verifikasi state saat callback
// Generate
const state = crypto.randomBytes(32).toString("hex");
session.set("oauth_state", state);
// Verify
if (callbackState !== session.get("oauth_state")) {
throw new Error("Invalid state");
}4. Validasi ID Token
Jika menggunakan ID token, validasi:
- Signature dengan public key dari JWKS (
/api/auth/jwks), algoritma EdDSA - Issuer (
iss) harus tepathttps://bukutamu.tabalongkab.go.id/api/auth(bukan origin tanpa/api/auth) - Audience (
aud) harus sama dengan Client ID - Expiration (
exp) belum lewat - Issued at (
iat) tidak di masa depan
5. Jangan Simpan Token di localStorage
// JANGAN - rentan XSS
localStorage.setItem("access_token", token);
// LEBIH BAIK - gunakan httpOnly cookie
// Set dari server side
res.cookie("access_token", token, {
httpOnly: true,
secure: true,
sameSite: "strict",
});6. Implementasi Token Refresh
Jangan tunggu token expired. Refresh proaktif sebelum expired:
// Refresh 5 menit sebelum expired
const refreshThreshold = 5 * 60 * 1000; // 5 menit dalam ms
const tokenExpiry = decodedToken.exp * 1000;
if (Date.now() > tokenExpiry - refreshThreshold) {
await refreshToken();
}7. Logout yang Benar
Saat user logout:
- Revoke token di server
- Hapus token dari storage
- Clear session
// authServerUrl = origin, mis. https://bukutamu.tabalongkab.go.id
await fetch(`${authServerUrl}/api/auth/oauth2/revoke`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
token: accessToken,
client_id: clientId,
client_secret: clientSecret,
}),
});
// Opsional: arahkan ke end-session endpoint OIDC
// https://bukutamu.tabalongkab.go.id/api/auth/oauth2/end-session
session.destroy();8. Gunakan Scope Minimum
Hanya minta scope yang benar-benar diperlukan. Jangan minta semua scope "just in case".
// SALAH - terlalu banyak scope
scope: "openid profile email organization personal location";
// BENAR - hanya yang diperlukan
scope: "openid profile email";