Introduction to tokens

Published Apr 12 2021 07:44 AM 8,136 Views

Introduction

 

This article is written to explain OAuth 2.0 and OpenID Connect bearer tokens (JWT) and concepts relating to Microsoft Azure AD and related technologies, but can most likely be applied elsewhere too.

Tokens are everywhere on the Internet. Even if you don't realise it, you may have just used one on the way to read this article! You may ask "What is a token and what do I need it for?" and my answer would be "Which token?". You see, there are different types of tokens all with their use cases.

This article is here to break down the different types, how and when they are used and what a token is comprised of. Hopefully this article will help you better understand tokens and apply this to getting started with other technologies such as Microsoft Graph.

 

What is a token?

A token is a list claims of something. A real world analogy could be a receipt from a purchase of goods you made. Let's say you needed to prove that you bought to goods to return something. The receipt would most likely contain the date of purchase, the price you paid and most importantly, the goods purchased. Essentially, you are making a claim of ownership using the receipt. A token works in a similar way - it is a way to claim "I am me" or "I am allowed to do this".

 

Token types

As explained in the introduction, there are many different types of tokens. This article will concentrate on the 3 most commonly used tokens in Azure AD:

 

ID tokens

ID tokens are used by a client to provide a user's identity. This is referred to as authentication. One example is a user entering their credentials in to a client and being given an ID token on sign-in success. By then having an ID token, the client then can access resources as the signed-in user without prompting the user.

 

Important Note: ID tokens should only be used for identity purposes and NOT be used to grant access to additional resources. This will be covered next

Access tokens

Access tokens are used by a client to obtain access to additional resources e.g. a protected API such as Microsoft Graph. This is referred to as authorization. With an access token, you can have a list of permissions (scopes) granted to you against a resource. You can then use these permissions in the access token to access protected resources that you would not be able to access with an ID token.

If you are still confused of the difference between authentication and authorization, @LuiseFreese sums it up perfectly:

 

01ed60e47ba84e66b302a6ef0e9035d2.png

 

The term "auth token" is widely used can become misleading as it could be interpreted as an authentication (ID) or authorization (access) token. It is important to distinguish the difference between the two and try to avoid using the term "auth token".

Refresh tokens

Refresh tokens can be issued with ID and access tokens. Tokens have a fixed lifetime and expire, but with a refresh token a client can obtain a token without prompting the user for input. A basic example could be you are signed in to a client and it is using an access token with Microsoft Graph. On expiry of the access token, instead of interrupting the user, a new access token is silently obtained before the old access token expires using the refresh token from the original access token.

 

What makes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ImX98Egypfrlbm0IBL5spFzL6JDZIRRJOu8vecJvj1mq-IUhGt0MacxX8jdxYLP-KUu2d9MbNKpCKJuZ7p8gwTL5B7NlUdh_dmSviPWrw

 

 

Makes sense, right? Of course not. Azure AD ID and access tokens are also referred to as "JWTs" or JSON Web Tokens. This means that the token is formatted as a JSON object and then "base64Url" encoded and signed and with a bit of extra security (we'll cover this soon), the end result is what we have above.

 

For an introduction in to JSON, I highly recommend Bob German's article here.

Decoding a token

Taking the sample above and putting in in to jwt.ms, it is possible to decode from "base64url" back to human-readable JSON.

 

e738e6ef6d6f41ab9e811c7d4e8d5371.png

 

You will now see a JWT is made up of 3 parts:

  • Header (red text)
  • Payload (blue text)
  • Signature (green text)

In the next two sections, we'll cover off each part.

 

Payload

A payload or body is the content of the token where the claims are stored. Most tokens contain standard claims such as:

  • "iss" - Identity of the service that issued the token e.g. https://login.microsoftonline.com/9122040d-6c67-4c5b-b112-36a304b66dad/v2.0
  • "sub" - The subject of the token e.g. the user
  • "aud" - The audience of the token, who the token is intended for. This is usually the client and not the user
  • "exp" - Expiry date of token in seconds since the Unix epoch
  • "iat" - Time token was issued (Unix epoch seconds)
  • "nbf" - Time token is valid from (Unix epoch seconds)

In addition to standard claims, there are custom claims in Azure AD tokens such as:

  • "name" - Name of the subject
  • "oid" - Azure AD object ID of the subject
  • "tid" - Azure AD tenant ID of the subject

One great feature of jwt.ms is that you can view detailed descriptions of standard and Azure AD claims.

Validating a token

You may be thinking: I just decoded a token on a webpage - how is that secure? JWTs are typically "signed" (not encrypted) with an algorithm and private key by the issuer of the token. This doesn't stop any of the payload or header from being decoded as the aim of a token isn't to hide information, but provide validity to it.

 

Never store secret information in a payload of a token as it can be easily decoded.

It is up to the recipient e.g. your client to validate the token to ensure it can be trusted.

There is a bit more nuance to the process than outlined here, but here is the summary on how to validate a token. Within the header part of the JWT, it contains the information on how the JWT was "signed" (what algorithm and private/public key pair was used). With this information it is possible for the client (if it has access to the public key) to validate the signature part of the JWT.

In addition to the signature, validation should also be taken place on the payload such as expiry time, audience and issuer.

 

Token usage

Now we are familiar with tokens. Let's cover how they could be used in a couple of scenarios:

 

OpenID Connect

OpenID Connect is a way to sign a user in to an application. It is used for authentication only and not authorization. In this example, the end-user (user-agent) requests access to an application. The application requires authentication, so the end-user is redirected to authenticate at the authorization sever (I know, confusing). If successful, an ID token is returned and then validated by the application. If it passes validation, access is granted to the application.

 

openidconnect.png

OAuth 2.0 auth code grant

Auth code grant flow is where a user is asked to sign-in at the authorization server (Azure AD). This time, however, instead of an ID token being returned, an authorization code is returned. A second request is then made to the authorization server with the authorization code, but this time an access token is returned. With the access token, a protected API can then be accessed.

 

authcodegrantflow.png

Wrap up

I hope you found this useful and understand more around what tokens are, the different types of tokens and how they are used.

1 Comment
Visitor

nice article, but there's a typo ... sever -> server

%3CLINGO-SUB%20id%3D%22lingo-sub-2267853%22%20slang%3D%22en-US%22%3EIntroduction%20to%20tokens%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2267853%22%20slang%3D%22en-US%22%3E%3CH1%20id%3D%22introduction%22%20id%3D%22toc-hId-1302376369%22%20id%3D%22toc-hId-1302376369%22%20id%3D%22toc-hId-1302376369%22%20id%3D%22toc-hId-1302376369%22%3EIntroduction%3C%2FH1%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EThis%20article%20is%20written%20to%20explain%20OAuth%202.0%20and%20OpenID%20Connect%20bearer%20tokens%20(JWT)%20and%20concepts%20relating%20to%20Microsoft%20Azure%20AD%20and%20related%20technologies%2C%20but%20can%20most%20likely%20be%20applied%20elsewhere%20too.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3ETokens%20are%20everywhere%20on%20the%20Internet.%20Even%20if%20you%20don't%20realise%20it%2C%20you%20may%20have%20just%20used%20one%20on%20the%20way%20to%20read%20this%20article!%20You%20may%20ask%20%3CEM%3E%22What%20is%20a%20token%20and%20what%20do%20I%20need%20it%20for%3F%22%3C%2FEM%3E%20and%20my%20answer%20would%20be%20%3CEM%3E%22Which%20token%3F%22%3C%2FEM%3E.%20You%20see%2C%20there%20are%20different%20types%20of%20tokens%20all%20with%20their%20use%20cases.%3C%2FP%3E%0A%3CP%3EThis%20article%20is%20here%20to%20break%20down%20the%20different%20types%2C%20how%20and%20when%20they%20are%20used%20and%20what%20a%20token%20is%20comprised%20of.%20Hopefully%20this%20article%20will%20help%20you%20better%20understand%20tokens%20and%20apply%20this%20to%20getting%20started%20with%20other%20technologies%20such%20as%20Microsoft%20Graph.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22what-is-a-token%22%20id%3D%22toc-hId--505078094%22%20id%3D%22toc-hId--505078094%22%20id%3D%22toc-hId--505078094%22%20id%3D%22toc-hId--505078094%22%3EWhat%20is%20a%20token%3F%3C%2FH1%3E%0A%3CP%3EA%20token%20is%20a%20list%20claims%20of%20%3CEM%3Esomething%3C%2FEM%3E.%20A%20real%20world%20analogy%20could%20be%20a%20receipt%20from%20a%20purchase%20of%20goods%20you%20made.%20Let's%20say%20you%20needed%20to%20prove%20that%20you%20bought%20to%20goods%20to%20return%20something.%20The%20receipt%20would%20most%20likely%20contain%20the%20date%20of%20purchase%2C%20the%20price%20you%20paid%20and%20most%20importantly%2C%20the%20goods%20purchased.%20Essentially%2C%20you%20are%20making%20a%20claim%20of%20ownership%20using%20the%20receipt.%20A%20token%20works%20in%20a%20similar%20way%20-%20it%20is%20a%20way%20to%20claim%20%3CEM%3E%22I%20am%20me%22%3C%2FEM%3E%20or%20%3CEM%3E%22I%20am%20allowed%20to%20do%20this%22%3C%2FEM%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22token-types%22%20id%3D%22toc-hId-1982434739%22%20id%3D%22toc-hId-1982434739%22%20id%3D%22toc-hId-1982434739%22%20id%3D%22toc-hId-1982434739%22%3EToken%20types%3C%2FH1%3E%0A%3CP%3EAs%20explained%20in%20the%20introduction%2C%20there%20are%20many%20different%20types%20of%20tokens.%20This%20article%20will%20concentrate%20on%20the%203%20most%20commonly%20used%20tokens%20in%20Azure%20AD%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22id-tokens%22%20id%3D%22toc-hId-876044854%22%20id%3D%22toc-hId-876044854%22%20id%3D%22toc-hId-876044854%22%20id%3D%22toc-hId-876044854%22%3EID%20tokens%3C%2FH3%3E%0A%3CP%3EID%20tokens%20are%20used%20by%20a%20client%20to%20provide%20a%20user's%20identity.%20This%20is%20referred%20to%20as%20%3CSTRONG%3Eauthentication%3C%2FSTRONG%3E.%20One%20example%20is%20a%20user%20entering%20their%20credentials%20in%20to%20a%20client%20and%20being%20given%20an%20ID%20token%20on%20sign-in%20success.%20By%20then%20having%20an%20ID%20token%2C%20the%20client%20then%20can%20access%20resources%20as%20the%20signed-in%20user%20without%20prompting%20the%20user.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3E%3CSTRONG%3EImportant%20Note%3A%3C%2FSTRONG%3E%20ID%20tokens%20should%20only%20be%20used%20for%20identity%20purposes%20and%20NOT%20be%20used%20to%20grant%20access%20to%20additional%20resources.%20This%20will%20be%20covered%20next%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH3%20id%3D%22access-tokens%22%20id%3D%22toc-hId--931409609%22%20id%3D%22toc-hId--931409609%22%20id%3D%22toc-hId--931409609%22%20id%3D%22toc-hId--931409609%22%3EAccess%20tokens%3C%2FH3%3E%0A%3CP%3EAccess%20tokens%20are%20used%20by%20a%20client%20to%20obtain%20access%20to%20%3CEM%3Eadditional%3C%2FEM%3E%20resources%20e.g.%20a%20protected%20API%20such%20as%20Microsoft%20Graph.%20This%20is%20referred%20to%20as%20%3CSTRONG%3Eauthorization%3C%2FSTRONG%3E.%20With%20an%20access%20token%2C%20you%20can%20have%20a%20list%20of%20permissions%20(scopes)%20granted%20to%20you%20against%20a%20resource.%20You%20can%20then%20use%20these%20permissions%20in%20the%20access%20token%20to%20access%20protected%20resources%20that%20you%20would%20not%20be%20able%20to%20access%20with%20an%20ID%20token.%3C%2FP%3E%0A%3CP%3EIf%20you%20are%20still%20confused%20of%20the%20difference%20between%20%3CSTRONG%3Eauthentication%3C%2FSTRONG%3E%20and%20%3CSTRONG%3Eauthorization%3C%2FSTRONG%3E%2C%20%3CA%20href%3D%22https%3A%2F%2Ftwitter.com%2FLuiseFreese%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3E%40LuiseFreese%3C%2FA%3E%20sums%20it%20up%20perfectly%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%2201ed60e47ba84e66b302a6ef0e9035d2.png%22%20style%3D%22width%3A%20602px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F271972i5C236DFE55530B38%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%2201ed60e47ba84e66b302a6ef0e9035d2.png%22%20alt%3D%2201ed60e47ba84e66b302a6ef0e9035d2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22file%3A%2F%2F%2F%2Fhome%2Flford%2Fcode%2Fpersonal%2Fdraft_posts%2FTokenIntroduction%2F01ed60e47ba84e66b302a6ef0e9035d2.png%22%20border%3D%220%22%20alt%3D%22%22%20%2F%3E%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EThe%20term%20%22auth%20token%22%20is%20widely%20used%20can%20become%20misleading%20as%20it%20could%20be%20interpreted%20as%20an%20%3CSTRONG%3Eauthentication%3C%2FSTRONG%3E%20(ID)%20or%20%3CSTRONG%3Eauthorization%3C%2FSTRONG%3E%20(access)%20token.%20It%20is%20important%20to%20distinguish%20the%20difference%20between%20the%20two%20and%20try%20to%20avoid%20using%20the%20term%20%22auth%20token%22.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH3%20id%3D%22refresh-tokens%22%20id%3D%22toc-hId-1556103224%22%20id%3D%22toc-hId-1556103224%22%20id%3D%22toc-hId-1556103224%22%20id%3D%22toc-hId-1556103224%22%3ERefresh%20tokens%3C%2FH3%3E%0A%3CP%3ERefresh%20tokens%20can%20be%20issued%20with%20ID%20and%20access%20tokens.%20Tokens%20have%20a%20fixed%20lifetime%20and%20expire%2C%20but%20with%20a%20refresh%20token%20a%20client%20can%20obtain%20a%20token%20without%20prompting%20the%20user%20for%20input.%20A%20basic%20example%20could%20be%20you%20are%20signed%20in%20to%20a%20client%20and%20it%20is%20using%20an%20access%20token%20with%20Microsoft%20Graph.%20On%20expiry%20of%20the%20access%20token%2C%20instead%20of%20interrupting%20the%20user%2C%20a%20new%20access%20token%20is%20silently%20obtained%20before%20the%20old%20access%20token%20expires%20using%20the%20refresh%20token%20from%20the%20original%20access%20token.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22what-makes-up-a-token%22%20id%3D%22toc-hId--952415817%22%20id%3D%22toc-hId--952415817%22%20id%3D%22toc-hId--952415817%22%20id%3D%22toc-hId--952415817%22%3EWhat%20makes%20up%20a%20token%3F%3C%2FH1%3E%0A%3CP%3ELet's%20look%20at%20a%20sample%20token%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EeyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImt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KUu2d9MbNKpCKJuZ7p8gwTL5B7NlUdh_dmSviPWrw%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EMakes%20sense%2C%20right%3F%20Of%20course%20not.%20Azure%20AD%20ID%20and%20access%20tokens%20are%20also%20referred%20to%20as%20%22JWTs%22%20or%20JSON%20Web%20Tokens.%20This%20means%20that%20the%20token%20is%20formatted%20as%20a%20JSON%20object%20and%20then%20%22base64Url%22%20encoded%20and%20signed%20and%20with%20a%20bit%20of%20extra%20security%20(we'll%20cover%20this%20soon)%2C%20the%20end%20result%20is%20what%20we%20have%20above.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EFor%20an%20introduction%20in%20to%20JSON%2C%20I%20highly%20recommend%20Bob%20German's%20article%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fmicrosoft-365-pnp-blog%2Fintroduction-to-json%2Fba-p%2F2049369%22%20target%3D%22_blank%22%3Ehere%3C%2FA%3E.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH3%20id%3D%22decoding-a-token%22%20id%3D%22toc-hId--2058805702%22%20id%3D%22toc-hId--2058805702%22%20id%3D%22toc-hId--2058805702%22%20id%3D%22toc-hId--2058805702%22%3EDecoding%20a%20token%3C%2FH3%3E%0A%3CP%3ETaking%20the%20sample%20above%20and%20putting%20in%20in%20to%20%3CA%20href%3D%22https%3A%2F%2Fjwt.ms%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ejwt.ms%3C%2FA%3E%2C%20it%20is%20possible%20to%20decode%20from%20%22base64url%22%20back%20to%20human-readable%20JSON.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22e738e6ef6d6f41ab9e811c7d4e8d5371.png%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F271976iDCF6321B4E43B55B%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22e738e6ef6d6f41ab9e811c7d4e8d5371.png%22%20alt%3D%22e738e6ef6d6f41ab9e811c7d4e8d5371.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CIMG%20src%3D%22file%3A%2F%2F%2F%2Fhome%2Flford%2Fcode%2Fpersonal%2Fdraft_posts%2FTokenIntroduction%2Fe738e6ef6d6f41ab9e811c7d4e8d5371.png%22%20border%3D%220%22%20alt%3D%22%22%20%2F%3E%3C%2FP%3E%0A%3CP%3EYou%20will%20now%20see%20a%20JWT%20is%20made%20up%20of%203%20parts%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EHeader%20(red%20text)%3C%2FLI%3E%0A%3CLI%3EPayload%20(blue%20text)%3C%2FLI%3E%0A%3CLI%3ESignature%20(green%20text)%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20the%20next%20two%20sections%2C%20we'll%20cover%20off%20each%20part.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22payload%22%20id%3D%22toc-hId-428707131%22%20id%3D%22toc-hId-428707131%22%20id%3D%22toc-hId-428707131%22%20id%3D%22toc-hId-428707131%22%3EPayload%3C%2FH3%3E%0A%3CP%3EA%20payload%20or%20body%20is%20the%20content%20of%20the%20token%20where%20the%20claims%20are%20stored.%20Most%20tokens%20contain%20standard%20claims%20such%20as%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%22iss%22%20-%20Identity%20of%20the%20service%20that%20issued%20the%20token%20e.g.%3CCODE%3E%3CA%20href%3D%22https%3A%2F%2Flogin.microsoftonline.com%2F9122040d-6c67-4c5b-b112-36a304b66dad%2Fv2.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Flogin.microsoftonline.com%2F9122040d-6c67-4c5b-b112-36a304b66dad%2Fv2.0%3C%2FA%3E%3C%2FCODE%3E%3C%2FLI%3E%0A%3CLI%3E%22sub%22%20-%20The%20subject%20of%20the%20token%20e.g.%20the%20user%3C%2FLI%3E%0A%3CLI%3E%22aud%22%20-%20The%20audience%20of%20the%20token%2C%20who%20the%20token%20is%20intended%20for.%20This%20is%20usually%20the%20client%20and%20not%20the%20user%3C%2FLI%3E%0A%3CLI%3E%22exp%22%20-%20Expiry%20date%20of%20token%20in%20seconds%20since%20the%20%3CA%20href%3D%22https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FUnix_time%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EUnix%20epoch%3C%2FA%3E%3C%2FLI%3E%0A%3CLI%3E%22iat%22%20-%20Time%20token%20was%20issued%20(Unix%20epoch%20seconds)%3C%2FLI%3E%0A%3CLI%3E%22nbf%22%20-%20Time%20token%20is%20valid%20from%20(Unix%20epoch%20seconds)%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20addition%20to%20standard%20claims%2C%20there%20are%20custom%20claims%20in%20Azure%20AD%20tokens%20such%20as%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%22name%22%20-%20Name%20of%20the%20subject%3C%2FLI%3E%0A%3CLI%3E%22oid%22%20-%20Azure%20AD%20object%20ID%20of%20the%20subject%3C%2FLI%3E%0A%3CLI%3E%22tid%22%20-%20Azure%20AD%20tenant%20ID%20of%20the%20subject%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EOne%20great%20feature%20of%20%3CA%20href%3D%22http%3A%2F%2Fjwt.ms%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ejwt.ms%3C%2FA%3E%20is%20that%20you%20can%20view%20detailed%20descriptions%20of%20standard%20and%20Azure%20AD%20claims.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CH3%20id%3D%22validating-a-token%22%20id%3D%22toc-hId--1378747332%22%20id%3D%22toc-hId--1378747332%22%20id%3D%22toc-hId--1378747332%22%20id%3D%22toc-hId--1378747332%22%3EValidating%20a%20token%3C%2FH3%3E%0A%3CP%3EYou%20may%20be%20thinking%3A%20I%20just%20decoded%20a%20token%20on%20a%20webpage%20-%20how%20is%20that%20secure%3F%20JWTs%20are%20typically%20%22signed%22%20(not%20encrypted)%20with%20an%20algorithm%20and%20private%20key%20by%20the%20issuer%20of%20the%20token.%20This%20doesn't%20stop%20any%20of%20the%20payload%20or%20header%20from%20being%20decoded%20as%20the%20aim%20of%20a%20token%20isn't%20to%20hide%20information%2C%20but%20provide%20validity%20to%20it.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3ENever%20store%20secret%20information%20in%20a%20payload%20of%20a%20token%20as%20it%20can%20be%20easily%20decoded.%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3EIt%20is%20up%20to%20the%20recipient%20e.g.%20your%20client%20to%20validate%20the%20token%20to%20ensure%20it%20can%20be%20trusted.%3C%2FP%3E%0A%3CP%3EThere%20is%20a%20bit%20more%20nuance%20to%20the%20process%20than%20outlined%20here%2C%20but%20here%20is%20the%20summary%20on%20how%20to%20validate%20a%20token.%20Within%20the%20header%20part%20of%20the%20JWT%2C%20it%20contains%20the%20information%20on%20how%20the%20JWT%20was%20%22signed%22%20(what%20algorithm%20and%20private%2Fpublic%20key%20pair%20was%20used).%20With%20this%20information%20it%20is%20possible%20for%20the%20client%20(if%20it%20has%20access%20to%20the%20public%20key)%20to%20validate%20the%20signature%20part%20of%20the%20JWT.%3C%2FP%3E%0A%3CP%3EIn%20addition%20to%20the%20signature%2C%20validation%20should%20also%20be%20taken%20place%20on%20the%20payload%20such%20as%20expiry%20time%2C%20audience%20and%20issuer.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH1%20id%3D%22token-usage%22%20id%3D%22toc-hId-155436584%22%20id%3D%22toc-hId-155436584%22%20id%3D%22toc-hId-155436584%22%20id%3D%22toc-hId-155436584%22%3EToken%20usage%3C%2FH1%3E%0A%3CP%3ENow%20we%20are%20familiar%20with%20tokens.%20Let's%20cover%20how%20they%20could%20be%20used%20in%20a%20couple%20of%20scenarios%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22openid-connect%22%20id%3D%22toc-hId--1522935160%22%20id%3D%22toc-hId--1522935160%22%20id%3D%22toc-hId--1522935160%22%20id%3D%22toc-hId--1522935160%22%3EOpenID%20Connect%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fopenid.net%2Fspecs%2Fopenid-connect-core-1_0.html%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EOpenID%20Connect%3C%2FA%3E%20is%20a%20way%20to%20sign%20a%20user%20in%20to%20an%20application.%20It%20is%20used%20for%20authentication%20only%20and%20not%20authorization.%20In%20this%20example%2C%20the%20end-user%20(user-agent)%20requests%20access%20to%20an%20application.%20The%20application%20requires%20authentication%2C%20so%20the%20end-user%20is%20redirected%20to%20authenticate%20at%20the%20authorization%20sever%20(I%20know%2C%20confusing).%20If%20successful%2C%20an%20ID%20token%20is%20returned%20and%20then%20validated%20by%20the%20application.%20If%20it%20passes%20validation%2C%20access%20is%20granted%20to%20the%20application.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22openidconnect.png%22%20style%3D%22width%3A%20662px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F271974i5D43CDF05BA48E56%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22openidconnect.png%22%20alt%3D%22openidconnect.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH2%20id%3D%22oauth-20-auth-code-grant%22%20id%3D%22toc-hId-964577673%22%20id%3D%22toc-hId-964577673%22%20id%3D%22toc-hId-964577673%22%20id%3D%22toc-hId-964577673%22%3EOAuth%202.0%20auth%20code%20grant%3C%2FH2%3E%0A%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftools.ietf.org%2Fhtml%2Frfc6749%23section-4.1%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAuth%20code%20grant%3C%2FA%3E%20flow%20is%20where%20a%20user%20is%20asked%20to%20sign-in%20at%20the%20authorization%20server%20(Azure%20AD).%20This%20time%2C%20however%2C%20instead%20of%20an%20ID%20token%20being%20returned%2C%20an%20authorization%20code%20is%20returned.%20A%20second%20request%20is%20then%20made%20to%20the%20authorization%20server%20with%20the%20authorization%20code%2C%20but%20this%20time%20an%20access%20token%20is%20returned.%20With%20the%20access%20token%2C%20a%20protected%20API%20can%20then%20be%20accessed.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22authcodegrantflow.png%22%20style%3D%22width%3A%20790px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F271975iB52A3CDF247368EB%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22authcodegrantflow.png%22%20alt%3D%22authcodegrantflow.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CH1%20id%3D%22wrap-up%22%20id%3D%22toc-hId--971959509%22%20id%3D%22toc-hId--971959509%22%20id%3D%22toc-hId--971959509%22%20id%3D%22toc-hId--971959509%22%3EWrap%20up%3C%2FH1%3E%0A%3CP%3EI%20hope%20you%20found%20this%20useful%20and%20understand%20more%20around%20what%20tokens%20are%2C%20the%20different%20types%20of%20tokens%20and%20how%20they%20are%20used.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2267853%22%20slang%3D%22en-US%22%3E%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3ETokens%20are%20everywhere%20on%20the%20Internet.%20Even%20if%20you%20don't%20realise%20it%2C%20you%20may%20have%20just%20used%20one%20on%20the%20way%20to%20read%20this%20article!%20You%20may%20ask%20%3C%2FSPAN%3E%3CEM%3E%22What%20is%20a%20token%20and%20what%20do%20I%20need%20it%20for%3F%22%3C%2FEM%3E%3CSPAN%3E%26nbsp%3Band%20my%20answer%20would%20be%20%3C%2FSPAN%3E%3CEM%3E%22Which%20token%3F%22%3C%2FEM%3E%3CSPAN%3E.%20You%20see%2C%20there%20are%20different%20types%20of%20tokens%20all%20with%20their%20use%20cases.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2267853%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EGetting%20started%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2278928%22%20slang%3D%22en-US%22%3ERe%3A%20Introduction%20to%20tokens%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2278928%22%20slang%3D%22en-US%22%3E%3CP%3Enice%20article%2C%20but%20there's%20a%20typo%20...%26nbsp%3B%3CSPAN%3Esever%26nbsp%3B-%26gt%3B%20server%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E
Co-Authors
Version history
Last update:
‎Apr 13 2021 08:57 AM
Updated by: