Skip to content

Commit

Permalink
support be, bs flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ritou committed Sep 29, 2023
1 parent 06cc489 commit 882d319
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
19 changes: 16 additions & 3 deletions lib/web_authn_lite/authenticator_data/flags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ defmodule WebAuthnLite.AuthenticatorData.Flags do
see https://www.w3.org/TR/webauthn/#flags
"""

defstruct [:flags, :up, :uv, :at, :ed]
defstruct [:flags, :up, :uv, :be, :bs, :at, :ed]

@type t :: %__MODULE__{
flags: binary,
up: boolean,
uv: boolean,
be: boolean,
bs: boolean,
at: boolean,
ed: boolean
}
Expand All @@ -23,9 +25,20 @@ defmodule WebAuthnLite.AuthenticatorData.Flags do
@spec from_binary(flags :: binary) :: {:ok, t} | {:error, :invalid_format}
def from_binary(flags) do
with true <- flags |> byte_size() == @flags_byte_size,
<<ed::size(1), at::size(1), _rfu2::size(3), uv::size(1), _rfu1::size(1), up::size(1)>> <-
<<ed::size(1), at::size(1), _rfu2::size(1), bs::size(1), be::size(1), uv::size(1),
_rfu1::size(1),
up::size(1)>> <-
flags do
{:ok, %__MODULE__{flags: flags, up: up == 1, uv: uv == 1, at: at == 1, ed: ed == 1}}
{:ok,
%__MODULE__{
flags: flags,
up: up == 1,
uv: uv == 1,
be: be == 1,
bs: bs == 1,
at: at == 1,
ed: ed == 1
}}
else
_ -> {:error, :invalid_format}
end
Expand Down
61 changes: 52 additions & 9 deletions test/lib/web_authn_lite/authenticator_data/flags_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,98 @@ defmodule WebAuthnLite.AuthenticatorData.FlagsTest do
%Flags{
ed: true,
at: true,
bs: true,
be: true,
uv: true,
up: true,
flags: <<207>>
flags: <<221>>
}} ==
<<1::size(1), 1::size(1), 1::size(3), 1::size(1), 1::size(1), 1::size(1)>>
<<1::size(1), 1::size(1), 0::size(1), 1::size(1), 1::size(1), 1::size(1), 0::size(1),
1::size(1)>>
|> Flags.from_binary()

assert {:ok,
%Flags{
ed: false,
at: true,
bs: true,
be: true,
uv: true,
up: true,
flags: "O"
flags: <<93>>
}} ==
<<0::size(1), 1::size(1), 1::size(3), 1::size(1), 1::size(1), 1::size(1)>>
<<0::size(1), 1::size(1), 0::size(1), 1::size(1), 1::size(1), 1::size(1), 0::size(1),
1::size(1)>>
|> Flags.from_binary()

assert {:ok,
%Flags{
ed: false,
at: false,
bs: true,
be: true,
uv: true,
up: true,
flags: <<15>>
flags: <<29>>
}} ==
<<0::size(1), 0::size(1), 1::size(3), 1::size(1), 1::size(1), 1::size(1)>>
<<0::size(1), 0::size(1), 0::size(1), 1::size(1), 1::size(1), 1::size(1), 0::size(1),
1::size(1)>>
|> Flags.from_binary()

assert {:ok,
%Flags{
ed: false,
at: false,
bs: false,
be: true,
uv: true,
up: true,
flags: <<13>>
}} ==
<<0::size(1), 0::size(1), 0::size(1), 0::size(1), 1::size(1), 1::size(1), 0::size(1),
1::size(1)>>
|> Flags.from_binary()

assert {:ok,
%Flags{
ed: false,
at: false,
bs: false,
be: false,
uv: true,
up: true,
flags: <<5>>
}} ==
<<0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 1::size(1), 0::size(1),
1::size(1)>>
|> Flags.from_binary()

assert {:ok,
%Flags{
ed: false,
at: false,
bs: false,
be: false,
uv: false,
up: true,
flags: <<3>>
flags: <<1>>
}} ==
<<0::size(1), 0::size(1), 0::size(3), 0::size(1), 1::size(1), 1::size(1)>>
<<0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1),
1::size(1)>>
|> Flags.from_binary()

assert {:ok,
%Flags{
ed: false,
at: false,
bs: false,
be: false,
uv: false,
up: false,
flags: <<0>>
}} ==
<<0::size(1), 0::size(1), 0::size(3), 0::size(1), 0::size(1), 0::size(1)>>
<<0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1), 0::size(1),
0::size(1)>>
|> Flags.from_binary()
end
end

0 comments on commit 882d319

Please sign in to comment.