janl commented on code in PR #5754:
URL: https://github.com/apache/couchdb/pull/5754#discussion_r2616322312


##########
src/couch/src/couch_secrets.erl:
##########
@@ -0,0 +1,180 @@
+% Licensed under the Apache License, Version 2.0 (the "License"); you may not
+% use this file except in compliance with the License.  You may obtain a copy 
of
+% the License at
+%
+%   http://www.apache.org/licenses/LICENSE-2.0
+%
+% Unless required by applicable law or agreed to in writing, software
+% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+% License for the specific language governing permissions and limitations under
+% the License.
+
+-module(couch_secrets).
+
+-behaviour(gen_server).
+-behaviour(config_listener).
+
+-include_lib("couch/include/couch_db.hrl").
+
+%% public api
+-export([sign/1, sign/2, verify/2, verify/3, secret_is_set/0]).
+
+%% gen_server functions
+-export([
+    start_link/0,
+    init/1,
+    handle_call/3,
+    handle_cast/2,
+    handle_continue/2,
+    handle_info/2
+]).
+
+%% config_listener functions
+-export([
+    handle_config_change/5,
+    handle_config_terminate/3
+]).
+
+sign(Message) ->
+    sign(Message, <<>>).
+
+sign(Message, ExtraSecret) ->
+    [HashAlgorithm | _] = couch_util:get_config_hash_algorithms(),
+    case current_secret_from_ets() of
+        undefined ->
+            throw({internal_server_error, <<"cookie auth secret is not 
set">>});
+        CurrentSecret ->
+            FullSecret = <<CurrentSecret/binary, ExtraSecret/binary>>,
+            couch_util:hmac(HashAlgorithm, FullSecret, Message)
+    end.
+
+verify(Message, ExpectedMAC) ->
+    verify(Message, <<>>, ExpectedMAC).
+
+verify(Message, ExtraSecret, ExpectedMAC) ->
+    FullSecrets = [<<Secret/binary, ExtraSecret/binary>> || Secret <- 
all_secrets_from_ets()],
+    AllAlgorithms = couch_util:get_config_hash_algorithms(),
+    verify(Message, AllAlgorithms, FullSecrets, ExpectedMAC).
+
+verify(Message, AllAlgorithms, FullSecrets, ExpectedMAC) ->
+    VerifyFun = fun({Secret, Algorithm}) ->
+        ActualMAC = couch_util:hmac(Algorithm, Secret, Message),
+        case crypto:hash_info(Algorithm) of
+            #{size := Size} when Size == byte_size(ExpectedMAC) ->
+                crypto:hash_equals(ExpectedMAC, ActualMAC);
+            _ ->
+                false
+        end
+    end,
+    lists:any(VerifyFun, [{S, A} || S <- FullSecrets, A <- AllAlgorithms]).
+
+secret_is_set() ->
+    current_secret_from_ets() /= undefined.
+
+start_link() ->
+    gen_server:start_link({local, ?MODULE}, ?MODULE, nil, []).
+
+init(nil) ->
+    ets:new(?MODULE, [named_table, {read_concurrency, true}]),
+    true = ets:insert(?MODULE, {{node(), current}, 
current_secret_from_config()}),
+    update_all_secrets(),
+    erlang:send_after(5000, self(), cache_cleanup),
+    ok = config:listen_for_changes(?MODULE, undefined),
+    {ok, nil, {continue, get_secrets}}.
+
+handle_call({insert, {Node, current}, Secret}, _From, State) when Node == 
node() ->
+    OldSecret = current_secret_from_ets(),
+    TimeoutSecs = chttpd_util:get_chttpd_auth_config_integer("timeout", 600),
+    ExpiresAt = erlang:system_time(second) + TimeoutSecs,
+    ets:insert(?MODULE, [{{Node, current}, Secret}, {{Node, ExpiresAt}, 
OldSecret}]),
+    update_all_secrets(),
+    {reply, ok, State};
+handle_call({insert, Key, Secret}, _From, State) ->
+    ets:insert(?MODULE, {Key, Secret}),
+    update_all_secrets(),
+    {reply, ok, State};
+handle_call(get_secrets, _From, State) ->
+    Secrets = ets:match_object(?MODULE, {{node(), '_'}, '_'}),
+    {reply, Secrets, State};
+handle_call(flush_cache, _From, State) ->

Review Comment:
   might be nice to leave a comment to that effect



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to