Authentication and Authorization
Authentication and authorization are two distinct concepts often confused.
You can be authenticated without being authorized to do something. AuthN is the gatekeeper at the door; authZ decides which rooms you can enter.
Authentication Methods
OAuth 2.0 and OIDC
The standard protocols underneath modern SaaS authentication.
The OAuth flows
The Resource Owner Password Credentials grant sends the userβs password through your application so you can trade it for a token. Leave that grant unused on new clients. PKCE adds a code verifier on public clients: the authorization server binds the code to a hash the client created, so a stolen code fails without the verifier.
LDAP holds the directory record, Active Directory is the Microsoft directory, and Kerberos issues tickets so later services skip another password prompt. SAML carries that directoryβs assertion into the SaaS app. Scopes on the access token name the API rights the app asked for.
JSON Web Tokens (JWT)
JWTs are the standard format for tokens in modern web auth.
Header.Payload.Signature
eyJhbGciOiJSUzI1NiIs... # header (algorithm, type)
.eyJzdWIiOiIxMjM0... # payload (claims: user ID, expiration, etc.)
.SflKxwRJSMeKKF2QT... # signature (HMAC or RSA, verifies integrity)
The payload contains claims like sub (subject = user ID), exp (expiration), iss (issuer), aud (audience). Plus custom claims (tenant ID, roles).
Best practices
- Short access tokens (5-15 minutes) + refresh tokens (longer-lived, stored securely).
- HTTPOnly cookies for browser apps β prevents JavaScript from reading the token (XSS protection).
- Asymmetric signing (RS256, ES256) β server signs with private key, services verify with public key.
- Validate every claim β
iss,aud,exp, custom claims you depend on.
An opaque access token is a random string with nothing readable inside it. The API learns who it is by calling the authorization server (token introspection) on each use. That round trip is the cost of immediate revocation: you delete the token server-side and the next call fails. OIDC identity tokens are JWTs and are checked locally. OAuth access tokens are often opaque even when the identity token is a JWT. A hybrid keeps local signature checks and refreshes revocation state on an interval.
Password hashing
bcrypt is an adaptive password hash. A work factor raises the CPU cost as hardware gets faster, so a stolen hash file stays expensive to search.
Argon2 won the Password Hashing Competition. It has three variants. Argon2d uses data-dependent memory access and resists time-memory trade-off attacks, with more exposure to side channels. Argon2i uses data-independent access and is built against side channels. Argon2id mixes them: a data-dependent start, then data-independent passes. Memory cost is a parameter beside CPU cost. Argon2id is the variant to pick for a new password store.
PBKDF2 iterates a pseudorandom function and is what many older standards still name. It has no memory hardness, so GPUs and ASICs narrow its advantage, and it remains a compatibility choice.
Store the hash, a unique salt, and the parameters that produced them. Verification repeats those parameters. When you raise the work factor or move from bcrypt to Argon2id, recompute the hash on the next successful login.
Authorization Models
For most SaaS, start with RBAC, layer in ABAC for nuanced cases (resource ownership, tenant boundaries), reach for ReBAC/PBAC when complexity demands it.
Multi-Tenant Authorization
Tenancy adds another dimension to authorization.
A typical pattern:
1. JWT carries tenant_id and user_id.
2. Every API call validates: user belongs to tenant? (else 403)
3. Every DB query includes WHERE tenant_id = ?
4. Authorization within tenant proceeds via RBAC/ABAC.
For added safety: Postgres Row-Level Security (RLS) can enforce tenant isolation at the DB level. App-level mistakes get caught.
Session Management
Identity-as-a-Service Options
Most SaaS shouldnβt build auth from scratch. Use a managed identity provider:
Common Pitfalls
Recap
- AuthN = who are you? AuthZ = what can you do? Different problems.
- Modern auth: OIDC (on top of OAuth 2.0) for the protocol; JWTs for the token format.
- Authorization models: RBAC (simple, default), ABAC (flexible), ReBAC/PBAC (complex needs).
- For multi-tenancy: always validate tenant membership; use RLS for safety net.
- Donβt build auth from scratch β use Auth0, Cognito, Clerk, or similar.
- Common pitfalls: OAuth-for-auth, localStorage for JWTs, long-lived tokens, weak password hashing.