0
0

More than 3 years have passed since last update.

Node.js: JWT のペイロード部をデコード

Posted at

次の記事を参考にしました。
JWTクレーム・セット部分のデコード方法

decode_jwt.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  decode_jwt.js
//
//                  Feb/07/2021
//
// ---------------------------------------------------------------
'use strict'

const fs = require("fs")

// ---------------------------------------------------------------
var atob = (base64) => {
    const buffer = Buffer.from( base64, 'base64' )
    const utf8 = buffer.toString('utf8') // Not "ascii"

    return utf8
}

// ---------------------------------------------------------------
var decodeJwt = (token) => {
    const base64Url = token.split('.')[1]
    const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/')

    const encodeURI = encodeURIComponent(atob(base64))
    const decodeString = decodeURIComponent(encodeURI)

    return JSON.parse(decodeString)
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const file_token = process.argv[2]

if (fs.existsSync(file_token))
    {
    const str_token = fs.readFileSync (file_token,'utf8')

    var jsonObj = decodeJwt(str_token)
    console.log(jsonObj)
    }

console.error ("*** 終了 ***")
// ---------------------------------------------------------------

実行方法

./decode_jwt.js token01.txt
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0