AccessToken Vs ID Token Vs Refresh Token - What? Why?When? (2024)

Introduction

This article demonstrates different types of tokens in OpenID Connect. At the end of this article, you will have a clear understanding of the below points,

  1. About JSON Web Tokens (JWT)
  2. What is an Access Token?
  3. Example of Access Token
  4. Why do we need an Access Token?
  5. What is an ID Token?
  6. Example of ID Token
  7. Why do we need an ID Token?
  8. What is a Refresh Token?
  9. Example of Refresh Token
  10. Why do we need a Refresh Token?

Related reads

  • OAuth2.0 And OpenID Connect (OIDC) Core Concepts - What? Why? How?
  • Understanding Workflow Of OAuth2.0 Authorization Grant Types

About JSON Web Tokens (JWT)

JWT i.e. JSON Web Tokens, are an important piece in ensuring trust and security in your application. JWT allows claims such as user data to be represented in a secure manner.

A JWT is represented as a sequence of base64url encoded values that are separated by a dot character. Its ideal format is like “Header.Payload.Signature”, where the header keeps metadata for the token. The payload is basically the claims of the entity (typically the user) and a signature for the signed token.

The Signed token is generated by combining the encoded JWT header and Payload and it is signed by using an encryption algorithm like HMAC SHA–256. The signature private key is always held by the server so it will be able to verify existing tokens as well as sign new tokens.

JWT could be used as an opaque identifier and could be inspected for additional information – such as identity attributes that it represents as claims.

Sample JWT token format could look like,

AccessToken Vs ID Token Vs Refresh Token - What? Why?When? (1)

What is an Access Token?

Access tokens are credentials used to access protected resources.

Access tokens are used as bearer tokens. A bearer token means that the bearer (who holds the access token) can access authorized resources without further identification.

Because of this, it is important that bearer tokens be protected.

These tokens usually have a short lifespan for security purposes. When it expires, the user must authenticate again to get a new access token limiting the exposure of the fact that it is a bearer token.

Access tokens must never be used for authentication. Access tokens cannot tell if the user has authenticated. The only user information the access token processes is the user id, located in sub-claims.

The application receives an access token after a user successfully authenticates and authorizes access. It is usually in JWT format but does not have to be.

The application should treat access tokens as opaque strings since they are meant for APIs. Your application should not attempt to decode them or expect to receive tokens in a

particular format.

This token does not contain any information about the user itself besides their ID (“sub”). It only contains authorization information about which actions the application is allowed to perform at the API (“scope”). This is what makes it useful for securing an API, but not for authenticating a user.

An access token is put in the Authorization header of your request, it usually looks like Bearer “access_token” that the API you are calling can verify and grant you access.

Example of Access Token

Here is the sample response from the token endpoint! The response includes the ID token and access token. Your application can use the access token to make API requests on behalf of the user.

{ "token_type": "Bearer", "expires_in": 86400, "access_token": "vCwWSQiaYhMHN2IbnEijtDWJ-BpiHbPohI6tOVrkrUrL2MqlF05K84MhBzvoC6iShEdUXl7t", "scope": "openid profile email photo", "id_token": "eyJraWQiOiJzMTZ0cVNtODhwREo4VGZCXzdrSEtQUkFQRjg1d1VEVGxteW85SUxUZTdzIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJuYW1lIjoiRnJpZ2h0ZW5lZCBIZXJyaW5nIiwiZW1haWwiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJpc3MiOiJodHRwczovL3BrLWRlbW8ub2t0YS5jb20vb2F1dGgyL2RlZmF1bHQiLCJhdWQiOiJpVnUwQUMyOFFHLXNwc1psWk1janFRY2EiLCJpYXQiOjE2MDQyMTY1MzgsImV4cCI6MTYwNjgwODUzOCwiYW1yIjpbInB3ZCJdfQ.ZoPvZPaomdOnnz2GFRGbgaW7PPWIMFDqSBp0gbN4An4a9F-Bc-4_T9EBGV8aGetyjZYAON0gjNV0p0NGFiwettePWKuxBzusuGCEd9iXWWUO9-WTF5e2AGr3_jkg34dbxfiFXy3KgH7m0czm809cMaiZ_ofLYgJHVD8lqMQoWifhoNhpjPqa19Svc3nCHzSYHUgTXQWvA56NmQvyVPh_OM7GMpc6zHopmihJqt3eREof8N-bOd7FL39jeam2-k1TFSDogyJE513aC0OssRADr_TWvtL8xoaPkXM_7bXYs9_7erXmzF9la0hvmOuasieetpLhOvFeoiOJWCU9xhxj4Q"}

AccessToken Vs ID Token Vs Refresh Token - What? Why?When? (2)

Why do we need an Access Token?

Access tokens are used to inform an API that the bearer of the token has been authorized to access the API and perform a predetermined set of actions specified by the scope. It is used to authorize API access.

What is an ID Token?

OpenID Connect always issues ID tokens along with access tokens to provide compatibility with OAuth and match the general tendency for authorizing identity.

ID token carries personal information about end-users that authenticate on an OpenID Connect flow. In addition, this security token contains claims data about the user as saved with the authentication server.

The ID token represents JWT.

For example, if there is an app that uses Google to log in users and to sync their calendars, google sends an ID token to the app that includes information about the user

This token authenticates the user to the application. The audience of this token is set to the application’s identifiers, which means that a specific application should consume the token.

Identity token payload contains “auth_time” (when the end user actually authenticated), “iss” (who has issued the token), “aud” (Intended audience), “sub” (unique identifier of the user), “idp” etc.

Example of ID Token (JWT)

Here is the sample response from the token endpoint! The response includes the ID token and access token.

{ "token_type": "Bearer", "expires_in": 86400, "access_token": "vCwWSQiaYhMHN2IbnEijtDWJ-BpiHbPohI6tOVrkrUrL2MqlF05K84MhBzvoC6iShEdUXl7t", "scope": "openid profile email photo", "id_token": "eyJraWQiOiJzMTZ0cVNtODhwREo4VGZCXzdrSEtQUkFQRjg1d1VEVGxteW85SUxUZTdzIiwiYWxnIjoiUlMyNTYifQ.eyJzdWIiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJuYW1lIjoiRnJpZ2h0ZW5lZCBIZXJyaW5nIiwiZW1haWwiOiJmcmlnaHRlbmVkLWhlcnJpbmdAZXhhbXBsZS5jb20iLCJpc3MiOiJodHRwczovL3BrLWRlbW8ub2t0YS5jb20vb2F1dGgyL2RlZmF1bHQiLCJhdWQiOiJpVnUwQUMyOFFHLXNwc1psWk1janFRY2EiLCJpYXQiOjE2MDQyMTY1MzgsImV4cCI6MTYwNjgwODUzOCwiYW1yIjpbInB3ZCJdfQ.ZoPvZPaomdOnnz2GFRGbgaW7PPWIMFDqSBp0gbN4An4a9F-Bc-4_T9EBGV8aGetyjZYAON0gjNV0p0NGFiwettePWKuxBzusuGCEd9iXWWUO9-WTF5e2AGr3_jkg34dbxfiFXy3KgH7m0czm809cMaiZ_ofLYgJHVD8lqMQoWifhoNhpjPqa19Svc3nCHzSYHUgTXQWvA56NmQvyVPh_OM7GMpc6zHopmihJqt3eREof8N-bOd7FL39jeam2-k1TFSDogyJE513aC0OssRADr_TWvtL8xoaPkXM_7bXYs9_7erXmzF9la0hvmOuasieetpLhOvFeoiOJWCU9xhxj4Q"}

Why do we need an ID Token?

The ID token is used to retrieve the user’s basic profile information like name, DOB, email, and phone, which is present in the authentication server. ID token should not be used to gain access to an API.

What is a Refresh token?

  • This token is a long-lived token compared to the access token and is used to request a new access token in cases where it has expired.
  • It can be considered as credentials used to obtain access tokens.
  • It's allowed for long-lived access and is highly confidential.
  • Refresh tokens can be used for grant types – authorization code and password credentials grant.
  • Refresh tokens are intended for use only with authorization servers and are never sent to resource servers.
  • You will receive this in an encoded format only that cannot be decoded. An example could be 494c427ace9e04dea03c7234cea96c5ca53e0ce4ea95147e961fd9ebcf8feb84

Example of Refresh Token

Here is the sample response from the token endpoint! The response includes the access token along with the refresh token.

{ "access_token": "ya29.a0AfH6SMARVjPq6G2y_P3hn3mbDdnRVrTGwO1ZkTXvUHye9wcpAPyiRKilq6Wh20TRbVx0nA1Nn8z1cpk_Jjs6qRwDvbOFNZhpA8e2GxDRcJ_PlrhlMnauvxktSDkjUyG-NWwuckHpiaOfr_uITriM0aS2t3HbGIKQJiU", "scope": "https://mail.google.com/", "token_type": "Bearer", "expires_in": 3599, "refresh_token": "1//0419Pth1mYFyBCgYIARAAGAQSNwF-L9IrcV8zK4wHDznJqUbeXrcEoE2O-Tmz7ryNpztTrLOiYOvs-0z4hxddGBpcKc0pEzLcWFI"}

Why do we need a Refresh token?

As the access token has defined lifetimes, there could be a possibility that the current access token becomes invalid or expires. This is the token used to request new access tokens without user interaction.

Requesting an access token using a refresh token

While requesting a refresh token, the scope should be set as offline_access to the scope parameter.

Method: POSTContent-Type: application/x-www-form-urlencodedAuthorization: Bearer "access token"URL: https://authorization-server.com/tokenQuery Parameters: grant_type=refresh_token refresh_token=<<refresh_token>> scope=offline_access

The application receives an access token with a redirect URI.

{ "token_type": "Bearer", "expires_in": 86400, "access_token": "QKY08tqDO8aeNebZbfgUFs1PH-cjerK2WBvE9FZpYGgZHnS_nLfhKYTECMBmPF_chz5GipOA", "scope": "photo offline_access", "refresh_token": "8V2dMpzRqB5cQDvoTb6X_Msl"}

Summary

Below is a quick reference of all the tokens at a glance.

  • Access tokens are used in token-based authentication to gain access to resources by using them as bearer tokens.
  • Refresh token is a long-lived special kind of token used to obtain a renewed access token.
  • ID token carries identity information encoded in the token itself, which must be a JWT. It must not contain any authorization information or any audience information — it is merely an identifier for the user.

Copyright Anupam Maiti. All rights reserved. No part of this article, including text, may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the copyright owner.

AccessToken Vs ID Token Vs Refresh Token - What? Why?When? (2024)

FAQs

AccessToken Vs ID Token Vs Refresh Token - What? Why?When? ›

Access tokens are used in token-based authentication to gain access to resources by using them as bearer tokens. Refresh token is a long-lived special kind of token used to obtain a renewed access token. ID token carries identity information encoded in the token itself, which must be a JWT.

What is the difference between ID token and access token and refresh token? ›

Access tokens are what the OAuth client uses to make requests to an API. The access token is meant to be read and validated by the API. An ID token contains information about what happened when a user authenticated, and is intended to be read by the OAuth client.

Should you use access token or ID token? ›

They can both be encoded as JWT, but the content and purpose are also different. An ID token contains the identity information about the authenticated users, and it is intended to be consumed by the front-end application. On the other hand, an access token represents a ticket with permission to consume an API.

Why separate access token and refresh token? ›

Refresh tokens are designed to be long-lived but must be revoked at need. Access tokens are designed to be short-lived, because they can't be revoked (in most cases).

What is the difference between ID token and access token in Azure AD? ›

ID tokens shouldn't be used for authorization purposes. Access tokens are used for authorization. The claims provided by ID tokens can be used for UX inside your application, as keys in a database, and providing access to the client application.

Can I use refresh token instead of access token? ›

Access tokens are temporary credentials that grant access to a protected resource, while refresh tokens are used to obtain new access tokens once the current ones expire.

What is the lifetime of access token vs refresh token? ›

Refresh tokens have a longer lifetime than access tokens. The default lifetime for the refresh tokens is 24 hours for single page apps and 90 days for all other scenarios.

What is the difference between ID token and access token API gateway? ›

The identity token is used to authorize API calls based on identity claims of the signed-in user. The access token is used to authorize API calls based on the custom scopes of specified access-protected resources. For more information, see Using Tokens with User Pools and Resource Server and Custom Scopes.

What is the purpose of ID token? ›

ID tokens are used in token-based authentication to cache user profile information and provide it to a client application, thereby providing better performance and experience.

What is the benefit of access token? ›

Tokens Offer Robust Security

Since tokens like JWT are stateless, only a secret key can validate it when received at a server-side application, which was used to create it. Hence they're considered the best and the most secure way of offering authentication.

Can a refresh token be used multiple times? ›

Every time you exchange the code for an access token or use the refresh token to get a new access token, the access token will be different. However, the refresh token will be always the same.

Where should I store access token and refresh token? ›

If your app needs to call APIs on behalf of the user, access tokens and (optionally) refresh tokens are needed. These can be stored server-side or in a session cookie. The cookie needs to be encrypted and have a maximum size of 4 KB.

What happens if a refresh token expires? ›

Refresh Token Rotation issues a refresh token that expires after a preset lifetime. After expiration, the user gets a new refresh token in the same family, or refresh tokens that share a family ID, or a new access token/refresh token pair.

What is the difference between access token ID token and refresh token? ›

ID Tokens, Access Tokens, and Refresh Tokens together enhance both security and user experiences in OIDC-enabled applications. While the ID Token confirms the user's identity, the Access Token governs access to server-protected resources, and the Refresh Token circumvents repeated manual user re-authentication.

Is session ID and access token same? ›

Imagine entering a secure facility: with sessions, it's like getting a stamp on entry, verified each time you move. Tokens, on the other hand, are like badges granting access at every turn. Sessions store data server-side, while tokens keep it local, offering flexibility and scalability.

How to refresh an ID token? ›

To refresh your access token and an ID token, you send a token request with a grant_type of refresh_token . Be sure to include the openid scope when you want to refresh the ID token. If the refresh token is valid, then you get back a new access token, a new ID token, and the refresh token.

What is the difference between Okta ID_token and access token? ›

Access tokens vs ID tokens

As mentioned earlier, it's important that the resource server (your server-side app) accepts only the access token from a client. Access tokens are intended for authorizing access to a resource. ID tokens, on the other hand, are intended for authentication.

What is the difference between Google ID and access token? ›

Unlike access tokens, which are opaque objects that cannot be inspected by the application, ID tokens are meant to be inspected and used by the application.

What is the difference between access token and ID token AWS? ›

The ID token contains claims about their identity, like their username, family name, and email address. The access token contains claims like scope that the authenticated user can use to access third-party APIs, Amazon Cognito user self-service API operations, and the UserInfo endpoint.

What is the difference between firebase refresh token and ID token? ›

Firebase ID tokens are short lived and last for an hour; the refresh token can be used to retrieve new ID tokens. Refresh tokens expire only when one of the following occurs: The user is deleted. The user is disabled.

Top Articles
Companies Need to Focus More on Cash Flow and Return on Capital - SPONSOR CONTENT FROM EY
International Travel Insurance | Travelex Insurance
Sandrail Options and Accessories
What to Do For Dog Upset Stomach
Craigslist Motorcycles Jacksonville Florida
Www.metaquest/Device Code
Erskine Plus Portal
Horned Stone Skull Cozy Grove
Campaign Homecoming Queen Posters
Lost Pizza Nutrition
Missing 2023 Showtimes Near Landmark Cinemas Peoria
2021 Lexus IS for sale - Richardson, TX - craigslist
Persona 4 Golden Taotie Fusion Calculator
Turning the System On or Off
Stihl Km 131 R Parts Diagram
Uc Santa Cruz Events
Nashville Predators Wiki
Hanger Clinic/Billpay
Lcwc 911 Live Incident List Live Status
Hobby Stores Near Me Now
Rochester Ny Missed Connections
Gen 50 Kjv
Nottingham Forest News Now
Lbrands Login Aces
Busch Gardens Wait Times
Mawal Gameroom Download
Craig Woolard Net Worth
Manuel Pihakis Obituary
Att U Verse Outage Map
Mp4Mania.net1
CVS Near Me | Somersworth, NH
3400 Grams In Pounds
Banana Republic Rewards Login
Marcus Roberts 1040 Answers
159R Bus Schedule Pdf
Htb Forums
R/Moissanite
All-New Webkinz FAQ | WKN: Webkinz Newz
Smite Builds Season 9
Random Animal Hybrid Generator Wheel
Hk Jockey Club Result
All Weapon Perks and Status Effects - Conan Exiles | Game...
Rise Meadville Reviews
The Sports Academy - 101 Glenwest Drive, Glen Carbon, Illinois 62034 - Guide
Lesly Center Tiraj Rapid
Michaelangelo's Monkey Junction
303-615-0055
Sunset On November 5 2023
Intuitive Astrology with Molly McCord
Latest Posts
Article information

Author: Terence Hammes MD

Last Updated:

Views: 5629

Rating: 4.9 / 5 (69 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Terence Hammes MD

Birthday: 1992-04-11

Address: Suite 408 9446 Mercy Mews, West Roxie, CT 04904

Phone: +50312511349175

Job: Product Consulting Liaison

Hobby: Jogging, Motor sports, Nordic skating, Jigsaw puzzles, Bird watching, Nordic skating, Sculpting

Introduction: My name is Terence Hammes MD, I am a inexpensive, energetic, jolly, faithful, cheerful, proud, rich person who loves writing and wants to share my knowledge and understanding with you.