Module: StandardId::SessionTypeResolver

Defined in:
lib/standard_id/session_type_resolver.rb

Overview

Resolves which Session subclass to create for a given auth flow.

Delegates to ‘StandardId.config.session.session_type_resolver` when set. Falls back to `DEFAULT` which mirrors the gem’s historical behaviour.

Return values from a configured resolver may be:

- A Session subclass (BrowserSession, DeviceSession, ServiceSession)
- A symbol (:browser, :device, :service) — mapped to the matching class
- `nil` / `false` — meaning "do not create a session for this flow".
  This is only honoured by callsites that support it (see `resolve!`
  vs `resolve_optional!`).

Constant Summary collapse

SYMBOL_MAP =
{
  browser: "StandardId::BrowserSession",
  device: "StandardId::DeviceSession",
  service: "StandardId::ServiceSession"
}.freeze
DEFAULT =

Default resolver — returns the class the gem would historically choose for each flow. Defined as a lambda (not a Proc) so missing kwargs raise.

->(request:, account:, flow:) {
  case flow
  when :web_sign_in then :browser
  when :api_device_auth then :device
  when :api_service_auth then :service
  when :oauth_token_issued then nil # gem historically persists no session here
  else
    # Unknown flow — raise loudly rather than silently defaulting to :browser.
    # A misspelled flow symbol or a newly-added flow that forgot to register
    # here would otherwise mint a BrowserSession with no indication anything
    # was wrong. Host apps that add custom flows are expected to supply
    # their own resolver.
    raise StandardId::ConfigurationError,
      "session_type_resolver: unknown flow #{flow.inspect}. " \
      "Configure StandardId.config.session.session_type_resolver to handle it, " \
      "or use one of: :web_sign_in, :api_device_auth, :api_service_auth, :oauth_token_issued."
  end
}

Class Method Summary collapse

Class Method Details

.resolve!(request:, account:, flow:) ⇒ Object

Resolve the session class for a flow where session creation is mandatory (web sign-in, API device/service auth). Returns a Class. Raises ConfigurationError on nil / unknown returns.



45
46
47
48
49
50
51
52
53
# File 'lib/standard_id/session_type_resolver.rb', line 45

def resolve!(request:, account:, flow:)
  klass = coerce(call_resolver(request: request, account: , flow: flow), flow: flow)
  if klass.nil?
    raise StandardId::ConfigurationError,
      "session_type_resolver returned nil for flow #{flow.inspect}; " \
      "this flow requires a session class (one of :browser, :device, :service)"
  end
  klass
end

.resolve_optional(request:, account:, flow:) ⇒ Object

Resolve the session class for a flow where session creation is optional (oauth_token_issued). Returns a Class or nil. Raises ConfigurationError on invalid non-nil returns.



58
59
60
# File 'lib/standard_id/session_type_resolver.rb', line 58

def resolve_optional(request:, account:, flow:)
  coerce(call_resolver(request: request, account: , flow: flow), flow: flow)
end