Commit e444c7f6babe956ef22a775b8ec197d58765b6a0
1 parent
c5de01f8
Exists in
master
and in
4 other branches
Generate the Rails secret token on first run.
Store the secret token in a .gitignored file called ".secret", which is created by the initializer if it doesn't exist.
Showing
2 changed files
with
18 additions
and
1 deletions
Show diff stats
.gitignore
config/initializers/secret_token.rb
1 | 1 | # Be sure to restart your server when you modify this file. |
2 | 2 | |
3 | +require 'securerandom' | |
4 | + | |
3 | 5 | # Your secret key for verifying the integrity of signed cookies. |
4 | 6 | # If you change this key, all old signed cookies will become invalid! |
5 | 7 | # Make sure the secret is at least 30 characters and all random, |
6 | 8 | # no regular words or you'll be exposed to dictionary attacks. |
7 | -Gitlab::Application.config.secret_token = '0a38e9a40ca5d66d7002a6ade0ed0f8b71058c820163f66cf65d91521ab55255ff708b9909b138008a7f13d68fec575def1dc3ff7200cd72b065896315e0bed2' | |
9 | + | |
10 | +def find_secure_token | |
11 | + token_file = Rails.root.join('.secret') | |
12 | + if File.exist? token_file | |
13 | + # Use the existing token. | |
14 | + File.read(token_file).chomp | |
15 | + else | |
16 | + # Generate a new token of 64 random hexadecimal characters and store it in token_file. | |
17 | + token = SecureRandom.hex(64) | |
18 | + File.write(token_file, token) | |
19 | + token | |
20 | + end | |
21 | +end | |
22 | + | |
23 | +Gitlab::Application.config.secret_token = find_secure_token | ... | ... |