Add :digits option - #39
Conversation
…id?/3 accordingly Added validate_digits/2 to validate the otp without using a fixed size
…code` use a totp_size of 8 Updated `returns true if it matches the verification code` name to specify that it is for the default totp_size
| *in seconds*) to be used. Default is `System.os_time(:second)`. | ||
| * `:period` - The period (in seconds) in which the code is valid. Default is `30`. | ||
| If this option is given to `verification_code/2`, it must also be given to `valid?/3`. | ||
| * `:totp_size` - The desired size of the totp. Default is 6. |
| defp validate_digits(<<e, e_rest::binary>>, <<a, a_rest::binary>>) | ||
| when byte_size(e_rest) > 0 and byte_size(a_rest) > 0 do | ||
| bxor(e, a) ||| validate_digits(e_rest, a_rest) | ||
| end | ||
|
|
||
| defp validate_digits(<<e, e_rest::binary>>, <<a, a_rest::binary>>) | ||
| when byte_size(e_rest) <= 0 and byte_size(a_rest) <= 0 do | ||
| bxor(e, a) | ||
| end |
There was a problem hiding this comment.
| defp validate_digits(<<e, e_rest::binary>>, <<a, a_rest::binary>>) | |
| when byte_size(e_rest) > 0 and byte_size(a_rest) > 0 do | |
| bxor(e, a) ||| validate_digits(e_rest, a_rest) | |
| end | |
| defp validate_digits(<<e, e_rest::binary>>, <<a, a_rest::binary>>) | |
| when byte_size(e_rest) <= 0 and byte_size(a_rest) <= 0 do | |
| bxor(e, a) | |
| end | |
| defp validate_digits(<<e, e_rest::binary>>, <<a, a_rest::binary>>) do | |
| bxor(e, a) ||| validate_digits(e_rest, a_rest) | |
| end | |
| defp validate_digits(<<>>, <<>>) do | |
| true | |
| end |
|
|
||
| @spec validate_digits(integer(), integer()) :: :error | integer() | ||
| defp validate_digits(e, a) | ||
| when byte_size(e) !== byte_size(a), do: :error |
There was a problem hiding this comment.
This should be a separate clause, otherwise we are checking the byte size on every operation. I'd just move it out of this function. :)
| totp_size > 10 && raise ArgumentError, "totp_size cannot be above 10" | ||
| totp_size <= 0 && raise ArgumentError, "totp_size cannot be 0 or under" |
There was a problem hiding this comment.
| totp_size > 10 && raise ArgumentError, "totp_size cannot be above 10" | |
| totp_size <= 0 && raise ArgumentError, "totp_size cannot be 0 or under" | |
| totp_size not in 6..10 && raise ArgumentError, "length must be between 6 and 10" |
|
Thank you for the review, I realised that the otp uri has an optional |
|
@LoicBersier I may be missing something (it has been a while), but the otpauth_uri is what we generate, so I think we need to update the docs to say "pass digits to otpauth_uri". Btw, given it is called "digits" in the otpauth_uri, we probably want to rename "length" to "digits". Sorry for the back and forth! |
…its for otpauth_uri
|
No worries! Added a mention of it in the doc and an example under "Generating URIs for QR Code" |
| *in seconds*) to be used. Default is `System.os_time(:second)`. | ||
| * `:period` - The period (in seconds) in which the code is valid. Default is `30`. | ||
| If this option is given to `verification_code/2`, it must also be given to `valid?/3`. | ||
| * `:totp_digits` - The desired length of the totp. Default is 6. |
There was a problem hiding this comment.
Sorry, let's call it just digits!
|
@kianmeng would you be so kind to give us a hand with CI? ❤️ |
|
💚 💙 💜 💛 ❤️ |
Add a
totp_sizeoption to allow having bigger totp tokens (limited at 10)It required changing the verification_code and valid? as it was hard coded for a length of 6