Class: NoPassword::Link::Verification

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Model, ActiveModel::Validations
Defined in:
lib/nopassword/link/verification.rb

Overview

Validates a token provided via a link against the challenge stored in session. Uses constant-time comparison to prevent timing attacks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(challenge:, provided_token:) ⇒ Verification

Returns a new instance of Verification.



15
16
17
18
# File 'lib/nopassword/link/verification.rb', line 15

def initialize(challenge:, provided_token:)
  @challenge = challenge
  @provided_token = provided_token
end

Instance Attribute Details

#challengeObject (readonly)

Returns the value of attribute challenge.



9
10
11
# File 'lib/nopassword/link/verification.rb', line 9

def challenge
  @challenge
end

#provided_tokenObject (readonly)

Returns the value of attribute provided_token.



9
10
11
# File 'lib/nopassword/link/verification.rb', line 9

def provided_token
  @provided_token
end

Instance Method Details

#different_browser?Boolean

Alias for clarity - this is the most common reason for missing challenge

Returns:

  • (Boolean)


45
46
47
# File 'lib/nopassword/link/verification.rb', line 45

def different_browser?
  missing_challenge?
end

#expired?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/nopassword/link/verification.rb', line 34

def expired?
  return true if challenge.created_at.nil?
  Time.current > challenge.expires_at
end

#missing_challenge?Boolean

Returns true if no challenge exists in session (e.g., different browser)

Returns:

  • (Boolean)


40
41
42
# File 'lib/nopassword/link/verification.rb', line 40

def missing_challenge?
  challenge.token.blank?
end

#persisted?Boolean

For form routing - verification is always "persisted" (exists in session)

Returns:

  • (Boolean)


25
26
27
# File 'lib/nopassword/link/verification.rb', line 25

def persisted?
  true
end

#to_paramObject

For form routing - use the provided token as the ID



30
31
32
# File 'lib/nopassword/link/verification.rb', line 30

def to_param
  provided_token
end

#verifyObject



20
21
22
# File 'lib/nopassword/link/verification.rb', line 20

def verify
  valid?
end