Skip to content

πŸ” Authentication & Authorization ​

Verifying identity and controlling access.

Authentication vs Authorization ​

  • Authentication (AuthN) β€” Who are you? (identity)
  • Authorization (AuthZ) β€” What can you do? (permissions)

Authentication methods ​

JWT (JSON Web Token) ​

Client β†’ Login β†’ Server returns JWT
Client β†’ Request + JWT in header β†’ Server validates JWT
  • Stateless β€” no server-side session
  • Contains claims (user ID, roles, expiry)
  • Short-lived access token (15min) + long-lived refresh token (7 days)
  • Store in httpOnly cookie (not localStorage)

Session-based ​

Client β†’ Login β†’ Server creates session, returns cookie
Client β†’ Request + cookie β†’ Server looks up session
  • Stateful β€” session stored server-side (Redis)
  • Easier to revoke than JWT
  • Better for traditional web apps

OAuth2 / OpenID Connect ​

  • Delegate authentication to a provider (Google, GitHub)
  • Use for "Sign in with..." flows
  • Authorization Code flow for server apps
  • PKCE flow for SPAs and mobile

Authorization models ​

RBAC (Role-Based) ​

User β†’ has Role β†’ Role has Permissions
Admin β†’ can: create, read, update, delete
Editor β†’ can: read, update
Viewer β†’ can: read

ABAC (Attribute-Based) ​

Allow if: user.department === resource.department AND user.level >= 3

More flexible, more complex.

Security checklist ​

  • [ ] Hash passwords with bcrypt/argon2
  • [ ] Rate limit login attempts
  • [ ] Implement account lockout after N failures
  • [ ] Use HTTPS everywhere
  • [ ] Validate tokens server-side on every request
  • [ ] Implement CSRF protection for cookie-based auth
  • [ ] Log authentication events (login, logout, failures)
  • [ ] Support MFA for sensitive operations

Pergame Knowledge Base