Class: StandardId::Passwordless::BaseStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/standard_id/passwordless/base_strategy.rb

Direct Known Subclasses

EmailStrategy, SmsStrategy

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, realm: StandardId::Passwordless::DEFAULT_REALM) ⇒ BaseStrategy

Returns a new instance of BaseStrategy.



6
7
8
9
# File 'lib/standard_id/passwordless/base_strategy.rb', line 6

def initialize(request, realm: StandardId::Passwordless::DEFAULT_REALM)
  @request = request
  @realm = realm.to_s
end

Instance Attribute Details

#realmObject (readonly)

Returns the value of attribute realm.



4
5
6
# File 'lib/standard_id/passwordless/base_strategy.rb', line 4

def realm
  @realm
end

#requestObject (readonly)

Returns the value of attribute request.



4
5
6
# File 'lib/standard_id/passwordless/base_strategy.rb', line 4

def request
  @request
end

Instance Method Details

#connection_typeObject

Raises:

  • (NotImplementedError)


11
12
13
# File 'lib/standard_id/passwordless/base_strategy.rb', line 11

def connection_type
  raise NotImplementedError
end

#find_account(username) ⇒ Object

Public wrapper to look up an existing account without creating one. Returns nil if no account is found for the given username.



115
116
117
118
# File 'lib/standard_id/passwordless/base_strategy.rb', line 115

def (username)
  validate_username!(username)
  (username)
end

#find_or_create_account(username) ⇒ Object

Public wrapper to reuse account lookup/creation outside OTP verification. When a custom account_factory is configured, delegates to it instead of the built-in find_or_create_account! logic.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/standard_id/passwordless/base_strategy.rb', line 96

def (username)
  validate_username!(username)

  factory = StandardId.config.passwordless.
  if factory.respond_to?(:call)
     = factory.call(
      identifier: username,
      params: request_params,
      request: request
    )
    raise StandardId::InvalidRequestError, "account_factory must return an account" unless .present?
    
  else
    find_or_create_account!(username)
  end
end

#identifier_classObject

Raises:

  • (NotImplementedError)


120
121
122
# File 'lib/standard_id/passwordless/base_strategy.rb', line 120

def identifier_class
  raise NotImplementedError
end

#sender_callbackObject



124
125
126
127
# File 'lib/standard_id/passwordless/base_strategy.rb', line 124

def sender_callback
  # Implement in subclasses
  nil
end

#start!(attrs) ⇒ Object

Start flow: validate recipient, create challenge, and trigger sender attrs: { connection:, username:, code_length:, expires_in:, metadata:, skip_sender: }



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/standard_id/passwordless/base_strategy.rb', line 17

def start!(attrs)
  username = attrs[:username]
  code_length = attrs[:code_length]
  expires_in = attrs[:expires_in]
   = attrs[:metadata] || {}
  skip_sender = attrs[:skip_sender] == true

  validate_username!(username)
  run_username_validator!(username)
  emit_code_requested(username)
  challenge = create_challenge!(
    username,
    code_length: code_length,
    expires_in: expires_in,
    metadata: 
  )
  emit_code_generated(challenge, username)
  sender_callback&.call(username, challenge.code) unless skip_sender
  emit_code_sent(username) unless skip_sender
  challenge
end