Class: Devise::FidoUsfRegistrationsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/devise/fido_usf_registrations_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/controllers/devise/fido_usf_registrations_controller.rb', line 35

def create
  begin
    response = U2F::RegisterResponse.load_from_json(params[:response])
    reg = u2f.register!(session[:challenges], response)

    pubkey = reg.public_key
    pubkey = Base64.decode64(reg.public_key) unless pubkey.bytesize == 65 && pubkey.byteslice(0) != "\x04"

    @device = FidoUsf::FidoUsfDevice.create!(
      user: resource,
      name: "Token ##{resource.fido_usf_devices.count + 1}",
      certificate: reg.certificate,
      key_handle: reg.key_handle,
      public_key: pubkey,
      counter: reg.counter,
      last_authenticated_at: Time.now
    )
    flash[:success] = I18n.t('fido_usf.flashs.device.registered')
  rescue U2F::Error => e
    @error_message = "Unable to register: #{e.class.name}"
    flash[:error] = @error_message
  ensure
    session.delete(:challenges)
  end

  respond_with resource, location: (resource)
end

#destroyObject



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/devise/fido_usf_registrations_controller.rb', line 21

def destroy
  device = resource.fido_usf_devices.find(params[:id])
  @fade_out_id = device.id unless params[:on_success_redirect_to]
  device.destroy
  unless params[:on_success_redirect_to]
    @devices = resource.fido_usf_devices.all
  end
  flash[:success] = I18n.t('fido_usf.flashs.device.removed')
  respond_to do |format|
    format.js
    format.html { redirect_to fido_usf_registration_url }
  end
end

#newObject



6
7
8
9
10
11
12
13
# File 'app/controllers/devise/fido_usf_registrations_controller.rb', line 6

def new
  @registration_requests = u2f.registration_requests
  session[:challenges] = @registration_requests.map(&:challenge)
  key_handles = resource.fido_usf_devices.map(&:key_handle)
  @sign_requests = u2f.authentication_requests(key_handles)
  @app_id = u2f.app_id
  render :new
end

#showObject

Show a list of all registered devices



16
17
18
19
# File 'app/controllers/devise/fido_usf_registrations_controller.rb', line 16

def show
  @devices = resource.fido_usf_devices.all
  render :show
end

#updateObject



63
64
65
66
67
68
69
70
# File 'app/controllers/devise/fido_usf_registrations_controller.rb', line 63

def update
  device = resource.fido_usf_devices.find(params[:id])
  device.update!(fido_usf_params)
  respond_to do |format|
    format.js
    format.html { redirect_to fido_usf_registration_url }
  end
end