2020-08-01 03:01:14 +02:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2020-09-13 01:03:17 +02:00
|
|
|
"errors"
|
2020-08-01 03:01:14 +02:00
|
|
|
"fmt"
|
2020-09-13 01:03:17 +02:00
|
|
|
"strings"
|
2020-08-01 03:01:14 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/jordanknott/taskcafe/internal/auth"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
2020-09-13 01:03:17 +02:00
|
|
|
"github.com/spf13/viper"
|
2020-08-01 03:01:14 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func newTokenCmd() *cobra.Command {
|
|
|
|
return &cobra.Command{
|
|
|
|
Use: "token",
|
|
|
|
Short: "Create a long lived JWT token for dev purposes",
|
|
|
|
Long: "Create a long lived JWT token for dev purposes",
|
|
|
|
Args: cobra.ExactArgs(1),
|
2020-09-13 01:03:17 +02:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
secret := viper.GetString("server.secret")
|
|
|
|
if strings.TrimSpace(secret) == "" {
|
|
|
|
return errors.New("server.secret must be set (TASKCAFE_SERVER_SECRET)")
|
|
|
|
}
|
|
|
|
token, err := auth.NewAccessTokenCustomExpiration(args[0], time.Hour*24, []byte(secret))
|
2020-08-01 03:01:14 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("issue while creating access token")
|
2020-09-13 01:03:17 +02:00
|
|
|
return err
|
2020-08-01 03:01:14 +02:00
|
|
|
}
|
|
|
|
fmt.Println(token)
|
2020-09-13 01:03:17 +02:00
|
|
|
return nil
|
2020-08-01 03:01:14 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|