@@ -26,6 +26,7 @@ import (
26
26
"fmt"
27
27
"io/ioutil"
28
28
"strings"
29
+ "time"
29
30
30
31
"github.com/spf13/cobra"
31
32
@@ -34,9 +35,10 @@ import (
34
35
35
36
var (
36
37
cmdAuth = & cobra.Command {
37
- Use : "auth" ,
38
- Short : "ArangoDB authentication helper commands" ,
39
- Run : cmdShowUsage ,
38
+ Use : "auth" ,
39
+ Short : "ArangoDB authentication helper commands" ,
40
+ PersistentPreRunE : persistentAuthPreFunE ,
41
+ Run : cmdShowUsage ,
40
42
}
41
43
cmdAuthHeader = & cobra.Command {
42
44
Use : "header" ,
52
54
jwtSecretFile string
53
55
user string
54
56
paths []string
55
- exp int64
57
+ exp string
58
+ expDuration time.Duration
56
59
}
57
60
)
58
61
@@ -65,7 +68,7 @@ func init() {
65
68
pf .StringVar (& authOptions .jwtSecretFile , "auth.jwt-secret" , "" , "name of a plain text file containing a JWT secret used for server authentication" )
66
69
pf .StringVar (& authOptions .user , "auth.user" , "" , "name of a user to authenticate as. If empty, 'super-user' authentication is used" )
67
70
pf .StringSliceVar (& authOptions .paths , "auth.paths" , nil , "a list of allowed pathes. The path must not include the '_db/DBNAME' prefix." )
68
- pf .Int64Var (& authOptions .exp , "auth.exp" , 0 , "an expiry date in seconds since epoche " )
71
+ pf .StringVar (& authOptions .exp , "auth.exp" , "" , "a time in which token should expire - based to current time. Supported units: h, m, s (default) " )
69
72
}
70
73
71
74
// mustAuthCreateJWTToken creates a the JWT token based on authentication options.
@@ -81,7 +84,7 @@ func mustAuthCreateJWTToken() string {
81
84
log .Fatal ().Err (err ).Msgf ("Failed to read JWT secret file '%s'" , authOptions .jwtSecretFile )
82
85
}
83
86
jwtSecret := strings .TrimSpace (string (content ))
84
- token , err := service .CreateJwtToken (jwtSecret , authOptions .user , "" , authOptions .paths , authOptions .exp )
87
+ token , err := service .CreateJwtToken (jwtSecret , authOptions .user , "" , authOptions .paths , authOptions .expDuration )
85
88
if err != nil {
86
89
log .Fatal ().Err (err ).Msg ("Failed to create JWT token" )
87
90
}
@@ -99,3 +102,40 @@ func cmdAuthTokenRun(cmd *cobra.Command, args []string) {
99
102
token := mustAuthCreateJWTToken ()
100
103
fmt .Println (token )
101
104
}
105
+
106
+ func persistentAuthPreFunE (cmd * cobra.Command , args []string ) error {
107
+ cmdMain .PersistentPreRun (cmd , args )
108
+
109
+ if authOptions .exp != "" {
110
+ d , err := durationParser (authOptions .exp , "s" )
111
+ if err != nil {
112
+ return err
113
+ }
114
+
115
+ if d < 0 {
116
+ return fmt .Errorf ("negative duration under --auth.exp is not allowed" )
117
+ }
118
+
119
+ authOptions .expDuration = d
120
+ }
121
+
122
+ return nil
123
+ }
124
+
125
+ func durationParser (duration string , defaultUnit string ) (time.Duration , error ) {
126
+ if d , err := time .ParseDuration (duration ); err == nil {
127
+ return d , nil
128
+ } else {
129
+ if ! strings .HasPrefix (err .Error (), "time: missing unit in duration " ) {
130
+ return 0 , err
131
+ }
132
+
133
+ duration = fmt .Sprintf ("%s%s" , duration , defaultUnit )
134
+
135
+ if d , err := time .ParseDuration (duration ); err == nil {
136
+ return d , nil
137
+ } else {
138
+ return 0 , err
139
+ }
140
+ }
141
+ }
0 commit comments