Skip to content
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

feat: add possibility of enabling WebAuthn as a second factor authentication #1

Merged
merged 8 commits into from
Jun 22, 2020

Conversation

santiagorodriguez96
Copy link
Contributor

@santiagorodriguez96 santiagorodriguez96 commented May 5, 2020

What

New Feature: Allow users to enable Webauthn as a Second-Factor Authentication

This adds a basic UI for enabling WebAuthn 2FA, with a basic button that takes users to a page were users can add their security keys. After they hit the Add Security Key button, they'll be prompted to enter their security key by their browser. Once that's done, the process will be completed and you users will added their security key.

It also adds a second page after signing in where users are asked to authenticate using their second factor – if enabled.

How

It adds a few conditions when checking if a user is authenticated, making that a user that has second factor enabled is authenticated only after the second factor is authenticated.
So now, we'll still be storing the user_id in the session, after users sign in, but we'll also store the boolean user_authenticated, which will be true if user's second factor its authenticated.

Steps

  • - Add [WebAuthn gem|https://github.com/cedarcode/webauthn-ruby]
  • - Add the possibility of adding one (or more) WebAuthn security key as second factor
  • - Allow users to see a list of their security keys
  • - If enabled, ask users for their second factor authentication when logging in

Screenshots

Credential Registration

Kapture 2020-05-11 at 10 58 54

Credential Authentication

Kapture 2020-05-11 at 11 37 50

How to Review

  • Commit by commit strongly recommended.

References

https://www.pivotaltracker.com/story/show/172354931

@santiagorodriguez96 santiagorodriguez96 added the enhancement New feature or request label May 5, 2020
@santiagorodriguez96 santiagorodriguez96 force-pushed the add-optional-2fa-authentication branch 2 times, most recently from 89b4533 to d4ed665 Compare May 6, 2020 15:49
Copy link
Contributor

@grzuy grzuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work so far!

Left a couple of comments on my first review pass.
I know a few comments I made are recommending changes in code that is consistent with what we have in the webauthn-rails-demo-app, but still commented what I think might be better and then we can go back and refactor in the other app.

Gemfile Outdated Show resolved Hide resolved
app/controllers/credentials_controller.rb Outdated Show resolved Hide resolved
app/controllers/credentials_controller.rb Outdated Show resolved Hide resolved
app/controllers/credentials_controller.rb Outdated Show resolved Hide resolved
app/controllers/credentials_controller.rb Outdated Show resolved Hide resolved
app/models/user.rb Outdated Show resolved Hide resolved
@santiagorodriguez96 santiagorodriguez96 force-pushed the add-optional-2fa-authentication branch 3 times, most recently from 81f96ae to 398f6fe Compare May 11, 2020 18:20
@santiagorodriguez96 santiagorodriguez96 marked this pull request as ready for review May 11, 2020 18:26
app/controllers/application_controller.rb Outdated Show resolved Hide resolved
app/models/user.rb Outdated Show resolved Hide resolved
config/initializers/webauthn.rb Outdated Show resolved Hide resolved
@grzuy grzuy changed the title feat: add possibility of enabling Webauthn as a second factor authentication feat: add possibility of enabling WebAuthn as a second factor authentication May 12, 2020
end

test 'verify_key should require initiated login and 2FA enabled' do
post verify_key_second_factor_authentication_path
Copy link
Contributor

@grzuy grzuy May 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure we are POSTing anything in the request, sounds more like a GET?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All is doing is asking the server for the options for credentails.get call. Just "asking for information", "getting information".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but how we get to execute the stimultus controllers' actions if we don't post a request with json format? I got a little lost here 🤔

config/routes.rb Show resolved Hide resolved
Copy link
Contributor

@grzuy grzuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

test/controllers/credentials_controller_test.rb Outdated Show resolved Hide resolved
test/controllers/home_controller_test.rb Outdated Show resolved Hide resolved
Copy link
Contributor

@grzuy grzuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work! 👏
Sorry for the delay in the code review.

app/controllers/webauthn_credentials_controller.rb Outdated Show resolved Hide resolved
This adds a basic UI for enabling WebAuthn 2FA, with a basic button
that takes users to a new credential page that asks them for their
credential's nickname. After they hit the `Add Security Key` button,
they'll be prompted to enter their security key by their browser.
Once that's done, the process will be completed and you users will
added their security key.

* Used stimulus to comunicate between our server and the user client
* Used WebAuthnJson gem for calling the browser to create the
  credentials
* Created Credential model that belongs to User.
* Add `current_challenge` to Users
This commit adds verification of second factor when attempting to sign
in, if enabled.
It adds a few conditions when checking if a user is authenticated,
making that a user that has second factor enabled is authenticated
only after the second factor is authenticated.
So now, we'll still be storing the `user_id` in the session, after users
sign in, but we'll also store the boolean `user_authenticated`, which
will be true if user's second factor its authenticated.
The flow for authenticating a credential is pretty similar to the one
that we did for registering them, with the difference that we want to
fetch an existing one instead of creating it, therefore we use methods
like `WebAuthn::Credential#options_for_get` and
`WebAuthn::Credential#from_get`.
@santiagorodriguez96 santiagorodriguez96 merged commit 794f944 into master Jun 22, 2020
@santiagorodriguez96 santiagorodriguez96 deleted the add-optional-2fa-authentication branch June 22, 2020 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants