Author: Pedro Lucas Porcellis <porcellis@eletrotupi.com>
cmd/keys: generate a fernet key This command is a helper to generate the Fernet Key that we'll use to encrypt and sign cookies on sessions. It outputs a safe url base64 encoded string, that we will need to write it down on `config.ini` file. When this key changes, it'll automatically make all sessions expires
cmd/keys.go | 28 ++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++
diff --git a/cmd/keys.go b/cmd/keys.go new file mode 100644 index 0000000000000000000000000000000000000000..43dc1161caa36c7728bd0a7eb89b4705806d4a68 --- /dev/null +++ b/cmd/keys.go @@ -0,0 +1,28 @@ +package cmd + +import ( + "fmt" + "log" + "os" + + "github.com/fernet/fernet-go" +) + +const usage = `Here's your fernet key. Save on your config.ini file! + +Remember that if this key changes, you will invalidate all sessions, as this key +is used to encrypt and sign every cookie. + +` + +func GenerateKey() { + var key fernet.Key + + if err := key.Generate(); err != nil { + log.Fatalf("Failed to generate the Fernet key due to %v", err) + os.Exit(1) + } + + fmt.Print(usage) + fmt.Printf("\t %v \n\n", key.Encode()) +} diff --git a/go.mod b/go.mod index aaa3533e77440317ca0047540d733a8bfe41c1da..2b957585277f6e6bf9ba59bea5c68ce100f5f5b3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ go 1.16 require ( + github.com/fernet/fernet-go v0.0.0-20191111064656-eff2850e6001 // indirect github.com/go-chi/chi/v5 v5.0.3 github.com/go-redis/redis/v8 v8.9.0 // indirect github.com/vaughan0/go-ini v0.0.0-20130923145212-a98ad7ee00ec // indirect diff --git a/go.sum b/go.sum index c247c94b038fe706f698e4deeb42f2f63039fe49..c4471760036e080c1d6476ea5973333495fbe08a 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +github.com/fernet/fernet-go v0.0.0-20191111064656-eff2850e6001 h1:/UMxx5lGDg30aioUL9e7xJnbJfJeX7vhcm57fa5udaI= +github.com/fernet/fernet-go v0.0.0-20191111064656-eff2850e6001/go.mod h1:2H9hjfbpSMHwY503FclkV/lZTBh2YlOmLLSda12uL8c= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/go-chi/chi/v5 v5.0.3 h1:khYQBdPivkYG1s1TAzDQG1f6eX4kD2TItYVZexL5rS4=