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

Fix addwindow #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.rebar3
.eunit
.rebar
ebin
Expand Down
25 changes: 12 additions & 13 deletions src/pot.erl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ valid_totp(Token, Secret, Opts) ->
Token ->
true;
_ ->
Window = proplists:get_value(window, Opts, 0),
Window = proplists:get_value(addwindow, Opts, 0),
Copy link
Collaborator

Choose a reason for hiding this comment

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

This looks wrong to me. window is the parameter that expands the number of allowed interval tokens to be used (basically +/- window intervals are considered valid). addwindow is supposed to offset the interval that you check.

Copy link

Choose a reason for hiding this comment

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

Hi @nalundgaard, that's very useful to know! It would be great if the docs described these options in a bit more detail?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@danturn Indeed! It's definitely worth tracking separately. The examples hint at what the options do, but they really don't explain in detail their intention, so you kind of have to piece it together from reading the code and being familiar with the hotp/totp RFC nomenclature.

Copy link
Collaborator

Choose a reason for hiding this comment

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

@danturn maybe you will like #22.

case check_candidate(Token, Secret, IntervalsNo - Window, IntervalsNo + Window, Opts) of
Copy link

Choose a reason for hiding this comment

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

Isn't addwidow a number that should multiply the interval_length? This is now subtracting/adding the result of time_interval(Opts) (big number like 52357080) by a relatively small number, Window = addwindow (-2-1,0,1,2,3).
Also if you really plan to remove support of window option, can you update the references of it in the README?

Copy link
Collaborator

Choose a reason for hiding this comment

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

addwindow is supposed to offset the interval for checking the totp, not change the interval_length.

false ->
false;
Expand All @@ -119,21 +119,20 @@ valid_totp(Token, Secret, Opts) ->
-spec time_interval(proplist()) -> time().
time_interval(Opts) ->
IntervalLength = proplists:get_value(interval_length, Opts, 30),
AddSeconds = proplists:get_value(addwindow, Opts, 0) * proplists:get_value(interval_length, Opts, 30),
Copy link

Choose a reason for hiding this comment

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

Could you please explain to me why this behavior should be changed? We extensively use this in our application tests.

{MegaSecs, Secs, _} = proplists:get_value(timestamp, Opts, os:timestamp()),
trunc((MegaSecs * 1000000 + (Secs + AddSeconds)) / IntervalLength).
trunc((MegaSecs * 1000000 + (Secs)) / IntervalLength).

check_candidate(Token, Secret, Current, Last, Opts) when Current =< Last ->
case Current of
Last ->
false;
check_candidate(_, _, Current, Last, _) when Current > Last ->
false;

check_candidate(Token, Secret, Current, Last, Opts) ->
Candidate = hotp(Secret, Current, Opts),
case Candidate of
Token ->
Current;
_ ->
Candidate = hotp(Secret, Current, Opts),
case Candidate of
Token ->
Current;
_ ->
check_candidate(Token, Secret, Current + 1, Last, Opts) end end.
check_candidate(Token, Secret, Current + 1, Last, Opts) end.


-spec prepend_zeros(token(), non_neg_integer()) -> token().
prepend_zeros(Token, N) ->
Expand Down