-
Notifications
You must be signed in to change notification settings - Fork 38
/
registrations_controller.rb
61 lines (51 loc) · 1.71 KB
/
registrations_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
# frozen_string_literal: true
class RegistrationsController < ApplicationController
def new
end
def create
user = User.new(username: params[:registration][:username])
create_options = relying_party.options_for_registration(
user: {
name: params[:registration][:username],
id: user.webauthn_id
},
authenticator_selection: { user_verification: "required" }
)
if user.valid?
session[:current_registration] = { challenge: create_options.challenge, user_attributes: user.attributes }
respond_to do |format|
format.json { render json: create_options }
end
else
respond_to do |format|
format.json { render json: { errors: user.errors.full_messages }, status: :unprocessable_entity }
end
end
end
def callback
user = User.create!(session[:current_registration][:user_attributes])
begin
webauthn_credential = relying_party.verify_registration(
params,
session[:current_registration][:challenge],
user_verification: true,
)
credential = user.credentials.build(
external_id: Base64.strict_encode64(webauthn_credential.raw_id),
nickname: params[:credential_nickname],
public_key: webauthn_credential.public_key,
sign_count: webauthn_credential.sign_count
)
if credential.save
sign_in(user)
render json: { status: "ok" }, status: :ok
else
render json: "Couldn't register your Security Key", status: :unprocessable_entity
end
rescue WebAuthn::Error => e
render json: "Verification failed: #{e.message}", status: :unprocessable_entity
ensure
session.delete(:current_registration)
end
end
end