Skip to content

jwt分析

代码

python
import base64
import json
from datetime import datetime
import sys
from pprint import pprint


jwt_token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJleHAiOjE2NzYzNTkyMTR9.Y0jn6ISY-ZJZ2JBXq-T70D5kHnnxzTgEghMh9nUO2gA'

if len(sys.argv) > 1:
	jwt_token = sys.argv[1]

if jwt_token:
	segments = jwt_token.strip('"').split('.')
	payload_base64 = segments[1]
	jwt = base64.urlsafe_b64decode(payload_base64 + '=' * (4 - len(payload_base64) % 4)).decode()
	print("[jwt]")
	pprint(json.loads(jwt))

	exp = json.loads(jwt).get('exp')
	if exp:
		print()
		print('[time]')
		print('当前时间:', datetime.isoformat(datetime.now(), sep=" ", timespec="seconds"))
		print('失效时间:', datetime.fromtimestamp(exp))
shell
python -c "import base64;exec(base64.b64decode('aW1wb3J0IGJhc2U2NAppbXBvcnQganNvbgpmcm9tIGRhdGV0aW1lIGltcG9ydCBkYXRldGltZQppbXBvcnQgc3lzCmZyb20gcHByaW50IGltcG9ydCBwcHJpbnQKCmp3dF90b2tlbiA9ICdleUpoYkdjaU9pSklVekkxTmlJc0luUjVjQ0k2SWtwWFZDSjkuZXlKMWMyVnlYMmxrSWpveExDSmxlSEFpT2pFMk56WXpOVGt5TVRSOS5ZMGpuNklTWS1aSloySkJYcS1UNzBENWtIbm54elRnRWdoTWg5blVPMmdBJwoKaWYgbGVuKHN5cy5hcmd2KSA+IDE6Cglqd3RfdG9rZW4gPSBzeXMuYXJndlsxXQoKaWYgand0X3Rva2VuOgoJc2VnbWVudHMgPSBqd3RfdG9rZW4uc3RyaXAoJyInKS5zcGxpdCgnLicpCglwYXlsb2FkX2Jhc2U2NCA9IHNlZ21lbnRzWzFdCglqd3QgPSBiYXNlNjQudXJsc2FmZV9iNjRkZWNvZGUocGF5bG9hZF9iYXNlNjQgKyAnPScgKiAoNCAtIGxlbihwYXlsb2FkX2Jhc2U2NCkgJSA0KSkuZGVjb2RlKCkKCXByaW50KCJband0XSIpCglwcHJpbnQoanNvbi5sb2Fkcyhqd3QpKQoKCWV4cCA9IGpzb24ubG9hZHMoand0KS5nZXQoJ2V4cCcpCglpZiBleHA6CgkJcHJpbnQoKQoJCXByaW50KCdbdGltZV0nKQoJCXByaW50KCflvZPliY3ml7bpl7Q6JywgZGF0ZXRpbWUuaXNvZm9ybWF0KGRhdGV0aW1lLm5vdygpLCBzZXA9IiAiLCB0aW1lc3BlYz0ic2Vjb25kcyIpKQoJCXByaW50KCflpLHmlYjml7bpl7Q6JywgZGF0ZXRpbWUuZnJvbXRpbWVzdGFtcChleHApKQo='.encode()).decode())" <jwt>

Released under the MIT License.