What are OAuth 2.0 roles, grant types, and authorization flows, and how do they work? Check out these basic concepts and start coding your own application!
OAuth 2.0 authorization framework is a protocol created to provide simple authorization flows for web, mobile, and desktop applications. It replaces the obsolete OAuth 1.0 protocol specified by RFC 5849.
In summary, OAuth 2.0 allows a user to grant a third-party website or application access to protected resources without revealing their long-term credentials or even their identity, delegating user authentication to the service hosting the user's account, such as Google, Facebook, or AWS Cognito.
This article aims to provide a basic overview of OAuth 2.0 roles, grant types, and authorization flows. At the end of this snippet, we will start coding an Angular 11 single-page application that uses Authorization Code Flow with PKCE, AWS Cognito, and AWS Amplify, with Spring Boot as the resource server.
First, let’s clarify two very important concepts:
Let’s consider a practical analogy for OAuth 2.0 protocol: Imagine you're traveling and you go to check in at a hotel. The hotel’s receptionist will register you as a hotel guest; at this moment, you are authenticated, but you still do not have access to a room. Once the employee provides you with a card to your room door, you possess the "access token." In other words, you are authorized to access the hotel's facilities, your room, and the floor on which it is located (this would be your token scope), until the key is revoked (or the token expires). This is a high level analogy for how OAuth 2.0 works.
The OAuth 2.0 protocol defines a few roles:
The steps below summarize the sequence of events to acquire access and consume a resource using the OAuth 2 protocol:
OAuth 2.0 grant type is, to put it briefly, a way the client application receives the access token. Authorization grants can be differentiated based on the client type and their trust level:
This flow uses a secret key (called code verifier) created by the client application and later verified by the authorization server. The client application also creates a way to transform the respective code verifier (this is called a code challenge) and sends it together with the verifier over HTTP in order to receive the authorization code. If an attacker tries to intercept the request and steal the authentication data, it will have access only to the authorization code and not be able to exchange it for a token, because the attacker must also possess the code verifier.
Authorization code with PCKE flow steps are better covered below:
GET https://mydomain.auth.us-east-1.amazoncognito.com/oauth2/authorize?
response_type=code&
client_id=ad398u21ijw3s9w3939&
redirect_uri=https://YOUR_APP/redirect_uri&
state=STATE&
scope=aws.cognito.signin.user.admin&
code_challenge_method=S256&
code_challenge=CODE_CHALLENGE
In the next section, we'll present an example of how to create a solution that uses OAuth2 and Authorization Code with PKCE flow.
The technical stack we will be working is:
Initial requirements include:
First, we will create our client application with Angular 11:
1. Open a terminal window and type the command below to create a new Angular application:
> ng new my-oauth2-app2. After serving it, make sure it is working by accessing http://localhost:4200/
> cd my-oauth2-app
> ng serve
npm install -g @aws-amplify/cli
i. Configure AWS Amplify running the following command:amplify configure
ii. Select your AWS region;
iii. Select your AWS profile name;
iv. When requested, add AdministratorAccess to your user (do not forget to take notes for your newly created accessKeyId and secretAccessKey);
v. Go back to the terminal window and complete the next configuration steps;
vi. Congratulations, we just setup AWS Amplify!
4. Go back to your Angular code and change src/polyfills.ts file:(window as any).global = window;5. Now it is time to install Amplify libraries in our Angular App:
(window as any).process = {
env: { DEBUG: undefined },
};
npm install --save aws-amplify @aws-amplify/ui-angular
6. Let’s check to see if there are errors with our application: npm start
b. Now access http://localhost:4200 and check that everything is fine;
7. Go to our Angular application source code:
a. Open src/app/app.module.ts file and add Amplify UI Angular module:
import { AmplifyUIAngularModule } from '@aws-amplify/ui-angular';
...imports: [8. Initializing AWS Amplify:
BrowserModule,
AppRoutingModule,
/* configure app with AmplifyUIAngularModule */
AmplifyUIAngularModule
],
...
amplify init
b. The previous command automatically creates a top level directory called “amplify” that stores all of our app capabilities, such as authentication, storage, authorization rules, etc.;
c. Fill in all required settings:
i. Enter the name for your project or keep the suggested default name;
ii. Enter environment name - in this case “dev” for now;
iii. Choose your default editor;
iv. Choose the type of app you are creating - JavaScript;
v. Select JavaScript framework type - angular;
vi. Select the source directory path: src
vii. Select the distribution directory path: dist/my-ouath2-app
viii. Build the command: npm run-script build
ix. Start the command: ng serve
d. Amazon profile: choose the same one created when we ran the “amplify configure” command;
9. Now, it’s time to use Amplify and configure the application’s authentication process with AWS Cognito:amplify add auth
ii. When asked:
1. Do you want to use the default authentication and security configuration? Default configuration
2. How do you want users to be able to sign in? Email
3. Do you want to configure advanced settings? No, I am done.
iii. Now deploy your authentication service changes to AWS:
1. Go to the terminal again and type:
amplify push
2. When asked, confirm the environment choice;amplify console
10. Finally, let’s add login UI to our application; AWS Amplify has an authentication UI component we can use that will provide the entire authentication flow and, consequently, it saves us some time:
11. Add the amplify-authenticator component to the top of src/app/app.component.html file:
<amplify-authenticator>
<div>
OAuth 2 Sample App
<amplify-sign-out></amplify-sign-out>
</div>
</amplify-authenticator>
12. Finally, serve the application and enjoy it. AWS Amplify has a lot of customization options for its authenticator component. You also might want to use your own login screen, and this option is available too; feel free to take a look at their documentation for more details.
In this article, we covered key concepts about OAuth 2.0 and Authorization Code with PCKE grant flow. We also started creating a full stack application with Angular 11, AWS Cognito, and Amplify.
Feel free to share comments and questions about your experience with this process below!
RFC-6749 documentation. Accessed 1.18.2021
OAuth 2.0 .Net. Accessed 1.18.2021
Digital Ocean Community. Accessed 1.18.2021
Google Developers. Accessed 1.18.2021
OpenId .Net. Accessed 1.18.2021
Amazon Cognito. Accessed 1.18.2021
Angular CLI. Accessed 1.18.2021
Amazon Amplify. Accessed 1.18.2021