Nginx-oppskrifter: Grunnleggende autentisering med Captcha

For authorization with CAPTCHA, we will need it nginx and its plugins encrypted-session, form-input, ctpp2, echo, headers-more, auth_request, auth_basic, set-misc. (I provided links to my forks because I made some changes that haven't been merged into the original repositories yet. You can also use a ready-made way.)

First, let's set

encrypted_session_key "abcdefghijklmnopqrstuvwxyz123456";

Next, just in case, we disable the authorization header

more_clear_input_headers Authorization;

Now we 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
    if ($auth_decrypt = "") { return 401 UNAUTHORIZED; } # if decryption failed, user is unauthorized
    more_set_input_headers "Authorization: Basic $auth_decrypt"; # replace authorization with basic (to use variable $remote_user)
    auth_basic_user_file /data/nginx/.htaccess; # set basic authorization file
    auth_basic Auth; # enable basic authorization
    echo -n OK; # user is authorized
}

For authorized users, show content from their folder

location / {
    alias html/$remote_user/;
}

And in the absence of authorization, show the authorization form with CAPTCHA

error_page 401 = @error401;
location @error401 {
    set_escape_uri $request_uri_escape $request_uri; # encodes the request
    return 303 /login?request_uri=$request_uri_escape; # redirects to the authorization form with captcha, preserving the request
}
location /=/login {
    default_type "text/html; charset=utf-8"; # sets the type
    if ($request_method = GET) { # just shows the authorization form with captcha
        template login.html.ct2; # sets the template
        ctpp2 on; # enables the templater
        set_secure_random_alphanum $csrf_random 32; # sets a random csrf
        encrypted_session_expires 300; # sets csrf lifetime to 5 minutes (5 * 60 = 300)
        set_encrypt_session $csrf_encrypt $csrf_random; # encrypts the random csrf
        set_encode_base64 $csrf_encode $csrf_encrypt; # encodes the encrypted csrf
        add_header Set-Cookie "CSRF=$csrf_encode; Max-Age=300"; # places the encrypted csrf in a cookie for 5 minutes (5 * 60 = 300)
        return 200 "{"csrf":"$csrf_random"}"; # returns json for the templater
    } # else - process the authorization form with captcha
    set_form_input $csrf_form csrf; # gets csrf from the form
    set_unescape_uri $csrf_unescape $csrf_form; # unescapes csrf from the form
    set_decode_base64 $csrf_decode $cookie_csrf; # decodes csrf from the cookie
    set_decrypt_session $csrf_decrypt $csrf_decode; # decrypts csrf from the cookie
    if ($csrf_decrypt != $csrf_unescape) { return 303 $request_uri; } # if csrf from the form does not match csrf from the cookie, redirect to show the form again
    set_form_input $captcha_form captcha; # gets the captcha from the form
    set_unescape_uri $captcha_unescape $captcha_form; # unescapes the captcha from the form
    set_md5 $captcha_md5 "secret${captcha_unescape}${csrf_decrypt}"; # calculates md5
    if ($captcha_md5 != $cookie_captcha) { return 303 $request_uri; } # if md5 does not match the captcha from the cookie, redirect to show the form again
    set_form_input $username_form username; # gets the username from the form
    set_form_input $password_form password; # gets the password from the form
    set_unescape_uri $username_unescape $username_form; # unescapes the username from the form
    set_unescape_uri $password_unescape $password_form; # unescapes the password from the form
    encrypted_session_expires 2592000; # sets session lifetime to 30 days (30 * 24 * 60 * 60 = 2592000)
    set $username_password "$username_unescape:$password_unescape"; # sets basic authorization
    set_encode_base64 $username_password_encode $username_password; # encodes basic authorization
    set_encrypt_session $auth_encrypt $username_password_encode; # encrypts basic authorization
    set_encode_base64 $auth_encode $auth_encrypt; # encodes the encrypted basic authorization
    add_header Set-Cookie "Auth=$auth_encode; Max-Age=2592000"; # places the encrypted basic authorization in an authorization cookie for 30 days (30 * 24 * 60 * 60 = 2592000)
    set $arg_request_uri_or_slash $arg_request_uri; # copies the request from the argument
    set_if_empty $arg_request_uri_or_slash "/"; # if argument is not specified, then start
    set_unescape_uri $request_uri_unescape $arg_request_uri_or_slash; # unescapes the request
    return 303 $request_uri_unescape; # redirects to the saved request
}

login.html

<html>
    <body>
        <form method="post" action="">
            <input type="hidden" name="csrf" value="<TMPL_var csrf>" />
            brukernavn: <input type="text" name="username" placeholder="Skriv inn brukernavn..." /><br />
            passord: <input type="password" name="password" /><br />
            kode: <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="no"/></form>
    </body>
</html>

Kilde: habr.com

Kjøp pålitelig hosting for nettsteder med DDoS-beskyttelse, VPS VDS-servere 🔥 Kjøp pålitelig hosting for nettsteder med DDoS-beskyttelse, VPS VDS-servere | ProHoster