2018-07-25 05:30:33 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-06 00:38:42 -04:00
|
|
|
# Registration information for U2F (universal 2nd factor) devices, like Yubikeys
|
|
|
|
|
2019-03-28 09:17:42 -04:00
|
|
|
class U2fRegistration < ApplicationRecord
|
2016-06-06 00:38:42 -04:00
|
|
|
belongs_to :user
|
2020-10-16 08:09:33 -04:00
|
|
|
|
2021-02-04 16:09:06 -05:00
|
|
|
after_create :create_webauthn_registration
|
|
|
|
after_update :update_webauthn_registration, if: :counter_changed?
|
|
|
|
|
|
|
|
def create_webauthn_registration
|
|
|
|
converter = Gitlab::Auth::U2fWebauthnConverter.new(self)
|
|
|
|
WebauthnRegistration.create!(converter.convert)
|
|
|
|
rescue StandardError => ex
|
|
|
|
Gitlab::AppJsonLogger.error(
|
|
|
|
event: 'u2f_migration',
|
|
|
|
error: ex.class.name,
|
|
|
|
backtrace: ::Gitlab::BacktraceCleaner.clean_backtrace(ex.backtrace),
|
|
|
|
message: "U2F to WebAuthn conversion failed")
|
2020-10-16 08:09:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2016-06-06 00:38:42 -04:00
|
|
|
|
2016-08-16 12:18:36 -04:00
|
|
|
def self.register(user, app_id, params, challenges)
|
2016-06-06 00:38:42 -04:00
|
|
|
u2f = U2F::U2F.new(app_id)
|
|
|
|
registration = self.new
|
|
|
|
|
|
|
|
begin
|
2016-08-16 12:18:36 -04:00
|
|
|
response = U2F::RegisterResponse.load_from_json(params[:device_response])
|
2016-06-06 00:38:42 -04:00
|
|
|
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,
|
2016-08-16 12:18:36 -04:00
|
|
|
user: user,
|
|
|
|
name: params[:name])
|
2016-06-06 00:38:42 -04:00
|
|
|
rescue JSON::ParserError, NoMethodError, ArgumentError
|
2019-04-12 08:28:07 -04:00
|
|
|
registration.errors.add(:base, _('Your U2F device did not send a valid JSON response.'))
|
2016-06-06 00:38:42 -04:00
|
|
|
rescue U2F::Error => e
|
|
|
|
registration.errors.add(:base, e.message)
|
|
|
|
end
|
|
|
|
|
|
|
|
registration
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2020-10-16 08:09:33 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def webauthn_credential_xid
|
|
|
|
# To find the corresponding webauthn registration, we use that
|
|
|
|
# the key handle of the u2f reg corresponds to the credential xid of the webauthn reg
|
|
|
|
# (with some base64 back and forth)
|
|
|
|
Base64.strict_encode64(Base64.urlsafe_decode64(key_handle))
|
|
|
|
end
|
2016-06-06 00:38:42 -04:00
|
|
|
end
|