Module: Altcha
- Defined in:
- lib/altcha-rails.rb
Defined Under Namespace
Classes: Challenge, ConfigurationError, Submission
Class Attribute Summary collapse
-
.algorithm ⇒ Object
Returns the value of attribute algorithm.
-
.cache_key_prefix ⇒ Object
Returns the value of attribute cache_key_prefix.
-
.hmac_key ⇒ Object
Returns the value of attribute hmac_key.
-
.max_number ⇒ Object
Returns the value of attribute max_number.
-
.timeout ⇒ Object
Returns the value of attribute timeout.
Class Method Summary collapse
-
.create_challenge(**options) ⇒ Object
Returns an Altcha::Challenge.
- .setup {|_self| ... } ⇒ Object
-
.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).
Class Attribute Details
.algorithm ⇒ Object
Returns the value of attribute algorithm.
13 14 15 |
# File 'lib/altcha-rails.rb', line 13 def algorithm @algorithm end |
.cache_key_prefix ⇒ Object
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_key ⇒ Object
Returns the value of attribute hmac_key.
13 14 15 |
# File 'lib/altcha-rails.rb', line 13 def hmac_key @hmac_key end |
.max_number ⇒ Object
Returns the value of attribute max_number.
13 14 15 |
# File 'lib/altcha-rails.rb', line 13 def max_number @max_number end |
.timeout ⇒ Object
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(**) Challenge.create(**) end |
.setup {|_self| ... } ⇒ Object
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 |