Search Sreeni's Insights

Filter by content type
cloud December 1, 2015 7 min read

What is OAuth 2.0 and OpenID Connect?

OAuth 2.0 and OpenID Connect explained through roles, grant types, access tokens, authorization versus authentication, and the purpose of an id_token.

Share:

What is OAuth 2.0 and OpenID Connect?

Applications are increasingly calling other applications on behalf of a user. A news reader may ask for permission to read a user’s subscriptions, while an administration tool may need permission to publish an article. If we are using the user’s password in every application, the password is copied across boundaries and the security risk is becoming larger.

OAuth 2.0 defines delegated authorization, while OpenID Connect (OIDC) builds an authentication layer on top of OAuth 2.0. In this article, we are going to see the roles, grant types and tokens in OAuth 2.0, and why an id_token makes OIDC different from OAuth authorization.

TL;DR Summary

  • OAuth 2.0 answers what a client is allowed to access.
  • OAuth has a resource owner, client, authorization server and resource server.
  • An access token is sent to the resource server and should contain limited permissions.
  • OpenID Connect adds authentication and standard identity claims to OAuth 2.0.
  • An id_token tells the client who authenticated, it is not the token for calling an API.
  • Choose a grant type based on the client type and threat model, not by copying a sample.

Challenges

Confusing authorization and authentication

OAuth is often described as a login protocol, but its core purpose is delegated access. If a client treats an access token as proof of identity without the correct protocol, it may make an unsafe decision. This confusion creates a blind spot between the identity system and the application.

Password sharing

A user should not give a third-party application its password just to grant access to a limited resource. If the application stores that password, revoking one permission may require changing the password everywhere. Delegated tokens reduce this coupling.

Token leakage

A token is a capability. A leaked access token can be used until it expires or is revoked. We must use HTTPS, limit scopes and keep tokens away from logs, URLs and untrusted storage, otherwise the security boundary is only theoretical.

What is OAuth 2.0?

OAuth 2.0 is an authorization framework which allows a client to obtain limited access to a protected resource on behalf of a resource owner.

In simple terms, the user grants a permission, an authorization server issues an access token, and the client presents that token to the resource server. OAuth does not by itself tell the client who the user is. It tells the resource server that a permission was granted.

OAuth 2.0 roles

The OAuth model has the following roles,

  • Resource owner - Usually the user who owns the data or can grant access.
  • Client - The application requesting access, such as a web application.
  • Authorization server - The system which authenticates the owner and issues tokens.
  • Resource server - The API which accepts an access token and protects data.

Consider a fund dashboard. The investor is the resource owner, the dashboard is the client, the identity platform is the authorization server, and the fund API is the resource server. The dashboard receives permission to read units, but it should not receive permission to publish a fund notice.

Why OAuth is important?

OAuth lets us define narrower permissions than “has the password”. The access token can have a scope such as articles.read, a short lifetime and an audience for one API. This supports least privilege, although the API must still validate the token and enforce the scope.

OAuth 2.0 grant types

Grant types are ways for a client to obtain an access token,

Authorization Code grant

A user-facing web application redirects the resource owner to the authorization server, receives a code, and exchanges it for tokens through a back channel. The client secret stays on the server. This is a strong general choice for a server-side application because the password is entered at the authorization server, not at the client.

Implicit grant

The implicit flow returns the access token directly in the browser redirect, without the code exchange. It is designed for browser applications like AngularJS SPA which cannot keep a client secret, and it is the flow which most identity providers are supporting for SPA today. Keep in mind that the token is exposed to the browser, so use short lifetimes, don’t issue refresh tokens to the browser and be mindful of tokens in browser history or referrers.

Resource Owner Password Credentials grant

The client receives the user’s username and password and exchanges them for a token. This removes the password from some later calls but still gives the client the password, so it should be limited to highly trusted first-party scenarios and is not a good default for third-party clients.

Client Credentials grant

A service calls another service as itself, without a user. The client authenticates with its own credential and receives a token for its application permission. This is useful for a background worker which publishes article indexing work, but it does not represent an end-user.

Selecting the grant type

In order to choose the right grant, we should start from the client type and the trust boundary,

Client situationGrant to considerReason
Server-side web applicationAuthorization CodeUser signs in at the authorization server and the secret stays on the server
Trusted first-party applicationResource Owner Password CredentialsOnly when the trusted application already owns the credential flow
Service without a userClient CredentialsRepresents the service, not an end-user
Browser-only application (SPA)ImplicitCannot keep a secret, token is exposed to the browser, hence keep lifetime short

Do not select a grant because a sample is available in a blog. The business data, the client storage and the ability to keep a secret are deciding the option. Normally, Authorization Code is the healthier choice for a server-side web application, while Client Credentials is suitable for a trusted background service.

Without a clear token model, we are taking reactive actions after a client is failing or a token is being accepted by the wrong API. A proactive approach is defining roles, audiences and expiry before the integration is going live, then checking those values in logs and tests. In order to reduce this security blind spot and the ripple effect across clients, we should start with the trust boundary.

What is OpenID Connect?

OpenID Connect is an identity protocol built on OAuth 2.0 which lets a client verify the user’s authentication and receive standard identity claims.

In simple terms, OAuth gives permission to access an API, while OIDC answers “who signed in?”. The authorization server returns an id_token, normally a JWT, for the client to validate and use as identity information. The access token remains the credential for the resource server.

The following diagram depicts the difference between an access token sent to an API and an id_token consumed by the client,

OAuth 2.0 and OpenID Connect token responsibilities
OAuth 2.0 and OpenID Connect token responsibilities

OIDC commonly adds a openid scope and claims such as sub, iss, aud and exp. The client must validate the id_token issuer, audience, signature and lifetime. It should not send an id_token to an API as if it were an access token, because the audience and purpose can be different.

How should we choose?

Start from the client type, resource owner interaction and permission required. A server-side ASP.NET application can use Authorization Code, while a service without a user can use Client Credentials. Use OIDC when the client needs a standard sign-in identity, and use OAuth access tokens when an API permission is the requirement.

Must Do and Don’ts

  • Must Do: Send access tokens only to the resource server and validate its audience.
  • Must Do: Use HTTPS and keep the token lifetime limited to the risk.
  • Don’t: Treat OAuth access token as proof of human identity.
  • Don’t: Send an id_token to an API, because the API is expecting an access token.
  • Be mindful: A token is a capability, whoever is holding it can use it until it is expired or revoked.

For the ASP.NET Web API implementation, refer the How to secure ASP.NET Web API with OAuth 2.0 bearer tokens (OWIN)? article for detailed information. For Forms Authentication, Windows Authentication and JWT comparison, refer the upcoming What is Authentication and Authorization in ASP.NET? article for detailed information.

Wrap up

In this article, we have looked at OAuth roles, grant types, access tokens, the difference between authorization and authentication, and the purpose of OpenID Connect id_token. I really appreciate that you took the time to read this article. In the next article, I will walk you through Authentication and Authorization choices in ASP.NET.

Related Posts