
OAuth 2.0 Is Not a Login Protocol: Where OpenID Connect Comes In
- كاتب موثّق
محتويات المقال
The common misconception
What actually happens
An access token is a key to an API
Any app can hand you someone else's token
OpenID Connect adds a statement for your app
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
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
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();using Microsoft.IdentityModel.JsonWebTokens;using Microsoft.IdentityModel.Protocols;using Microsoft.IdentityModel.Protocols.OpenIdConnect;using Microsoft.IdentityModel.Tokens;var app = WebApplication.CreateBuilder(args).Build();const string Issuer = "https://id.example.com";var metadata = new ConfigurationManager<OpenIdConnectConfiguration>( $"{Issuer}/.well-known/openid-configuration", new OpenIdConnectConfigurationRetriever());app.MapPost("/auth/external", async (TokenLogin body, Users users) =>{ var result = await new JsonWebTokenHandler().ValidateTokenAsync(body.Token, new TokenValidationParameters { ConfigurationManager = metadata, // the issuer's signing keys ValidIssuer = Issuer, ValidAudience = "shop-mobile", // our client_id }); if (!result.IsValid) return Results.Unauthorized(); var subject = (string)result.Claims["sub"]; return Results.Ok(await users.SignInAsync(Issuer, subject));});app.Run();shop-mobile, one for a second client called quiz-app, and one with the right audience signed by a different key. The real output:
aud=shop-mobile: IsValid=True, sub=248289761001aud=quiz-app: IsValid=False, SecurityTokenInvalidAudienceException: IDX10214: Audience validation failed.forged: IsValid=False, SecurityTokenInvalidSignatureExceptionHow to apply it in your project
Find every token your backend accepts as a login Search for endpoints that take a provider token and call a userinfo or "me" endpoint to decide who the user is. Switch sign-in to ID tokens Request the openidscope and send the ID token to the backend. Validate issuer, audience, signature and expiry with your platform's OIDC library, never by decoding alone.Re-key accounts by issuer and subject Store issandsubfor every external login, and match on them. Migrate accounts that were linked by email.Clean up the flow Use the authorization code flow with PKCE, check stateandnonce, and remove any implicit-flow configuration.Keep access tokens for API calls Use 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 issplussub, never by email alone.
التعليقات (0)