OAuth2 has become the de facto in modern web application security. If you are a front end, back end or mobile developer, chances are you have had to consume or secure protected resources with OAuth2. As such, having a good understanding of OAuth2 is invaluable. When implementing or using OAuth2 in your application, you typically face with four different grant types. Knowing the differences between the four grant types and which one to use can be quiet confusing. In this blog post series, I go over the different grant types by providing examples. This post is part of the series about OAuth2. In this post, I’ll cover the Authorization Code Grant and when it is appropriate to use it.
If you are not familiar with the jargons, this post may help.
With the authorization code grant, the flow looks something like this:
When your application redirects the user to the authorization endpoint to obtain authorization code, typically you provide an optional state parameter to protect against CSRF – Cross side request forgery. A cross side request forgery is basically a type of attack which an attacker treats the user into providing sensitive information to a server which contain the attacker’s resource. For instance, an attacker can send a request to the application’s redirect endpoint with an authorization or access token code along with a malicious url to treat the user into accessing that url with the attacker’s access token. That url may be a form where the user enters credentials which then get saved on the server which the attacker has access to.
To protect against this type of attack, your application should ensure to validate the redirection endpoint and check for the state parameter. It should match with the value in the user’s agent cookie. The application should send the state value when making a request to the authorization server and validate that it gets the same value back when the authentication server redirects the browser back to the redirection url and include the authorization code.
The Authorization Code Grant provides certain security benefits:
You should consider the Authorization Code Grant if you are developing a backend API or mobile application which has the capability to keep the access token safe. For comparison, the Authorization Code Grant would not be ideal for a front-end application which uses javascript as the application cannot keep the access token hidden from the resource owner anyways. Front-end application could benefit from using the Implicit Grant which provides better performance as there is no extra request to obtain the access token.
https://tools.ietf.org/html/rfc6749#section-1.3.1
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