-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode-sd-jwt
executable file
·68 lines (51 loc) · 1.08 KB
/
decode-sd-jwt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env ruby
#
# OVERVIEW
# --------
#
# This script decodes an SD-JWT.
#
# USAGE
# -----
#
# decode-sd-jwt ${SD_JWT}
#
require "base64"
require "digest/sha2"
require "json"
def prettify_json(input)
JSON.pretty_generate(JSON.parse(input))
end
def sha256(input)
Digest::SHA256.digest(input)
end
def decode_base64url(input)
Base64.urlsafe_decode64(input)
end
def encode_base64url(input)
Base64.urlsafe_encode64(input, padding: false)
end
def decode_disclosure(input)
digest = encode_base64url(sha256(input))
decoded = decode_base64url(input)
prettify_json "{ \"digest\": \"#{digest}\", \"#{input}\": #{decoded} }"
end
def decode_jwt(input)
fields = input.split('.')
header = prettify_json(decode_base64url(fields[0]))
payload = prettify_json(decode_base64url(fields[1]))
"#{header}\n#{payload}"
end
def decode_sd_jwt(input)
output = ""
input.split('~').each do |field|
if field.include?('.')
output << decode_jwt(field)
else
output << decode_disclosure(field)
end
output << "\n"
end
output
end
puts decode_sd_jwt(ARGV[0])