1+ // Package cmd contains the command-line interface commands.
12package cmd
23
34import (
@@ -8,99 +9,58 @@ import (
89 "github.com/spf13/cobra"
910)
1011
11- var decodeES256Cmd = & cobra.Command {
12- Use : "es256" ,
13- Short : "decode JWT token" ,
14- Long : `decode JWT token` ,
15- Run : func (cmd * cobra.Command , args []string ) {
16- var (
17- j cryptojwt.Decoder
18- err error
19- )
20- if privateKeyFile == "" && publicKeyFile == "" {
21- fmt .Println ("private key file or public key file is mandatory" )
22- fmt .Println (cmd .UsageString ())
23- os .Exit (1 )
24- }
25- if token == "" {
26- fmt .Println ("token is mandatory" )
27- fmt .Println (cmd .UsageString ())
28- os .Exit (1 )
29- }
30- if publicKeyFile != "" {
31- j = cryptojwt .NewES256DecoderWithPublicKeyFile (publicKeyFile )
32- } else {
33- j = cryptojwt .NewES256DecoderWithPrivateKeyFile (privateKeyFile )
34- }
35- if err != nil {
36- fmt .Println (err )
37- os .Exit (1 )
38- }
39- t , err := j .Decode (token )
40- if err != nil {
41- fmt .Println (err )
42- os .Exit (1 )
43- }
44- fmt .Println (t )
45- },
12+ //nolint:dupl // Similar structure needed for different algorithms
13+ func createESDecodeCommand (_ /* alg */ , use string , pubKeyDecoder , privKeyDecoder func (string ) cryptojwt.Decoder ) * cobra.Command {
14+ return & cobra.Command {
15+ Use : use ,
16+ Short : "decode JWT token" ,
17+ Long : `decode JWT token` ,
18+ Run : func (cmd * cobra.Command , _ []string ) {
19+ if privateKeyFile == "" && publicKeyFile == "" {
20+ fmt .Println ("private key file or public key file is mandatory" )
21+ fmt .Println (cmd .UsageString ())
22+ os .Exit (1 )
23+ }
24+ if token == "" {
25+ fmt .Println ("token is mandatory" )
26+ fmt .Println (cmd .UsageString ())
27+ os .Exit (1 )
28+ }
29+
30+ var j cryptojwt.Decoder
31+ if publicKeyFile != "" {
32+ j = pubKeyDecoder (publicKeyFile )
33+ } else {
34+ j = privKeyDecoder (privateKeyFile )
35+ }
36+
37+ t , err := j .Decode (token )
38+ if err != nil {
39+ fmt .Println (err )
40+ os .Exit (1 )
41+ }
42+ fmt .Println (t )
43+ },
44+ }
4645}
4746
48- var decodeES384Cmd = & cobra.Command {
49- Use : "es384" ,
50- Short : "decode es384 JWT token" ,
51- Long : `decode es384 JWT token` ,
52- Run : func (cmd * cobra.Command , args []string ) {
53- var j cryptojwt.Decoder
54- if privateKeyFile == "" && publicKeyFile == "" {
55- fmt .Println ("private key file or public key file is mandatory" )
56- fmt .Println (cmd .UsageString ())
57- os .Exit (1 )
58- }
59- if token == "" {
60- fmt .Println ("token is mandatory" )
61- fmt .Println (cmd .UsageString ())
62- os .Exit (1 )
63- }
64- if publicKeyFile != "" {
65- j = cryptojwt .NewES384DecoderWithPublicKeyFile (publicKeyFile )
66- } else {
67- j = cryptojwt .NewES384DecoderWithPrivateKeyFile (privateKeyFile )
68- }
69- t , err := j .Decode (token )
70- if err != nil {
71- fmt .Println (err )
72- os .Exit (1 )
73- }
74- fmt .Println (t )
75- },
76- }
47+ var decodeES256Cmd = createESDecodeCommand (
48+ "ES256" ,
49+ "es256" ,
50+ cryptojwt .NewES256DecoderWithPublicKeyFile ,
51+ cryptojwt .NewES256DecoderWithPrivateKeyFile ,
52+ )
7753
78- var decodeES512Cmd = & cobra.Command {
79- Use : "es512" ,
80- Short : "decode es512 JWT token" ,
81- Long : `decode es512 JWT token` ,
82- Run : func (cmd * cobra.Command , args []string ) {
83- var j cryptojwt.Decoder
84- if privateKeyFile == "" && publicKeyFile == "" {
85- fmt .Println ("private key file or public key file is mandatory" )
86- fmt .Println (cmd .UsageString ())
87- os .Exit (1 )
88- }
89- if token == "" {
90- fmt .Println ("token is mandatory" )
91- fmt .Println (cmd .UsageString ())
92- os .Exit (1 )
93- }
94- if publicKeyFile != "" {
95- j = cryptojwt .NewES512DecoderWithPublicKeyFile (publicKeyFile )
96- } else {
97- j = cryptojwt .NewES512DecoderWithPrivateKeyFile (privateKeyFile )
98- }
99- t , err := j .Decode (token )
100- if err != nil {
101- fmt .Println (err )
102- os .Exit (1 )
103- }
104- fmt .Println (t )
105- },
106- }
54+ var decodeES384Cmd = createESDecodeCommand (
55+ "ES384" ,
56+ "es384" ,
57+ cryptojwt .NewES384DecoderWithPublicKeyFile ,
58+ cryptojwt .NewES384DecoderWithPrivateKeyFile ,
59+ )
60+
61+ var decodeES512Cmd = createESDecodeCommand (
62+ "ES512" ,
63+ "es512" ,
64+ cryptojwt .NewES512DecoderWithPublicKeyFile ,
65+ cryptojwt .NewES512DecoderWithPrivateKeyFile ,
66+ )
0 commit comments