util::http::cookie_auth (public)

 util::http::cookie_auth [ -headers headers ] [ -auth_vars auth_vars ] \
    [ -auth_url auth_url ] [ -auth_form auth_form ] \
    [ -auth_cookies auth_cookies ] [ -preference preference ]

Defined in packages/acs-tcl/tcl/http-client-procs.tcl

This proc implements the generic pattern for cookie-based authentication: user logs in a webpage providing username, password and optionally other information in a form, page replies generating one or more authentication cookies by which user will be recognized on subsequent interaction with the server. By this method was possible, for example, to authenticate on a remote OpenACS installation providing 'email' and 'password' as credentials to the /register/ page, and using 'ad_session_id' and 'ad_user_login' as 'auth_cookies'. This proc is a bit hacky and is nowadays not clear if it makes sense anymore... This proc takes care to submit to the login form also every other formfield on the login page. This is important because this (often hidden) formfields can contain tokens necessary for the authentication process.

ns_set of request headers that will be populated with auth headers. If not specified, a new ns_set will be created. Existing cookies will be overwritten.
Variables issued to the login page in 'export_vars -url' form.
Login url
Form to put our data into. If not specified, there must be only one form on the login page, otherwise proc will throw an error.
Cookies we should look for in the response from the login page to obtain authentication data. If not specified, this will refer to every cookie received into 'set-cookie' response headers.
(defaults to "native curl") (optional)
ns_set of headers containing authentication data

Partial Call Graph (max 5 caller/called nodes):
%3 export_vars export_vars (public) util::html::get_form util::html::get_form (public) util::html::get_form_vars util::html::get_form_vars (public) util::html::get_forms util::html::get_forms (public) util::http::get util::http::get (public) util::http::cookie_auth util::http::cookie_auth util::http::cookie_auth->export_vars util::http::cookie_auth->util::html::get_form util::http::cookie_auth->util::html::get_form_vars util::http::cookie_auth->util::html::get_forms util::http::cookie_auth->util::http::get

No testcase defined.
Source code:
    if {$headers eq ""} {
        set headers [ns_set create headers]

    # Normalize url. Slashes at the end can make the same url don't
    # look the same for the server, if we retrieve the same url from
    # the 'action' attribute of the form.
    set auth_url [string trimright $auth_url "/"]
    set base_url [split $auth_url "/"]
    set base_url [lindex $base_url 0]//[lindex $base_url 2]

    # Call login url to obtain login form
    set r [util::http::get -url $auth_url -preference $preference]

    # Get cookies from response
    util::http::set_cookies  -resp_headers [dict get $r headers]  -headers      $headers  -cookie_names $auth_cookies

    # Obtain and export form vars not provided explicitly
    set form [util::html::get_forms -html [dict get $r page]]
    set form [util::html::get_form -forms $form -id $auth_form]

    set a [dict get $form attributes]
    # Action could be different from original login url I take that
    # from form attributes.
    if {[dict exists $a action]} {
        set auth_url ${base_url}[dict get $a action]
        set auth_url [string trimright $auth_url "/"]

    set formvars [util::html::get_form_vars -form $form]
    set formvars [export_vars -exclude $auth_vars $formvars]
    # Export vars provided explicitly in caller scope
    set auth_vars [uplevel [list export_vars -url $auth_vars]]
    # Join form vars with our vars
    set formvars [join [list $formvars $auth_vars"&"]

    # Call login url with authentication parameters. Just retrieve the
    # first response, as it is common for login pages to redirect
    # somewhere, but we just need to steal the cookies.
    set r [util::http::post  -url $auth_url  -body $formvars  -headers $headers  -max_depth 0  -preference $preference]

    # Get cookies from response
    util::http::set_cookies  -resp_headers [dict get $r headers]  -headers      $headers  -cookie_names $auth_cookies

    return $headers
XQL Not present:
Generic, PostgreSQL, Oracle
[ hide source ] | [ make this the default ]
Show another procedure: