-
Notifications
You must be signed in to change notification settings - Fork 41
Refactor JS #241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Refactor JS #241
Changes from 13 commits
ac998c2
1532925
cb11999
521b0c2
7bddc67
871ab25
cbf503e
a990e01
bd72e31
6115812
e19ef24
9b46e85
6fe61ca
6002921
e93e3e8
95b5664
331859e
c42c9fc
7a35c2c
81ad153
c2753ed
1c92613
5307cb1
449c9cb
e8b7ea3
9e58703
672bcb1
b9edbe2
23a316e
55e10a6
e35488d
3accfb1
18b76a5
72e5f40
82a766f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class CredentialsController < ApplicationController | ||
| def create | ||
| def create_options | ||
santiagorodriguez96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| create_options = WebAuthn::Credential.options_for_create( | ||
| user: { | ||
| id: current_user.webauthn_id, | ||
|
|
@@ -18,8 +18,8 @@ def create | |
| end | ||
| end | ||
|
|
||
| def callback | ||
| webauthn_credential = WebAuthn::Credential.from_create(params) | ||
| def create | ||
| webauthn_credential = WebAuthn::Credential.from_create(JSON.parse(credential_params[:public_key_credential])) | ||
|
|
||
| begin | ||
| webauthn_credential.verify(session[:current_registration]["challenge"], user_verification: true) | ||
|
|
@@ -29,13 +29,13 @@ def callback | |
| ) | ||
|
|
||
| if credential.update( | ||
| nickname: params[:credential_nickname], | ||
| nickname: credential_params[:nickname], | ||
| public_key: webauthn_credential.public_key, | ||
| sign_count: webauthn_credential.sign_count | ||
| ) | ||
| render json: { status: "ok" }, status: :ok | ||
| redirect_to root_path, notice: "Security Key registered successfully" | ||
| else | ||
| render json: "Couldn't add your Security Key", status: :unprocessable_content | ||
| redirect_to root_path, alert: "Couldn't register your Security Key" | ||
|
||
| end | ||
| rescue WebAuthn::Error => e | ||
| render json: "Verification failed: #{e.message}", status: :unprocessable_content | ||
|
|
@@ -51,4 +51,8 @@ def destroy | |
|
|
||
| redirect_to root_path | ||
| end | ||
|
|
||
| def credential_params | ||
| params.require(:credential).permit(:public_key_credential, :nickname) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,12 @@ class RegistrationsController < ApplicationController | |
| def new | ||
| end | ||
|
|
||
| def create | ||
| user = User.new(username: params[:registration][:username]) | ||
| def create_options | ||
| user = User.new(username: registration_params[:username]) | ||
|
|
||
| create_options = WebAuthn::Credential.options_for_create( | ||
| user: { | ||
| name: params[:registration][:username], | ||
| name: registration_params[:username], | ||
| id: user.webauthn_id | ||
| }, | ||
| authenticator_selection: { user_verification: "required" } | ||
|
|
@@ -28,8 +28,8 @@ def create | |
| end | ||
| end | ||
|
|
||
| def callback | ||
| webauthn_credential = WebAuthn::Credential.from_create(params) | ||
| def create | ||
| webauthn_credential = WebAuthn::Credential.from_create(JSON.parse(registration_params[:public_key_credential])) | ||
|
|
||
| user = User.new(session[:current_registration]["user_attributes"]) | ||
|
|
||
|
|
@@ -38,22 +38,26 @@ def callback | |
|
|
||
| user.credentials.build( | ||
| external_id: webauthn_credential.id, | ||
| nickname: params[:credential_nickname], | ||
| nickname: registration_params[:nickname], | ||
| public_key: webauthn_credential.public_key, | ||
| sign_count: webauthn_credential.sign_count | ||
| ) | ||
|
|
||
| if user.save | ||
| sign_in(user) | ||
|
|
||
| render json: { status: "ok" }, status: :ok | ||
| redirect_to root_path, notice: "Security Key registered successfully" | ||
| else | ||
| render json: "Couldn't register your Security Key", status: :unprocessable_content | ||
| redirect_to new_registration_path, alert: "Couldn't register your Security Key" | ||
|
||
| end | ||
| rescue WebAuthn::Error => e | ||
| render json: "Verification failed: #{e.message}", status: :unprocessable_content | ||
| ensure | ||
| session.delete(:current_registration) | ||
| end | ||
| end | ||
|
|
||
| def registration_params | ||
| params.require(:registration).permit(:username, :nickname, :public_key_credential) | ||
nicolastemciuc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| end | ||
| end | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| // Configure your import map in config/importmap.rb. Read more: https://github.com/rails/importmap-rails | ||
| import "controllers" | ||
| import "credential" | ||
| import "messenger" | ||
| import Rails from "@rails/ujs"; | ||
|
|
||
| Rails.start(); |
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
|
|
||
| export default class extends Controller { | ||
| static targets = ["hiddenCredentialInput"] | ||
| static values = { optionsUrl: String } | ||
|
|
||
| async create() { | ||
| try { | ||
| const optionsResponse = await fetch(this.optionsUrlValue, { | ||
| method: "POST", | ||
| body: new FormData(this.element), | ||
| headers: { | ||
| "X-CSRF-Token": document.querySelector('meta[name="csrf-token"]')?.getAttribute("content") | ||
santiagorodriguez96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
|
|
||
| const optionsJson = await optionsResponse.json(); | ||
| console.log(optionsJson); | ||
|
|
||
| if (optionsResponse.ok) { | ||
| console.log("Creating new public key credential..."); | ||
|
|
||
| const credential = await navigator.credentials.create({ publicKey: PublicKeyCredential.parseCreationOptionsFromJSON(optionsJson) }); | ||
| this.hiddenCredentialInputTarget.value = JSON.stringify(credential); | ||
| this.element.submit(); | ||
| } else { | ||
| alert(optionsJson.errors?.[0] || "Sorry, something wrong happened."); | ||
| } | ||
| } catch (error) { | ||
| alert(error.message || error); | ||
| } | ||
| } | ||
|
|
||
| async get() { | ||
| try { | ||
| const optionsResponse = await fetch(this.optionsUrlValue, { | ||
|
||
| method: "POST", | ||
| body: new FormData(this.element), | ||
| headers: { | ||
| "X-CSRF-Token": document.querySelector('meta[name="csrf-token"]')?.getAttribute("content") | ||
| } | ||
| }); | ||
|
|
||
| const optionsJson = await optionsResponse.json(); | ||
| console.log(optionsJson); | ||
|
|
||
| if (optionsResponse.ok) { | ||
| console.log("Getting public key credential..."); | ||
|
|
||
| const credential = await navigator.credentials.get({ publicKey: PublicKeyCredential.parseRequestOptionsFromJSON(optionsJson) }) | ||
| this.hiddenCredentialInputTarget.value = JSON.stringify(credential); | ||
| this.element.submit(); | ||
| } else { | ||
| alert(optionsJson.errors?.[0] || "Sorry, something wrong happened."); | ||
| } | ||
| } catch (error) { | ||
| alert(error.message || error); | ||
| } | ||
| } | ||
| } | ||
This file was deleted.
This file was deleted.

Uh oh!
There was an error while loading. Please reload this page.