Project

General

Profile

Bug #2109 » security.rake

put it into lib/tasks/ - Sandor Szücs, 01/05/2013 08:05 AM

 
namespace :security do

desc 'Generate new security token'
task :generate_token do
l = 128
token = nil
if RUBY_VERSION >= "1.9"
require 'securerandom'
token = SecureRandom.urlsafe_base64(l)[0..(l-1)]
elsif File.exists? "/dev/random"
token_illegal = File.read("/dev/random", l*4)
_zero = 0x30
_end = 0x7a
range = _end - _zero
token_ary = token_illegal.split(//).map do |b|
c = ((b[0] % range )+ 0x30).chr
if not c.match(/[0-9A-Za-z]/)
next
end
c
end
token = token_ary.select{|c| c}.join("")
token = token[0..(l-1)]
else
raise SecurityError, "You have to replace the security token in config/initializers/secret_token.rb yourself"
end
File.open("config/initializers/secret_token.rb", "w") do |fd|
fd.write("# Be sure to restart your server when you modify this file.

# Your secret key for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!
# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
Foreman::Application.config.secret_token = '#{token}'
")
end
end
end
    (1-1/1)