OpenID Connect in single-page and native apps
A relying partyAn application that accepts and relies on a sign-in from another service, the name used in OpenID Connect and for passkeys. SAML calls the same role a service provider. 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 ConnectAn identity layer on top of OAuth 2.0 that lets an application sign people in with an account at another service. It adds the ID token, standard claims, and discovery to the OAuth exchange.: where each one validates its ID tokenIn OpenID Connect, a signed JWT from the provider, addressed to one client, saying which account authenticated, when, and in answer to which request. It reports a sign-in and is not used to call APIs., why that tokenA piece of data issued by a trusted service that a client presents as a credential, for example to call an API or to report a sign-in. Some tokens carry readable claims; others are random strings that only the issuer can interpret. never goes to an APIAn application programming interface: a defined way for software to interact with a service. A web API offers endpoints where other programs send HTTP requests., 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 OAuthA framework that lets a person give an application limited access to their resources at another service, using tokens instead of sharing their password. OAuth is about access; OpenID Connect adds sign-in. 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 secretA shared secret that a confidential client uses to prove itself to the authorization server, much like a password for an application. It belongs on a backend, never in code sent to browsers or phones.. They are public clients, and both use the authorization code flowThe main OAuth flow: the browser visits the authorization server, where the person signs in and approves access, then carries an authorization code back to the client, which exchanges it for tokens directly. When the client runs on a server, the tokens never pass through the browser. with PKCEProof Key for Code Exchange, pronounced pixie: the client creates a secret for each authorization attempt, sends only a hash of it at the start, and proves it holds the secret when redeeming the code, so a stolen authorization code is useless to anyone else., 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 endpointThe authorization server address where clients exchange an authorization code, refresh token, or other grant for tokens, in a direct request rather than through a browser redirect., and holds the ID token and access tokenA token that a client presents to an API to use access it has been granted, usually limited in what it allows and how long it lasts. It authorizes API calls; it does not tell the client who signed in. in memory. In the second, a small backend on book.printer.example is the relying party. It is a confidential clientAn OAuth client, typically running on a backend, that can keep a credential such as a client secret or private key protected, and so can authenticate itself to the authorization server. 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
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 address | Example | Who can receive it |
|---|---|---|
| Claimed HTTPS address | https://printer.example/app/callback | Only an app the operating system has verified for printer.example |
| Private-use scheme | com.example.printer:/oauth/callback | Any app that registers the same scheme |
| Loopback address | http://127.0.0.1:51004/callback | Any 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.
Try it in the Lab
- Sign a native app in with OpenID Connect and keep its ID token in its placeSign the printer's native app in with PKCE, validate its ID token on the device, see the photo API refuse that ID token, then sign the app out by revoking and ending the provider session.