-
Notifications
You must be signed in to change notification settings - Fork 6
/
jwt.go
62 lines (51 loc) · 1.21 KB
/
jwt.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
/*
Copyright © 2022 KAI CHU CHUNG <cage.chung@gmail.com>
*/
package cmd
import (
"encoding/json"
"fmt"
"strings"
"github.com/atotto/clipboard"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/cage1016/alfred-devtoys/lib"
)
// jwtCmd represents the jwt command
var jwtCmd = &cobra.Command{
Use: "jwt",
Short: "Jwt decode",
Run: runJwt,
}
func runJwt(c *cobra.Command, args []string) {
query := args[0]
if strings.TrimSpace(query) == "" {
query, _ = clipboard.ReadAll()
}
logrus.Debugf("query: %s", query)
token, err := lib.JWTdecode(query)
if err == nil {
a, _ := json.Marshal(token.Header)
b, _ := json.Marshal(token.Claims)
wf.NewItem(string(a)).
Subtitle("⌘+L, ↩ Action in Header").
Valid(true).
Arg(string(a)).
Largetype(string(a)).
Icon(JwtIcon).
Var("action", "action in alfred")
wf.NewItem(string(b)).
Subtitle("⌘+L, ↩ Action in Payload").
Valid(true).
Arg(string(b)).
Largetype(string(b)).
Icon(JwtIcon).
Var("action", "action in alfred")
} else {
wf.NewItem(fmt.Sprintf("`%s` is invalid jwt", query)).Subtitle("Try a different query?").Icon(JwtGrayIcon)
}
wf.SendFeedback()
}
func init() {
rootCmd.AddCommand(jwtCmd)
}