[Node.js] Creating JWTs (JSON Web Tokens) in Node
In this lesson we will look at all of the pieces that combine together to create a JWT (j AWT) or JSON Web Token. You will use node to create a JWT, and then verify it in the JWT debugger.
What is the JSON Web Token structure?
JSON Web Tokens consist of three parts separated by dots (.), which are:
- Header
- Payload
- Signature
Therefore, a JWT typically looks like the following.
xxxxx.yyyyy.zzzzz
Let's break down the different parts.
Create a header:
The header typically consists of two parts: the type of the token, which is JWT, and the hashing algorithm being used, such as HMAC SHA256 or RSA.
let header = {
typ: 'JWT',
alg: 'HS256'
};
header = new Buffer(JSON.stringify(header)).toString('base64');
console.log(header);
Create a paylaod:
The second part of the token is the payload, which contains the claims. Claims are statements about an entity (typically, the user) and additional metadata. There are three types of claims: reserved, public, and privateclaims.
let payload = {
iat: Date.now(),
iss: 'nodebotanist',
username: 'nodebotanist'
};
payload = new Buffer(JSON.stringify(payload)).toString('base64');
console.log("payload", payload);
Create a signature:
To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.
For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
let key = header + '.' + payload;
let signature = crypto.createHmac('sha256', 'zhentian');
signature.update(key);
key = signature.digest('base64'); let token = header + '.' +payload + '.' + key
console.log("token", token)
----------------
let header = {
typ: 'JWT',
alg: 'HS256'
};
header = new Buffer(JSON.stringify(header)).toString('base64');
console.log(header);
let payload = {
iat: Date.now(),
iss: 'nodebotanist',
username: 'nodebotanist'
};
payload = new Buffer(JSON.stringify(payload)).toString('base64');
console.log("payload", payload);
let key = header + '.' + payload;
let signature = crypto.createHmac('sha256', 'zhentian');
signature.update(key);
key = signature.digest('base64');
let token = header + '.' +payload + '.' + key
console.log("token", token)
[Node.js] Creating JWTs (JSON Web Tokens) in Node的更多相关文章
- JSON Web Tokens(JWT)
现在API越来越流行,如何安全保护这些API? JSON Web Tokens(JWT)能提供基于JSON格式的安全认证.它有以下特点: JWT是跨不同语言的,JWT可以在 .NET, Python, ...
- Implement JSON Web Tokens Authentication in ASP.NET Web API and Identity 2.1 Part 3 (by TAISEER)
http://bitoftech.net/2015/02/16/implement-oauth-json-web-tokens-authentication-in-asp-net-web-api-an ...
- Koa--基于Node.js平台的下一代web开发框架的安装
koa 是由 Express 原班人马打造的,致力于成为一个更小.更富有表现力.更健壮的 Web 框架. 使用 koa 编写 web 应用,通过组合不同的 generator,可以免除重复繁琐的回调函 ...
- Node.js 从零开发 web server博客项目[express重构博客项目]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[数据存储]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[登录]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[接口]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- Node.js 从零开发 web server博客项目[项目介绍]
web server博客项目 Node.js 从零开发 web server博客项目[项目介绍] Node.js 从零开发 web server博客项目[接口] Node.js 从零开发 web se ...
- JWT & JSON Web Tokens
JSON Web Tokens https://jwt.io json web token example https://jwt.io/introduction/ https://medium.co ...
随机推荐
- Qt for PC,Qt for iOS,Qt for Android (居士的博客)
http://blog.csdn.net/Esonpo/article/details/38081607 http://blog.csdn.net/Esonpo/article/details/380 ...
- delphi中的各种文件类型介绍
1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...
- Linux下删除大量文件
主要参考了http://www.slashroot.in/which-is-the-fastest-method-to-delete-files-in-linux 首先建立50万个文件 ➜ test ...
- ActionBar官方教程(7)自定义操作项的view,如何得到它及处理它的事件
Adding an Action View An action view is a widget that appears in the action bar as a substitute for ...
- Maprduce重写参考
Maprduce数据流走向图: 流程解释 Input files 功能描述:存储在HDFS中的文件数据 InputFormat 功能描述:1 ...
- C# 6和 VB 12的最新特性列表
随着下个版本的C#发布日逐渐临近,那些还没有完成的特性必须被砍掉.最近从特性列表中被砍掉的特性包括主要构造函数(primary constructor)和声明表达式(declaration expre ...
- mysql shell 备份脚本
使用 mysqldump 备份数据库,通过 FTP 上传到备份服务器,同时在本地保留备份文件. 新建一个 Shell 脚本文件 vi /home/work/backup.sh 添加如下内容: #! / ...
- 虚拟主机 (Virtual Host)
虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...
- WinForm触摸屏程序功能界面长时间不操作自动关闭回到主界面 z
操作者经常会在执行了某操作后,没有返还主界面就结束了操作然后离开了,程序应该关闭功能窗体自动回到主界面方便下一位操作者操作.那么对于WinForm程序怎么实现呢? 实现原理:拦截Application ...
- 用FSM写Case,玩过没?
一.引言 测试工程师小新一是一名安卓客户端测试工程师,对于安卓客户端的功能测试.自动化测试和性能测试方面都有着非常丰富的经验.最近小新一被通知负责某二手交易APP的功能测试,在初步了解了该APP后,小 ...