In this post, I’ll discuss the Resource Owner Password Credentials (ROPC) grant and when you should use it.
In a ROPC flow, the user gives the credentials directly to the client application, usually by mean of a login form over which the client application has complete control. In this flow, the client application does not redirect the user to an authorization server for authentication. However, the client application submits a request to the authorization server, passing over the user’s credentials to obtain an access token on behalf of the user. If the client is a confidential client or has been provided a secret key, the client also needs to authenticate against the authorization server using its client id and secret when requesting a token.
As an example, let’s look at a fictitious online book store -NumberOneBookStore which sells books online and provides personal recommendations based on purchase history. The company provides a web app and a RESTful API for accessing users’ data. To use the services, a user needs to register an account and authenticate against the company’s own directory. The engineering team has decided to implement the OAUTH ROPC grant flow for protecting the API. Below is the high level flow of a user who visits the company’s website to purchase books:
According to the spec,
“The resource owner password credentials grant type is suitable in cases where the resource owner has a trust relationship with the client, such as the device operating system or a highly privileged application”.
In the example, the user is okay with giving the web app the credentials because the user trusts the app as it is from the company of which the user uses the services. Furthermore, the web app is a highly privileged application as the authorization server only allows the ROPC grant type requests from the NumberOneBookStore web app. It would be a serious security risk if for example, the authorization server issued an access token to a 3rd party application which took the user’s credentials for NumberOneBookStore and submitted a ROPC grant type request to the authorization server. In that case, the 3rd party application would be impersonating the user. That is why it is critical that the authorization server validates the client’s credentials to ensure it only issues access tokens to trusted applications.Another use case of the ROPC grant type, according to the spec, is
“to migrate existing clients using direct authentication schemes such as HTTP Basic or Digest authentication to OAuth by converting the stored credentials to an access token.”
In the example, the NumberOneBookStore team could save some resources using the HTTP Basic authentication as it does not require an authorization server. However, using an access token offers several OAuth features including scope, token expiry time, and refresh token. For example, by using the flow, the NumberOneBookStore team achieve these benefits:
Lastly, according to the spec,
“The authorization server should take special care when enabling this grant type and only allow it when other flows are not viable.”
In the example, the NumberOneBookStore team could have implemented the Implicit Grant type which would redirect the user to the authorization server’s login endpoint. However, using the Implicit Grant flow mean the user has to navigate back and forth between the web app and the authorization server. For the example, I personally feel the user has the better experience when using the ROPC flow.
Hopefully, you now have a better understanding of the ROPC grant type and when to use it.
Using MSAL angular to authenticate a user against azure ADB2C via authorization code flow with Proof Key for Code Exchange.
Azure AD authentication in angular using MSAL angular v2 library
Common frameworks, libraries and design patterns I use
Authenticate against azure ad using certificate in a client credentials flow
Migrating from Microsoft.AspNetCore.Authentication.AzureAD to Microsoft Identity Web authentication library to integrate with Azure AD.
Integrate Azure AD B2C reset password user flow in angular using oidc-client-js.
Integrate Azure AD B2C profile editing user flow in angular using oidc-client-js.
Using OAuth2 Client Credentials grant type in Azure ADB2C