Skip to content

OAuth2, OIDC, and single sign-on

Introduction

OAuth2 solves "authorizing a third party to access my data"; OIDC adds "confirming who I am" on top of it; single sign-on (SSO) means signing in once and passing through everywhere.

How the three terms differ

Term Full meaning What it solves
OAuth 2.0 Open Authorization Authorization: “allow app A to access some of my data on B”
OIDC OpenID Connect Authentication: on top of OAuth2, obtain “who the user is” (ID Token)
SSO Single sign-on Experience: sign in once, use many apps

Note: plain OAuth2 is not a login protocol. “Sign in with WeChat” and similar flows actually rely on OIDC or a platform-specific equivalent.

Why use it

  • Never touch passwords: user passwords stay with the identity provider (IdP, such as Google, enterprise AD, or Keycloak); your system neither stores nor can leak them.
  • Central management: hire, leave, and permission changes happen in one place.
  • Stronger security: you can enable multi-factor authentication (MFA) once for everyone (for example a second factor such as an SMS code at login).

How to do it

  1. Use the Authorization Code flow with PKCE. It is the recommended approach for web, mobile, and single-page apps today; do not use the older Implicit flow.
  2. After login, confirm identity with the ID Token, and bind the local account to the IdP’s unique identifier (sub); do not use email as the unique key.
  3. Redirect URIs must be configured as an exact allowlist.
  4. Validate the state parameter against CSRF, and validate the ID Token’s signature, issuer, audience, and expiry (libraries handle this for you).
  5. Roles and permissions still belong in your own system; see Role-based access control (RBAC).
  6. Never put the client_secret in code or the frontend; see Secrets and configuration management.

Component comparison

Keycloak

Self-hosted open sourceOpen sourceOpsHigh

A self-deployed unified login center: multiple systems redirect to it for login, and one login works everywhere.

Best forUnified login across multiple internal enterprise systems, with data that must stay on your own servers.

Avoid whenThere is only one application, or nobody responsible for ops.

Pros

Single sign-on, social login, and a user management console are all ready-made

The data is in your hands, with no per-user pricing

Complete protocol support, with integration options in every language

Cons

A service that needs long-term maintenance and upgrades and uses a lot of memory

Complex configuration, and troubleshooting requires understanding the OAuth/OIDC protocols

Without dedicated ops, an outage means nobody can log in to any system

Official docs ↗

Authentik

Self-hosted open sourceOpen sourceOpsMedium

Another open source unified login center, with a more modern UI and login flows configurable by graphical drag and drop.

Best forSmall teams self-hosting unified login who want a UI easier to pick up than Keycloak.

Avoid whenYou need plenty of proven cases and third-party integration material as assurance.

Pros

Friendly UI, with login flows configured visually

Relatively lightweight to deploy (usually as containers)

Suits small teams self-hosting unified login for internal systems

Cons

Smaller community and less material than Keycloak, so answers to obscure problems are harder to find

Also needs self-deployment, backup, and upgrades

Relatively few large enterprise-grade case studies

Official docs ↗

Managed service (Auth0, etc.)

Managed serviceFree tier + paidOpsLow

The provider runs the unified login center; you only configure and integrate it, with no servers to deploy yourself.

Best forProducts that want to quickly support many login methods and have no ops investment.

Avoid whenThe budget is tight and the user base large, or data cannot leave the country.

Pros

You do not operate it yourself, and connecting login is fairly quick

Social login and enterprise account linking are fairly complete

The provider handles security patches and protocol updates

Cons

Costs rise sharply once usage grows, and the enterprise tier costs more

User data lives with the provider, so moving away is awkward

Access speed in China and data compliance need a separate review

Official docs ↗

Direct integration with WeChat/GitHub and other third-party logins

Hand-rolledOpen sourceOpsLow

Skip any intermediate service and write code yourself following the docs of platforms such as WeChat and GitHub, letting users log in with those accounts.

Best forSmall projects that need only one or two third-party logins.

Avoid whenYou need to integrate many platforms, or share login across multiple systems.

Pros

No new service, and no extra deployment cost

Connecting only one or two logins keeps the amount of code small

Users already know WeChat or GitHub, so sign-up friction is low

Cons

Each extra platform means doing the integration again

Exchanging tokens and linking accounts is yours to build, and holes are easy

If the platform changes its rules or requires a qualification, you are affected directly

Checklist

  • Use the Authorization Code flow with PKCE enabled
  • Redirect URIs are an exact allowlist
  • Use a mature library or identity service; do not implement the protocol yourself
  • Use sub as the unique identifier for the external account
  • client_secret is not in the repository or the frontend
  • This system still enforces permission checks (see Authentication and authorization)