Authenticate using LDAP - php

I have an ssh access to a LDAP server.
After accessing through ssh I can access to phpldapadmin. I have an admin DN and a password.
What needs to be done is I have to authenticate users using ldap whenever they try to login to a website.
Website is using jaggery for the server side code. But php is okay too.
I haven't worked with ldap and ssh. So if somebody can help on this I'll really appreciate it.

You have to download the adLDAP.php here:
http://adldap.sourceforge.net/download.php
Then the autentication is quiet simple:
$adldap = new adLDAP(array('base_dn'=>'DC=domainexample,DC=suffixexample', 'account_suffix'=>'#domainexample.suffix'));
$authUser = $adldap->authenticate('user', 'password');
if ($authUser == true) {
echo "User authenticated successfully<br>";
}
else {
echo "User authentication unsuccessful<br>";
}
You have only to be careful with the DC attribution: it depends on the LDAP network.

PHP has an extension to work with LDAP. You don't need to install third-party libraries.
Work with ldap and session is what you need.
http://us2.php.net/manual/pt_BR/book.ldap.php

Related

Laravel with LDAP

I'm trying to connect to OpenLDAP using Laravel. All I did was follow the https://ldaprecord.com/docs/laravel/v1/ tutorial. But this tutorial only connect to trial LDAP. I need to connect to my localhost openLDAP.
I'm using these .env configs:
LDAP_LOGGING=true
LDAP_CONNECTION=default
LDAP_HOST=127.0.0.1
LDAP_USERNAME=null
LDAP_PASSWORD=null
LDAP_PORT=10389
LDAP_BASE_DN="dc=example,dc=com"
LDAP_TIMEOUT=5
LDAP_SSL=false
LDAP_TLS=false
Ldap connection test comes successful. But when I try to login using UI, I get that credentials are wrong. i only have created one user so cant be wrong.
From the documentation: "To connect to your LDAP server, a username and password is required to be able to query and run operations on your server(s)."
So you need to enter a (proxy) username and password at the LDAP_USERNAME and LDAP_PASSWORD prompts in order for the application to be able to interact with the LDAP server.
My guess is that you need to enter the user DN and not the short username: uid=username,ou=people,dc=example,dc=com or similar

phpseclib SFTP failed user authentication

i'm attempting to test an SFTP connection thru my web app running on laravel 5.7 framework using phpseclib . This is the authentication code, using only password authentication. The SFTP server is also using IP whitelisting as additional security.
$sftp = new SFTP(env('SFTP_HOST'));
if (!$sftp->login(env('SFTP_USER'), env('SFTP_PASSWORD'))) {
echo $sftp->getLog();
}
The -vvv logs almost make me wonder if the server is using multi factor authentication. Like you have to provide both an RSA key and a password. If so you should be able to achieve that thusly:
$key = new RSA;
$key->loadKey(file_get_contents('/home/forge/.ssh/id_rsa'));
$sftp = new SFTP(env('SFTP_HOST'));
if (!$sftp->login(env('SFTP_USER'), $key, env('SFTP_PASSWORD'))) {
echo $sftp->getLog();
}
The -vvv logs also indicate that it's skipping straight to keyboard-interactive auth - that it's not even trying password auth. You can force phpseclib to do keyboard-interactive by doing $sftp->login(env('SFTP_USER'), ['Password:' => env('SFTP_PASSWORD')]) instead of what you are doing.
Maybe some combination of these two tips will help you out.

Authenticate users against Azure Active Directory from PHP web application

I have a website in PHP and want to authentificate users against Azure Active Directory. With ldap_connect and bind I have no problems to do this to a local AD-Server in our company.
function checkADAccount($username, $password)
{
$adServer = "myADServer";
$ldaprdn = 'myDomain' . "\\" . $username;
$ldap = ldap_connect($adServer);
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
$bind = #ldap_bind($ldap, $ldaprdn, $password);
if ($bind) {
#ldap_close($ldap); // Verbindung schließen
return true;
} else {
return false;
}
}
But my application is hosted at Amazon Web Services (AWS) and is not in the same domain, so I can't use this way.
With ldap_connect and bind this functions to a local AD-Server in our company. But my application is hosted at Amazon Web Services (AWS) and is not in the same domain, so I can't use this way.
I tested graphapi with the ClientId and key for my company from Azure. So I can read the users-data, but I don't see any possibility to check a user/password combination. I only want to check, if there exists a user with this password, I don't need to read it from there.
So I tried to modify the LDAP-solution from above by changing the parameter of the adServer and of the user
$ldaprdn = 'sAMAccountname=' . $username . ',cn=users' . ',dc=myDomain,dc=com';
But I allwaws get: "Warning: ldap_bind(): Unable to bind to server: Can't contact LDAP server ". I tried for this multiple versions for the adServer but any delievers a binding.
Do you have any idea where is the error. I suggest with ldap_connect is the wron way, espacially I don't know which server-adress is the right one and how I had to tell the ClientId and key.
Currently, Azure AD doesn't support LDAP connection. It provides OAuth2 authentication and authorization. You can refer to Authentication Scenarios for Azure AD for detailed Azure AD scenarios.
For your requirement to authenticate users in your domain, you can leverage this code sample to implement authentication via Azure AD.
Otherwise, you can follow Authorization Code Grant Flow build your custom code to authenticate your users.
Any further concern, please feel free to let me know.

Single-Sign-On from Windows Workstation to PHP script running on Linux server that is not part of the same domain

Edit: After researching further this morning I have tried to make my post more specific:
I have a php script running on a web server in my company's data centre. A client of ours needs to access that script from their user's windows workstations using single-sign-on. The php server does not have access to the domain that the windows users are logged on to.
Is there a way for the users to open Internet Explorer (or similar), go to the website I am developing and the website 'know' their username? The site will also need to deny access to anyone that is not part of the client's domain.
So to be clear, the main issue I am experiencing is that the server the php script runs on IS NOT part of the domain that the users accessing it are on.
Side note: if true SSO is not possible and the user has to retype their username and password again, that is ok, but I guess this would mean the authentication is done by the php script, which would be impossible because it cannot access the clients AD server.
Thanks :)
Original post:
Just after some links or general information to begin with. I am building a php based website on example1.com but to access it, users need to authenticate against active directory on example2.local. The domain controller for example2.local is not accessible on the internet. Users will be logged on to Windows workstations in example2.local domain and have internet accessibility though.
Is there any way this can work? I have been told to look into 'single sign on', but everything I've found so far suggests the ad domain should be example2.com (not .local). A colleague here assures me that domain controllers are not normally accessible to the wider internet.. so how is this normally done?
Also the client this is for has emphasized the need for security.
Thanks
Edit: After reading some more about SSO I'm possibly more confused, but I would like to add that all my php application needs to know is that 1) the user is authenticated in their domain and 2) their username. What is the simplest way to achieve this? Thanks!
http://www.php.net/manual/en/book.ldap.php
I actually just implemented a basic LDAP authentication script. This was my first draft of the script before I changed and messed with it to fit with my setup. This basically connects to the server, if not the scrpt is killed. Then tries to authenticate the given username and password, if not, it fails. Quick and simple.
$ldapHost = "example2.com";
$ldapPort = 666;
$ldapBaseDn = "ldap base dn here";
$username = "username";
$password = "password";
$ldapUserDn = "uid=$username,".$ldapBaseDn;
echo "LDAP query test\n";
echo "Connecting ...\n";
$ldapConn = ldap_connect($ldapHost, $ldapPort)
or die("Unable to connect to LDAP server: $ldapHost\n");
echo "Authenticating user ...\n";
$isValid = ldap_bind($ldapConn, $ldapUserDn, $password)
or die("Unable to authenticate user: $username\n");
echo "User $username is successfully authenticated!\n";
echo "Closing connection ...\n";
ldap_close($ldapConn);
exit();
?>

Authenticate against ldap using PHP, active directory, while using IE/Firefox

This code below checks for the user's credentials against ldap
<?php
$ldaphost = "ldap.domain.com";
$ldapport = 389;
$ds = ldap_connect($ldaphost, $ldapport)
or die("Could not connect to $ldaphost");
if ($ds)
{
$username = "johndoe#domain.com";
$upasswd = "pass";
$ldapbind = ldap_bind($ds, $username, $upasswd);
if ($ldapbind)
{print "Congratulations! $username is authenticated.";}
else
{print "Access Denied!";}
}
?>
My users use Firefox and IE, and I know that can pass their ActiveDirectory credentials seamlessly.
I just want to check the AD group to see if that username is found in there, if so, display the page, otherwise prompt to enter in credentials.
Since our users are already logged into the domain controller, I want to grab their username, check to see if it was found in the specific group, then let them in, otherwise prompt user to input credentials. How is this possible?
You actually do not need to communicate with the Active Directory server from your PP code to achieve what you want given the fact that you use IIS as your web server.
The key word here is Integrated Windows Authentication - that's the wording djn looked for. If this option is turned on (and anonymous access is denied) IIS will check the supplied credentials against the Active Directory and the NTFS filesystem privileges of the requested resources. You can therefore control access to your files using simple NTFS access control mechanisms.
If your users use IE they even don't have to type in their credentials as this is done automatically via so called SPNEGO (Simple and Protected GSSAPI Negotiation Mechanism) and its underlying mechanisms Kerberos or NTLMSSP depending on what your client and server is capable of processing.
As far as I know Firefox is able to hand over the Windows logon credentials to your server automatically too. You ony have to adjust a configuration option to turn on that feature - don't know if this information is still valid with Firefox 3.5.x.
If you're running Apache on a *nix-system you'll have to resort to some server-side-module to handle a Integrated Windows Authentication-like system. Possible options are (don't know whether they are actually still maintained or stable):
mod_auth_ntlm_winbind
mod_auth_kerb
mod_ntlm
For Apache on Windows there are:
mod_ntlm (outdated; not the same as mod_ntlm above)
mod_auth_sspi (successor of mod_ntlm)
Please be aware that most of these modules seem to be very old.
Working just now on a similar setup: I skipped all of that LDAP stuff having the web server authenticating the client with AD before letting him in (sorry, I can't remember what's this called in the M$ alternate universe).
If the client reaches the PHP script he's in AD and I have his username both in $_SERVER["AUTH_USER"] and in $_SERVER["LOGON_USER"], otherwise he never gets to the script.

Categories