I intended to write this code in ES6. My purpose of this script is, when the blog admin has logged in, the server will send tokens with getToken(). If someone requests blog admin operations, the server will validate the token with validateToken(jwt_token_string)./* JWT module * --- * ../config *  : export the json object config,  *    {jwt: {header, payload, secret}, admin:{username, userpw}} * * _getSignature(encoded_header, encoded_payload) *  : a private function that generates sha256 hashed signiture * * getToken() *  : return JWT token string * * validateToken(jwt_token_string) *  : return true if jwt_token_string is valid * */'use strict';import config from '../config';import crypto from 'crypto';const _getSignature = function(encoded_header, encoded_payload) {    /* get encoded header and payload */    /* return signature with secrete key) */    return crypto.createHmac('sha256', JSON.stringify(config.jwt.secret))                .update(encoded_header + '.' + encoded_payload)                .digest('base64')                .replace('=', '');}const getToken = function() {    /* return JWT token string */    let encodedHeader = new Buffer(JSON.stringify(config.jwt.header))        .toString('base64').replace('=', '');    let encodedPayload = new Buffer(JSON.stringify(config.jwt.payload))        .toString('base64').replace('=', '');    let signature = _getSignature(encodedHeader, encodedPayload);    return (encodedHeader + '.' + encodedPayload + '.' + signature);}const validateToken = function(jwt_token_string) {    /* get JWT token string */    /* return True if it has valid signature */    let tokens= jwt_token_string.split('.'),        header = tokens[0],        payload = tokens[1],        signature = tokens[2],        created_signature = _getSignature(header, payload);    return signature === created_signature;}export default { getToken, validateToken }Here are the mock config data:// config/jwt.jsimport admin from './admin';export default {    header: {        typ: JWT,        alg: HS256    },    payload: {        iss: blog,        username: admin.username,    },    secret: foo$,}// config/admin.jsexport default {    username: kyle,    password: 1234,}
JWT script that generates tokens for admin and validates Token string
javascript;node.js;jwt