Skip to content

Commit 5466810

Browse files
committed
3.2 add secret token
> Railsでセッション変数の暗号化に使用するための、いわゆる秘密トークン (secret token) を必ず更新することが重要です。
1 parent 803c2da commit 5466810

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
1414
# Ignore all logfiles and tempfiles.
1515
/log/*.log
1616
/tmp
17+
18+
.DS_Store
19+
.secret

config/initializers/secret_token.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Be sure to restart your server when you modify this file.
2+
3+
# Your secret key is used for verifying the integrity of signed cookies.
4+
# If you change this key, all old signed cookies will become invalid!
5+
6+
# Make sure the secret is at least 30 characters and all random,
7+
# no regular words or you'll be exposed to dictionary attacks.
8+
# You can use `rake secret` to generate a secure secret key.
9+
10+
# Make sure your secret_key_base is kept private
11+
# if you're sharing your code publicly.
12+
require 'securerandom'
13+
14+
def secure_token
15+
token_file = Rails.root.join('.secret')
16+
if File.exist?(token_file)
17+
# Use the existing token.
18+
File.read(token_file).chomp
19+
else
20+
# Generate a new token and store it in token_file.
21+
token = SecureRandom.hex(64)
22+
File.write(token_file, token)
23+
token
24+
end
25+
end
26+
27+
SampleApp::Application.config.secret_key_base = secure_token

0 commit comments

Comments
 (0)