iilyak commented on a change in pull request #3568:
URL: https://github.com/apache/couchdb/pull/3568#discussion_r635224152



##########
File path: src/couch/src/couch_httpd_auth.erl
##########
@@ -305,126 +368,175 @@ 
cookie_auth_header(#httpd{user_ctx=#user_ctx{name=User}, auth={Secret, true}}=Re
     CookieHeader = couch_util:get_value("Set-Cookie", Headers, ""),
     Cookies = mochiweb_cookies:parse_cookie(CookieHeader),
     AuthSession = couch_util:get_value("AuthSession", Cookies),
-    if AuthSession == undefined ->
-        TimeStamp = make_cookie_time(),
-        [cookie_auth_cookie(Req, ?b2l(User), Secret, TimeStamp)];
-    true ->
-        []
+    if
+        AuthSession == undefined ->
+            TimeStamp = make_cookie_time(),
+            [cookie_auth_cookie(Req, ?b2l(User), Secret, TimeStamp)];
+        true ->
+            []
     end;
-cookie_auth_header(_Req, _Headers) -> [].
+cookie_auth_header(_Req, _Headers) ->
+    [].
 
 cookie_auth_cookie(Req, User, Secret, TimeStamp) ->
     SessionData = User ++ ":" ++ erlang:integer_to_list(TimeStamp, 16),
     Hash = couch_util:hmac(sha, Secret, SessionData),
-    mochiweb_cookies:cookie("AuthSession",
+    mochiweb_cookies:cookie(
+        "AuthSession",
         couch_util:encodeBase64Url(SessionData ++ ":" ++ ?b2l(Hash)),
-        [{path, "/"}] ++ cookie_scheme(Req) ++ max_age() ++ cookie_domain() ++ 
same_site()).
+        [{path, "/"}] ++ cookie_scheme(Req) ++ max_age() ++ cookie_domain() ++ 
same_site()
+    ).
 
 ensure_cookie_auth_secret() ->
     case config:get("couch_httpd_auth", "secret", undefined) of
         undefined ->
             NewSecret = ?b2l(couch_uuids:random()),
             config:set("couch_httpd_auth", "secret", NewSecret),
             NewSecret;
-        Secret -> Secret
+        Secret ->
+            Secret
     end.
 
 % session handlers
 % Login handler with user db
 handle_session_req(Req) ->
     handle_session_req(Req, couch_auth_cache).
 
-handle_session_req(#httpd{method='POST', mochi_req=MochiReq}=Req, AuthModule) 
->
+handle_session_req(#httpd{method = 'POST', mochi_req = MochiReq} = Req, 
AuthModule) ->
     ReqBody = MochiReq:recv_body(),
-    Form = case MochiReq:get_primary_header_value("content-type") of
-        % content type should be json
-        "application/x-www-form-urlencoded" ++ _ ->
-            mochiweb_util:parse_qs(ReqBody);
-        "application/json" ++ _ ->
-            {Pairs} = ?JSON_DECODE(maybe_decompress(Req, ReqBody)),
-            lists:map(fun({Key, Value}) ->
-              {?b2l(Key), ?b2l(Value)}
-            end, Pairs);
-        _ ->
-            []
-    end,
+    Form =
+        case MochiReq:get_primary_header_value("content-type") of
+            % content type should be json
+            "application/x-www-form-urlencoded" ++ _ ->
+                mochiweb_util:parse_qs(ReqBody);
+            "application/json" ++ _ ->
+                {Pairs} = ?JSON_DECODE(maybe_decompress(Req, ReqBody)),
+                lists:map(
+                    fun({Key, Value}) ->
+                        {?b2l(Key), ?b2l(Value)}
+                    end,
+                    Pairs
+                );
+            _ ->
+                []
+        end,
     UserName = ?l2b(extract_username(Form)),
     Password = ?l2b(couch_util:get_value("password", Form, "")),
     ?LOG_DEBUG(#{what => login_attempt, user => UserName}),
-    couch_log:debug("Attempt Login: ~s",[UserName]),
-    {ok, UserProps, _AuthCtx} = case AuthModule:get_user_creds(Req, UserName) 
of
-        nil -> {ok, [], nil};
-        Result -> Result
-    end,
+    couch_log:debug("Attempt Login: ~s", [UserName]),
+    {ok, UserProps, _AuthCtx} =
+        case AuthModule:get_user_creds(Req, UserName) of
+            nil -> {ok, [], nil};
+            Result -> Result
+        end,
     case authenticate(Password, UserProps) of
         true ->
             verify_totp(UserProps, Form),
             % setup the session cookie
             Secret = ?l2b(ensure_cookie_auth_secret()),
             UserSalt = couch_util:get_value(<<"salt">>, UserProps),
             CurrentTime = make_cookie_time(),
-            Cookie = cookie_auth_cookie(Req, ?b2l(UserName), <<Secret/binary, 
UserSalt/binary>>, CurrentTime),
+            Cookie = cookie_auth_cookie(
+                Req,
+                ?b2l(UserName),
+                <<Secret/binary, UserSalt/binary>>,
+                CurrentTime
+            ),
             % TODO document the "next" feature in Futon
-            {Code, Headers} = case couch_httpd:qs_value(Req, "next", nil) of
-                nil ->
-                    {200, [Cookie]};
-                Redirect ->
-                    {302, [Cookie, {"Location", couch_httpd:absolute_uri(Req, 
Redirect)}]}
-            end,
-            send_json(Req#httpd{req_body=ReqBody}, Code, Headers,
+            {Code, Headers} =
+                case couch_httpd:qs_value(Req, "next", nil) of
+                    nil ->
+                        {200, [Cookie]};
+                    Redirect ->
+                        {302, [Cookie, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}]}
+                end,
+            send_json(
+                Req#httpd{req_body = ReqBody},
+                Code,
+                Headers,
                 {[
                     {ok, true},
                     {name, UserName},
                     {roles, couch_util:get_value(<<"roles">>, UserProps, [])}
-                ]});
+                ]}
+            );
         false ->
             authentication_warning(Req, UserName),
             % clear the session
-            Cookie = mochiweb_cookies:cookie("AuthSession", "", [{path, "/"}] 
++ cookie_scheme(Req)),
-            {Code, Headers} = case couch_httpd:qs_value(Req, "fail", nil) of
-                nil ->
-                    {401, [Cookie]};
-                Redirect ->
-                    {302, [Cookie, {"Location", couch_httpd:absolute_uri(Req, 
Redirect)}]}
-            end,
-            send_json(Req, Code, Headers, {[{error, 
<<"unauthorized">>},{reason, <<"Name or password is incorrect.">>}]})
+            Cookie = mochiweb_cookies:cookie(
+                "AuthSession",
+                "",
+                [{path, "/"}] ++ cookie_scheme(Req)
+            ),
+            {Code, Headers} =
+                case couch_httpd:qs_value(Req, "fail", nil) of
+                    nil ->
+                        {401, [Cookie]};
+                    Redirect ->
+                        {302, [Cookie, {"Location", 
couch_httpd:absolute_uri(Req, Redirect)}]}
+                end,
+            send_json(
+                Req,
+                Code,
+                Headers,
+                {[{error, <<"unauthorized">>}, {reason, <<"Name or password is 
incorrect.">>}]}
+            )
     end;
 % get user info
 % GET /_session
-handle_session_req(#httpd{method='GET', user_ctx=UserCtx}=Req, _AuthModule) ->
+handle_session_req(#httpd{method = 'GET', user_ctx = UserCtx} = Req, 
_AuthModule) ->
     Name = UserCtx#user_ctx.name,
     ForceLogin = couch_httpd:qs_value(Req, "basic", "false"),
     case {Name, ForceLogin} of
         {null, "true"} ->
             throw({unauthorized, <<"Please login.">>});
         {Name, _} ->
-            send_json(Req, {[
-                % remove this ok
-                {ok, true},
-                {<<"userCtx">>, {[
-                    {name, Name},
-                    {roles, UserCtx#user_ctx.roles}
-                ]}},
-                {info, {[
-                    {authentication_handlers, [
-                       N || {N, _Fun} <- Req#httpd.authentication_handlers]}
-                ] ++ maybe_value(authenticated, UserCtx#user_ctx.handler, 
fun(Handler) ->
-                        Handler
-                    end) ++ maybe_value(authentication_db, 
config:get("chttpd_auth", "authentication_db"), fun(Val) ->
-                        ?l2b(Val)
-                    end)}}
-            ]})
+            send_json(
+                Req,
+                {[
+                    % remove this ok
+                    {ok, true},
+                    {<<"userCtx">>,
+                        {[
+                            {name, Name},
+                            {roles, UserCtx#user_ctx.roles}
+                        ]}},
+                    {info, {
+                        [
+                            {authentication_handlers, [
+                                N
+                             || {N, _Fun} <- Req#httpd.authentication_handlers
+                            ]}
+                        ] ++
+                            maybe_value(authenticated, 
UserCtx#user_ctx.handler, fun(Handler) ->
+                                Handler
+                            end) ++
+                            maybe_value(
+                                authentication_db,
+                                config:get("chttpd_auth", "authentication_db"),
+                                fun(Val) ->
+                                    ?l2b(Val)
+                                end
+                            )
+                    }}
+                ]}
+            )
     end;
 % logout by deleting the session
-handle_session_req(#httpd{method='DELETE'}=Req, _AuthModule) ->
-    Cookie = mochiweb_cookies:cookie("AuthSession", "", [{path, "/"}] ++
-        cookie_domain() ++ cookie_scheme(Req)),
-    {Code, Headers} = case couch_httpd:qs_value(Req, "next", nil) of
-        nil ->
-            {200, [Cookie]};
-        Redirect ->
-            {302, [Cookie, {"Location", couch_httpd:absolute_uri(Req, 
Redirect)}]}
-    end,
+handle_session_req(#httpd{method = 'DELETE'} = Req, _AuthModule) ->
+    Cookie = mochiweb_cookies:cookie(
+        "AuthSession",
+        "",
+        [{path, "/"}] ++

Review comment:
       -1. I prefer 
   
   ```
   [{path, "/"}]
       ++ cookie_domain() 
       ++ cookie_scheme(Req)
   ```
   
   




-- 
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.

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


Reply via email to