Module: Tokenifier::Cipher

Included in:
Tokenifier
Defined in:
lib/tokenifier/cipher.rb

Instance Method Summary collapse

Instance Method Details

#cipher(*args, &block) ⇒ Object



12
13
14
15
# File 'lib/tokenifier/cipher.rb', line 12

def cipher(*args, &block)
  aes = Gibberish::AES.new(args.first || secret)
  block_given? ? yield(aes) : aes
end

#load_rails_secretObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tokenifier/cipher.rb', line 17

def load_rails_secret
  return unless defined?(Rails)

  filename = Rails.root.join('config', 'tokenifier.yml')

  if File.exists?(filename)
    config = YAML.load(
      ERB.new(
        IO.read(filename)
      ).result
    )[Rails.env]

    @secret = config['secret']
  else
    @secret = Tokenifier::Random.secret

    Rails.logger.warn "*** Tokenifier warning:"
    Rails.logger.warn "*** Config tokenifier.yml file not found."
    Rails.logger.warn "*** Run rails g tokenifier:install to generate one"
    Rails.logger.warn "*** Using secret: #{@secret}"
  end

  @secret
end

#secretObject



6
7
8
9
10
# File 'lib/tokenifier/cipher.rb', line 6

def secret
  @secret ||= load_rails_secret
  @secret ||= Tokenifier::Random.secret
  @secret
end