123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154(* Ocsigen
* http://www.ocsigen.org
* Module authbasic.ml
* Copyright (C) 2008 Stéphane Glondu
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, with linking exception;
* either version 2.1 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*)openLwt.Infixletsection=Lwt_log.Section.make"ocsigen:ext:access-control"typeauth=string->string->boolLwt.texceptionBad_config_tag_for_authofstringletregister_basic_authentication_method,get_basic_authentication_method=letfun_auth=ref(fun_config->raise(Bad_config_tag_for_auth"<unknown basic authentication method>"))in(* register_basic_authentication_method *)(funnew_fun_auth->letold_fun_auth=!fun_authinfun_auth:=(funconfig->tryold_fun_authconfigwithBad_config_tag_for_auth_c->new_fun_authconfig)),(* get_basic_authentication_method *)(funconfig->!fun_authconfig)(* Basic authentication with a predefined login/password (example) *)let_=letopenXmlinregister_basic_authentication_method@@function|Element("plain",["login",login;"password",password],_)->(funlp->Lwt.return(login=l&&password=p))|_->raise(Ocsigen_extensions.Bad_config_tag_for_extension"not for htpasswd")letgen~realm~authrs=letreject()=leth=Cohttp.Header.init_with"WWW-Authenticate"(Printf.sprintf"Basic realm=\"%s\""realm)inLwt_log.ign_info~section"AUTH: invalid credentials!";Lwt.fail(Ocsigen_cohttp.Ext_http_error(`Unauthorized,None,Someh))andinvalid_header()=Lwt_log.ign_info~section"AUTH: invalid Authorization header";Lwt.fail(Ocsigen_cohttp.Ocsigen_http_error(Ocsigen_cookie_map.empty,`Bad_request))inletvalidate~errs=matchCohttp.Auth.credential_of_stringswith|`Basic(user,pass)->authuserpass>>=funb->ifbthenLwt.return(Ocsigen_extensions.Ext_nexterr)elsereject()|`Other_s->invalid_header()inmatchrswith|Ocsigen_extensions.Req_not_found(err,ri)->(matchOcsigen_request.headerri.Ocsigen_extensions.request_infoOcsigen_header.Name.authorizationwith|Somes->validate~errs|None->reject())|Ocsigen_extensions.Req_found_->Lwt.returnOcsigen_extensions.Ext_do_nothingletparse_configelement=letrealm_ref=ref""inletrest_ref=ref[]inOcsigen_extensions.(Configuration.process_element~in_tag:"host"~other_elements:(funt__->raise(Bad_config_tag_for_extensiont))~elements:[Configuration.element~name:"authbasic"~attributes:[Configuration.attribute~name:"realm"~obligatory:true(funs->realm_ref:=s)]~other_elements:(funnameattrscontent->rest_ref:=Xml.Element(name,attrs,content)::!rest_ref)()]element);letrealm=!realm_refinletauth=match!rest_refwith|[x]->get_basic_authentication_methodx|_->Ocsigen_extensions.badconfig"Bad syntax for tag authbasic"ingen~realm~auth(** Registration of the extension *)let()=Ocsigen_extensions.register~name:"authbasic"~fun_site:(fun______->parse_config)()letrealm=Ocsigen_server.Site.Config.key()letauth=Ocsigen_server.Site.Config.key()letextension=Ocsigen_server.Site.create_extension(fun{Ocsigen_server.Site.Config.accessor}->matchaccessorrealm,accessorauthwith|Somerealm,Someauth->gen~realm~auth|_,_->failwith"Authbasic realm and/or auth not set")