couchdb-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rnew...@apache.org
Subject chttpd commit: updated refs/heads/master to 5974a8f
Date Wed, 19 Oct 2016 10:16:15 GMT
Repository: couchdb-chttpd
Updated Branches:
  refs/heads/master 4e9b4a64c -> 5974a8f54


Make auth handlers configurable.


Project: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/repo
Commit: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/commit/5974a8f5
Tree: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/tree/5974a8f5
Diff: http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/diff/5974a8f5

Branch: refs/heads/master
Commit: 5974a8f54ebce69ff932df15f5fd455f5c664b7d
Parents: 4e9b4a6
Author: Daniel Mu╠łnch <dani.munch@gmail.com>
Authored: Sat Oct 15 20:16:53 2016 +0200
Committer: Robert Newson <rnewson@apache.org>
Committed: Wed Oct 19 11:16:01 2016 +0100

----------------------------------------------------------------------
 src/chttpd.erl | 33 +++++++++++++++++++++++++++++----
 1 file changed, 29 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/couchdb-chttpd/blob/5974a8f5/src/chttpd.erl
----------------------------------------------------------------------
diff --git a/src/chttpd.erl b/src/chttpd.erl
index d55aa5e..2ae45c4 100644
--- a/src/chttpd.erl
+++ b/src/chttpd.erl
@@ -117,6 +117,8 @@ start_link(Name, Options) ->
          end,
     ok = couch_httpd:validate_bind_address(IP),
 
+    set_auth_handlers(),
+
     Options1 = Options ++ [
         {loop, fun ?MODULE:handle_request/1},
         {name, Name},
@@ -464,12 +466,35 @@ extract_cookie(#httpd{mochi_req = MochiReq}) ->
     end.
 %%% end hack
 
-authenticate_request(Req) ->
-    AuthenticationFuns = [
-        {<<"cookie">>, fun chttpd_auth:cookie_authentication_handler/1},
-        {<<"default">>, fun chttpd_auth:default_authentication_handler/1},
+set_auth_handlers() ->
+    AuthenticationDefault =  "{chttpd_auth, cookie_authentication_handler},
+      {chttpd_auth, default_authentication_handler}",
+    AuthenticationSrcs = couch_httpd:make_fun_spec_strs(
+        config:get("chttpd", "authentication_handlers", AuthenticationDefault)),
+    AuthHandlers = lists:map(
+        fun(A) -> {auth_handler_name(A), couch_httpd:make_arity_1_fun(A)} end, AuthenticationSrcs),
+    AuthenticationFuns = AuthHandlers ++ [
         fun chttpd_auth:party_mode_handler/1 %% must be last
     ],
+    ok = application:set_env(chttpd, auth_handlers, AuthenticationFuns).
+
+% SpecStr is a string like "{my_module, my_fun}"
+% Takes the first token of the function name in front '_' as auth handler name
+% e.g.
+% chttpd_auth:default_authentication_handler: default
+% chttpd_auth_cookie_authentication_handler: cookie
+% couch_http_auth:proxy_authentication_handler: proxy
+%
+% couch_http:auth_handler_name can't be used here, since it assumes the name
+% of the auth handler to be the 6th token split by [\\W_]
+% - this only works for modules with exactly two underscores in their name
+% - is not very robust (a space after the ',' is assumed)
+auth_handler_name(SpecStr) ->
+    {ok, {_, Fun}} = couch_util:parse_term(SpecStr),
+    hd(string:tokens(atom_to_list(Fun), "_")).
+
+authenticate_request(Req) ->
+    {ok, AuthenticationFuns} = application:get_env(chttpd, auth_handlers),
     authenticate_request(Req, chttpd_auth_cache, AuthenticationFuns).
 
 authenticate_request(#httpd{} = Req0, AuthModule, AuthFuns) ->


Mime
View raw message