Github user kxepal commented on a diff in the pull request:
https://github.com/apache/couchdb-couch/pull/12#discussion_r19597615
--- Diff: src/couch_httpd_auth.erl ---
@@ -430,3 +432,45 @@ max_age() ->
config:get("couch_httpd_auth", "timeout", "600")),
[{max_age, Timeout}]
end.
+
+reject_if_two_factor(User) ->
+ case get_two_factor_config(User) of
+ undefined ->
+ ok;
+ _ ->
+ throw({unauthorized, <<"Name or password is incorrect.">>})
+ end.
+
+verify_two_factor(User, Form) ->
+ case get_two_factor_config(User) of
+ undefined ->
+ ok;
+ {Props} ->
+ Key = couch_util:get_value(<<"key">>, Props),
+ Token = ?l2b(couch_util:get_value("token", Form, "")),
+ verify_token(Key, Token)
+ end.
+
+get_two_factor_config(User) ->
+ couch_util:get_value(<<"2f">>, User).
+
+verify_token(Key, Token) ->
+ Now = make_cookie_time(),
+ Tokens = [generate_token(Key, Now - 30),
+ generate_token(Key, Now),
+ generate_token(Key, Now + 30)],
+ %% evaluate all tokens in constant time
+ Match = lists:foldl(fun(T, Acc) -> couch_util:verify(T, Token) or Acc
end,
+ false, Tokens),
+ case Match of
+ true ->
+ ok;
+ _ ->
+ throw({unauthorized, <<"Name or password is incorrect.">>})
+ end.
+
+generate_token(Key, Timestamp) ->
+ integer_to_binary(couch_totp:generate(Key, Timestamp, 30, 6)).
--- End diff --
Can we make algo configurable? Let it be by default those which will give
us compatibility with older erlang releases, but people with modern one will be
able to change since they don't need in such compatibility.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---