-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
143 lines (114 loc) · 3.11 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io"
"log"
"os"
"github.com/lestrrat-go/jwx/v2/jwa"
"github.com/lestrrat-go/jwx/v2/jwt"
)
var (
VERSION = ""
flags = flag.NewFlagSet("jwtutil", flag.ExitOnError)
fSecret = flags.String("secret", "", "JWT secret for encoding/decoding (be safe!!)")
fSilent = flags.Bool("silent", false, "Silent mode. Print JWT token only")
fEncode = flags.Bool("encode", false, "Encode new JWT token")
fDecode = flags.Bool("decode", false, "Decode existing JWT token")
fToken = flags.String("token", "", "+decode, jwt token to decode")
fExp = flags.Int64("exp", -1, "+encode, set expiry claim (must be unix timestamp value)")
fClaims = flags.String("claims", "", "+encode, set extra claims as json object")
)
func main() {
flags.Parse(os.Args[1:])
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, usage, VERSION)
os.Exit(1)
}
var stderr io.Writer = os.Stderr
if *fSilent {
stderr = io.Discard
}
if *fDecode {
// Decode passed jwt token string
if *fToken == "" {
fmt.Fprintln(stderr, "-token flag cannot be empty.")
return
}
var token jwt.Token
var err error
if *fSecret != "" {
token, err = jwt.Parse([]byte(*fToken), jwt.WithKey(jwa.HS256, []byte(*fSecret)))
} else {
token, err = jwt.Parse([]byte(*fToken))
}
if err != nil {
log.Fatal(err)
}
fmt.Fprintln(stderr, "\nToken decoding details:")
if *fSecret != "" {
if err := jwt.Validate(token); err != nil {
fmt.Fprintln(stderr, " * Token is invalid!")
} else {
fmt.Fprintln(stderr, " * Token is valid!")
}
}
fmt.Fprintf(stderr, "\nToken claims:\n")
claims, _ := token.AsMap(context.Background())
for k, v := range claims {
fmt.Fprintf(stderr, " * %v: %+v\n", k, v)
}
fmt.Fprintln(stderr)
return
}
if *fSecret == "" && *fSecret != "<>" {
fmt.Fprintln(stderr, "jwtutil: secret is empty.")
return
}
if *fEncode {
// Encode new JWT token.
token := jwt.New()
var claims map[string]interface{}
if *fClaims != "" {
err := json.Unmarshal([]byte(*fClaims), &claims)
if err != nil {
fmt.Fprintln(stderr, "Error! -claims flag is invalid json.", err)
return
}
}
for k, v := range claims {
err := token.Set(k, v)
if err != nil {
log.Fatal(err)
}
}
if *fExp > 0 {
token.Set("exp", *fExp)
}
tokenPayload, err := jwt.Sign(token, jwt.WithKey(jwa.HS256, []byte(*fSecret)))
if err != nil {
log.Fatal(err)
}
tokenStr := string(tokenPayload)
fmt.Fprintln(stderr)
fmt.Fprintln(stderr, "Token:", tokenStr)
fmt.Printf("%s", tokenStr)
claims, _ = token.AsMap(context.Background())
fmt.Fprintf(stderr, "\n\nClaims: %#v\n", claims)
fmt.Fprintln(stderr)
return
}
}
var usage = `jwtutil %s
usage:
# New JWT token
$ jwtutil -secret=besafe -encode
# New JWT token with expiry (unix timestamp value)
$ jwtutil -secret=besafe -encode -exp=1585272657
# New JWT token with custom claims
$ jwtutil -secret=besafe -encode -claims='{"account":1234}'
# Decode JWT
$ jwtutil -secret=besafe -decode -token='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50IjoxMjM0fQ.WrPyTSoovFETG6pW0wFepaAv9-VTIfeSHU5imhPqs7g'
`