type1fool
I am implementing passwordless authentication in an application, communicating the WebAuthn API via LiveView JS hooks. Over the past few days I have cleared a few hurdles as I’ve learned about the API from webauthn.guide.
The JS hook is properly sending back the attestation and client data to the LiveView. I can decode the client data and compare the challenge and origin. I can also decode the CBOR attestation, but this is where I get stuck. It’s not clear what to do with attestation_a and attestation_b.
The WebAuthn guide shows authData, ftm, and attStmt keys once the CBOR is decoded. I looked into decoding on the client, but that seemed to be a bad idea. The docs for Wax and CBOR have been helpful up to this point, and I’ve even dug into source code a bit. However, I’m not sure if the CBOR is being decoded correctly or what I need to do next to get authData and other attestation data.
If you have suggestions or examples, I would appreciate it. ![]()
Decoded Attestation
attestation_a: %{0 => <<3, 0, 116, 4, 0, 100, _REDACTED_>>, <<1, 0, 102>> => 2}
attestation_b: <<0, 110, 8, 0, 101, 9, 0, 103, _REDACTED_, 18, 0, 104, 19, 0, 97, 20, 0,
117, 21, 0, 116, 22, 0, 104, 23, 0, 68, ...>>
LiveView Credentials Event Handler
def handle_event(
"credentials",
%{"attestation" => attestation, "clientData" => client_data, "type" => _type},
%{assigns: %{challenge: server_challenge}} = socket
) do
%{"challenge" => client_challenge, "origin" => client_origin} =
client_data
|> List.to_string()
|> Jason.decode!()
|> Map.update!("challenge", &Base.decode64!(&1, padding: false))
# TODO: DECODE ATTESTATION
{:ok, attestation_a, attestation_b} =
attestation
|> Base.decode64!()
|> CBOR.decode()
IO.inspect(attestation_a, label: "attestation_a")
IO.inspect(attestation_b, label: "attestation_b")
with true <- client_challenge == server_challenge,
true <- client_origin == LiveShowyWeb.Endpoint.url() do
# TODO: STORE PUB KEY AFTER VALIDATION PASSES
# TODO: REDIRECT TO REFERRER OR HOME AFTER STORING PUB KEY
{:noreply, put_flash(socket, :info, "Registration was successful")}
else
_ ->
{:noreply, put_flash(socket, :error, "Registration failed")}
end
end
HandleWebAuthn Hook
const HandleWebAuthn = {
mounted() {
console.info(`HandleWebAuthn mounted`)
if (navigator.credentials) {
console.info(`WebAuthn is supported by this browser.`)
this.pushEvent("webauthn-supported", true)
window.addEventListener("phx:challenge", async (data) => {
const { appName, challenge, user } = data.detail
const publicKey = {
challenge: Uint8Array.from(challenge, c => c.charCodeAt(0)),
rp: {
name: appName,
id: document.location.host,
},
user: {
id: Uint8Array.from(user.id, c => c.charCodeAt(0)),
name: user.email,
displayName: user.username
},
pubKeyCredParams: [{ alg: -7, type: "public-key" }],
timeout: 60000,
attestation: "none",
authenticatorSelection: {
authenticatorAttachment: "platform",
userVerification: "discouraged",
},
}
const { response, type } = await navigator.credentials.create({ publicKey })
const { attestationObject, clientDataJSON } = response
const clientData = Array.from(new Uint8Array(clientDataJSON))
const attestation = Array.from(new Uint8Array(attestationObject))
.map(String.fromCharCode).join("")
this.pushEvent("credentials", {attestation: btoa(attestation), clientData, type})
})
} else {
console.error(`WebAuthn is not supported by this browser.`)
this.pushEvent("webauthn-supported", false)
}
}
}
Trending in Questions
Other Trending Topics
Latest Phoenix Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
type1fool
I should clarify that I’m not using Wax at the moment since I want to A) keep my dependencies minimal and B) understand WebAuthn by implementing it myself.
Once this is working for registration and login, I would like to open source the work as a LiveComponent for LiveView apps.
type1fool
Yesterday I added Wax to create a registration challenge, and I’ve hit a new hurdle. Frequently, the random challenge bytes contain char codes that translate to
-and_when they’re encoded as base64. This happens even if I overwrite the challenge bytes using:crypto.strong_rand_bytes(32).The hyphen and underscore are listed in the URL and filename safe alphabet section of the Base docs, but not in the preceding alphabet section.
If I use
Base.url_encode64/2&Base.url_decode64/2, I frequently get challenges which cannot be decoded by the browser:Is there some way to prevent hyphens and underscores in these challenges?
tangui
Have you taken a look at wax_demo?
Encoding with WebAuthn is hard to get it right, but there are some examples of how to decode it in the browser for use in the JS webauthn API.
al2o3cr
First off, you must use the
url_varieties of the encoder. In particular, you should be using them with thepadding: falseoption - the spec mandates that padding should always be omittedRe: the challenges that can’t be decoded by the browser - it’s hard to say what’s happening there. Calling
window.atob("anHfz4KTB0gvIAbdOgggj4kzi1VbtrBJqrRR94GcWCA")in my local browser console gives'jqßÏ\x82\x93\x07H/ \x06Ý:\b \x8F\x893\x8BU[¶°Iª´Q÷\x81\x9CX 'without an errortype1fool
@tangui Yes, I’ve been diligently reviewing the docs and example repo. Thank you for Wax and the examples, btw!!!
@al2o3cr Thanks for posting. When I use
Base.url_encode64(challenge.bytes, padding: false), I routinely get a hash that can’t be decoded, and it seems to happen only when there are underscores or hyphens.Eventually, I did get a challenge which could be decoded by the browser, and the touchID prompt appeared. The next request produced a challenge that couldn’t be decoded.
Exadra37
You can’t do that, they need to be base64url encoded.
That strings seem to be base64url encoded, because of the
-.On Firefox I can decode your
anHfz4KTB0gvIAbdOgggj4kzi1VbtrBJqrRR94GcWCAthat will output binary.The only think I can think of now is that
atobon your browser may require the padding to be present, aka=or==at the end of the string, but I may be wrong.type1fool
@Exadra37 As long as there are no hyphens or underscores, the challenge is encoded and decoded without a problem. The challenge intermittently contains char codes 62 (
-) & 63 (_), which Chrome & Firefox don’t want to decode.Padded
Not Padded
type1fool
Whoo!!! This was wild!
It crossed my mind that I could double encode and decode the bytes on the server, and voila! It works!
Now, the challenge bytes are decoded successfully and reliably on the client, and the returned bytes match on the server.
Thanks to everyone who chimed in!
derek-zhou
base64 use
+and/, url_base64 use_and-. Elixir supports both. Javascript atob/btoa is the first kind.Also, if you are handling binary data, it is better to use this npm package base64-js in javascript. It will decode to and encode from a byte array.
al2o3cr
I believe
window.atobis not the right thing to use for this - the HTML specification describes what it does, and it isn’t “decode URL-safe base64”: