[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 ...
随机推荐
- Http Get Post put delete
HTTP POST GET 本质区别详解一 原理区别 一般在浏览器中输入网址访问资源都是通过GET方式:在FORM提交中,可以通过Method指定提交方式为GET或者POST,默认为GET提交 Htt ...
- python还不能作为主要编程语言的原因:
1.不太熟悉,容易犯新手错误,2.调试方法不同3.写了一个函数,是否语法正确,不能知道,只有具体调用它的时候才知道4.编辑器太业余,没有输入联想功能5.要查找一个函数或变量在哪里定义的,只能通过搜索的 ...
- DedeCms 5.7友情链接模块注入漏洞
漏洞版本: DedeCms 5.7 漏洞描述: DedeCms基于PHP+MySQL的技术开发,是目前国内应用最广泛的php类CMS系统. DedeCms 5.7前台提交友情链接处,可以插入恶意JS代 ...
- -_-#【减少 DOM 访问】“离线”更新节点,再将它们添加到树中
Minimize DOM Access javascript 之 DOM 优化 <!DOCTYPE html> <html> <head> <meta cha ...
- 学习Python前序
最近一直在学习有关Python语言.回顾的时候,发现学习过程中的有些东西被遗漏了.故记录在此......加深记忆,方便查找. The reason: 语言如此多,why choose Pyth ...
- aggregate 和 treeAggregate 的对比
1.定义 [aggregate] /** * Aggregate the elements of each partition, and then the results for all the pa ...
- 【JS】Intermediate5:Scope
1.Scope=variable visibility a variable’s scope is the part of your code that can access and modify t ...
- Linux/U-Boot Git Repo
Linux: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git U-Boot: git://git.denx.de/ ...
- Kooboo中怎么写Page Plugin -摘自官方文档
Page plugin development Page plugin is an add-on to Kooboo CMS, and is responsible for making data s ...
- json里的日期字符串 怎么 转换成 javascript 的 Date 对象?
“/Date(1232035200000)/” 怎么转换成 javascript 的 Date 对象 做法:new Date(+/\d+/.exec(value)[1]); value就是json字 ...