Sponsored Links
-->

Friday, March 30, 2018

JSON Web Token Introduction - JWT_1 - YouTube
src: i.ytimg.com

JSON Web Token (JWT, sometimes pronounced ) is a JSON-based open standard (RFC 7519) for creating access tokens that assert some number of claims. For example, a server could generate a token that has the claim "logged in as admin" and provide that to a client. The client could then use that token to prove that it is logged in as admin. The tokens are signed by the server's key, so the client and server are both able to verify that the token is legitimate. The tokens are designed to be compact, URL-safe and usable especially in web browser single sign-on (SSO) context. JWT claims can be typically used to pass identity of authenticated users between an identity provider and a service provider, or any other type of claims as required by business processes. The tokens can also be authenticated and encrypted.

JWT relies on other JSON-based standards: JWS (JSON Web Signature) RFC 7515 and JWE (JSON Web Encryption) RFC 7516.


Video JSON Web Token



Structure

JWTs generally have three parts: a header, a payload, and a signature. The header identifies which algorithm is used to generate the signature, and looks something like this:

  header = '{"alg":"HS256","typ":"JWT"}'  

HS256 indicates that this token is signed using HMAC-SHA256.

The payload contains the claims to make:

  payload = '{"loggedInAs":"admin","iat":1422779638}'  

As suggested in the JWT spec, a timestamp called iat (issued at) is installed.

The signature is calculated by base64url encoding the header and payload and concatenating them with a period as a separator:

  key           = 'secretkey'  unsignedToken = encodeBase64Url(header) + '.' + encodeBase64Url(payload)  signature     = HMAC-SHA256(key, unsignedToken)   

To put it all together, the signature is base64url encoded. The three separate parts are concatenated using periods:

  token = encodeBase64Url(header) + '.' + encodeBase64Url(payload) + '.' + encodeBase64Url(signature) # token is now: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJsb2dnZWRJbkFzIjoiYWRtaW4iLCJpYXQiOjE0MjI3Nzk2Mzh9.gzSraSYS8EXBxLN_oWnFSRgCzcmJmMjLiuyu5CSpyHI   

The output is three Base64url strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact compared to XML-based standards such as SAML. Typical cryptographic algorithms used are HMAC with SHA-256 (HS256) and RSA signature with SHA-256 (RS256). JWA (JSON Web Algorithms) RFC 7518 introduces many more for both authentication and encryption.


Maps JSON Web Token



Use

In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned and must be saved locally (typically in local or session storage, but cookies can also be used), instead of the traditional approach of creating a session in the server and returning a cookie.

Whenever the user wants to access a protected route or resource, the user agent should send the JWT, typically in the Authorization header using the Bearer schema. The content of the header might look like the following:

Authorization: Bearer eyJhbGci...<snip>...yu5CSpyHI

This is a stateless authentication mechanism as the user state is never saved in server memory. The server's protected routes will check for a valid JWT in the Authorization header, and if it is present, the user will be allowed to access protected resources. As JWTs are self-contained, all the necessary information is there, reducing the need to query the database multiple times.


JWT Token Security with Fusion Sales Cloud | Oracle Angelo ...
src: cdn.app.compendium.com


Standard fields

The Internet drafts define the following standard fields ("claims") that can be used inside a JWT claim set:

  • Issuer (iss) - identifies principal that issued the JWT;
  • Subject (sub) - identifies the subject of the JWT;
  • Audience (aud) - The "aud" (audience) claim identifies the recipients that the JWT is intended for. Each principal intended to process the JWT must identify itself with a value in the audience claim. If the principal processing the claim does not identify itself with a value in the aud claim when this claim is present, then the JWT must be rejected.
  • Expiration time (exp) - The "exp" (expiration time) claim identifies the expiration time on or after which the JWT must not be accepted for processing. The value should be in NumericDate format.
  • Not before (nbf) - Similarly, the not-before time claim identifies the time on which the JWT will start to be accepted for processing.
  • Issued at (iat) - The "iat" (issued at) claim identifies the time at which the JWT was issued.
  • JWT ID (jti) - case sensitive unique identifier of the token even among different issuers.

The following fields can be used in authentication headers:

  • Token type (typ) - If present, it is recommended to set this to JWT.
  • Content type (cty) - If nested signing or encryption is employed, it is recommended to set this to JWT, otherwise omit this field.
  • Message authentication code algorithm (alg) - The issuer can freely set an algorithm to verify the signature on the token. However, some supported algorithms are insecure.
  • All other headers introduced by JWS and JWE

How to create JWT (JSON web token) using pure JavaScript (and ...
src: www.ourwonderfulworld.net


Implementations

JWT implementations exist for

  • Common Lisp
  • Go
  • .NET
  • Perl
  • Perl6
  • PHP
  • Python
  • Ruby
  • Rust
  • Scala
  • Clojure
  • Elixir
  • Erlang
  • Haskell
  • Java
  • JavaScript
  • Lua
  • Node.js

Single-Use Tokens w/ JWT
src: www.jbspeakr.cc


References


Authenticating a Flask API Using JSON Web Tokens - YouTube
src: i.ytimg.com


External links

  • jwt.io - specialized website about JWT with tools and documentation, maintained by Auth0
  • Spring Boot JWT Auth - Integrating JWT authentication with Spring framework

Source of the article : Wikipedia

Comments
0 Comments