[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 ...
随机推荐
- Foundation和UIKit框架图
学习Foundation和UIKit的时候比较容易忽视的一个问题: 对于一个新的类,知道它的用法和属性方法,但往往忽视了它的继承关系, 了解类的继承关系能帮助加深对其理解. 另外在官方文档中每一个类的 ...
- mysql通过frm+ibd文件还原data
此方法只适合innodb_file_per_table = 1 当误删除ibdata 该怎么办? 如下步骤即可恢复: 1.准备工作 1)准备一台纯洁的mysql环境[从启动到现在没有 ...
- 物理地址为20位 如10000H 用段地址*16+偏移地址表示
段地质在cpu中,为16位 段地质*16则变成物理首地址20位,这个物理首地址必定是16的倍数. 偏移地址16位, 则表明一个段的大小为64k. 同时也表明16位地址的寻址能力为64kb
- [codility]Prefix-set
这题很简单,一开始用了set.但后来一想这样其实是n*logn的,而且没有利用所有的数都在0..N-1之间.那么可以直接用vector当hashset. // you can also use inc ...
- Android 开发绕不过的坑:你的 Bitmap 究竟占多大内存?
0.写在前面 本文涉及到屏幕密度的讨论,这里先要搞清楚 DisplayMetrics 的两个变量,摘录官方文档的解释: density:The logical density of the displ ...
- Java 输入流读取文本文件换行符问题
一问题 在学习流编程的过程中,我遇到了一下问题.首先来看一下我写的java源程序: package StreamLearn; import java.io.*; public class TestFi ...
- Linux 性能监测工具总结
前言: Linux系统出现问题时,我们不仅需要查看系统日志信息,而且还要使用大量的性能监测工具来判断究竟是哪一部分(内存.CPU.硬盘……)出了问题.在Linux系统中,所有的运行参数保存在虚拟目录/ ...
- BZOJ_3527_[ZJOI2014]_力_(FFT+卷积)
描述 题面: http://wenku.baidu.com/link?url=D2ORnA9xjgSxa2GlYLB7gGiYgBcXsy-Aw0kVYTjTE-iYhH1s7h8xXGmnaMwl3 ...
- Luogu_1565_牛宫_(最大子矩阵)
描述 http://www.luogu.org/problem/show?pid=1565 给出一个n*m的矩阵,求最大的且和值为正的子矩阵. 分析 很容易想到的是用前缀和维护,暴力枚举左上角和右下角 ...
- 【转】 如何查看linux版本 如何查看LINUX是多少位
原文网址:http://blog.csdn.net/hongweigg/article/details/7192471 一.如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! ...