Module: Altcha

Defined in:
lib/altcha-rails.rb

Defined Under Namespace

Classes: Challenge, ConfigurationError, Submission

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.algorithmObject

Returns the value of attribute algorithm.



13
14
15
# File 'lib/altcha-rails.rb', line 13

def algorithm
  @algorithm
end

.cache_key_prefixObject

Returns the value of attribute cache_key_prefix.



13
14
15
# File 'lib/altcha-rails.rb', line 13

def cache_key_prefix
  @cache_key_prefix
end

.hmac_keyObject

Returns the value of attribute hmac_key.



13
14
15
# File 'lib/altcha-rails.rb', line 13

def hmac_key
  @hmac_key
end

.max_numberObject

Returns the value of attribute max_number.



13
14
15
# File 'lib/altcha-rails.rb', line 13

def max_number
  @max_number
end

.timeoutObject

Returns the value of attribute timeout.



13
14
15
# File 'lib/altcha-rails.rb', line 13

def timeout
  @timeout
end

Class Method Details

.create_challenge(**options) ⇒ Object

Returns an Altcha::Challenge. Its #to_json produces the payload the widget expects via the ‘challenge` attribute.



28
29
30
# File 'lib/altcha-rails.rb', line 28

def self.create_challenge(**options)
  Challenge.create(**options)
end

.setup {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Altcha)

    the object that the method was called on



22
23
24
# File 'lib/altcha-rails.rb', line 22

def self.setup
  yield self
end

.verify(base64_string) ⇒ Object

Verifies a base64-encoded JSON submission AND records it in Rails.cache for replay protection (atomic via ‘unless_exist: true`, TTL = timeout). Returns the Altcha::Submission on a fresh accept, nil on failure (invalid crypto, expired, spliced, or replay within the timeout window).



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/altcha-rails.rb', line 36

def self.verify(base64_string)
  submission = Submission.verify(base64_string)
  return nil unless submission

  if Rails.cache.write("#{cache_key_prefix}#{submission.signature}", true,
                       expires_in: timeout, unless_exist: true)
    submission
  else
    nil # replay
  end
end