{"id":9905,"date":"2021-07-31T01:48:20","date_gmt":"2021-07-30T22:48:20","guid":{"rendered":"https:\/\/kifarunix.com\/?p=9905"},"modified":"2024-03-18T19:24:54","modified_gmt":"2024-03-18T16:24:54","slug":"configure-ldap-based-http-basic-authentication","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-ldap-based-http-basic-authentication\/","title":{"rendered":"Configure LDAP Based HTTP Basic Authentication"},"content":{"rendered":"\n
In this tutorial, you will learn how to configure LDAP based HTTP basic authentication<\/a>. HTTP supports various frameworks for controlling and restricting access to various web resources. One of these frameworks is HTTP Authentication frameworks. The Basic HTTP authentication scheme transmits credentials as user-id\/password pairs encoded using Base64. This scheme is not considered to be a secure method of user authentication unless used in conjunction with some external secure system such as TLS (Transport Layer Security, as the user-id and password are passed over the network as cleartext.<\/em><\/p>\n\n\n\n In this setup, we will create a simple HTML page and enable HTTP basic authentication.<\/p>\n\n\n\n We assume you already have Basic authentication on whatever your web server is to restrict access to some resources.<\/p>\n\n\n\n However, for the sake of the demos, let us install Apache web server and configure basic authentication for a basic web page.<\/p>\n\n\n\n Create a simple html web page;<\/p>\n\n\n\n We will configure HTTP basic authentication for this page;<\/p>\n\n\n\n Check Apache configuration syntax and reload Apache service;<\/p>\n\n\n\n If the output is, If you try to access the page, you will be prompted to authenticate;<\/p>\n\n\n\n The usual way to authenticate is via a user\/password file, as specified by the line, So for example, you can create the credentials file and add a user\/password using the The above command creates the password file and adds a user called kifarunix. Upon pressing ENTER, you are prompted to enter your password. The Base64 encoded password is added to the file. This is how the file looks like;<\/p>\n\n\n\n So with that, you can now authenticate to your web resource with username\/password.<\/p>\n\n\n\n What if instead of using the usual username\/password authentication file, you want to enable OpenLDAP based basic authentication?<\/p>\n\n\n\n Install and setup OpenLDAP server<\/p>\n\n\n\n Of course you need to have an OpenLDAP directory setup and running. You can check the link below to setup OpenLDAP.<\/p>\n\n\n\n Install and Setup OpenLDAP on Rocky Linux 8<\/a><\/p>\n\n\n\n Install and Enable Required HTTP modules<\/p>\n\n\n\n With Apache web server, there are three types of modules that are required for authentication and authorization process<\/a>. <\/p>\n\n\n\n To configure LDAP based HTTP authentication, you need to enable Install LDAP module on RHEL based distros;<\/p>\n\n\n\n When, installed, it is enabled automatcally. <\/p>\n\n\n\n This module can be enabled on an Apache server on Debian based systes like Ubuntu by running the command below;<\/p>\n\n\n\n To verify that the module is enabled, run either of the commands below depending on your system distro.<\/p>\n\n\n\n Both commands output should be similar to;<\/p>\n\n\n\n In my OpenLDAP server, i have a group of web developers called webdev<\/strong>. Querying groups on my OpenLDAP server just to show you about this.<\/p>\n\n\n\n Sample group;<\/p>\n\n\n\n From the above, a user whose uid is devadmin<\/strong> is a member of the webdev<\/strong> group.<\/p>\n\n\n\n You can learn more on setting OpenLDAP member groups by following the link below;<\/p>\n\n\n\n How to Create OpenLDAP Member Groups<\/a><\/p>\n\n\n\n So, we need to update the web server basic authentication with this information.<\/p>\n\n\n\n In the above user\/password file based configuration;<\/p>\n\n\n\n We will replace it such that it looks like;<\/p>\n\n\n\n If your LDAP server is configured with TLS\/SSL, then use this configuration instead;<\/p>\n\n\n\n In my setup, my LDAP server setup with SSL\/TLS, hence, download the certificate with the command below;<\/p>\n\n\n\n Copy the certificate between See more Examples<\/a>.<\/p>\n\n\n\n If you are using CentOS\/RHEL based distro, you need to update SELinux, if not already done, to allow httpd to connect to network, ldap, authlogin nsswitch ldap, and enable nis, respectively.<\/p>\n\n\n\n Check Apache syntax for errors and restart it;<\/p>\n\n\n\n CentOS<\/p>\n\n\n\n Ubuntu\/Debian<\/p>\n\n\n\n Restart Apache<\/p>\n\n\n\n CentOS<\/p>\n\n\n\n Ubuntu\/Debian<\/p>\n\n\n\n You can now access your web page. When prompted for credentials, provide LDAP credentials.<\/p>\n\n\n\n Upon successful login, you land on your page, which in my setup looks like below;<\/p>\n\n\n\n Also, the access logs shows this activity;<\/p>\n\n\n\n And there you go. You have successfully integrated web HTTP basic authentication with OpenLDAP.<\/p>\n\n\n\n Configure Squid Proxy OpenLDAP Authentication on pfSense<\/a><\/p>\n\n\n\n Configure SSSD for OpenLDAP Authentication on CentOS 8<\/a><\/p>\n\n\n\nConfiguring LDAP Based HTTP Basic Authentication<\/h2>\n\n\n\n
dnf install httpd<\/code><\/pre>\n\n\n\n
cat > \/var\/www\/html\/index.html << 'EOL'\n<!DOCTYPE html>\n<html>\n<body>\n<h1><center>Enabling OpenLDAP Based HTTP<\/center><\/h1>\n<h1><center>Basic Authentication<\/center><\/h1>\n<\/body>\n<\/html>\nEOL<\/code><\/pre>\n\n\n\n
cat > \/etc\/httpd\/conf.d\/basic-auth.conf << 'EOL'\n<Directory \/var\/www\/html>\n AuthType Basic\n AuthName \"HTTP Basic Authentication\"\n AuthUserFile \/etc\/httpd\/conf\/.htpasswd\n require valid-user\n<\/Directory>\nEOL<\/code><\/pre>\n\n\n\n
httpd -t<\/code><\/pre>\n\n\n\n
Syntax OK<\/strong><\/code>, then restart Apache;<\/p>\n\n\n\n
systemctl restart httpd <\/code><\/pre>\n\n\n\n
<\/figure><\/a><\/div>\n\n\n\n
AuthUserFile<\/code><\/strong>, in the configuration above.<\/p>\n\n\n\n
htpasswd<\/strong><\/code> utility. htpasswd utility is provided by the httpd-tools package;<\/p>\n\n\n\n
htpasswd -c \/etc\/httpd\/conf\/.htpasswd kifarunix<\/code><\/pre>\n\n\n\n
cat \/etc\/httpd\/conf\/.htpasswd<\/code><\/pre>\n\n\n\n
kifarunix:$apr1$8HNgjKFG$MQ6UgholhZtTaJeiXH\/dg1<\/code><\/pre>\n\n\n\n
Integrate HTTP Basic Authentication with OpenLDAP<\/h3>\n\n\n\n
mod_authnz_ldap<\/strong><\/code> module, which can authenticate users through an ldap directory. The module is provided by the mod_ldap package on CentOS\/RHEL based systems and ships with Apache package on Debian based systems.<\/p>\n\n\n\n
dnf install mod_ldap<\/code><\/pre>\n\n\n\n
a2enmod authnz_ldap<\/code><\/pre>\n\n\n\n
httpd -M | grep ldap<\/code><\/pre>\n\n\n\n
apachectl -M | grep ldap<\/code><\/pre>\n\n\n\n
authnz_ldap_module (shared)\n ldap_module (shared)<\/code><\/pre>\n\n\n\n
ldapsearch -H ldapi:\/\/\/ -Y EXTERNAL -LLL -Q -b \"dc=ldapmaster,dc=kifarunix-demo,dc=com\" uid=* memberOf<\/code><\/pre>\n\n\n\n
dn: uid=devadmin,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\nmemberOf: cn=webdev,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com<\/strong><\/code><\/pre>\n\n\n\n
<Directory \/var\/www\/html>\n AuthType Basic\n AuthName \"HTTP Basic Authentication\"\n AuthUserFile \/etc\/httpd\/conf\/.htpasswd\n require valid-user\n<\/Directory><\/code><\/pre>\n\n\n\n
\n<Directory \/var\/www\/html>\n AuthType Basic\n AuthName \"LDAP Based HTTP Basic Authentication\"\n AuthBasicProvider ldap\n AuthLDAPURL ldap:\/\/ldap.kifarunix-demo.com\/dc=ldapmaster,dc=kifarunix-demo,dc=com?uid\n AuthLDAPBindDN cn=readonly,ou=system,dc=ldapmaster,dc=kifarunix-demo,dc=com\n AuthLDAPBindPassword bindDNpass<\/strong>\n Require ldap-group cn=webdev,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com\n<\/Directory>\n<\/code><\/pre>\n\n\n\n
\n
AuthLDAPURL<\/code><\/strong>: Specifies the LDAP server, the base DN, the attribute to use in the search, as well as the extra search filter to use.<\/li>\n\n\n\n
\n
'cn=read only,ou=sys tem,dc=ldapmaster,dc=kifarunix-demo,dc=com'<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n
Require<\/strong><\/code>: Specifies a resource that a user is allowed to access. mod_authnz_ldap extends the authorization types with
ldap-user<\/code>,
ldap-dn<\/code>,
ldap-group<\/code>,
ldap-attribute<\/code> and
ldap-filter<\/code>.<\/li>\n<\/ul>\n\n\n\n
\nLDAPTrustedGlobalCert CA_BASE64 \/etc\/ssl\/certs\/ldap.pem<\/strong>\n<Directory \/var\/www\/html>\n AuthType Basic\n AuthName \"LDAP Based HTTP Basic Authentication\"\n AuthBasicProvider ldap\n AuthLDAPURL ldap:\/\/ldap.kifarunix-demo.com\/dc=ldapmaster,dc=kifarunix-demo,dc=com?uid\n AuthLDAPBindDN cn=readonly,ou=system,dc=ldapmaster,dc=kifarunix-demo,dc=com\n AuthLDAPBindPassword bindDNpass\n Require ldap-group cn=webdev,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com\n<\/Directory>\n<\/code><\/pre>\n\n\n\n
openssl s_client -connect ldap.kifarunix-demo.com:389 -starttls ldap -showcerts < \/dev\/null | openssl x509 -text | sed -ne '\/-BEGIN CERTIFICATE-\/,\/-END CERTIFICATE-\/p'<\/code><\/pre>\n\n\n\n
-----BEGIN CERTIFICATE-----<\/code><\/strong> and
-----END CERTIFICATE-----<\/code><\/strong>, and put it in a file specified by the line LDAPTrustedGlobalCert above, \/etc\/ssl\/certs\/ldap.pem<\/strong>.<\/p>\n\n\n\n
setsebool -P httpd_can_network_connect 1<\/code><\/pre>\n\n\n\n
setsebool -P httpd_can_network_connect 1<\/code><\/pre>\n\n\n\n
setsebool -P httpd_can_connect_ldap 1<\/code><\/pre>\n\n\n\n
setsebool -P authlogin_nsswitch_use_ldap 1<\/code><\/pre>\n\n\n\n
setsebool -P nis_enabled 1<\/code><\/pre>\n\n\n\n
httpd -t<\/code><\/pre>\n\n\n\n
apachectl -t<\/code><\/pre>\n\n\n\n
systemctl restart httpd<\/code><\/pre>\n\n\n\n
systemctl restart apache2<\/code><\/pre>\n\n\n\n
Authenticating with LDAP credentials<\/h3>\n\n\n\n
<\/figure>\n\n\n\n
<\/figure>\n\n\n\n
192.168.60.3 - devadmin [31\/Jul\/2021:01:18:54 +0300] \"GET \/ HTTP\/1.1\" 200 150 \"-\" \"Mozilla\/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko\/20100101 Firefox\/89.0\"\n192.168.60.3 - devadmin [31\/Jul\/2021:01:18:55 +0300] \"GET \/favicon.ico HTTP\/1.1\" 404 196 \"http:\/\/192.168.60.19\/\" \"Mozilla\/5.0 (X11; Ubuntu; Linux x86_64; rv:89.0) Gecko\/20100101 Firefox\/89.0\"<\/code><\/pre>\n\n\n\n
Other Tutorials<\/h3>\n\n\n\n