Beta

Create a tenant

A new tenant starts with its own users, OAuth settings, audit history and logs. You are its first Tenant Admin.

OpenID Connect in single-page and native apps

A relying party does not have to be a server with a secret store. This lesson follows the printer's browser editor and phone app as they sign Ava in with OpenID Connect: where each one validates its ID token, why that token never goes to an API, how the response finds its way back to the right app, and what signing out of an app actually ends.

Where the relying party runs

The printer is growing two new front ends. One is a photo book editor that runs entirely in Ava's browser at book.printer.example, loading once and then talking to the printer's order API as she drags photos around. The other is a phone app for ordering prints on the go. Both need a Continue with your photo account button, and neither is a server like the collage app's in Implementing a relying party, with a secret store and a callback handler of its own. All domains, identifiers and tokens in these examples are fictional.

The OAuth lessons on browser applications, backends for frontends and native applications covered how such apps get access tokens. OpenID Connect adds an ID token to the same exchanges, and with it three questions: who validates the ID token, how the response finds its way back to the right app, and what signing out means for an app that lives on a phone.

In both apps the code runs on a device Ava controls. Anyone can download the editor's JavaScript or unpack the phone app and read whatever it contains, so neither can keep a client secret. They are public clients, and both use the authorization code flow with PKCE, because the verifier is the only proof that the app redeeming a code is the one that started the attempt.

For the editor, the printer's developers have two designs to choose from. In the first, the editor is the relying party itself: its JavaScript sends Ava to the photo service, receives the code at its own page, redeems it at the token endpoint, and holds the ID token and access token in memory. In the second, a small backend on book.printer.example is the relying party. It is a confidential client with its own secret, it runs the sign-in exactly as the printer's main site does, and the editor receives nothing but a session cookie that its scripts cannot read. That backend is the backend for frontend.

The editor handles orders and delivery addresses, so the printer chooses the backend. A script injected into the editor could still act through Ava's session while the page is open, but it could not carry tokens away to use elsewhere. The phone app has no such choice: it is the relying party, and it keeps its tokens in the phone's secure storage.

Validating the ID token in the app

Wherever the relying party runs, that is where the ID token is validated, with the checks from Validating an ID token. The editor's backend does it on the server. The phone app does it on the phone: the issuer is https://auth.photos.example, the audience contains the app's own client ID, the nonce is the one this attempt created, and the token is current. An app that redeemed the code itself, over a TLS connection to the token endpoint, may rely on that connection instead of checking the signature, as Receiving the ID token explained. Everything else is still the app's to check.

The easy mistake comes next. The phone app has a validated ID token for user-2048, and it wants to load Ava's past orders from the printer's order API. Sending the ID token as the bearer token looks natural: it is signed, it names her, and it is fresh. Decoded, its audience gives it away:

{
  "iss": "https://auth.photos.example",
  "sub": "user-2048",
  "aud": "printer-app-ios",
  "iat": 1790845200,
  "exp": 1790845500,
  "nonce": "demo-nonce-41"
}

The token was written for printer-app-ios, to tell the app who signed in. It was never addressed to the order API, it carries no scopes, and anything that intercepted it on its way to the API could replay it to any other service that made the same mistake. The order API asks for an access token issued for the order API and checks its audience, its scopes and its type, so an ID token presented there is refused. The access token the app received in the same token response is the one written for that job: its aud names the order API, https://orders.printer.example, and it carries the orders.read scope. The same goes for the editor: its backend holds the access token and calls the API, and the browser never presents an ID token to anyone.

One token response to printer-app-ios

Tells the app who signed in

sub
user-2048
aud
printer-app-ios
scope
none

Sent to the order API

Refused

Written for the order API

sub
user-2048
aud
https://orders.printer.example
scope
orders.read

Sent to the order API

Ava's past orders

Both tokens came back in one response. The order API refuses the ID token, written for the app, and accepts the access token written for the API.

Sometimes an app does need to tell its own backend who signed in, for example to open a server-side session. The backend then becomes a second party that validates the ID token: same issuer, the app's client ID as the audience, a nonce the backend issued for this sign-in so that a token captured elsewhere cannot be replayed into it, and a short window. After that it gives the app a session of its own. The ID token is checked once, as evidence of a sign-in, and is not presented again on every request.

Returning to the app

The phone app does not show the photo service's sign-in page inside its own screens. It opens the phone's browser, which shows a real address bar, shares Ava's existing photo service session, and keeps her password away from the app. Native apps must not use an embedded web view for this, because an embedded view lets the app read everything typed into it. The hard part is the way back: when the photo service redirects, the phone has to hand the response to the printer's app and to nothing else.

Return addressExampleWho can receive it
Claimed HTTPS addresshttps://printer.example/app/callbackOnly an app the operating system has verified for printer.example
Private-use schemecom.example.printer:/oauth/callbackAny app that registers the same scheme
Loopback addresshttp://127.0.0.1:51004/callbackAny program listening on that port, which suits desktop apps

A claimed HTTPS address is the strongest, because the printer proves to the operating system that it controls printer.example. A private-use scheme is common and weaker, and the PKCE verifier is what makes a code useless to another app that registered the same scheme. When the printer registers its app at the photo service, it says what kind of client it is with application_type, either native or web, the default. The provider uses that to decide which return addresses make sense: private-use schemes and loopback addresses belong to native apps, while a web application has a real HTTPS address and is refused a private-use scheme. Providers differ on whether a native registration may also list claimed HTTPS addresses.

The editor's way back is simpler, because it already lives at an HTTPS address. With the backend design, the callback is the backend's, on the editor's own site, with the code in the query string and the usual pending attempt. Had the printer chosen the design without a backend, the code would come back to the editor's page, its JavaScript would redeem it, and the provider's token endpoint and key set would need to accept requests from the editor's origin.

Signing out of an app

Signing out of the phone app starts where Local logout and provider sessions did, with the app's own state. It deletes its access token and its session, and it revokes its refresh token at the photo service, because deleting a refresh token from the phone does nothing to the copy the provider still honors. That ends the printer's access. It does not end Ava's photo service session, which lives in the phone's browser, so the next sign-in may finish without a password. Whether that is a convenience or a surprise depends on whose phone it is.

If the app should end the provider session too, it sends the phone's browser to the photo service's end session endpoint, as RP-initiated logout described, with its last ID token as id_token_hint and a registered return address that leads back into the app. Some platforms also offer a private browser session for sign-in that shares no cookies at all. It leaves nothing to sign out of, and no single sign-on either.

The provider-initiated logout methods fit these apps unevenly. Front-channel logout loads a relying party page in a hidden frame, which a phone app does not have and which third-party cookie restrictions often break for a single-page app. Back-channel logout sends a logout token to a server. The editor's backend can receive one and end its session on the spot. A phone app with no server of its own cannot, so it learns that Ava signed out only when its own backend tells it, or, if the photo service ends refresh tokens when its session ends, the next time it refreshes and the photo service refuses.

Check your understanding

Try these questions before moving on. If an answer isn't right, use the feedback and try again.

The phone app validates its ID token, then sends it as the bearer token to the printer's order API, which checks the signature and expiry and serves the orders. What is wrong? 1 of 3

Choose one answer.

The printer's phone app needs a return address for sign-in. Which choice best ensures that only the printer's app receives the response? 2 of 3

Choose one answer.

Ava chooses Sign out in the phone app, and the app deletes its tokens from the phone and does nothing else. What is still true afterward? 3 of 3

Choose one answer.

Try it in the Lab

We value your privacy

We use cookies and similar technologies to enhance your browsing experience, and analytics to understand our traffic. By clicking "Allow all", you consent to optional analytics. Cookie Policy

Learn identity