To prepare the CAPTCHA authentication, we will need the main and its plugins , , , , , , , . (I provided links to my forks, as I made some changes that I have not yet managed to push to the original repositories. You can also use .)
First, let's set
encrypted_session_key "abcdefghijklmnopqrstuvwxyz123456";Next, just to be safe, we disable the authorization header
more_clear_input_headers Authorization;Now we protect everything with authentication
auth_request /auth;
location = /auth {
internal;
subrequest_access_phase on; # allow authentication phase in subrequest
auth_request off; # do not use authentication
set_decode_base64 $auth_decode $cookie_auth; # decode authentication cookie
set_decrypt_session $auth_decrypt $auth_decode; # decrypt authentication
if ($auth_decrypt = "") { return 401 UNAUTHORIZED; } # if decryption failed, the user is not authenticated
more_set_input_headers "Authorization: Basic $auth_decrypt"; # replace authentication with basic (to use variable $remote_user)
auth_basic_ldap_realm Auth; # enable LDAP authentication
auth_basic_ldap_url ldap://ldap.server.com; # set address
auth_basic_ldap_bind_dn dn.server.com; # set suffix
echo -n OK; # user authenticated
}For authorized users, show content from their folder
location / {
alias html/$remote_user/;
}And if authorization is absent, show the authorization form with CAPTCHA
error_page 401 = @error401;
location @error401 {
set_escape_uri $request_uri_escape $request_uri; # encode request
return 303 /login?request_uri=$request_uri_escape; # redirect to login form with captcha, preserving request
}
location =/login {
default_type "text/html; charset=utf-8"; # set content type
if ($request_method = GET) { # if only displaying the login form with captcha
template login.html.ct2; # set template
ctpp2 on; # enable template engine
set_secure_random_alphanum $csrf_random 32; # generate random csrf
encrypted_session_expires 300; # set csrf lifetime to 5 minutes (5 * 60 = 300)
set_encrypt_session $csrf_encrypt $csrf_random; # encrypt random csrf
set_encode_base64 $csrf_encode $csrf_encrypt; # encode encrypted csrf
add_header Set-Cookie "CSRF=$csrf_encode; Max-Age=300"; # save encrypted csrf in cookie for 5 minutes (5 * 60 = 300)
return 200 "{"csrf":"$csrf_random"}"; # return json for template engine
} # otherwise - process the login form with captcha
set_form_input $csrf_form csrf; # get csrf from form
set_unescape_uri $csrf_unescape $csrf_form; # decode csrf from form
set_decode_base64 $csrf_decode $cookie_csrf; # decode csrf from cookie
set_decrypt_session $csrf_decrypt $csrf_decode; # decrypt csrf from cookie
if ($csrf_decrypt != $csrf_unescape) { return 303 $request_uri; } # if csrf from form does not match csrf from cookie, redirect to show form again
set_form_input $captcha_form captcha; # get captcha from form
set_unescape_uri $captcha_unescape $captcha_form; # decode captcha from form
set_md5 $captcha_md5 "secret${captcha_unescape}${csrf_decrypt}"; # calculate md5
if ($captcha_md5 != $cookie_captcha) { return 303 $request_uri; } # if md5 does not match captcha from cookie, redirect to show form again
set_form_input $username_form username; # get username from form
set_form_input $password_form password; # get password from form
set_unescape_uri $username_unescape $username_form; # decode username from form
set_unescape_uri $password_unescape $password_form; # decode password from form
encrypted_session_expires 2592000; # set session lifetime to 30 days (30 * 24 * 60 * 60 = 2592000)
set $username_password "$username_unescape:$password_unescape"; # set basic authorization
set_encode_base64 $username_password_encode $username_password; # encode basic authorization
set_encrypt_session $auth_encrypt $username_password_encode; # encrypt basic authorization
set_encode_base64 $auth_encode $auth_encrypt; # encode encrypted basic authorization
add_header Set-Cookie "Auth=$auth_encode; Max-Age=2592000"; # save encrypted basic authorization in auth cookie for 30 days (30 * 24 * 60 * 60 = 2592000)
set $arg_request_uri_or_slash $arg_request_uri; # copy request from argument
set_if_empty $arg_request_uri_or_slash "/"; # if argument not set, then start
set_unescape_uri $request_uri_unescape $arg_request_uri_or_slash; # decode request
return 303 $request_uri_unescape; # redirect to saved request
}login.html
<html>
<body>
<form method="post" action="">
<input type="hidden" name="csrf" value="<TMPL_var csrf>" />
username: <input type="text" name="username" placeholder="Enter User Name..." /><br />
password: <input type="password" name="password" /><br />
captcha: <img src="/captcha?csrf=<TMPL_var csrf>"/><input type="text" name="captcha" autocomplete="off" /><br />
<input type="submit" name="submit" value="submit" />
<input type="hidden" name="trp-form-language" value="en"/></form>
</body>
</html>Source: habr.com
