Skip to content

Commit e948ceb

Browse files
committed
Added ability to configure token expiry time.
1 parent fdf9996 commit e948ceb

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

cmd/jwt-generate.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@ var jwtGenerateCmd = &cobra.Command{
2727
username := args[0]
2828
canRead, _ := cmd.Flags().GetBool("can-read")
2929
canWrite, _ := cmd.Flags().GetBool("can-write")
30+
expiresInStr, _ := cmd.Flags().GetString("expires-in")
31+
32+
var expiresInClaim *jwt.NumericDate
33+
if expiresInStr == "none" {
34+
// leave the expiresInClaim as nil
35+
} else {
36+
expiresIn, err := time.ParseDuration(expiresInStr)
37+
if err != nil {
38+
logger.Fatal("failed to parse expires-in duration", zap.Error(err))
39+
}
40+
41+
expiresInClaim = jwt.NewNumericDate(time.Now().Add(expiresIn))
42+
}
3043

3144
var roles []string
3245
if canWrite {
@@ -57,7 +70,7 @@ var jwtGenerateCmd = &cobra.Command{
5770
Issuer: "dino",
5871
Subject: username,
5972
Audience: []string{"client"},
60-
ExpiresAt: jwt.NewNumericDate(time.Now().Add(365 * 24 * time.Hour)),
73+
ExpiresAt: expiresInClaim,
6174
},
6275
})
6376

@@ -74,4 +87,5 @@ func init() {
7487
jwtCmd.AddCommand(jwtGenerateCmd)
7588
jwtGenerateCmd.Flags().Bool("can-read", true, "Whether the token can read data")
7689
jwtGenerateCmd.Flags().Bool("can-write", true, "Whether the token can write data")
90+
jwtGenerateCmd.Flags().String("expires-in", "8766h", "How long before the token expires (e.g. 24h, 30m, 10s, -1h) or 'none' for no expiration claim")
7791
}

0 commit comments

Comments
 (0)