xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

OAuth 2.0 JWT All In One

OAuth 2.0 JWT All In One

OAuth 2.0

JWT

JSON Web Tokens

https://jwt.io

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, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-10-13 19:26  xgqfrms  阅读(119)  评论(2编辑  收藏  举报