Class: Passkeyed::Credential

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/passkeyed/credential.rb,
sig/passkeyed/credential.rbs

Overview

Inherits Passkeyed.base_record_class (ApplicationRecord / ActiveRecord::Base) at load time; declared here against ActiveRecord::Base for typing.

Instance Method Summary collapse

Instance Method Details

#record_sign_in!(new_sign_count, backed_up: nil) ⇒ void

This method returns an undefined value.

Persist the state a successful assertion produces: the advanced signature counter, and — when the columns are present — a last-used timestamp and the current backup state (a device-bound passkey may later start syncing, flipping the flag). The columns are guarded so authentication keeps working on apps that trimmed the optional columns from the migration.

Parameters:

  • new_sign_count (Integer)
  • backed_up: (Boolean, nil) (defaults to: nil)


31
32
33
34
35
36
37
38
39
40
# File 'lib/passkeyed/credential.rb', line 31

def record_sign_in!(new_sign_count, backed_up: nil)
  attributes = { sign_count: new_sign_count }
  attributes[:last_used_at] = Time.current if has_attribute?(:last_used_at)
  attributes[:backed_up] = backed_up if !backed_up.nil? && has_attribute?(:backed_up)
  assign_attributes(attributes)
  # Everything written here is server-derived, so skip validations: a record
  # invalid by rules added after it was persisted (e.g. a nickname cap
  # introduced in a future version) must not start failing every sign-in.
  save!(validate: false)
end

#transportsArray[String]?

transports is stored as a JSON string (portable across databases); expose it as the Array WebAuthn reported, e.g. ["internal", "hybrid"]. Unparseable stored data (a manual write, a botched import) reads as nil rather than raising out of every page that lists credentials.

Returns:

  • (Array[String], nil)


46
47
48
49
50
51
# File 'lib/passkeyed/credential.rb', line 46

def transports
  raw = super
  raw.is_a?(String) ? JSON.parse(raw) : raw
rescue JSON::ParserError
  nil
end

#transports=(value) ⇒ Object

Parameters:

  • value (Array[String], String, nil)

Returns:

  • (Object)


53
54
55
56
# File 'lib/passkeyed/credential.rb', line 53

def transports=(value)
  value = JSON.generate(value) unless value.nil? || value.is_a?(String)
  super
end