{"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<p>In this tutorial, you will learn how to configure LDAP based <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTTP\/Authentication\" target=\"_blank\" rel=\"noreferrer noopener\">HTTP basic authentication<\/a>. <em>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<h2 class=\"wp-block-heading\">Configuring LDAP Based HTTP Basic Authentication<\/h2>\n\n\n\n<p>In this setup, we will create a simple HTML page and enable HTTP basic authentication.<\/p>\n\n\n\n<p>We assume you already have Basic authentication on whatever your web server is to restrict access to some resources.<\/p>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>dnf install httpd<\/code><\/pre>\n\n\n\n<p>Create a simple html web page;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; \/var\/www\/html\/index.html &lt;&lt; 'EOL'\n&lt;!DOCTYPE html&gt;\n&lt;html&gt;\n&lt;body&gt;\n&lt;h1&gt;&lt;center&gt;Enabling OpenLDAP Based HTTP&lt;\/center&gt;&lt;\/h1&gt;\n&lt;h1&gt;&lt;center&gt;Basic Authentication&lt;\/center&gt;&lt;\/h1&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\nEOL<\/code><\/pre>\n\n\n\n<p>We will configure HTTP basic authentication for this page;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>cat &gt; \/etc\/httpd\/conf.d\/basic-auth.conf &lt;&lt; 'EOL'\n&lt;Directory \/var\/www\/html&gt;\n    AuthType Basic\n    AuthName \"HTTP Basic Authentication\"\n    AuthUserFile \/etc\/httpd\/conf\/.htpasswd\n    require valid-user\n&lt;\/Directory&gt;\nEOL<\/code><\/pre>\n\n\n\n<p>Check Apache configuration syntax and reload Apache service;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>httpd -t<\/code><\/pre>\n\n\n\n<p>If the output is, <code><strong>Syntax OK<\/strong><\/code>, then restart Apache;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart httpd <\/code><\/pre>\n\n\n\n<p>If you try to access the page, you will be prompted to authenticate;<\/p>\n\n\n\n<div><a href=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/basic-auth.png\" class=\"td-modal-image\"><figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"909\" height=\"440\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/basic-auth.png\" alt=\"\" class=\"wp-image-9924\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/basic-auth.png?v=1627684768 909w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/basic-auth-768x372.png?v=1627684768 768w\" sizes=\"(max-width: 909px) 100vw, 909px\" \/><\/figure><\/a><\/div>\n\n\n\n<p>The usual way to authenticate is via a user\/password file, as specified by the line, <strong><code>AuthUserFile<\/code><\/strong>, in the configuration above.<\/p>\n\n\n\n<p>So for example, you can create the credentials file and add a user\/password using the <code><strong>htpasswd<\/strong><\/code> utility. htpasswd utility is provided by the httpd-tools package;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>htpasswd -c \/etc\/httpd\/conf\/.htpasswd kifarunix<\/code><\/pre>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>cat \/etc\/httpd\/conf\/.htpasswd<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>kifarunix:$apr1$8HNgjKFG$MQ6UgholhZtTaJeiXH\/dg1<\/code><\/pre>\n\n\n\n<p>So with that, you can now authenticate to your web resource with username\/password.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Integrate HTTP Basic Authentication with OpenLDAP<\/h3>\n\n\n\n<p>What if instead of using the usual username\/password authentication file, you want to enable OpenLDAP based basic authentication?<\/p>\n\n\n\n<p>Install and setup OpenLDAP server<\/p>\n\n\n\n<p>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<p><a href=\"https:\/\/kifarunix.com\/install-and-setup-openldap-on-rocky-linux-8\/\">Install and Setup OpenLDAP on Rocky Linux 8<\/a><\/p>\n\n\n\n<p>Install and Enable Required HTTP modules<\/p>\n\n\n\n<p>With Apache web server, there are <a href=\"https:\/\/httpd.apache.org\/docs\/trunk\/howto\/auth.html#related\" target=\"_blank\" rel=\"noreferrer noopener\">three types of modules that are required for authentication and authorization process<\/a>. <\/p>\n\n\n\n<p>To configure LDAP based HTTP authentication, you need to enable <code><strong>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<p>Install LDAP module on RHEL based distros;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnf install mod_ldap<\/code><\/pre>\n\n\n\n<p>When, installed, it is enabled automatcally. <\/p>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>a2enmod authnz_ldap<\/code><\/pre>\n\n\n\n<p>To verify that the module is enabled, run either of the commands below depending on your system distro.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>httpd -M | grep ldap<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apachectl -M | grep ldap<\/code><\/pre>\n\n\n\n<p>Both commands output should be similar to;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> authnz_ldap_module (shared)\n ldap_module (shared)<\/code><\/pre>\n\n\n\n<p>In my OpenLDAP server, i have a group of web developers called <strong>webdev<\/strong>. Querying groups on my OpenLDAP server just to show you about this.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ldapsearch -H ldapi:\/\/\/ -Y EXTERNAL -LLL -Q -b \"dc=ldapmaster,dc=kifarunix-demo,dc=com\" uid=* memberOf<\/code><\/pre>\n\n\n\n<p>Sample group;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dn: uid=devadmin,ou=people,dc=ldapmaster,dc=kifarunix-demo,dc=com\n<strong>memberOf: cn=webdev,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com<\/strong><\/code><\/pre>\n\n\n\n<p>From the above, a user whose uid is <strong>devadmin<\/strong> is a member of the <strong>webdev<\/strong> group.<\/p>\n\n\n\n<p>You can learn more on setting OpenLDAP member groups by following the link below;<\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-create-openldap-member-groups\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Create OpenLDAP Member Groups<\/a><\/p>\n\n\n\n<p>So, we need to update the web server basic authentication with this information.<\/p>\n\n\n\n<p>In the above user\/password file based configuration;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;Directory \/var\/www\/html&gt;\n    AuthType Basic\n    AuthName \"HTTP Basic Authentication\"\n    AuthUserFile \/etc\/httpd\/conf\/.htpasswd\n    require valid-user\n&lt;\/Directory&gt;<\/code><\/pre>\n\n\n\n<p>We will replace it such that it looks like;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n&lt;Directory \/var\/www\/html&gt;\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 <strong>bindDNpass<\/strong>\n    Require ldap-group cn=webdev,ou=groups,dc=ldapmaster,dc=kifarunix-demo,dc=com\n&lt;\/Directory&gt;\n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>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<li><strong>AuthLDAPBindDN<\/strong>: An optional DN to bind with during the search phase.\n<ul class=\"wp-block-list\">\n<li>if your DN has spaces, enclose it in single quotes eg <code>'cn=read only,ou=sys tem,dc=ldapmaster,dc=kifarunix-demo,dc=com'<\/code>.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>AuthLDAPBindPassword<\/strong>: An optional password to bind with during the search phase<\/li>\n\n\n\n<li><code><strong>Require<\/strong><\/code>: Specifies a resource that a user is allowed to access. mod_authnz_ldap extends the authorization types with&nbsp;<code>ldap-user<\/code>,&nbsp;<code>ldap-dn<\/code>,&nbsp;<code>ldap-group<\/code>,&nbsp;<code>ldap-attribute<\/code>&nbsp;and&nbsp;<code>ldap-filter<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>If your LDAP server is configured with TLS\/SSL, then use this configuration instead;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>\n<strong>LDAPTrustedGlobalCert CA_BASE64 \/etc\/ssl\/certs\/ldap.pem<\/strong>\n&lt;Directory \/var\/www\/html&gt;\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&lt;\/Directory&gt;\n<\/code><\/pre>\n\n\n\n<p>In my setup, my LDAP server setup with SSL\/TLS, hence, download the certificate with the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>openssl s_client -connect ldap.kifarunix-demo.com:389 -starttls ldap -showcerts &lt; \/dev\/null | openssl x509 -text | sed -ne '\/-BEGIN CERTIFICATE-\/,\/-END CERTIFICATE-\/p'<\/code><\/pre>\n\n\n\n<p>Copy the certificate between <strong><code>-----BEGIN CERTIFICATE-----<\/code><\/strong> and <strong><code>-----END CERTIFICATE-----<\/code><\/strong>, and put it in a file specified by the line LDAPTrustedGlobalCert above, <strong>\/etc\/ssl\/certs\/ldap.pem<\/strong>.<\/p>\n\n\n\n<p>See more <a href=\"https:\/\/httpd.apache.org\/docs\/trunk\/mod\/mod_authnz_ldap.html#examples\" target=\"_blank\" rel=\"noreferrer noopener\">Examples<\/a>.<\/p>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>setsebool -P httpd_can_network_connect 1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>setsebool -P httpd_can_network_connect 1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>setsebool -P httpd_can_connect_ldap 1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>setsebool -P authlogin_nsswitch_use_ldap 1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>setsebool -P nis_enabled 1<\/code><\/pre>\n\n\n\n<p>Check Apache syntax for errors and restart it;<\/p>\n\n\n\n<p>CentOS<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>httpd -t<\/code><\/pre>\n\n\n\n<p>Ubuntu\/Debian<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apachectl -t<\/code><\/pre>\n\n\n\n<p>Restart Apache<\/p>\n\n\n\n<p>CentOS<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart httpd<\/code><\/pre>\n\n\n\n<p>Ubuntu\/Debian<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart apache2<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Authenticating with LDAP credentials<\/h3>\n\n\n\n<p>You can now access your web page. When prompted for credentials, provide LDAP credentials.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1016\" height=\"424\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/LDAP-basic-auth.png\" alt=\"\" class=\"wp-image-9925\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/LDAP-basic-auth.png?v=1627684805 1016w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/LDAP-basic-auth-768x321.png?v=1627684805 768w\" sizes=\"(max-width: 1016px) 100vw, 1016px\" \/><\/figure>\n\n\n\n<p>Upon successful login, you land on your page, which in my setup looks like below;<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1016\" height=\"313\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/test-page.png\" alt=\"\" class=\"wp-image-9926\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/test-page.png?v=1627684821 1016w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/07\/test-page-768x237.png?v=1627684821 768w\" sizes=\"(max-width: 1016px) 100vw, 1016px\" \/><\/figure>\n\n\n\n<p>Also, the access logs shows this activity;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>192.168.60.3 - devadmin &#91;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 &#91;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<p>And there you go. You have successfully integrated web HTTP basic authentication with OpenLDAP.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Other Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-squid-proxy-openldap-authentication-on-pfsense\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure Squid Proxy OpenLDAP Authentication on pfSense<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-sssd-for-openldap-authentication-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure SSSD for OpenLDAP Authentication on CentOS 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/setup-apache-guacamole-openldap-authentication\/\" target=\"_blank\" rel=\"noreferrer noopener\">Setup Apache Guacamole OpenLDAP Authentication<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to configure LDAP based HTTP basic authentication. HTTP supports various frameworks for controlling and restricting access to various<\/p>\n","protected":false},"author":1,"featured_media":9928,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,254,253],"tags":[202,3916,3913,3914,248,3915],"class_list":["post-9905","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-apache","category-web-servers","tag-apache","tag-basic-auth","tag-enable-basic-auth-ldap-authentication","tag-enable-basic-authentication-ldap","tag-ldap","tag-ldap-based-basic-http-auth","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/9905"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=9905"}],"version-history":[{"count":9,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/9905\/revisions"}],"predecessor-version":[{"id":21707,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/9905\/revisions\/21707"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/9928"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=9905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=9905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=9905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}