aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar √Čtienne Deparis 2022-07-12 19:24:17 +0200
committerGravatar √Čtienne Deparis 2022-07-12 19:25:46 +0200
commita99060128222cb11ef06fe8ba26b92910ae846fa (patch)
treec31a971aabcec0ee6ecf2260cfa79878e23cd654
parent97f3623ac9e94b3cde1413286631138f6b54b8e5 (diff)
downloadbiberons-a99060128222cb11ef06fe8ba26b92910ae846fa.tar.gz
biberons-a99060128222cb11ef06fe8ba26b92910ae846fa.tar.xz
biberons-a99060128222cb11ef06fe8ba26b92910ae846fa.zip
feat: Add a rake task to ease cookie secret string generation
-rw-r--r--README.org3
-rw-r--r--lib/tasks/admin.rake14
2 files changed, 17 insertions, 0 deletions
diff --git a/README.org b/README.org
index a0472f6..f406abb 100644
--- a/README.org
+++ b/README.org
@@ -24,6 +24,9 @@ this repository, containing the following lines:
export BIBERON_COOKIE_DOMAIN=127.0.0.1
#+end_src
+The ~admin:session_secret~ rake task exists to help you generate a session
+secret string.
+
* Run
You can start a local server by using the following rake task.
diff --git a/lib/tasks/admin.rake b/lib/tasks/admin.rake
index 59219a2..43403fb 100644
--- a/lib/tasks/admin.rake
+++ b/lib/tasks/admin.rake
@@ -1,5 +1,7 @@
# frozen_string_literal: true
+require 'securerandom'
+
namespace :admin do
desc 'Create a new baby'
task :new_baby, [:name, :password] do |_, args|
@@ -11,4 +13,16 @@ namespace :admin do
baby.password = args[:password]
baby.save_changes
end
+
+ desc 'Create a secure session secret'
+ task :session_secret, [:length] do |_, args|
+ length = (args[:length]&.to_i || 128)
+ # Char 33 to 126 in ascii table are [a-zA-Z0-9] and some symbols
+ # Do not pick " ' \ or `
+ data = (33..126).to_a - [34, 39, 92, 96]
+ data_length = data.length
+ indexes = []
+ length.times { indexes << SecureRandom.random_number(data_length) }
+ puts data.values_at(*indexes).map(&:chr).join
+ end
end