Class: Unmagic::Passkeys::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/unmagic/passkeys/configuration.rb

Overview

Central configuration for the passkeys engine, set through a single Unmagic::Passkeys.configure block. The defaults here are the single source of truth — there is no separate Rails engine config.

# config/initializers/passkeys.rb
Unmagic::Passkeys.configure do |config|
config.relying_party_name           = "Shopping"
config.request_challenge_expiration  = 5.minutes
config.default_request_options       = { user_verification: :required }
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/unmagic/passkeys/configuration.rb', line 45

def initialize
  @parent_class_name = "ApplicationRecord"
  @routes_prefix = "/unmagic/passkeys"
  @draw_routes = true
  @challenge_url = nil
  @default_creation_options = {}
  @default_request_options = {}
  @creation_challenge_expiration = 10.minutes
  @request_challenge_expiration = 5.minutes
  @relying_party_id = nil
  @relying_party_name = nil
  @base_controller = "ApplicationController"
  @sign_in = nil
  @sign_out = nil
  @current_holder = nil
end

Instance Attribute Details

#base_controllerObject

The controller the engine's base controllers inherit from. Inherit from the host's ApplicationController so the hook blocks below can call your app's session helpers (e.g. start_new_session_for).



43
44
45
# File 'lib/unmagic/passkeys/configuration.rb', line 43

def base_controller
  @base_controller
end

#challenge_urlObject

Optional callable, +instance_exec+'d in the view, returning the URL the form helpers point the challenge fetch at. Defaults to the engine's challenge path when nil.



27
28
29
# File 'lib/unmagic/passkeys/configuration.rb', line 27

def challenge_url
  @challenge_url
end

#creation_challenge_expirationObject

How long an issued challenge stays valid, per ceremony.



34
35
36
# File 'lib/unmagic/passkeys/configuration.rb', line 34

def creation_challenge_expiration
  @creation_challenge_expiration
end

#default_creation_optionsObject

Global option defaults merged into every registration / authentication ceremony.



31
32
33
# File 'lib/unmagic/passkeys/configuration.rb', line 31

def default_creation_options
  @default_creation_options
end

#default_request_optionsObject

Global option defaults merged into every registration / authentication ceremony.



31
32
33
# File 'lib/unmagic/passkeys/configuration.rb', line 31

def default_request_options
  @default_request_options
end

#draw_routesObject

Where the stateless challenge endpoint is mounted, and whether the engine draws it at all. Override these in config/application.rb if you need them applied before the engine draws its routes.



22
23
24
# File 'lib/unmagic/passkeys/configuration.rb', line 22

def draw_routes
  @draw_routes
end

#parent_class_nameObject

The Active Record base class the Credential model inherits from.



17
18
19
# File 'lib/unmagic/passkeys/configuration.rb', line 17

def parent_class_name
  @parent_class_name
end

#relying_party_idObject

Relying party identity. When nil, falls back to the request host and Rails.application.name respectively.



38
39
40
# File 'lib/unmagic/passkeys/configuration.rb', line 38

def relying_party_id
  @relying_party_id
end

#relying_party_nameObject

Relying party identity. When nil, falls back to the request host and Rails.application.name respectively.



38
39
40
# File 'lib/unmagic/passkeys/configuration.rb', line 38

def relying_party_name
  @relying_party_name
end

#request_challenge_expirationObject

How long an issued challenge stays valid, per ceremony.



34
35
36
# File 'lib/unmagic/passkeys/configuration.rb', line 34

def request_challenge_expiration
  @request_challenge_expiration
end

#routes_prefixObject

Where the stateless challenge endpoint is mounted, and whether the engine draws it at all. Override these in config/application.rb if you need them applied before the engine draws its routes.



22
23
24
# File 'lib/unmagic/passkeys/configuration.rb', line 22

def routes_prefix
  @routes_prefix
end

Instance Method Details

#current_holder(&block) ⇒ Object



89
90
91
# File 'lib/unmagic/passkeys/configuration.rb', line 89

def current_holder(&block)
  block ? @current_holder = block : @current_holder
end

#sign_in(&block) ⇒ Object

Controller hooks

Each is a block +instance_exec+'d in the engine's base controllers, so it has full access to the request, session, and any helpers your base_controller provides. Call with a block to set, without to read.

sign_in turns an authenticated holder into an app session (required to use the sign-in flow). sign_out tears it down. current_holder returns the signed-in holder (used by the credentials management controller).

Account creation (signup) is the host app's responsibility — once you've created/identified a holder, register a passkey for it with holder.passkeys.register(params) and call sign_in.

Unmagic::Passkeys.configure do |config|
config.        { |holder| start_new_session_for(holder) }
config.sign_out       { terminate_session }
config.current_holder { Current.user }
end


81
82
83
# File 'lib/unmagic/passkeys/configuration.rb', line 81

def (&block)
  block ? @sign_in = block : @sign_in
end

#sign_out(&block) ⇒ Object



85
86
87
# File 'lib/unmagic/passkeys/configuration.rb', line 85

def sign_out(&block)
  block ? @sign_out = block : @sign_out
end