-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (135 loc) · 3.22 KB
/
main.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"bufio"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
"github.com/TylerBrock/colorjson"
"github.com/dgrijalva/jwt-go"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
)
type publicCert struct {
Kid string `json:"kid"`
Pem string `json:"cert"`
}
type accesskeys struct {
Keys []interface{} `json:"keys"`
PublicCert publicCert `json:"public_cert"`
PublicCerts []publicCert `json:"public_certs"`
}
type jwtTime struct {
Iat string `json:"iat"`
Exp string `json:"exp"`
}
func handleTime(claims map[string]interface{}) {
out := map[string]interface{}{}
iat := claims["iat"].(float64)
iatTime := time.Unix(int64(iat), 0)
out["iat"] = fmt.Sprintf("%s -- %s", iatTime.Format(time.RFC3339), humanize.Time(iatTime))
exp := claims["exp"].(float64)
expTime := time.Unix(int64(exp), 0)
out["exp"] = fmt.Sprintf("%s -- %s", expTime.Format(time.RFC3339), humanize.Time(expTime))
fmt.Println()
prettyPrintWithColor(out)
}
func getToken() string {
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
// data is being piped to stdin
scanner := bufio.NewScanner(os.Stdin)
if scanner.Scan() {
return scanner.Text()
}
} else {
// stdin is from a terminal
}
return ""
}
func getKey(token *jwt.Token) (interface{}, error) {
claims := convertClaims(token.Claims)
authDomain := claims["iss"].(string)
res, err := http.Get(authDomain + "/cdn-cgi/access/certs")
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, err
}
buf, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
defer res.Body.Close()
kid := token.Header["kid"]
accessKeys := accesskeys{}
json.Unmarshal(buf, &accessKeys)
for _, key := range accessKeys.PublicCerts {
if key.Kid == kid {
pubKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(key.Pem))
if err != nil {
return nil, err
}
return pubKey, nil
}
}
return nil, errors.New("couldn't find key")
}
func prettyPrintNoColor(data interface{}) {
prettyPrint, err := json.MarshalIndent(data, "", " ")
if err != nil {
fmt.Println("failed to pretty print")
return
}
fmt.Println(string(prettyPrint))
}
func prettyPrintWithColor(data interface{}) {
f := colorjson.NewFormatter()
f.Indent = 2
s, _ := f.Marshal(data)
fmt.Println(string(s))
}
func convertClaims(claims jwt.Claims) map[string]interface{} {
t := map[string]interface{}{}
b, _ := json.Marshal(claims)
json.Unmarshal(b, &t)
return t
}
func main() {
var verify bool
var time bool
flag.BoolVar(&verify, "v", false, "verify the JWT")
flag.BoolVar(&time, "t", false, "humanize the timestamps")
flag.Parse()
parser := jwt.Parser{}
claims := jwt.MapClaims{}
var token *jwt.Token
var err error
tokenString := getToken()
if verify {
token, err = parser.ParseWithClaims(tokenString, &claims, getKey)
if token.Valid {
color.Green("Valid token")
} else {
color.Red("Invalid token")
}
} else {
token, _, err = parser.ParseUnverified(tokenString, &claims)
if err != nil {
fmt.Println("failed to parse token")
return
}
}
c := convertClaims(claims)
prettyPrintWithColor(token.Header)
fmt.Println()
prettyPrintWithColor(c)
if time {
handleTime(c)
}
}