mirror of
https://github.com/elyby/chrly.git
synced 2025-05-31 14:11:51 +05:30
The configuration file was deleted in favor of using environment variables.
Token generation functionality remove. Secret token now provided via CHRLY_SECRET env variable.
This commit is contained in:
19
cmd/root.go
19
cmd/root.go
@@ -3,6 +3,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"elyby/minecraft-skinsystem/bootstrap"
|
||||
|
||||
@@ -10,8 +11,6 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var cfgFile string
|
||||
|
||||
var RootCmd = &cobra.Command{
|
||||
Use: "chrly",
|
||||
Short: "Implementation of Minecraft skins system server",
|
||||
@@ -29,22 +28,10 @@ func Execute() {
|
||||
|
||||
func init() {
|
||||
cobra.OnInitialize(initConfig)
|
||||
RootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.test.yaml)")
|
||||
}
|
||||
|
||||
func initConfig() {
|
||||
if cfgFile != "" {
|
||||
viper.SetConfigFile(cfgFile)
|
||||
} else {
|
||||
viper.SetConfigName("config")
|
||||
viper.AddConfigPath("/etc/minecraft-skinsystem")
|
||||
viper.AddConfigPath(".")
|
||||
}
|
||||
|
||||
viper.AutomaticEnv()
|
||||
|
||||
if err := viper.ReadInConfig(); err == nil {
|
||||
// TODO: show only on verbose mode
|
||||
fmt.Println("Using config file:", viper.ConfigFileUsed())
|
||||
}
|
||||
replacer := strings.NewReplacer(".", "_")
|
||||
viper.SetEnvKeyReplacer(replacer)
|
||||
}
|
||||
|
@@ -47,7 +47,7 @@ var serveCmd = &cobra.Command{
|
||||
SkinsRepo: skinsRepo,
|
||||
CapesRepo: capesRepo,
|
||||
Logger: logger,
|
||||
Auth: &auth.JwtAuth{},
|
||||
Auth: &auth.JwtAuth{Key: []byte(viper.GetString("chrly.secret"))},
|
||||
}
|
||||
|
||||
if err := cfg.Run(); err != nil {
|
||||
@@ -58,4 +58,11 @@ var serveCmd = &cobra.Command{
|
||||
|
||||
func init() {
|
||||
RootCmd.AddCommand(serveCmd)
|
||||
viper.SetDefault("server.host", "")
|
||||
viper.SetDefault("server.port", 80)
|
||||
viper.SetDefault("storage.redis.host", "localhost")
|
||||
viper.SetDefault("storage.redis.port", 6379)
|
||||
viper.SetDefault("storage.redis.poll", 10)
|
||||
viper.SetDefault("storage.filesystem.basePath", "data")
|
||||
viper.SetDefault("storage.filesystem.capesDirName", "capes")
|
||||
}
|
||||
|
48
cmd/token.go
48
cmd/token.go
@@ -6,60 +6,24 @@ import (
|
||||
|
||||
"elyby/minecraft-skinsystem/auth"
|
||||
|
||||
"github.com/segmentio/go-prompt"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var tokenCmd = &cobra.Command{
|
||||
Use: "token",
|
||||
Short: "API tokens manipulation",
|
||||
}
|
||||
|
||||
var createCmd = &cobra.Command{
|
||||
Use: "create",
|
||||
Short: "Creates a new token, which allows to interact with Chrly API",
|
||||
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",
|
||||
Short: "Re-creates the secret key, which invalidate all tokens",
|
||||
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{Key: []byte(viper.GetString("chrly.secret"))}
|
||||
token, err := jwtAuth.NewToken(auth.SkinScope)
|
||||
if err != nil {
|
||||
log.Fatalf("Unable to create new token. The error is %v\n", err)
|
||||
}
|
||||
|
||||
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.")
|
||||
fmt.Printf("%s\n", token)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
tokenCmd.AddCommand(createCmd, resetCmd)
|
||||
RootCmd.AddCommand(tokenCmd)
|
||||
}
|
||||
|
Reference in New Issue
Block a user