diff --git a/README.md b/README.md index b8a5c93..8826e6f 100644 --- a/README.md +++ b/README.md @@ -25,23 +25,33 @@ This module requires the following : * php-pgsql or php-mysql * httpd * postgresql or mariadb (mysql) -* postgresql-server or maridb-server +* postgresql-server or mariadb-server * git Obviously, you must have a Mattermost Server installed and be administrator on it, and a LDAP server configured. ### Pre-install -* For Centos 7, RHEL 7 and Fedora : + Install required packages : + +* For Centos 7, RHEL 7 and Fedora : ``` #For PostgreSQL -sudo yum -y --nogpgcheck install httpd php postgresql-server postgresql php-ldap php-pdo php-psql git +sudo yum -y --nogpgcheck install httpd php postgresql-server postgresql php-ldap php-pdo php-pgsql git #For MySQL sudo yum -y --nogpgcheck install httpd php mariadb-server mariadb php-ldap php-pdo php-mysql git ``` +* For Debian, ubuntu, Mint : +``` +#For PostgreSQL +sudo apt-get -y install httpd php postgresql-server postgresql php-ldap php-pdo php-pgsql git -Start and enable service for Apache and Database : +#For MySQL +sudo apt-get -y install httpd php mariadb-server mariadb php-ldap php-pdo php-mysql git +``` + +Start and enable service for Apache and Database (for all distribution using systemd): ``` #For PostgreSQL sudo systemctl start httpd @@ -63,14 +73,47 @@ Your system is ready to install and run Mattermost-LDAP module. ## Install Clone (or download and extract) this repository in your /var/www/html (or your httpd root directory) : ``` +cd ~ git clone https://github.com/crivaledaz/Mattermost-LDAP.git +cd Mattermost-LDAP +cp -r oauth/ /var/www/html/ ``` -You need to create a database for the oauth server. For this purpose, you can use the script "init_postgres.sh" or "init_mysql.sh". These scripts try to configure your database automatically, by creating a new user and a new database associated for the oauth server. Scripts also create all tables necessary for the module. If script failed, please report here, and try to configure manually your database by adapting command in scripts. Before running the script you can change the default settings by editing the .sh file and modifying configuration variables at the beginning of the file. +You need to create a database for the oauth server. For this purpose, you can use the script "init_postgres.sh" or "init_mysql.sh". These scripts try to configure your database automatically, by creating a new user and a new database associated for the oauth server. Scripts also create all tables necessary for the module. If script failed, please report here, and try to configure manually your database by adapting command in scripts. Before running the script you can change the default settings by editing the config_init.sh file and modifying configuration variables. For postgresql, you can copy and paste following lines : +``` +nano config_init.sh +./init_postgres.sh +``` This script will automatically create and add a new client in the oauth server, returning a client id and a client secret. You need to keep these two token to configure Mattermost. Please be sure the client secret remained secret. The redirect url in the script must comply with the hostname of your Mattermost server, else Mattermost could not get data from the Oauth server. -### configuration + +## Configuration + +* Init script configuration : +#### oauth_user +Oauth user in the database. This user must have right on the oauth database to store oauth tokens. By default : oauth +#### oauth_pass +Oauth user password in the database. By default, oauth_secure-pass +#### ip +Hostname or IP address of the database. By default : 127.0.0.1 +#### port +The port to connect to the database. By default : 5432 (postgres) +#### oauth_db_name +Database name for oauth server. By default : oauth_db +#### client_id +The application ID shared with mattermost. This ID should be a random token. You can use openssl to generate this token (openssl rand -hex 32). By default, this variable contain the openssl command, which use the openssl package. The token will be printed at the end of the script. +#### client_secret +The application secret shared with mattermost. This secret should be a random token. You can use openssl to generate this token (openssl rand -hex 32). By default, this variable contain the openssl command, which use the openssl package. The token will be printed at the end of the script. Secret must be different of the client ID. +#### redirect_uri +The callback address where oauth will send tokens to Mattermost. Normally it should be http://mattermost.company.com/signup/gitlab/complete +#### grant_types +The type of authentification use by Mattermost. It should be "authorization_code". +#### scope +The scope of authentification use by Mattermost. It should be "api". +#### user_id +The username of the user who create the Mattermost client in Oauth. This field has no impact, and could be used as a commentary field. By default this field is empty. + * Mattermost : Active Gitlab authentication in system console > Gitlab (or config.json on server) and fill application id and secret with the two token got during install section. For the next fields use this : ``` @@ -80,20 +123,41 @@ Token Endpoint: http://HOSTNAME/oauth/token.php ``` Change HOSTNAME by hostname or ip of the server where you have installed Mattermost-LDAP module. -* Database credential -Edit oauth/server.php and adapt, with your settings, variables for database connection : -``` -$dsn = 'pgsql:dbname=oauth_db;host=localhost;port=5432'; -$username = 'oauth'; -$password = 'oauth_secure-pass'; -``` +* Database credentials +Edit oauth/config_db.php and adapt, with your settings, to set up database in PHP. + +#### $host +Hostname or IP address of the database. (ex : localhost) +#### $port +The port of your database to connect. (ex : 5432 for postgres) +#### $name +Database name for oauth server. If you use init script make sure to use the same database name. (ex : oauth_db) +#### $type +Database type to adapt PDO to your database server. Should be mysql or pgsql. +#### $username +Oauth user in the database. This user must have right on the oauth database to store oauth tokens. If you use init script make sure to use the same database user. (ex : oauth) +#### $password +Oauth user password in the database. If you use init script make sure to use the same database user. (ex : oauth_secure-pass) * LDAP config -Edit oauth/LDAP/ldap_config.php to provide your ldap address and port. -Edit oauth/resource.php to change the base directory name ($base) and the filter ($filter) to comply with your LDAP configuration. -Edit oauth/connexion.php to change the relative directory name ($rdn) to comply with your LDAP configuration. +Edit oauth/LDAP/config_ldap.php : +1. Provide your ldap address and port. +2. Change the base directory name ($base) and the filter ($filter) to comply with your LDAP configuration, these variables will be use in resource.php. +3. Change the relative directory name suffix ($rdn) to comply with your LDAP configuration, this variable will be use in connexion.php. -To try your configuration you can use the LDAP library for PHP or ldapsearch command in a shell. +#### $hostname +Your LDAP hostname or LDAP IP, to connect to the LDAP server. +#### $port +Your LDAP port, to connect to the LDAP server. By default : 389. +#### $rdn +The LDAP Relative Directory Name suffix to identify a user in LDAP, see LDAP.php class for more information (use to check user credentials on LDAP). Note that user id (uid) will be add to this suffix to produce a complete relative directory name. The uid is provided by username field in the form from oauth/index.php. For more information, refer to ldap_bind() in php documentation. +#### $base +The base directory name of your LDAP server. (ex : ou=People,o=Company) +#### $filter +Additional filters for your LDAP, see LDAP.php class for more information (used to get user informations). Note that the user id (uid) will be add to the filter (concat) to get only user data from the LDAP. The uid is provided by username field in the form from oauth/index.php. + + +To try your configuration you can use ldap.php available at the root of this project which use the LDAP library for PHP or you can use ldapsearch command in a shell. Configure LDAP is certainly the most difficult step. @@ -128,13 +192,13 @@ I wish to thank my company and my colleagues for their help and support. Also, I Try to restart httpd service. If this persists verify your LDAP configuration or your credentials. * PHP date timezone error - Edit php.ini to set up date.timezone option and restart httpd service. + Edit php.ini to set up date.timezone option and restart httpd service, or use the date_default_timezone_set() function in config_db.php - * Token request failes + * Token request failed Try to add a new rule in your firewall (or use iptables -F on both Mattermost server and Oauth server) * .htaccess does not work - Add following lines to your php.ini and restart httpd service. + Add following lines to your httpd.conf and restart httpd service. ``` AllowOverride All @@ -143,9 +207,3 @@ I wish to thank my company and my colleagues for their help and support. Also, I - - - - - - diff --git a/config_init.sh b/config_init.sh new file mode 100644 index 0000000..5bffcea --- /dev/null +++ b/config_init.sh @@ -0,0 +1,16 @@ +#####################################--CONFIGURATION FILE--######################################## + +#Client configuration +client_id=`openssl rand -hex 32` +client_secret=`openssl rand -hex 32` +redirect_uri="http://mattermost.company.com:8065/signup/gitlab/complete" +grant_types="authorization_code" +scope="api" +user_id="" + +#Database configuration +oauth_user="oauth" +oauth_db_name="oauth_db" +oauth_pass="oauth_secure-pass" +ip="127.0.0.1" +port="5432" \ No newline at end of file diff --git a/init_mysql.sh b/init_mysql.sh index 0b77101..cf0f36a 100755 --- a/init_mysql.sh +++ b/init_mysql.sh @@ -1,24 +1,10 @@ #!/bin/bash #This script need right to become mysql user (so root) and to read/write in httpd directory -#######################################--CONFIGURATION--########################################### +source config_init.sh -#Client configuration -client_id=`openssl rand -hex 32` -client_secret=`openssl rand -hex 32` -redirect_uri="http://hostname.com:8065/signup/gitlab/complete" -grant_types="authorization_code" -scope="api" -user_id="" - -#Database configuration -oauth_user="oauth" -oauth_db_name="oauth_db" -oauth_pass="oauth_secure-pass" -ip="127.0.0.1" -port="3306" +#If script does not work, fill the following variable with the mysql account password mysql_pass="" - #######################################--Fonctions--############################################### ok() { echo -e '\e[32m'$1'\e[m'; } diff --git a/init_postgres.sh b/init_postgres.sh index aa2ab24..493b25e 100755 --- a/init_postgres.sh +++ b/init_postgres.sh @@ -1,22 +1,7 @@ #!/bin/bash #This script need right to become postgres user (so root) and to read/write in httpd directory -#######################################--CONFIGURATION--########################################### - -#Client configuration -client_id=`openssl rand -hex 32` -client_secret=`openssl rand -hex 32` -redirect_uri="http://hostname.com:8065/signup/gitlab/complete" -grant_types="authorization_code" -scope="api" -user_id="" - -#Database configuration -oauth_user="oauth" -oauth_db_name="oauth_db" -oauth_pass="oauth_secure-pass" -ip="127.0.0.1" -port="5432" +source conf_init.sh #######################################--Fonctions--############################################### diff --git a/ldap.php b/ldap.php new file mode 100644 index 0000000..14c81db --- /dev/null +++ b/ldap.php @@ -0,0 +1,62 @@ +LDAP : Test Center"; +echo "Attempting to connect LDAP server ...
"; +$ldap=ldap_connect($hostname); + +if ($ldap) { + echo "Successful connection !
"; + echo "Checking LDAP credentials ...
"; + $is_valid=ldap_bind($ldap,$dn,$pass); + + if ($is_valid) { + echo "Successful authentication !
"; + echo "Getting user informations ...
"; + $user_data=ldap_search($ldap, $base, "mail=" . $email); + + if ($user_data) + { + echo "Data recovered with success !
"; + echo "Extracting useful data :

"; + $info_user = ldap_get_entries($ldap, $user_data); + for ($i=0; $i<$info_user["count"]; $i++) { + + echo "dn: " . $info_user[$i]["dn"] . "
"; + echo "cn: " . $info_user[$i]["cn"][0] . "
"; + echo "uid: " . $info_user[$i]["uid"][0] . "
"; + echo "email: " . $info_user[$i]["mail"][0] . "

"; + } + } else { + echo "No data recovered !

"; + } + } else { + echo "Identification has failed ... Check your credentials

"; + } + + echo "Closing LDAP connection."; + ldap_close($ldap); +} else { + echo "Impossible to connect to LDAP server !"; +} diff --git a/oauth/LDAP/config_ldap.php b/oauth/LDAP/config_ldap.php index fce21d7..32b4cf6 100644 --- a/oauth/LDAP/config_ldap.php +++ b/oauth/LDAP/config_ldap.php @@ -1,3 +1,21 @@ validateAuthorizeRequest($request, $response)) { // if user is not yet authenticated, he is redirected. if (!isset($_SESSION['uid'])) { - //store the authorize request - $_SESSION['auth_page']=end(explode("/", strip_tags(trim($_SERVER['REQUEST_URI'])))); + //store the authorize request + $explode_url=explode("/", strip_tags(trim($_SERVER['REQUEST_URI']))); + $_SESSION['auth_page']=end($explode_url); header('Location: index.php'); exit(); } @@ -33,7 +34,7 @@ if (!isset($_SESSION['uid'])) if (empty($_POST)) { exit('
-
+
'); diff --git a/oauth/config_db.php b/oauth/config_db.php new file mode 100644 index 0000000..f161fe3 --- /dev/null +++ b/oauth/config_db.php @@ -0,0 +1,14 @@ +
'; echo 'Click here to come back to login page'; } else @@ -20,12 +20,12 @@ else // Check received data length (to prevent code injection) if (strlen($_POST['user']) > 15) { - echo 'Strange username ... Please try again'; + echo 'Strange username ... Please try again

'; echo 'Click here to come back to login page'; } elseif (strlen($_POST['password']) > 50 || strlen($_POST['password']) <= 7) { - echo 'Strange password ... Please try again'; + echo 'Strange password ... Please try again

'; echo 'Click here to come back to login page'; } else @@ -41,9 +41,10 @@ else //##################################################\\ // /!\ Adapt here with your LDAP config /!\ \\ + // Now you can do this in ldap_config.php \\ //##################################################\\ - $rdn = 'uid=' . $user . ',ou=People,o=Company'; + $rdn = 'uid=' . $user . $rdn_suffix; /****************************************************/ @@ -67,7 +68,7 @@ else // check login on LDAP has failed. Login and password were invalid or LDAP is unreachable else { - echo "Authetification failed ... Check your username and password.
If error persist contact your administrator.

"; + echo "Authentication failed ... Check your username and password.
If error persist contact your administrator.

"; echo 'Click here to come back to login page'; } } diff --git a/oauth/index.php b/oauth/index.php index 04453e8..1b39240 100644 --- a/oauth/index.php +++ b/oauth/index.php @@ -5,16 +5,16 @@ session_start(); - LDAP Connection Interface + Interface de connexion LDAP
- Connection + Connexion

-
- +
+

diff --git a/oauth/resource.php b/oauth/resource.php index 92cc44d..5ff20d9 100644 --- a/oauth/resource.php +++ b/oauth/resource.php @@ -28,10 +28,10 @@ $assoc_id = $info_oauth["assoc_id"]; //##################################################\\ // /!\ Adapt here with your LDAP config /!\ \\ +// Now you can do this in ldap_config.php \\ //##################################################\\ -$base = "o=Company"; -$filter = "uid=" . $uid; +$filter = $filter . "uid=" . $uid; /****************************************************/ diff --git a/oauth/server.php b/oauth/server.php index 307001b..76be9f8 100644 --- a/oauth/server.php +++ b/oauth/server.php @@ -3,13 +3,8 @@ * Adapted from Oauth2-server-php cookbook * @see http://bshaffer.github.io/oauth2-server-php-docs/cookbook/ */ - -$dsn = 'pgsql:dbname=oauth_db;host=localhost;port=5432'; -$username = 'oauth'; -$password = 'oauth_secure-pass'; - -// error reporting (this is a demo, after all!) -ini_set('display_errors',1);error_reporting(E_ALL); +// Include database configuration +require_once('config_db.php'); // Autoloading (composer is preferred, but for this example let's just do this) require_once('OAuth2/Autoloader.php');