devise_fido_usf 0.1.7 → 0.1.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 42abbd5255725a51542abfb644b763acb7d88bc0
4
- data.tar.gz: d4a778d571a0d53f8b795019b7b0f4d926c74463
3
+ metadata.gz: 96c8b3e2463da3287d1e62f498c6ad481a98e928
4
+ data.tar.gz: ef2e2c7a249c4ceef0d07f2d121b45be1ae0d491
5
5
  SHA512:
6
- metadata.gz: b3731b702a48ab0544f31482d1b00e6ddf3972ff22ff83fed7d098e111d5b3c4098eb2f9681bfcd179493ef4d3fe57c94027f52e88be5cdc5dcb061dc22ee69d
7
- data.tar.gz: 538830aa044e285f94d089d89c8b21f7cd077f84c27fe3853409a4d790c17d068870e9f5329c77959b74efbe8baf0444734993d9e159fadc1dc51d9ba0fe5a6f
6
+ metadata.gz: f2b0854980aacbbc4480475d193ba47ec360f4b72729895303bbf8935d5225c38c7c63d95d52c6ce615518f430b0e7c9ef80c6d4c5e4a3c258cfce69edc42130
7
+ data.tar.gz: a8be7220ea71e7e0aa9655403fe0e639994a9ed4607530ecfcdebacdf2e65481bccc7185663fbc58a35eb6adc37f31a8196ee7b11e7cd9fd88c0fadb08214e72
@@ -1,10 +1,10 @@
1
1
  class Devise::FidoUsfRegistrationsController < ApplicationController
2
- before_action :authenticate_user!
2
+ before_action :authenticate_resource!
3
3
 
4
4
  def new
5
5
  @registration_requests = u2f.registration_requests
6
6
  session[:challenges] = @registration_requests.map(&:challenge)
7
- key_handles = current_user.fido_usf_devices.map(&:key_handle)
7
+ key_handles = resource.fido_usf_devices.map(&:key_handle)
8
8
  @sign_requests = u2f.authentication_requests(key_handles)
9
9
  @app_id = u2f.app_id
10
10
  render :new
@@ -12,19 +12,21 @@ class Devise::FidoUsfRegistrationsController < ApplicationController
12
12
 
13
13
  # Show a list of all registered devices
14
14
  def show
15
- @devices = current_user.fido_usf_devices.all
15
+ @devices = resource.fido_usf_devices.all
16
16
  render :show
17
17
  end
18
18
 
19
19
  def destroy
20
- device = current_user.fido_usf_devices.find(params[:id])
21
- @fade_out_id = device.id
20
+ device = resource.fido_usf_devices.find(params[:id])
21
+ @fade_out_id = device.id unless params[:on_success_redirect_to]
22
22
  device.destroy
23
- @devices = current_user.fido_usf_devices.all
23
+ unless params[:on_success_redirect_to]
24
+ @devices = resource.fido_usf_devices.all
25
+ end
24
26
  flash[:success] = I18n.t('fido_usf.flashs.device.removed')
25
27
  respond_to do |format|
26
28
  format.js
27
- format.html { redirect_to user_fido_usf_registration_url }
29
+ format.html { redirect_to fido_usf_registration_url }
28
30
  end
29
31
  end
30
32
 
@@ -34,39 +36,55 @@ class Devise::FidoUsfRegistrationsController < ApplicationController
34
36
  reg = u2f.register!(session[:challenges], response)
35
37
 
36
38
  pubkey = reg.public_key
37
- pubkey = Base64.decode64(reg.public_key) unless pubkey.bytesize == 65 && pubkey.byteslice(0) != "\x04"
38
-
39
+ pubkey = Base64.decode64(reg.public_key) unless pubkey.bytesize == 65 && pubkey.byteslice(0) != "\x04"
40
+
39
41
  @device = FidoUsf::FidoUsfDevice.create!(
40
- user: current_user,
41
- name: "Token ##{current_user.fido_usf_devices.count+1}",
42
- certificate: reg.certificate,
43
- key_handle: reg.key_handle,
44
- public_key: pubkey,
45
- counter: reg.counter,
46
- last_authenticated_at: Time.now)
42
+ user: resource,
43
+ name: "Token ##{resource.fido_usf_devices.count + 1}",
44
+ certificate: reg.certificate,
45
+ key_handle: reg.key_handle,
46
+ public_key: pubkey,
47
+ counter: reg.counter,
48
+ last_authenticated_at: Time.now
49
+ )
47
50
  flash[:success] = I18n.t('fido_usf.flashs.device.registered')
48
51
  rescue U2F::Error => e
49
52
  @error_message = "Unable to register: #{e.class.name}"
50
- flash[:error] = @error_message
53
+ flash[:error] = @error_message
51
54
  ensure
52
55
  session.delete(:challenges)
53
56
  end
54
57
 
55
58
  respond_to do |format|
56
59
  format.js
57
- format.html { redirect_to user_fido_usf_registration_url }
60
+ format.html { redirect_to fido_usf_registration_url }
58
61
  end
59
62
  end
60
63
 
61
64
  def update
62
- device = current_user.fido_usf_devices.find(params[:id])
65
+ device = resource.fido_usf_devices.find(params[:id])
63
66
  device.update!(fido_usf_params)
64
67
  respond_to do |format|
65
68
  format.js
66
- format.html { redirect_to user_fido_usf_registration_url }
69
+ format.html { redirect_to fido_usf_registration_url }
67
70
  end
68
71
  end
69
72
 
73
+ if respond_to?(:helper_method)
74
+ helpers = %w[resource_name]
75
+ helper_method(*helpers)
76
+ end
77
+
78
+ protected
79
+
80
+ def resource_name
81
+ devise_mapping.name
82
+ end
83
+
84
+ def devise_mapping
85
+ @devise_mapping ||= request.env['devise.mapping']
86
+ end
87
+
70
88
  private
71
89
 
72
90
  def fido_usf_params
@@ -78,4 +96,17 @@ class Devise::FidoUsfRegistrationsController < ApplicationController
78
96
  # use base_url as app_id, e.g. 'http://localhost:3000'
79
97
  @u2f ||= U2F::U2F.new(request.base_url)
80
98
  end
99
+
100
+ def resource
101
+ send("current_#{resource_name}")
102
+ end
103
+
104
+ def authenticate_resource!
105
+ send("authenticate_#{resource_name}!")
106
+ end
107
+
108
+ def fido_usf_registration_url
109
+ params[:on_success_redirect_to].presence ||
110
+ send("#{resource_name}_fido_usf_registration_url")
111
+ end
81
112
  end
@@ -2,7 +2,7 @@
2
2
  <p>Please insert one of your registered keys and press the button within 15 seconds</p>
3
3
  <p id="waiting">Waiting...</p>
4
4
  <p id="error" style="display: none;"></p>
5
- <%= form_tag user_fido_usf_authentication_path(), method: 'post' do %>
5
+ <%= form_tag send("#{resource_name}_fido_usf_authentication_path"), method: 'post' do %>
6
6
  <%= hidden_field_tag :response %>
7
7
  <% end %>
8
8
  <script>
@@ -1,5 +1,5 @@
1
1
  <tr id="device_<%= device.id %>">
2
2
  <td><%= device.name %></td>
3
3
  <td><%= l(device.last_authenticated_at, format: :long) %></td>
4
- <td><%= link_to 'Delete', user_fido_usf_registration_path(id: device.id), remote: true, :method => :delete, data: {confirm: "Should device #{device.name} be deleted?" } %></td>
4
+ <td><%= link_to 'Delete', send("#{resource_name}_fido_usf_registration_path", id: device.id), remote: true, method: :delete, data: { confirm: "Should device #{device.name} be deleted?" } %></td>
5
5
  </tr>
@@ -3,7 +3,7 @@
3
3
  <p id="waiting">Waiting...</p>
4
4
  <p id="error" style="display: none;"></p>
5
5
 
6
- <%= form_tag user_fido_usf_registration_path(), method: 'post' do %>
6
+ <%= form_tag send("#{resource_name}_fido_usf_registration_path"), method: 'post' do %>
7
7
  <%= hidden_field_tag :response %>
8
8
  <% end %>
9
9
 
@@ -2,4 +2,4 @@
2
2
  <p>List of registered devices:</p>
3
3
  <%= render 'devise/fido_usf_registrations/devices' %>
4
4
  <p><%= link_to 'Back', root_path %></p>
5
- <p><%= link_to 'Add', new_user_fido_usf_registration_path %></p>
5
+ <p><%= link_to 'Add', send("new_#{resource_name}_fido_usf_registration_path") %></p>
@@ -7,22 +7,24 @@ module DeviseFidoUsf
7
7
 
8
8
  included do
9
9
  before_action :check_request_and_redirect_to_verify_fido_usf,
10
- if: :is_user_signing_in?
10
+ if: :user_signing_in?
11
11
  end
12
12
 
13
13
  private
14
- def is_devise_sessions_controller?
15
- self.class == Devise::SessionsController || self.class.ancestors.include?(Devise::SessionsController)
14
+
15
+ def devise_sessions_controller?
16
+ self.class == Devise::SessionsController ||
17
+ self.class.ancestors.include?(Devise::SessionsController)
16
18
  end
17
19
 
18
- def is_user_signing_in?
20
+ def user_signing_in?
19
21
  if devise_controller? && signed_in?(resource_name) &&
20
- is_devise_sessions_controller? &&
21
- self.action_name == "create"
22
+ devise_sessions_controller? &&
23
+ action_name == 'create'
22
24
  return true
23
25
  end
24
26
 
25
- return false
27
+ false
26
28
  end
27
29
 
28
30
  def check_request_and_redirect_to_verify_fido_usf
@@ -4,7 +4,10 @@ module Devise
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  included do
7
- has_many :fido_usf_devices, class_name: 'FidoUsf::FidoUsfDevice', foreign_key: 'user_id', dependent: :destroy
7
+ has_many :fido_usf_devices,
8
+ as: :user,
9
+ class_name: 'FidoUsf::FidoUsfDevice',
10
+ dependent: :destroy
8
11
  end
9
12
  end
10
13
  end
@@ -1,3 +1,3 @@
1
1
  module DeviseFidoUsf
2
- VERSION = '0.1.7'
2
+ VERSION = '0.1.8'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise_fido_usf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - H. Gregor Molter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-12-30 00:00:00.000000000 Z
11
+ date: 2018-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise