Blog / Identity

How to add SSO and MFA to a legacy application without breaking it

A step-by-step guide to adding SSO and MFA to an older application with local passwords, without a rewrite: who owns MFA, OIDC, identity mapping, rollout.

Mariusz Karabanowicz ·

A new glass door with a small green status light set into the stone doorway of an old building, at dusk

Business customers increasingly ask the same thing: let our people sign in to your application with our own identity provider, under our own MFA policy. For an application built around a local users table and a password form, that request often arrives long before a rewrite is realistic.

It does not need a rewrite. Single sign-on (SSO) can be scoped to authentication, and the rest of the application can stay as it is, provided a handful of decisions are made deliberately. This guide walks through them in the order they usually come up.

Step 1: Decide who owns the second factor

Before choosing a protocol, decide which party is responsible for multi-factor authentication (MFA). There are two common shapes, and they lead to different designs.

Your customers already run an identity provider that enforces MFA. Then your application should trust it. The user is redirected to the customer’s sign-in page, completes the customer’s MFA policy, and comes back with a signed result. Adding a second prompt inside your application only gives their users two prompts under two policies. Where you must be sure a second factor was actually used, check what the provider reports about how the user authenticated. OpenID Connect defines the acr and amr claims for this, but support varies by provider and the mere presence of a claim proves nothing: agree with the customer’s administrators which values you accept, and refuse or ask for a stronger sign-in when the result is weaker.

Your users come from many organizations, and not all of them have a provider that enforces MFA. Then your own identity layer has to provide the second factor for those users, alongside federation with the customers who do have one. A managed identity service with configurable sign-in flows is usually a better fit than building one-time codes, authenticator-app enrollment and recovery yourself.

Getting this wrong is expensive in both directions. Build your own MFA for customers who already have one, and you add friction for no gain. Rely on customer-side MFA for users who have none, and there is no second factor at all.

Step 2: Choose the protocol and put it in one place

Most current identity providers support OpenID Connect (OIDC), which is built on OAuth 2.0. Some enterprise customers will ask for SAML instead. Whichever you support, the architecture that keeps a legacy application manageable is the same: handle the protocol in one place at the edge of the application.

That one place sends the user to the provider, receives the response, validates it and hands the rest of the application a simple result: this is user X. Everything behind it keeps the same idea of “the current user” it always had. If the codebase has no middleware for OIDC, a small, well-tested component or a reverse proxy in front of the application is better than scattering protocol code through old pages. With a proxy, make sure the application cannot be reached directly, and strip any identity headers a client sends before the proxy sets its own.

SSO sign-in flow handled at one edge of a legacy application Six steps from top to bottom. The browser opens the application. The application edge redirects the user to the identity provider. The provider signs the user in under the customer's MFA policy. The edge validates the ID token, maps the issuer and subject to an existing user, and the legacy application then starts its own session. 1. Browser User opens the application No session yet, so the edge takes over 2. Application edge Redirect to the provider Sends PKCE challenge, state and nonce 3. Identity provider User signs in The customer's own MFA policy applies 4. Application edge Validate the ID token Signature, issuer, audience, expiry, nonce 5. Application edge Map to the existing user By issuer and subject, never email alone 6. Legacy application Start the application session New session ID, rest of the app unchanged
The sign-in flow when SSO is handled at one edge: the legacy application only sees the final result, a known user with a new session.

For a browser-based application, use the authorization code flow with PKCE. The OWASP OAuth 2.0 Cheat Sheet and the IETF’s RFC 9700 on OAuth 2.0 security both recommend PKCE for all client types. Use the S256 challenge method and generate a fresh verifier for every sign-in attempt, tied to that user’s browser. You also need protection against cross-site request forgery: OWASP notes that in OpenID Connect the nonce provides it, and otherwise a one-time state value does. Whichever you use, check it when the response comes back. Register exact redirect URLs and avoid anything that behaves as an open redirect.

If you will accept sign-ins from several providers, for example one per customer, remember which provider you sent each user to and confirm the response came from that same issuer before you exchange the code. RFC 9700 describes this “mix-up” attack.

Step 3: Validate the ID token completely

Receiving a token is not the same as trusting it. The OpenID Connect Core specification lists what the application must check. As a practical baseline:

  • the token is signed, verified against the issuer’s published keys, with only the algorithms you configured accepted and never none (a stricter rule than the specification’s minimum, and the right one for most applications)
  • iss matches the issuer you expect
  • aud contains your client ID and no audience you do not trust, and azp, if present, is your client ID
  • exp has not passed, and iat and sub are present
  • nonce equals the value you sent, if you sent one

Use a maintained library for this wherever the stack allows. A hand-written validator that skips one of these checks is a common way for SSO to become a security hole.

Step 4: Map the external identity to the user you already have

Signing in is the easy half. The application still has its own users, roles and data, and a successful login at the provider means nothing until it resolves to one of them.

Key the link on the issuer and subject, not on email. The spec defines sub as a locally unique identifier that is never reassigned within the issuer. Microsoft’s identity team makes the same point in The False Identifier Anti-pattern: always and only use the subject and issuer together as the primary key for a federated account. Email addresses change, get reused after someone leaves, and are not guaranteed to be unique, so they are fine as information but not as a key.

In practice that means a small table, or two columns on the users table, holding the issuer, the subject and the local user ID, with a uniqueness constraint on the issuer and subject pair. Also settle these questions before you write code:

  • First sign-in. How does an existing user connect to their external identity the first time? Use email only to find a candidate account, never to complete the link. Have the user also sign in to the existing local account to prove they own it, or run an administrator-controlled migration, and log the link either way. An automatic merge on matching email can let one person take over another person’s account.
  • No matching account. What happens when someone authenticates successfully but has no account? Reject, create one with minimal rights, or send them to a request flow. Decide it on paper first. This is the case that generates support tickets on the first day.
  • Roles. Does the provider send groups or roles, or does the application keep its own? Keeping authorization in the application is the lower-risk first step. Mapping provider groups to roles can come later.

Step 5: Handle sessions and logout deliberately

After a successful sign-in, the application still issues its own session. The OWASP Session Management Cheat Sheet says to renew the session ID after any privilege level change, and login is the main case. Invalidate the previous identifier and do not carry an anonymous session over into the signed-in one. Keep your own idle and absolute session timeouts: an expired ID token, or an account disabled at the provider, does not end your application session by itself.

Plan logout as well. Ending the local session does not end the session at the provider, so the next click on “sign in” may log the user straight back in. OpenID Connect RP-Initiated Logout lets the application redirect the user to the provider’s end-session endpoint, passing the ID token as a hint and, optionally, a registered URL to return to. Provider support varies and the request does not guarantee that every other application is signed out, so end the local session first and do it even if the provider step fails. Decide what “sign out” should mean for your users, and test it.

Step 6: Stop storing what you no longer need

Moving authentication to an external provider removes a liability. Once the provider is the only way in, remove password storage along with the reset and change-password flows that went with it. If you keep a break-glass route for administrators, restrict it, audit it and test it.

Step 7: Roll out in stages

Do not switch every user on the same day.

  1. Pilot. Enable SSO for one customer or one internal group and keep password sign-in for everyone else. Once an account has moved, disable its password sign-in and password reset. Otherwise the old route stays open and sidesteps the customer’s MFA policy.
  2. Watch. Track sign-in failures and support requests, especially the no-matching-account case.
  3. Expand. Move customers across one at a time. Make identity configuration (domains, providers, policies) a setting an operator can change, not a code change, because the second customer will ask for something the first did not.
  4. Retire passwords. Remove local passwords for accounts that are fully migrated, and keep a restricted, audited emergency route until the new flow has proved itself.

Common pitfalls

  • Linking accounts on email address alone.
  • Skipping PKCE, or both state and nonce, because “it works without them”.
  • Trusting the ID token without checking issuer, audience, expiry and signature.
  • Forgetting that logout is two sessions, not one.
  • Leaving password sign-in open for accounts that now use SSO.
  • Building MFA for customers who already enforce it, or relying on it for users who have none.
  • Spreading protocol code across the old application instead of keeping it at one edge.

A short checklist

  • Decide which party owns MFA: your application or the customer’s identity provider.
  • Use the authorization code flow with PKCE (S256), and keep the protocol handling in one place.
  • Validate signature, iss, aud, exp and nonce on every ID token.
  • Link external identities by issuer and subject, with a first-time linking step that proves ownership of the existing account.
  • Define what happens when sign-in succeeds but no local account exists.
  • Renew the session on sign-in, keep your own timeouts, and decide what logout does at the provider.
  • Disable password sign-in for accounts that have moved, and remove password storage once the provider is the only way in.
  • Roll out customer by customer, with a restricted emergency route until the flow is proven.

An older application can meet current security expectations without being rewritten, as long as the work is scoped to identity and the rest of the platform is left alone. For one real example, see our case study on adding SSO to BenefitConnect.

Sources

All posts

Modernizing access to an older application?

Tell us how users sign in today and what your customers expect. We will tell you plainly how we would approach it.