chrly/cmd/token.go

66 lines
1.5 KiB
Go
Raw Normal View History

2018-01-16 02:22:22 +05:30
package cmd
import (
"fmt"
"log"
"elyby/minecraft-skinsystem/auth"
"github.com/segmentio/go-prompt"
"github.com/spf13/cobra"
)
var tokenCmd = &cobra.Command{
Use: "token",
2018-02-15 02:19:22 +05:30
Short: "API tokens manipulation",
2018-01-16 02:22:22 +05:30
}
var createCmd = &cobra.Command{
Use: "create",
2018-02-15 02:19:22 +05:30
Short: "Creates a new token, which allows to interact with Chrly API",
2018-01-16 02:22:22 +05:30
Run: func(cmd *cobra.Command, args []string) {
jwtAuth := &auth.JwtAuth{}
for {
token, err := jwtAuth.NewToken(auth.SkinScope)
if err != nil {
if _, ok := err.(*auth.SigningKeyNotAvailable); !ok {
log.Fatalf("Unable to create new token. The error is %v\n", err)
}
log.Println("Signing key not available. Creating...")
err := jwtAuth.GenerateSigningKey()
if err != nil {
log.Fatalf("Unable to generate new signing key. The error is %v\n", err)
}
continue
}
fmt.Printf("%s\n", token)
}
},
}
var resetCmd = &cobra.Command{
Use: "reset",
2018-02-15 02:19:22 +05:30
Short: "Re-creates the secret key, which invalidate all tokens",
2018-01-16 02:22:22 +05:30
Run: func(cmd *cobra.Command, args []string) {
if !prompt.Confirm("Do you really want to invalidate all exists tokens?") {
fmt.Println("Aboart.")
return
}
jwtAuth := &auth.JwtAuth{}
if err := jwtAuth.GenerateSigningKey(); err != nil {
log.Fatalf("Unable to generate new signing key. The error is %v\n", err)
}
fmt.Println("Token successfully regenerated.")
},
}
func init() {
tokenCmd.AddCommand(createCmd, resetCmd)
RootCmd.AddCommand(tokenCmd)
}