Class: SignedForm::GateKeeper

Inherits:
Object
  • Object
show all
Defined in:
lib/signed_form/gate_keeper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ GateKeeper

Returns a new instance of GateKeeper


5
6
7
8
9
10
11
12
13
# File 'lib/signed_form/gate_keeper.rb', line 5

def initialize(controller)
  @controller = controller
  @params     = controller.params
  @request    = controller.request

  extract_and_verify_form_signature
  verify_destination
  verify_digest
end

Instance Attribute Details

#allowed_attributesObject (readonly)

Returns the value of attribute allowed_attributes


3
4
5
# File 'lib/signed_form/gate_keeper.rb', line 3

def allowed_attributes
  @allowed_attributes
end

Instance Method Details

#extract_and_verify_form_signatureObject


19
20
21
22
23
24
25
26
27
28
29
# File 'lib/signed_form/gate_keeper.rb', line 19

def extract_and_verify_form_signature
  data, signature = @params['form_signature'].split('--', 2)
  hmac = SignedForm::HMAC.new secret_key: SignedForm.secret_key

  signature ||= ''

  raise Errors::InvalidSignature, "Form signature is not valid" unless hmac.verify signature, data

  @allowed_attributes = Marshal.load Base64.strict_decode64(data)
  @options            = allowed_attributes.delete(:_options_)
end

#optionsObject


15
16
17
# File 'lib/signed_form/gate_keeper.rb', line 15

def options
  @options ||= {}
end

#verify_destinationObject

Raises:


31
32
33
34
35
36
# File 'lib/signed_form/gate_keeper.rb', line 31

def verify_destination
  return unless options[:method] && options[:url]
  raise Errors::InvalidURL if options[:method].to_s.casecmp(@request.request_method) != 0
  url = @controller.url_for(options[:url])
  raise Errors::InvalidURL if url != @request.fullpath && url != @request.url
end

#verify_digestObject


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/signed_form/gate_keeper.rb', line 38

def verify_digest
  return unless options[:digest]

  return if options[:digest_expiration] && Time.now < options[:digest_expiration]

  digestor = options[:digest]
  given_digest = digestor.to_s
  digestor.view_paths = @controller.view_paths.map(&:to_s)
  digestor.refresh
  raise Errors::ExpiredForm unless given_digest == digestor.to_s
end