Skip to content

Commit

Permalink
Generate secure secret key programmaticaly
Browse files Browse the repository at this point in the history
  • Loading branch information
dskecse committed Mar 3, 2014
1 parent a847754 commit 324d880
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.bundle .bundle
.secret
db/*.sqlite3 db/*.sqlite3
log/*.log log/*.log
tmp/ tmp/
Expand Down
1 change: 0 additions & 1 deletion config/app_config.example.yml
@@ -1,7 +1,6 @@
common: &common common: &common
host: 'localhost:3000' host: 'localhost:3000'
mail_sender: 'noreply@example.com' mail_sender: 'noreply@example.com'
secret: '69d0d5b549ec27657c1987d78ba3ef41c7cb51cd08343dcdd49a05ed69e1374188ba8817e780f7c459283c7a32ba82c02ea0abfe7594edca4136889bec019a7e'
smtp_settings: smtp_settings:
:address: 'smtp.gmail.com' :address: 'smtp.gmail.com'
:port: 587 :port: 587
Expand Down
13 changes: 12 additions & 1 deletion config/initializers/secret_token.rb
Expand Up @@ -4,4 +4,15 @@
# If you change this key, all old signed cookies will become invalid! # If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random, # Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks. # no regular words or you'll be exposed to dictionary attacks.
Depot2::Application.config.secret_token = AppConfig.secret require 'securerandom'

def secure_token
token_file = Rails.root.join('.secret')
unless File.exists?(token_file)
# Generate a new token and store it in token_file.
File.write(token_file, SecureRandom.hex(64))
end
File.read(token_file).chomp
end

Depot2::Application.config.secret_token = secure_token

0 comments on commit 324d880

Please sign in to comment.