Class: TwoFactorAuth::ClientData

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
app/models/two_factor_auth/client_data.rb

Direct Known Subclasses

AuthenticationClientData

Instance Method Summary collapse

Constructor Details

#initialize(*args, &blk) ⇒ ClientData

Returns a new instance of ClientData.

Raises:

  • (ArgumentError)


23
24
25
26
27
# File 'app/models/two_factor_auth/client_data.rb', line 23

def initialize *args, &blk
  super
  decompose_attrs if encoded.present?
  raise ArgumentError, "correct_typ is mandatory" if correct_typ.blank?
end

Instance Method Details

#client_data_has_correct_keysObject



43
44
45
46
47
# File 'app/models/two_factor_auth/client_data.rb', line 43

def client_data_has_correct_keys
  if attrs.keys != %w{typ challenge origin cid_pubkey}
    errors.add :attrs, "has wrong keys: #{attrs.keys.join(', ')}"
  end
end

#decompose_attrsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/two_factor_auth/client_data.rb', line 29

def decompose_attrs
  self.json = TwoFactorAuth::websafe_base64_decode(encoded)
  self.attrs = JSON.parse(json)

  self.typ        = attrs['typ']
  self.challenge  = attrs['challenge']
  self.origin     = attrs['origin']
  self.cid_pubkey = attrs['cid_pubkey']
rescue ArgumentError => e
  errors.add(:encoded, "Can't decode base64: #{e.message}")
rescue JSON::ParserError => e
  errors.add(:json, "Can't parse json: #{e.message}")
end

#persisted?Boolean

Returns:

  • (Boolean)


55
# File 'app/models/two_factor_auth/client_data.rb', line 55

def persisted? ; false ; end

#typ_correctObject



49
50
51
52
53
# File 'app/models/two_factor_auth/client_data.rb', line 49

def typ_correct
  if typ != correct_typ
    errors.add :typ, "should be navigator.id.getAssertion but is #{typ}"
  end
end