الانتقال إلى المحتوى
الأمان (Security)OAuth و OIDC و JWT· 7 دقائق للقراءة

OAuth 2.0 Is Not a Login Protocol: Where OpenID Connect Comes In

An access token lets its holder call an API; it doesn't say which app it was issued to. Sign users in with a validated ID token, keyed by issuer and subject.

ReadIn

مقال كل يوم يفرق معاك

22 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A mobile team adds "Sign in with" a large identity provider. The app gets an access token from the provider's SDK and posts it to the backend, which calls the provider's userinfo endpoint and signs in whoever's email comes back. A security researcher then reports a finding. She built a small quiz app on the same provider, signed into it with a test account, and sent the quiz app's access token to the team's endpoint. The backend gave her a session as that user. Nothing was stolen in the usual sense: the token was valid, just not for this app.

The common misconception

The belief: OAuth 2.0 is an authentication protocol, so a valid access token proves who the user is.

It's an easy step to take. Many "Sign in with" buttons run OAuth flows under the hood. The user really does log in at the provider during that flow. And the userinfo endpoint, called with the token, returns that user's name and email. Everything about it feels like a login.

But the provider authenticated the user for itself, then issued a credential for an API. The was never meant to carry that login to your app. RFC 6749, the OAuth 2.0 spec, says so outright: "Authenticating resource owners to clients is out of scope for this specification."

What actually happens

An access token is a key to an API

RFC 6749 calls OAuth an "authorization framework" that lets an application "obtain limited access to an HTTP service". Its definition of the token is about access, not identity: "Access tokens are credentials used to access protected resources", "a string representing an authorization issued to the client", and "The string is usually opaque to the client." It's a for the API. It says the holder may call that API with these scopes. It doesn't say which app received it, or that the user is in front of your app right now.

Any app can hand you someone else's token

That gap is a known attack, spelled out in RFC 6749 itself: "Servers communicating with native applications that rely on being passed an access token in the back channel to identify the user of the client may be similarly compromised by an attacker creating a compromised application that can inject arbitrary stolen access tokens." The attacker doesn't need to steal anything: users who sign into the attacker's own app hand it valid tokens. RFC 9700, the OAuth security best practice, is blunt about plain OAuth: "There is no way to detect such an injection attack in pure-OAuth flows since the token is issued without any binding to the transaction or the particular user agent."

OpenID Connect adds a statement for your app

describes itself as "a simple identity layer on top of the OAuth 2.0 protocol" that "enables Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server". Its key addition is the , a that "contains Claims about the Authentication of an End-User by an Authorization Server when using a Client". The audience claim is what closes the gap: aud "MUST contain the OAuth 2.0 client_id of the Relying Party". A token issued to the quiz app names the quiz app, so your backend can reject it. OWASP's Authentication Cheat Sheet puts the split in one line: "Use OIDC for authentication/SSO; use OAuth for authorization to APIs."

Validate the ID token, then trust iss and sub

The spec's validation rules are short: the issuer "MUST exactly match", aud must contain your client_id, the signature must be checked with "the keys provided by the Issuer", the current time "MUST be before" exp, and a nonce you sent must come back unchanged. Then pick the right key for the account. OIDC is specific: "the only guaranteed unique identifier for a given End-User is the combination of the iss Claim and the sub Claim." Not the email.

Signing in with a token, wrong and right

C# Program.cs
var app = WebApplication.CreateBuilder(args).Build();app.MapPost("/auth/external",    async (TokenLogin body, IHttpClientFactory factory, Users users) =>{    // Any access token from this provider passes, whichever app it was for    var http = factory.CreateClient();    http.DefaultRequestHeaders.Authorization = new("Bearer", body.Token);    var profile = await http.GetFromJsonAsync<Profile>(        "https://id.example.com/userinfo");    return profile is null        ? Results.Unauthorized()        : Results.Ok(await users.SignInByEmailAsync(profile.Email));});app.Run();

The wrong version asks the provider one question: is this token good for your API? It always is. The right version asks the question that matters to your app: was this token issued to us, by our issuer, and is it signed with the issuer's keys? We ran the validation in a scratch app against a local issuer that served its metadata and public key, with three RS256 tokens: one for shop-mobile, one for a second client called quiz-app, and one with the right audience signed by a different key. The real output:

text
aud=shop-mobile: IsValid=True, sub=248289761001aud=quiz-app: IsValid=False, SecurityTokenInvalidAudienceException:  IDX10214: Audience validation failed.forged: IsValid=False, SecurityTokenInvalidSignatureException
Access token (OAuth 2.0)ID token (OpenID Connect)
Made forThe API (resource server)Your app (the client)
What it saysThe holder may call this API with these scopesThis user authenticated at this issuer, for this client
FormatNot defined by OAuth; often opaque to the clientAlways a JWT
AudienceThe APIYour client_id
Use it toCall the APISign the user in

How to apply it in your project

  1. Find every token your backend accepts as a loginSearch for endpoints that take a provider token and call a userinfo or "me" endpoint to decide who the user is.
  2. Switch sign-in to ID tokensRequest the openid scope and send the ID token to the backend. Validate issuer, audience, signature and expiry with your platform's OIDC library, never by decoding alone.
  3. Re-key accounts by issuer and subjectStore iss and sub for every external login, and match on them. Migrate accounts that were linked by email.
  4. Clean up the flowUse the authorization code flow with PKCE, check state and nonce, and remove any implicit-flow configuration.
  5. Keep access tokens for API callsUse the access token only to call the provider's API. Your own API should accept only access tokens whose audience is your API, as RFC 9700 recommends.

Key takeaways

  • OAuth 2.0 delegates access to APIs; authenticating users to your app is outside its scope.
  • An access token doesn't say which app it was issued to, so any app can replay its users' tokens at yours.
  • OpenID Connect's ID token is made for your app, with your client_id in aud.
  • Validate issuer, audience, signature, expiry and nonce on the server before creating a session.
  • Identify users by iss plus sub, never by email alone.

📚 المصادر

  1. RFC 6749: The OAuth 2.0 Authorization Framework
  2. OpenID Connect Core 1.0
  3. RFC 9700: Best Current Practice for OAuth 2.0 Security
  4. OpenID Connect on the Microsoft identity platform — Microsoft Learn
  5. Authentication Cheat Sheet — OWASP Cheat Sheet Series

التعليقات (0)