Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/credentials_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def callback
webauthn_credential.verify(session[:current_registration]["challenge"], user_verification: true)

credential = current_user.credentials.find_or_initialize_by(
external_id: Base64.strict_encode64(webauthn_credential.raw_id)
external_id: webauthn_credential.id
)

if credential.update(
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def callback
webauthn_credential.verify(session[:current_registration]["challenge"], user_verification: true)

user.credentials.build(
external_id: Base64.strict_encode64(webauthn_credential.raw_id),
external_id: webauthn_credential.id,
nickname: params[:credential_nickname],
public_key: webauthn_credential.public_key,
sign_count: webauthn_credential.sign_count
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def callback
user = User.find_by(username: session[:current_authentication]["username"])
raise "user #{session[:current_authentication]["username"]} never initiated sign up" unless user

credential = user.credentials.find_by(external_id: Base64.strict_encode64(webauthn_credential.raw_id))
credential = user.credentials.find_by(external_id: webauthn_credential.id)

begin
webauthn_credential.verify(
Expand Down
18 changes: 6 additions & 12 deletions app/javascript/controllers/feature_detection_controller.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
import { Controller } from "@hotwired/stimulus";
import { supported as WebAuthnSupported } from "@github/webauthn-json";

export default class extends Controller {
static targets = ["message"]

connect() {
if (!WebAuthnSupported()) {
this.messageTarget.innerHTML = "This browser doesn't support WebAuthn API";
this.element.classList.remove("hidden");
} else {
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable().then((available) => {
if (!available) {
this.messageTarget.innerHTML = "We couldn't detect a user-verifying platform authenticator";
this.element.classList.remove("hidden");
}
});
}
PublicKeyCredential.isUserVerifyingPlatformAuthenticatorAvailable().then((available) => {
if (!available) {
this.messageTarget.innerHTML = "We couldn't detect a user-verifying platform authenticator";
this.element.classList.remove("hidden");
}
});
}
}
13 changes: 8 additions & 5 deletions app/javascript/credential.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as WebAuthnJSON from "@github/webauthn-json"
import { showMessage } from "messenger";

function getCSRFToken() {
Expand Down Expand Up @@ -31,8 +30,10 @@ function callback(url, body) {
});
}

function create(callbackUrl, credentialOptions) {
WebAuthnJSON.create({ "publicKey": credentialOptions }).then(function(credential) {
function create(callbackUrl, data) {
const credentialOptions = PublicKeyCredential.parseCreationOptionsFromJSON(data);

navigator.credentials.create({ "publicKey": credentialOptions }).then(function(credential) {
callback(callbackUrl, credential);
}).catch(function(error) {
showMessage(error);
Expand All @@ -41,8 +42,10 @@ function create(callbackUrl, credentialOptions) {
console.log("Creating new public key credential...");
}

function get(credentialOptions) {
WebAuthnJSON.get({ "publicKey": credentialOptions }).then(function(credential) {
function get(data) {
const credentialOptions = PublicKeyCredential.parseRequestOptionsFromJSON(data);

navigator.credentials.get({ "publicKey": credentialOptions }).then(function(credential) {
callback("/session/callback", credential);
}).catch(function(error) {
showMessage(error);
Expand Down
1 change: 0 additions & 1 deletion config/importmap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
pin "@material/snackbar", to: "https://ga.jspm.io/npm:@material/snackbar@4.0.0/dist/mdc.snackbar.js"
pin "@material/textfield", to: "https://ga.jspm.io/npm:@material/textfield@4.0.0/dist/mdc.textfield.js"
pin "@material/top-app-bar", to: "https://ga.jspm.io/npm:@material/top-app-bar@4.0.0/dist/mdc.topAppBar.js"
pin "@github/webauthn-json", to: "https://ga.jspm.io/npm:@github/webauthn-json@2.1.1/dist/esm/webauthn-json.js"

# turbolinks
pin "turbolinks", to: "https://ga.jspm.io/npm:turbolinks@5.2.0/dist/turbolinks.js"
2 changes: 1 addition & 1 deletion test/controllers/registrations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class RegistrationsControllerTest < ActionDispatch::IntegrationTest
username: "bob",
credentials: [
Credential.new(
external_id: Base64.strict_encode64(webauthn_credential.raw_id),
external_id: webauthn_credential.id,
nickname: "Bob's USB Key",
public_key: webauthn_credential.public_key,
sign_count: webauthn_credential.sign_count
Expand Down