forked from rexray/rexray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmds_60_token.go
260 lines (241 loc) · 6.23 KB
/
cmds_60_token.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package cli
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
"time"
jcrypto "github.com/SermoDigital/jose/crypto"
"github.com/SermoDigital/jose/jws"
"github.com/akutz/gotil"
"github.com/rexray/rexray/util"
"github.com/spf13/cobra"
)
const (
lsSrvrAuthKey = "libstorage.server.auth.key"
rrTokKeyEnv = "REXRAY_TOKEN_KEY"
rrTokKeyKey = "rexray.token.key"
lsSrvrAuthAlg = "libstorage.server.auth.alg"
rrTokAlgEnv = "REXRAY_TOKEN_ALG"
rrTokAlgKey = "rexray.token.alg"
rrTokExpEnv = "REXRAY_TOKEN_EXP"
rrTokExpKey = "rexray.token.exp"
// 8760 hours is 365 days
defaultTokenExp = time.Duration(8760 * time.Hour)
)
var (
zd = time.Duration(0)
)
func init() {
initCmdFuncs = append(initCmdFuncs, func(c *CLI) {
c.initTokenCmds()
c.initTokenFlags()
})
}
func (c *CLI) initTokenCmds() {
c.tokenCmd = &cobra.Command{
Use: "token",
Short: "The token manager",
Run: func(cmd *cobra.Command, args []string) {
cmd.Usage()
},
}
c.c.AddCommand(c.tokenCmd)
c.tokenNewCmd = &cobra.Command{
Use: "new",
Aliases: []string{"create", "c", "n"},
Short: "Create a new token",
Example: util.BinFileName + " token new [OPTIONS] SUBJECT [SUBJECT...]",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.Usage()
os.Exit(1)
}
key, alg := c.tokenValidateKeyAndAlg()
exp := c.tokenValidateExp()
now := time.Now().UTC()
for _, a := range args {
clm := jws.Claims{}
if exp != zd {
clm.SetExpiration(now.Add(exp))
}
clm.SetIssuedAt(now)
clm.SetNotBefore(now)
clm.SetSubject(a)
jwt := jws.NewJWT(clm, alg)
buf, err := jwt.Serialize(key)
if err != nil {
c.ctx.WithError(err).Fatal("error serializing token")
}
fmt.Println(string(buf))
}
},
}
c.tokenCmd.AddCommand(c.tokenNewCmd)
c.tokenDecodeCmd = &cobra.Command{
Use: "decode",
Aliases: []string{"dec", "d"},
Short: "Decodes a token",
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
// is there stdin?
stat, _ := os.Stdin.Stat()
if (stat.Mode() & os.ModeCharDevice) == 0 {
rdr := bufio.NewScanner(os.Stdin)
for rdr.Scan() {
args = append(args, rdr.Text())
}
} else {
cmd.Usage()
os.Exit(1)
}
}
var (
key []byte
alg jcrypto.SigningMethod
toks []*authToken
)
if c.verify {
key, alg = c.tokenValidateKeyAndAlg()
}
for _, a := range args {
jwt, err := jws.ParseJWT([]byte(a))
if err != nil {
c.ctx.WithError(err).Fatal("error: invalid token")
}
if c.verify {
fmt.Printf("key=%v\n", string(key))
fmt.Printf("alg=%v\n", alg.Alg())
if err := jwt.Validate(key, alg); err != nil {
c.ctx.WithError(err).Fatal(
"error: invalid token signature")
}
}
t := &authToken{}
if v, ok := jwt.Claims().Subject(); ok {
t.Subject = v
}
if v, ok := jwt.Claims().Expiration(); ok {
t.Expires = v
}
if v, ok := jwt.Claims().IssuedAt(); ok {
t.IssuedAt = v
}
if v, ok := jwt.Claims().NotBefore(); ok {
t.NotBefore = v
}
toks = append(toks, t)
}
c.mustMarshalOutput(toks, nil)
},
}
c.tokenCmd.AddCommand(c.tokenDecodeCmd)
}
func (c *CLI) tokenValidateKeyAndAlg() ([]byte, jcrypto.SigningMethod) {
var keyBuf []byte
if c.key == "" {
if c.key = os.Getenv(rrTokKeyEnv); c.key == "" {
if c.key = c.config.GetString(rrTokKeyKey); c.key == "" {
c.key = c.config.GetString(lsSrvrAuthKey)
if c.key == "" {
c.ctx.Fatal("error: missing token key")
}
}
}
}
if gotil.FileExists(c.key) {
buf, err := ioutil.ReadFile(c.key)
if err != nil {
c.ctx.WithField("keyFilePath", c.key).WithError(err).Fatal(
"error reading key file")
}
keyBuf = buf
} else {
keyBuf = []byte(c.key)
}
if c.alg == "" {
if c.alg = os.Getenv(rrTokAlgEnv); c.alg == "" {
if c.alg = c.config.GetString(rrTokAlgKey); c.alg == "" {
c.alg = c.config.GetString(lsSrvrAuthAlg)
if c.alg == "" {
c.ctx.Fatal("error: missing token alg")
}
}
}
}
alg, ok := parseSigningMethod(c.alg)
if !ok {
c.ctx.Fatal("error: invalid token alg")
}
return keyBuf, alg
}
func (c *CLI) tokenValidateExp() time.Duration {
var (
err error
exp = c.expires
)
if exp == zd {
if v := os.Getenv(rrTokExpEnv); v != "" {
if exp, err = time.ParseDuration(v); err != nil {
c.ctx.Fatal("error: invalid token exp")
}
return exp
}
if v := c.config.GetString(rrTokExpKey); v != "" {
if exp, err = time.ParseDuration(v); err != nil {
c.ctx.Fatal("error: invalid token exp")
}
return exp
}
c.ctx.WithField("exp", defaultTokenExp).Info(
"Using default token expiration duration")
exp = defaultTokenExp
}
return exp
}
func (c *CLI) initTokenFlags() {
c.tokenNewCmd.Flags().StringVar(&c.key, "key", "",
"The key used to sign the token.")
c.tokenNewCmd.Flags().StringVar(&c.alg, "alg", "HS256",
"The algorithm used to sign the token.")
c.tokenNewCmd.Flags().DurationVar(&c.expires, "exp",
zd,
"The period of time until the token expires. Valid time units are "+
`"ns", "us" (or "µs"), "ms", "s", "m", "h".`)
c.tokenDecodeCmd.Flags().StringVar(&c.key, "key", "",
"The key used to verify the token's signature")
c.tokenDecodeCmd.Flags().BoolVar(&c.verify, "verify", false,
"A flag indicating whether or not to verify the token's signature. "+
"If true and the signature is invalid or cannot be validated the "+
"token will not be decoded.")
}
var signingMethods = []jcrypto.SigningMethod{
jcrypto.SigningMethodES256,
jcrypto.SigningMethodES384,
jcrypto.SigningMethodES512,
jcrypto.SigningMethodHS256,
jcrypto.SigningMethodHS384,
jcrypto.SigningMethodHS512,
jcrypto.SigningMethodPS256,
jcrypto.SigningMethodPS384,
jcrypto.SigningMethodPS512,
jcrypto.SigningMethodRS256,
jcrypto.SigningMethodRS384,
jcrypto.SigningMethodRS512,
}
// parseSigningMethod parses the signing method from the provided method name.
func parseSigningMethod(methodName string) (jcrypto.SigningMethod, bool) {
for _, v := range signingMethods {
if strings.EqualFold(v.Alg(), methodName) {
return v, true
}
}
return nil, false
}
type authToken struct {
Subject string `json:"sub"`
Expires time.Time `json:"exp"`
NotBefore time.Time `json:"nbf"`
IssuedAt time.Time `json:"iat"`
}