liveshowy / webauthn_components Goto Github PK
View Code? Open in Web Editor NEWWebauthnComponents allows Phoenix developers to quickly add passwordless authentication to LiveView applications.
License: MIT License
WebauthnComponents allows Phoenix developers to quickly add passwordless authentication to LiveView applications.
License: MIT License
Migrations are generated out of correct order, with the users
table created after the migrations that depend on them.
Steps to reproduce the behavior:
mix wac.install
on a newly-generated Phoenix app, using Elixir 1.15.x.users
table generated after the associated tables that require foreign key associations to that table. See listing:priv/repo/migrations
❯
ls
.rw-r--r-- 52 djp staff 2023-11-28 11:38 .formatter.exs
.rw-r--r-- 667 djp staff 2023-11-28 11:42 20231128004224_user_keys.exs
.rw-r--r-- 461 djp staff 2023-11-28 11:42 20231128004225_user_tokens.exs
.rw-r--r-- 300 djp staff 2023-11-28 11:42 20231128004226_users.exs
mix ecto.migrate
.mix ecto.migrate
Compiling 15 files (.ex)
Generated example app
11:42:39.934 [info] == Running 20231128004224 Example.Repo.Migrations.CreateUserKeys.change/0 forward
11:42:39.935 [info] create table user_keys
** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "users" does not exist
(ecto_sql 3.11.0) lib/ecto/adapters/sql.ex:1054: Ecto.Adapters.SQL.raise_sql_call_error/1
(elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
(ecto_sql 3.11.0) lib/ecto/adapters/sql.ex:1161: Ecto.Adapters.SQL.execute_ddl/4
(ecto_sql 3.11.0) lib/ecto/migration/runner.ex:348: Ecto.Migration.Runner.log_and_execute_ddl/3
(elixir 1.15.7) lib/enum.ex:1693: Enum."-map/2-lists^map/1-1-"/2
(ecto_sql 3.11.0) lib/ecto/migration/runner.ex:311: Ecto.Migration.Runner.perform_operation/3
(stdlib 3.17.2.4) timer.erl:166: :timer.tc/1
(ecto_sql 3.11.0) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/8
users
table should always be created first.
Elixir 1.15.7
Erlang 24.x
Update JS hook to use handleEvent
instead of creating and removing event listeners.
https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#push_event/3
webauthn.*
(ie webauthn.registration_challenge
)handleEvent
Running into issues where I can't use this on Safari. Both iOS and MacOS versions fail with different error messages.
On iOS I get a flash message when I go to the sign-in route that says "Passkeys not supported on this browser."
On MacOS I get errors in the console:
[debug] HANDLE EVENT "register" in MyPkAppWeb.AuthenticationLive
Component: WebauthnComponents.RegistrationComponent
Parameters: %{"value" => ""}
[debug] Replied in 542µs
[debug] HANDLE EVENT "error" in MyPkAppWeb.AuthenticationLive
Component: WebauthnComponents.RegistrationComponent
Parameters: %{"message" => "This request has been cancelled by the user.", "name" => "NotAllowedError", "stack" => ""}
[debug] Replied in 107µs
[warning] [unhandled_message: {MyPkAppWeb.AuthenticationLive, {:error, %{"message" => "This request has been cancelled by the user.", "name" => "NotAllowedError", "stack" => ""}}}]
To end on a positive note - both Firefox and Chrome on MacOS works as expected and across browsers! UX is 👌
Steps to reproduce the behavior:
ecto.create
wac.install
/sign-in
I should be able to sign up and login like on Chrome and Firefox.
Add any other context about the problem here.
At the Denver Elixir meetup, one of the attendees (@RGENT) raised a concern about token security (video link).
In short, the token saved in sessionStorage may be accessed by 3rd party JS dependencies. Suggested mitigations include cookie storage and/or adding content security policy (CSP).
Because these component interact exclusively via WebSockets, cookies may not be an option here. CSP is likely the better solution, though other alternatives may exist.
https://github.com/actions/setup-elixir
To support multiple keys for user accounts and MFA usage, and to separate concerns, the components need to be split up.
RegistrationComponent
AuthenticationComponent
TokenComponent
SupportComponent
registration_hook.js
authentication_hook.js
token_hook.js
support_hook.js
PasskeyComponent
Often, a user may want to register additional keys after creating an account for backup or survivorship purposes. The Passkey package should provide an API for registering additional keys when an application wants to support this feature.
SecondaryPasskey
LiveComponentuser.id
& user.displayName
to handlePasskeyRegistration
in passkey_hook.js
I believe the two mix
commands in README.MD
to generate the database structures should be using mix phx.gen.context
rather than ecto.gen.migration
mix hex.outdated
to list outdated depsAs of v0.6.0, WebauthnComponents can be used to easily scaffold Passkey authentication in new Phoenix apps. For existing applications with password-based authentication or 0Auth, more extensive documentation is needed.
type=button
finger print
, key
*, cloud
, face smile
:telemetry
as a dependencyPasskeyComponent
Logger
Assigning and authenticating users with tokens in LiveView is a tedious process to implement. If possible, a generator for LiveView on_mount
hooks would help developers get up to speed quickly, assuming requirements are met (see #15).
User
, UserKey
, UserToken
, or UserProfile
schemas don't exist:assign_user
hook:require_user
hookInstead of setting the token in JS SessionStorage, add a session controller which saves the token in a cookie.
This may eliminate the need for connected?(socket)
checks.
Update the demo app to test & document usage.
With discoverable credentials, username and email data is not required up front. And, schemas for UserKey, UserToken, and UserProfile may differ in surprising ways from traditional applications.
So, custom generators may helpful for developers implementing Passkey support for the first time.
Users
contextUser
schemaUserKeys
contextUserKey
schemaUserTokens
contextUserToken
schemaI'm always frustrated when I need to input again and again my email when I need sign-in. Enabling Passkey Autofill with mediation: "conditional"
and autocomplete="username webauthn"
on email input, helps that.
Implements Conditional UI to enable Passkey autofill
Sign in with a passkey through form autofill
Add passkeys to the browser autofill
Explainer: WebAuthn Conditional UI
As a user of this component, I would like to provide my own changeset from my custom LiveView and see the form render fields based on the changeset's required fields.
changeset
prop of type struct
changeset
Is your feature request related to a problem? Please describe.
I've found this library to be a great start to adding passkeys to my application, but specific implementations make it challenging to add as-is. The token component is great, but when I have push navigation the current setup means that I have to check in all of my handle_params as well as all of my handle_event callbacks to know whether a user is already loaded, and every single live view I make needs to copy the same 3+ handle_info callbacks in order to work.
This might be more for the example repo than for the library itself, but if the hooks were broken apart a bit more it might be more clear how to use connect params to load a user in on_mount.
Describe the solution you'd like
Describe alternatives you've considered
I've instead used this library as a reference for writing my own components and hooks. If you find security issues or more complete integration code, I'll need to follow your fixes and apply them to my code.
Additional context
This is great work! It and your conference presentation were really helpful for me to add passkey support to my app.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.