Class: U2fRegistration

Inherits:
ApplicationRecord show all
Defined in:
app/models/u2f_registration.rb

Overview

Registration information for U2F (universal 2nd factor) devices, like Yubikeys

Constant Summary

Constants inherited from ApplicationRecord

ApplicationRecord::MAX_PLUCK

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

cached_column_list, #create_or_load_association, declarative_enum, default_select_columns, id_in, id_not_in, iid_in, pluck_primary_key, primary_key_in, #readable_by?, safe_ensure_unique, safe_find_or_create_by, safe_find_or_create_by!, #to_ability_name, underscore, where_exists, where_not_exists, with_fast_read_statement_timeout, without_order

Methods included from SensitiveSerializableHash

#serializable_hash

Class Method Details

.authenticate(user, app_id, json_response, challenges) ⇒ Object


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/u2f_registration.rb', line 47

def self.authenticate(user, app_id, json_response, challenges)
  response = U2F::SignResponse.load_from_json(json_response)
  registration = user.u2f_registrations.find_by_key_handle(response.key_handle)
  u2f = U2F::U2F.new(app_id)

  if registration
    u2f.authenticate!(challenges, response, Base64.decode64(registration.public_key), registration.counter)
    registration.update(counter: response.counter)
    true
  end
rescue JSON::ParserError, NoMethodError, ArgumentError, U2F::Error
  false
end

.register(user, app_id, params, challenges) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/models/u2f_registration.rb', line 25

def self.register(user, app_id, params, challenges)
  u2f = U2F::U2F.new(app_id)
  registration = self.new

  begin
    response = U2F::RegisterResponse.load_from_json(params[:device_response])
    registration_data = u2f.register!(challenges, response)
    registration.update(certificate: registration_data.certificate,
                        key_handle: registration_data.key_handle,
                        public_key: registration_data.public_key,
                        counter: registration_data.counter,
                        user: user,
                        name: params[:name])
  rescue JSON::ParserError, NoMethodError, ArgumentError
    registration.errors.add(:base, _('Your U2F device did not send a valid JSON response.'))
  rescue U2F::Error => e
    registration.errors.add(:base, e.message)
  end

  registration
end

Instance Method Details

#create_webauthn_registrationObject


11
12
13
14
15
16
# File 'app/models/u2f_registration.rb', line 11

def create_webauthn_registration
  converter = Gitlab::Auth::U2fWebauthnConverter.new(self)
  WebauthnRegistration.create!(converter.convert)
rescue StandardError => ex
  Gitlab::ErrorTracking.track_exception(ex, u2f_registration_id: self.id)
end

#update_webauthn_registrationObject


18
19
20
21
22
23
# File 'app/models/u2f_registration.rb', line 18

def update_webauthn_registration
  # When we update the sign count of this registration
  # we need to update the sign count of the corresponding webauthn registration
  # as well if it exists already
  WebauthnRegistration.find_by_credential_xid(webauthn_credential_xid)&.update_attribute(:counter, counter)
end