- 
                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 all 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 | 
|---|---|---|
|  | @@ -32,6 +32,7 @@ body { | |
|  | ||
| .center { | ||
| display: flex; | ||
| justify-content: center; | ||
| } | ||
|  | ||
| .center input { | ||
|  | ||
| 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 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,29 @@ 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 | ||
| render json: { message: "Security Key registered successfully", redirect_to: root_path }, | ||
| status: :ok | ||
| else | ||
| render json: "Couldn't register your Security Key", status: :unprocessable_content | ||
| render json: { message: "Couldn't register your Security Key", redirect_to: registration_path }, | ||
| status: :unprocessable_content | ||
| end | ||
| rescue WebAuthn::Error => e | ||
| render json: "Verification failed: #{e.message}", status: :unprocessable_content | ||
| render json: { message: "Verification failed: #{e.message}", redirect_to: registration_path }, | ||
| 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. I'm getting an error here – we should be using  | ||
| status: :unprocessable_content | ||
| ensure | ||
| session.delete(:current_registration) | ||
| end | ||
| end | ||
|  | ||
| def registration_params | ||
| params.expect(registration: [:username, :nickname, :public_key_credential]) | ||
| 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 | 
|---|---|---|
|  | @@ -4,7 +4,7 @@ class SessionsController < ApplicationController | |
| def new | ||
| end | ||
|  | ||
| def create | ||
| def options | ||
| user = User.find_by(username: session_params[:username]) | ||
|  | ||
| if user | ||
|  | @@ -25,8 +25,8 @@ def create | |
| end | ||
| end | ||
|  | ||
| def callback | ||
| webauthn_credential = WebAuthn::Credential.from_get(params) | ||
| def create | ||
| webauthn_credential = WebAuthn::Credential.from_get(JSON.parse(session_params[:public_key_credential])) | ||
|  | ||
| user = User.find_by(username: session[:current_authentication]["username"]) | ||
| raise "user #{session[:current_authentication]["username"]} never initiated sign up" unless user | ||
|  | @@ -44,9 +44,10 @@ def callback | |
| credential.update!(sign_count: webauthn_credential.sign_count) | ||
| sign_in(user) | ||
|  | ||
| render json: { status: "ok" }, status: :ok | ||
| render json: { message: "Security Key authenticated successfully", redirect_to: root_path }, status: :ok | ||
| rescue WebAuthn::Error => e | ||
| render json: "Verification failed: #{e.message}", status: :unprocessable_content | ||
| render json: { message: "Verification failed: #{e.message}", redirect_to: session_path }, | ||
| 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. Same here! We should use  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. I'm not getting any of the messages rendered in the page 😕 | ||
| status: :unprocessable_content | ||
| ensure | ||
| session.delete(:current_authentication) | ||
| end | ||
|  | @@ -61,6 +62,6 @@ def destroy | |
| private | ||
|  | ||
| def session_params | ||
| params.require(:session).permit(:username) | ||
| params.expect(session: [:username, :public_key_credential]) | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| // 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"; | ||
| import "@rails/request.js" | ||
|  | ||
| 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,79 @@ | ||
| import { Controller } from "@hotwired/stimulus" | ||
| import { showMessage } from "messenger"; | ||
|  | ||
| export default class extends Controller { | ||
| static targets = ["hiddenCredentialInput", "submitButton"] | ||
| static values = { optionsUrl: String, submitUrl: String } | ||
|  | ||
| async create() { | ||
| try { | ||
| const response = await fetch(this.optionsUrlValue, { | ||
| method: "POST", | ||
| body: new FormData(this.element), | ||
| }); | ||
|  | ||
| const credentialOptionsJson = await response.json(); | ||
| console.log(credentialOptionsJson); | ||
|  | ||
| if (response.ok) { | ||
| console.log("Creating new public key credential..."); | ||
|  | ||
| const credential = await navigator.credentials.create({ publicKey: PublicKeyCredential.parseCreationOptionsFromJSON(credentialOptionsJson) }); | ||
| this.hiddenCredentialInputTarget.value = JSON.stringify(credential); | ||
|  | ||
| const submitResponse = await fetch(this.submitUrlValue, { | ||
| method: "POST", | ||
| body: new FormData(this.element), | ||
| }); | ||
|  | ||
| const submitResponseJson = await submitResponse.json(); | ||
|  | ||
| const { redirect_to } = submitResponseJson; | ||
|  | ||
| window.location.replace(redirect_to || "/"); | ||
| } else { | ||
| showMessage(credentialOptionsJson.errors?.[0] || "Sorry, something wrong happened."); | ||
| this.submitButtonTarget.disabled = false; | ||
| } | ||
| } catch (error) { | ||
| showMessage(error.message || "Sorry, something wrong happened."); | ||
| this.submitButtonTarget.disabled = false; | ||
| } | ||
| } | ||
|  | ||
| async get() { | ||
| try { | ||
| const response = await fetch(this.optionsUrlValue, { | ||
| method: "POST", | ||
| body: new FormData(this.element), | ||
| }); | ||
|  | ||
| const credentialOptionsJson = await response.json(); | ||
| console.log(credentialOptionsJson); | ||
|  | ||
| if (response.ok) { | ||
| console.log("Getting public key credential..."); | ||
|  | ||
| const credential = await navigator.credentials.get({ publicKey: PublicKeyCredential.parseRequestOptionsFromJSON(credentialOptionsJson) }) | ||
| this.hiddenCredentialInputTarget.value = JSON.stringify(credential); | ||
|  | ||
| const submitResponse = await fetch(this.submitUrlValue, { | ||
| method: "POST", | ||
| body: new FormData(this.element), | ||
| }); | ||
|  | ||
| const submitResponseJson = await submitResponse.json(); | ||
|  | ||
| const { redirect_to } = submitResponseJson; | ||
|  | ||
| window.location.replace(redirect_to || "/"); | ||
| } else { | ||
| showMessage(credentialOptionsJson.errors?.[0] || "Sorry, something wrong happened."); | ||
| this.submitButtonTarget.disabled = false; | ||
| } | ||
| } catch (error) { | ||
| showMessage(error.message || "Sorry, something wrong happened."); | ||
| this.submitButtonTarget.disabled = false; | ||
| } | ||
| } | ||
| } | 

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