Apache Basic Authentication: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 24: Line 24:
==Config==
==Config==
<source lang="apache">
<source lang="apache">
# mod_dbd configuration
<VirtualHost *:80>
DBDriver mysql
    ServerName pi4.dev.shahed.biz
DBDParams "host=127.0.0.1,port=3306,user=apache,pass=password,dbname=apache"
    ServerAdmin admin@dev.shahed.biz
    DocumentRoot /var/www/html


DBDMin  4
    DBDriver mysql
DBDKeep 8
    DBDParams "host=127.0.0.1,port=3306,user=apache,pass=apache,dbname=apache"
DBDMax  20
DBDExptime 300


<Directory "/var/chorke/www/dev.chorke.org/soft/">
    DBDMin  4
  AuthType Basic
    DBDKeep 8
  AuthName Team
    DBDMax  20
  AuthBasicProvider dbd
    DBDExptime 300


  # mod_authn_dbd SQL
    Alias /soft "/var/www/soft/"
  AuthDBDUserPWQuery \
     <Directory "/var/www/soft">
     "SELECT u.user_pass FROM m00te00x00 u WHERE u.user_name = %s AND u.is_signin = 1 AND u.is_active = 1 AND u.is_locked = 0 AND u.user_expired > trunc(sysdate) AND u.pass_expired > trunc(sysdate)"
        AuthType Basic
        AuthName Academia
        Require valid-user
        AuthBasicProvider dbd
        Options Indexes MultiViews FollowSymLinks


  # mod_authz_core configuration
        AuthDBDUserPWQuery \
  Require dbd-group team
            "SELECT u.password FROM m00te00x00 u WHERE u.username = %s AND u.deleted_at IS NULL and u.deleted_by IS NULL AND IFNULL(u.is_signed_in, 0) = 0 AND IFNULL(u.is_activated, 0) = 1 AND IFNULL(u.is_unlocked, 0) = 1 AND IFNULL(u.user_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE() AND IFNULL(u.pass_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE()"


  # mod_authz_dbd configuration
        Require dbd-group System
  AuthzDBDQuery \
        Require dbd-group Admin
    "SELECT g.group_name FROM m00te00x00 u LEFT JOIN m00tj00x00 a ON u.user_code = a.user_code LEFT JOIN m00ts00x00 g ON a.group_code = g.group_code WHERE u.user_name = %s AND u.is_signin = 1 AND u.is_active = 1 AND u.is_locked = 0 AND u.user_expired > trunc(sysdate) AND u.pass_expired > trunc(sysdate) AND a.is_active = 1 AND g.is_active = 1"


  # when a user fails to be authenticated or authorized,
        AuthzDBDQuery \
  # invite them to login; this page should provide a link
            "SELECT g.name FROM m00te00x00 u LEFT JOIN m00tj00x00 j ON u.id = j.user_id LEFT JOIN m00ts00x00 g ON j.group_id = g.id WHERE u.username = %s AND j.deleted_at IS NULL and j.deleted_by IS NULL AND g.deleted_at IS NULL and g.deleted_by IS NULL AND IFNULL(g.is_activated, 0) = 1 AND IFNULL(g.expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE()"
  # to /team-private/login.html
  ErrorDocument 401 "/login-info.html"


  <Files "login.html">
        ErrorDocument 401 "/login-info.html"
    AuthDBDUserPWQuery \
        "SELECT u.user_pass FROM m00te00x00 u WHERE u.user_name = %s AND u.is_signin = 1 AND u.is_active = 1 AND u.is_locked = 0 AND u.user_expired > trunc(sysdate) AND u.pass_expired > trunc(sysdate)"


    Require dbd-login
        <Files "login.html">
    AuthzDBDQuery \
        AuthDBDUserPWQuery \
        "UPDATE m00te00x00 SET is_signin = 1 WHERE user_name = %s"
            "SELECT u.password FROM m00te00x00 u WHERE u.username = %s AND u.deleted_at IS NULL and u.deleted_by IS NULL AND IFNULL(u.is_signed_in, 0) = 0 AND IFNULL(u.is_activated, 0) = 1 AND IFNULL(u.is_unlocked, 0) = 1 AND IFNULL(u.user_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE() AND IFNULL(u.pass_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE()"
    AuthzDBDLoginToReferer On
  </Files>


  <Files "logout.html">
        Require dbd-login
    Require dbd-logout
        AuthzDBDQuery \
    AuthzDBDQuery \
            "UPDATE m00te00x00 SET is_signed_in = 1 WHERE username = %s"
        "UPDATE m00te00x00 SET is_signin = 0 WHERE user_name = %s"
        AuthzDBDLoginToReferer On
  </Files>
        </Files>
</Directory>
 
        <Files "logout.html">
        Require dbd-logout
        AuthzDBDQuery \
            "UPDATE m00te00x00 SET is_signed_in = 0 WHERE username = %s"
        </Files>
    </Directory>
 
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
</source>
</source>



Revision as of 04:28, 17 January 2021

Install

apt -y install mariadb-server mariadb-client
apt install libaprutil1-dbd-mysql
a2enmod dbd
a2enmod authn_dbd
a2enmod authz_dbd
a2enmod authn_socache
systemctl restart mysql
update-rc.d mysql enable
systemctl restart apache2
update-rc.d apache2 enable

Config

<VirtualHost *:80>
    ServerName pi4.dev.shahed.biz
    ServerAdmin [email protected]
    DocumentRoot /var/www/html

    DBDriver mysql
    DBDParams "host=127.0.0.1,port=3306,user=apache,pass=apache,dbname=apache"

    DBDMin  4
    DBDKeep 8
    DBDMax  20
    DBDExptime 300

    Alias /soft "/var/www/soft/"
    <Directory "/var/www/soft">
        AuthType Basic
        AuthName Academia
        Require valid-user
        AuthBasicProvider dbd
        Options Indexes MultiViews FollowSymLinks

        AuthDBDUserPWQuery \
            "SELECT u.password FROM m00te00x00 u WHERE u.username = %s AND u.deleted_at IS NULL and u.deleted_by IS NULL AND IFNULL(u.is_signed_in, 0) = 0 AND IFNULL(u.is_activated, 0) = 1 AND IFNULL(u.is_unlocked, 0) = 1 AND IFNULL(u.user_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE() AND IFNULL(u.pass_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE()"

        Require dbd-group System
        Require dbd-group Admin

        AuthzDBDQuery \
            "SELECT g.name FROM m00te00x00 u LEFT JOIN m00tj00x00 j ON u.id = j.user_id LEFT JOIN m00ts00x00 g ON j.group_id = g.id WHERE u.username = %s AND j.deleted_at IS NULL and j.deleted_by IS NULL AND g.deleted_at IS NULL and g.deleted_by IS NULL AND IFNULL(g.is_activated, 0) = 1 AND IFNULL(g.expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE()"

        ErrorDocument 401 "/login-info.html"

        <Files "login.html">
        AuthDBDUserPWQuery \
            "SELECT u.password FROM m00te00x00 u WHERE u.username = %s AND u.deleted_at IS NULL and u.deleted_by IS NULL AND IFNULL(u.is_signed_in, 0) = 0 AND IFNULL(u.is_activated, 0) = 1 AND IFNULL(u.is_unlocked, 0) = 1 AND IFNULL(u.user_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE() AND IFNULL(u.pass_expired_at, SYSDATE() + INTERVAL 1 DAY) > SYSDATE()"

        Require dbd-login
        AuthzDBDQuery \
            "UPDATE m00te00x00 SET is_signed_in = 1 WHERE username = %s"
        AuthzDBDLoginToReferer On
        </Files>

        <Files "logout.html">
        Require dbd-logout
        AuthzDBDQuery \
            "UPDATE m00te00x00 SET is_signed_in = 0 WHERE username = %s"
        </Files>
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Query

Oracle

SELECT
    u.user_pass AS "password"
FROM
    m00te00x00 u
WHERE
    u.user_name     = '&user_name'
    AND u.is_signin = 1
    AND u.is_active = 1
    AND u.is_locked = 0
    AND u.user_expired > trunc(sysdate)
    AND u.pass_expired > trunc(sysdate);

-- update sign in
UPDATE m00te00x00
SET
    is_signin = 1
WHERE
    user_name = '&user_name';
--
-- find groups by user
--
SELECT
    g.group_name AS "group"
FROM
    m00te00x00 u
    LEFT JOIN m00tj00x00 a ON u.user_code  = a.user_code
    LEFT JOIN m00ts00x00 g ON a.group_code = g.group_code
WHERE
    u.user_name     = '&user_name'
    AND u.is_signin = 1
    AND u.is_active = 1
    AND u.is_locked = 0
    AND u.user_expired > trunc(sysdate)
    AND u.pass_expired > trunc(sysdate)
    AND a.is_active = 1
    AND g.is_active = 1;

MySQL

SELECT
    u.user_pass  AS "password"
FROM
    m00te00x00 u
WHERE
    u.user_name     = 'user_name'
    AND u.is_signin = 1
    AND u.is_active = 1
    AND u.is_locked = 0
    AND u.user_expired > DATE(SYSDATE())
    AND u.pass_expired > DATE(SYSDATE());

-- update sign in
UPDATE m00te00x00
SET
    is_signin = 1
WHERE
    user_name = 'user_name';
--
-- find groups by user
--
SELECT
    g.group_name AS "group"
FROM
    m00te00x00 u
    LEFT JOIN m00tj00x00 a ON u.user_code  = a.user_code
    LEFT JOIN m00ts00x00 g ON a.group_code = g.group_code
WHERE
    u.user_name     = 'user_name'
    AND u.is_signin = 1
    AND u.is_active = 1
    AND u.is_locked = 0
    AND u.user_expired > DATE(SYSDATE())
    AND u.pass_expired > DATE(SYSDATE())
    AND a.is_active = 1
    AND g.is_active = 1;

PgSQL

SELECT
    u.user_pass  AS "password"
FROM
    m00te00x00 u
WHERE
    u.user_name     = 'user_name'
    AND u.is_signin = 1
    AND u.is_active = 1
    AND u.is_locked = 0
    AND u.user_expired > DATE(NOW())
    AND u.pass_expired > DATE(NOW());

-- update sign in
UPDATE m00te00x00
SET
    is_signin = 1
WHERE
    user_name = 'user_name';
--
-- find groups by user
--
SELECT
    g.group_name AS "group"
FROM
    m00te00x00 u
    LEFT JOIN m00tj00x00 a ON u.user_code  = a.user_code
    LEFT JOIN m00ts00x00 g ON a.group_code = g.group_code
WHERE
    u.user_name     = 'user_name'
    AND u.is_signin = 1
    AND u.is_active = 1
    AND u.is_locked = 0
    AND u.user_expired > DATE(NOW())
    AND u.pass_expired > DATE(NOW())
    AND a.is_active = 1
    AND g.is_active = 1;

MySQL Apache User

CREATE USER 'apache'@'%' IDENTIFIED VIA mysql_native_password USING '***';
GRANT USAGE ON *.* TO 'apache'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
CREATE DATABASE IF NOT EXISTS `apache`;
GRANT ALL PRIVILEGES ON `apache`.* TO 'apache'@'%';

Knowledge

ls -lah /usr/lib/apache2/modules/mod_authn_socache.so
htpasswd -nbs system p@$$w0rd
htpasswd -nbs admin p@$$w0rd

References