forked from hahwul/jwt-hack
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencode.go
43 lines (37 loc) · 1.08 KB
/
encode.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
package cmd
import (
"encoding/json"
"fmt"
jwtInterface "github.com/hahwul/jwt-hack/pkg/jwt"
"github.com/spf13/cobra"
log "github.com/sirupsen/logrus"
)
var secret, algo string
// encodeCmd represents the encode command
var encodeCmd = &cobra.Command{
Use: "encode [JSON]",
Short: "Encode json to JWT",
Run: func(cmd *cobra.Command, args []string) {
if len(args) >= 1 {
mapInterface := []byte(args[0])
var raw map[string]interface{}
if err := json.Unmarshal(mapInterface, &raw); err != nil {
// err
log.Error("JSON Unmarshal Error")
panic(0)
}
log.WithFields(log.Fields{
"algorithm": algo,
}).Info("Encoded result")
fmt.Println(jwtInterface.JWTencode(raw, secret, algo))
} else {
log.Error("Arguments Error")
log.Error("e.g jwt-hack encode {JWT_CODE} --secret={YOUR_SECRET}")
}
},
}
func init() {
rootCmd.AddCommand(encodeCmd)
encodeCmd.PersistentFlags().StringVar(&secret, "secret", "", "secret key for JWT signature")
encodeCmd.PersistentFlags().StringVar(&algo, "algorithm", "HS256", "Algorithm of JWT\ne.g) HS/RS/ECDA - 256,384,512")
}