Lorixen.
EN

PKCE for app logins: why mobile and browser apps need it

Short answer: PKCE (Proof Key for Code Exchange, pronounced "pixie") adds a one-time secret to the OAuth login flow so that an intercepted authorisation code is useless to anyone except the app that started the login. Mobile apps and browser-based single-page apps can't keep a client secret, so they need it. Current OAuth security guidance goes further and recommends PKCE for practically every client. Use the authorisation code flow with PKCE and the S256 method, open logins in the system browser, and drop the old implicit flow.

The problem PKCE solves

In the OAuth authorisation code flow, your app sends the user to the identity provider to sign in. The provider sends back a short-lived authorisation code, and your app exchanges that code for tokens.

For a server-side web app, the exchange includes a client secret that only the server knows, so a stolen code alone isn't enough. A mobile app or a JavaScript app running in the browser can't keep a secret: anyone can unpack the app or read the page's code. RFC 7636, the PKCE specification, describes the resulting threat as the authorisation code interception attack, where a malicious app or process on the same device intercepts the code returned to your app and exchanges it for tokens itself.

How PKCE works

PKCE replaces the fixed client secret with a fresh one per login:

  1. Before redirecting to the login page, the app generates a random code_verifier. RFC 7636 specifies it as 43 to 128 characters from a defined set of unreserved characters, with enough randomness to be unguessable.
  2. The app hashes it with SHA-256 and sends the result, the code_challenge, with code_challenge_method=S256 in the authorisation request.
  3. The provider remembers the challenge and returns an authorisation code as usual.
  4. When exchanging the code for tokens, the app sends the original code_verifier.
  5. The provider hashes the verifier and checks it matches the challenge. If not, no tokens.

An attacker who intercepts the code doesn't have the verifier, which never left the app until the final exchange, so the code is worthless to them.

Use S256, not plain

RFC 7636 also defines a plain method, where the challenge is the verifier itself. It exists for clients that can't compute SHA-256, which is essentially none today. With plain, an attacker who can see the authorisation request also sees the verifier. The specification notes that plain mitigates only the simpler attack scenario. Always use S256, and if your provider lets you, configure it to reject plain.

Who needs PKCE

Originally PKCE was aimed at native apps. Guidance has since widened. The OAuth 2.0 Security Best Current Practice, RFC 9700, published in 2025, recommends PKCE as the main protection against authorisation code injection and related attacks, and it treats the implicit grant, where tokens come straight back in the redirect URL, as something not to use.

In practice:

App type Needs PKCE?
iOS or Android app Yes
Single-page app (React, Vue and so on) Yes
Desktop app Yes
Server-rendered web app with a client secret Recommended as well

Most identity providers now support PKCE for all client types, and many require it for public clients.

Do logins in the system browser

RFC 8252, the best current practice for OAuth in native apps, says authorisation requests from native apps should only be made through external user agents, primarily the user's browser, rather than an embedded web view inside the app. The reasons:

  • In an embedded web view, the app can read what the user types, including their password, which defeats the point of OAuth.
  • The system browser may already be signed in to the provider, so users log in faster.
  • Users can see the real address bar and the provider's page.

On mobile, the platform's browser-based authentication sessions give an in-app feel while still using the system browser. The same RFC describes how the redirect gets back to the app: claimed https links, private-use URI schemes or loopback addresses for desktop apps.

Implementation checklist

Most apps should use a well-maintained OAuth or OpenID Connect client library rather than building the flow. Whatever you use, confirm that:

  • A new verifier is generated for every login attempt, with a secure random generator.
  • The verifier is stored only for the duration of the login, then discarded.
  • The state parameter, or the OpenID Connect nonce, is also used and checked, to tie the response to the request.
  • Redirect URIs are registered exactly with the provider, with no wildcards.
  • Tokens are stored securely afterwards: the platform keychain or keystore on mobile, and memory or secure cookies in the browser.

Migrating from the implicit flow

Older single-page apps often use the implicit flow, receiving an access token directly in the URL fragment. To move:

  1. Enable the authorisation code flow with PKCE for the app in your identity provider.
  2. Update the client library and configuration.
  3. Test login, logout and token refresh, including on mobile browsers.
  4. Turn off the implicit grant for that client once everything works.

A worked example

Take a hypothetical Chicago dog-walking company whose customer app used an embedded web view for login and an old implicit-flow setup for its web dashboard. A security review flagged both. The team switched the mobile app to the system browser authentication session with authorisation code and PKCE using S256, moved the dashboard to the same flow through a maintained library, registered exact redirect URIs and disabled the implicit grant. Users noticed one change: they were often already signed in.

FAQ

Does PKCE replace the client secret for server apps?

No. Server apps that can keep a secret should keep using it. PKCE adds protection on top.

Is PKCE enough to secure my API?

No. It protects the login flow. Your API still needs to validate tokens and check permissions.

Do I need PKCE with "Sign in with Apple" or Google?

If your app uses their OAuth or OpenID Connect flows directly, yes, and their libraries generally handle it.

If your app's login flow predates current OAuth guidance, ask us to review it.

Sources

WhatsAppBook a call