For authorization setup with captcha, we need the main and its plugins , , , , , , , . (I provided links to my forks because I made some changes that I haven't been able to push to the original repositories yet. You can also use .)
First, let's set
encrypted_session_key "abcdefghijklmnopqrstuvwxyz123456";Next, just in case, let's disable the authorization header
more_clear_input_headers Authorization;Now, let's protect everything with authorization
auth_request /auth;
location =/auth {
internal;
subrequest_access_phase on; # allow authorization phase in subrequest
auth_request off; # do not use authorization
set_decode_base64 $auth_decode $cookie_auth; # decode authorization cookie
set_decrypt_session $auth_decrypt $auth_decode; # decrypt authorization cookie
if ($auth_decrypt = "") { return 401 UNAUTHORIZED; } # if decryption failed, user is not authorized
more_set_input_headers "Authorization: Basic $auth_decrypt"; # replace authorization with basic (to use $remote_user variable)
auth_basic_user_file /data/nginx/.htaccess; # set basic authorization file
auth_basic Auth; # enable basic authorization
echo -n OK; # user authorized
}For authorized users, we show content from their folder
location / {
alias html/$remote_user/;
}And in the absence of authorization, we display the authorization form with captcha
error_page 401 = @error401;
location @error401 {
set_escape_uri $request_uri_escape $request_uri; # kodning af forespørgsel
return 303 /login?request_uri=$request_uri_escape; # omdirigering til loginformular med captcha, gemmer forespørgslen
}
location =/login {
default_type "text/html; charset=utf-8"; # angiv type
if ($request_method = GET) { # hvis kun at vise loginformular med captcha
template login.html.ct2; # angiv skabelon
ctpp2 on; # aktiver skabeloner
set_secure_random_alphanum $csrf_random 32; # angiv tilfældig csrf
encrypted_session_expires 300; # angiv CSRF-levetid 5 minutter (5 * 60 = 300)
set_encrypt_session $csrf_encrypt $csrf_random; # krypter tilfældig csrf
set_encode_base64 $csrf_encode $csrf_encrypt; # kodning af krypteret csrf
add_header Set-Cookie "CSRF=$csrf_encode; Max-Age=300"; # placere krypteret csrf i cookie i 5 minutter (5 * 60 = 300)
return 200 "{"csrf":"$csrf_random"}"; # returnere json til skabelonmotor
} # ellers - behandle loginformular med captcha
set_form_input $csrf_form csrf; # få csrf fra formular
set_unescape_uri $csrf_unescape $csrf_form; # afkode csrf fra formular
set_decode_base64 $csrf_decode $cookie_csrf; # afkode csrf fra cookie
set_decrypt_session $csrf_decrypt $csrf_decode; # dekryptere csrf fra cookie
if ($csrf_decrypt != $csrf_unescape) { return 303 $request_uri; } # hvis csrf fra formular ikke matcher csrf fra cookie, så omdirigere for at vise formular igen
set_form_input $captcha_form captcha; # få captcha fra formular
set_unescape_uri $captcha_unescape $captcha_form; # afkode captcha fra formular
set_md5 $captcha_md5 "secret${captcha_unescape}${csrf_decrypt}"; # beregne md5
if ($captcha_md5 != $cookie_captcha) { return 303 $request_uri; } # hvis md5 ikke matcher captcha fra cookie, så omdirigere for at vise formular igen
set_form_input $username_form username; # få brugernavn fra formular
set_form_input $password_form password; # få adgangskode fra formular
set_unescape_uri $username_unescape $username_form; # afkode brugernavn fra formular
set_unescape_uri $password_unescape $password_form; # afkode adgangskode fra formular
encrypted_session_expires 2592000; # angiv sessions levetid 30 dage (30 * 24 * 60 * 60 = 2592000)
set $username_password "$username_unescape:$password_unescape"; # angiv basic autentificering
set_encode_base64 $username_password_encode $username_password; # kodning af basic autentificering
set_encrypt_session $auth_encrypt $username_password_encode; # kryptering af basic autentificering
set_encode_base64 $auth_encode $auth_encrypt; # kodning af krypteret basic autentificering
add_header Set-Cookie "Auth=$auth_encode; Max-Age=2592000"; # placere krypteret basic autentificering i autorisationscookie i 30 dage (30 * 24 * 60 * 60 = 2592000)
set $arg_request_uri_or_slash $arg_request_uri; # kopiere forespørgsel fra argument
set_if_empty $arg_request_uri_or_slash "/"; # hvis argument ikke er angivet, så begynd
set_unescape_uri $request_uri_unescape $arg_request_uri_or_slash; # afkode forespørgsel
return 303 $request_uri_unescape; # omdirigere til gemt forespørgsel
}login.html
<html>
<body>
<form method="post" action="">
<input type="hidden" name="csrf" value="<TMPL_var csrf>" />
brugernavn: <input type="text" name="username" placeholder="Indtast brugernavn..." /><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="send" />
<input type="hidden" name="trp-form-language" value="da"/></form>
</body>
</html>Kilde: habr.com
