123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121(* 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=Logs.Src.create"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)inLogs.info~src:section(funfmt->fmt"AUTH: invalid credentials!");Lwt.fail(Ocsigen.Ocsigen_cohttp.Ext_http_error(`Unauthorized,None,Someh))andinvalid_header()=Logs.info~src:section(funfmt->fmt"AUTH: invalid Authorization header");Lwt.fail(Ocsigen.Ocsigen_cohttp.Ocsigen_http_error(Ocsigen_cookie_map.empty,`Bad_request))inletvalidate~err~requests=matchCohttp.Auth.credential_of_stringswith|`Basic(user,pass)->authuserpass>>=funb->ifbthen((* Record the authenticated user for the access log (%u field). *)Ocsigen.Request.set_remote_userrequestuser;Lwt.return(Ocsigen.Extensions.Ext_nexterr))elsereject()|`Other_s->invalid_header()inmatchrswith|Ocsigen.Extensions.Req_not_found(err,ri)->(letrequest=ri.Ocsigen.Extensions.request_infoinmatchOcsigen.Request.headerrequestOcsigen_http.Header.Name.authorizationwith|Somes->validate~err~requests|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 for the config file: *)let()=Ocsigen.Extensions.register~name:"authbasic"~fun_site:(fun______->parse_config)()(** Instruction for static linking without config file: *)letrun~realm~auth()___=gen~realm~auth