# Authentication

It's advisable to know general concepts around OAuth2.0 and OIDC before you start doing anything with Authentication or Authorization or Amazon Cognito in general.&#x20;

Here is an excellent article by Kim Maida (former Auth0) on Authentication and Authorization concepts. She managed to explain all the related concepts in easy to follow explanation. Highly recommend giving it a quick check.

{% embed url="https://dev.to/kimmaida/authorization-and-authentication-for-everyone-27j3" %}

## Introduction

In our application, we will start with the basic Cognito setup. Users will be able to signup/sign-in from the Web page directly and access other AWS services using the Identity Pool role.&#x20;

Later we will integrate the Hosted UI and Social Sign-in using Google to showcase how we can leverage the OIDC and OAuth2 concepts.

## Basic Concepts

* OpenID Connect
* OAuth2
* JWT
* SAML

## OpenID Connect

OIDC is an identity layer for authenticating users with an authorization server. OIDC declares a _fixed format_ for ID tokens, which is known as JSON Web Token or JWT (jot).

![](https://1642315733-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCam2LtfwWxgwwpbHcu%2F-MD3Zl4lEpkOSSb5GfLh%2F-MD4Fw_WTIWqnN4Vbanf%2Foidc-basic-flow.png?alt=media\&token=1ea27fba-9ec2-4e86-a223-5f6f53f97c5b)

## OAuth 2.0

OAuth 2, a successor to OAuth 1 is an Authorization protocol. OAuth2 is meant for delegated authorization, for example:

* Granting access to use Tweet APIs on behalf of the Twitter user.&#x20;
* LinkedIn is asking to link Google so that he can show you how many of your contacts are already using LinkedIn and whom you can invite.

OAuth2 returns Access Token that can be used to access the target APIs and Refresh Tokens that can be used to generate the new Access Tokens without the user interaction.&#x20;

![](https://1642315733-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MCam2LtfwWxgwwpbHcu%2F-MD3Zl4lEpkOSSb5GfLh%2F-MD4FsfCCxgVuqLWzBII%2Foauth_overview.png?alt=media\&token=1dae0502-f285-4210-b3f6-b41c083b3c92)

## JWT or JSON Web Tokens

JWT or jot tokens are composed of three URL-safe string segments concatenated with periods.

* Header segment
* Payload segment
* Crypto segment

The header segment is a JSON object containing a signing algorithm and token type.

```
{
  "alg": "RS256",
  "typ": "JWT"
}
```

The payload segment contains the JSON object with data [_claims_](https://dev.to/kimmaida/authorization-and-authentication-for-everyone-27j3?utm_source=digest_mailer\&utm_medium=email\&utm_campaign=digest_email#claims), which are statements about the user and the authentication event.

```
{
  "sub": "112233",
  "name": "Dhaval Nagar",
  "admin": true,
  "iat": 1516239022
}
```

The final segment is the **crypto segment** or **signature**. JWTs are signed so they can't be modified in transit. When an authorization server issues a token, it signs it using a key.





