OAuth 2.0 JWT All In One
OAuth 2.0 JWT All In One
OAuth 2.0
JWT
JSON Web Tokens
const header = {
"alg": "HS256",
"typ": "JWT"
};
btoa(header);
// 'W29iamVjdCBPYmplY3Rd'
btoa(JSON.stringify(header));
// 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
atob('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9');
// '{"alg":"HS256","typ":"JWT"}'
JSON.parse(atob('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'));
// {alg: 'HS256', typ: 'JWT'}
// encode / decode
class Base64 {
constructor() {
// super();
}
static decode(str = '') {
return JSON.parse(atob(str));
}
static encode(obj = {}) {
return btoa(JSON.stringify(obj));
}
}
// eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c
Base64.decode("eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ")
// {sub: '1234567890', name: 'John Doe', iat: 1516239022}
HMACSHA256(
base64UrlEncode(header) + "." + base64UrlEncode(payload),
"abc$2022&05"
)
// secret base64 encoded
refs
https://www.cnblogs.com/xgqfrms/tag/JWT/
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/13807079.html
未经授权禁止转载,违者必究!