From fc406f72f80302b2031c0bce8c53ada4579e44e0 Mon Sep 17 00:00:00 2001
From: William Enright <william.enright@savoirfairelinux.com>
Date: Thu, 4 Jun 2020 18:08:58 -0400
Subject: [PATCH] fixed login endpoint being possibly filtered

Change-Id: Iec3570e94cb03f8a5a646f385907f499bec3022a
---
 datastore/pom.xml                             |  6 +++
 jams-ca/pom.xml                               |  6 +++
 jams-server/pom.xml                           | 12 +++++
 .../jams/server/servlets/LoginServlet.java    | 50 ++++++++-----------
 .../install/CreateServerSettingsServlet.java  |  2 +-
 .../src/main/resources/webapp/js/api.js       | 18 +++----
 .../resources/webapp/js/cookies-manager.js    |  1 +
 .../webapp/js/identity-management.js          |  1 +
 .../src/main/resources/webapp/js/search.js    | 14 ++++--
 .../src/main/resources/webapp/js/user.js      |  2 +-
 10 files changed, 66 insertions(+), 46 deletions(-)

diff --git a/datastore/pom.xml b/datastore/pom.xml
index d197f3ec..cc897acf 100644
--- a/datastore/pom.xml
+++ b/datastore/pom.xml
@@ -22,6 +22,12 @@
             <artifactId>derby</artifactId>
             <version>${debry.version}</version>
         </dependency>
+        <dependency>
+            <groupId>net.jami</groupId>
+            <artifactId>jams-common</artifactId>
+            <version>2.0</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/jams-ca/pom.xml b/jams-ca/pom.xml
index d6003640..8e98f3c9 100644
--- a/jams-ca/pom.xml
+++ b/jams-ca/pom.xml
@@ -17,6 +17,12 @@
             <version>${revision}</version>
             <scope>compile</scope>
         </dependency>
+        <dependency>
+            <groupId>net.jami</groupId>
+            <artifactId>jams-common</artifactId>
+            <version>2.0</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/jams-server/pom.xml b/jams-server/pom.xml
index 279ea341..258eaecc 100644
--- a/jams-server/pom.xml
+++ b/jams-server/pom.xml
@@ -61,6 +61,18 @@
             <artifactId>asm</artifactId>
             <version>${asm.version}</version>
         </dependency>
+        <dependency>
+            <groupId>net.jami</groupId>
+            <artifactId>datastore</artifactId>
+            <version>2.0</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>net.jami</groupId>
+            <artifactId>jami-nameserver</artifactId>
+            <version>2.0</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/jams-server/src/main/java/net/jami/jams/server/servlets/LoginServlet.java b/jams-server/src/main/java/net/jami/jams/server/servlets/LoginServlet.java
index df1b56b4..6e905abe 100644
--- a/jams-server/src/main/java/net/jami/jams/server/servlets/LoginServlet.java
+++ b/jams-server/src/main/java/net/jami/jams/server/servlets/LoginServlet.java
@@ -1,25 +1,25 @@
 /*
-* Copyright (C) 2020 by Savoir-faire Linux
-* Authors: William Enright <william.enright@savoirfairelinux.com>
-*          Ndeye Anna Ndiaye <anna.ndiaye@savoirfairelinux.com>
-*          Johnny Flores <johnny.flores@savoirfairelinux.com>
-*          Mohammed Raza <mohammed.raza@savoirfairelinux.com>
-*          Felix Sidokhine <felix.sidokhine@savoirfairelinux.com>
-*
-*
-* This program is free software; you can redistribute it and/or modify
-* it under the terms of the GNU General Public License as published by
-* the Free Software Foundation; either version 3 of the License, or
-* (at your option) any later version.
-*
-* This program is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-* GNU General Public License for more details.
-*
-* You should have received a copy of the GNU General Public License
-* along with this program.  If not, see <https://www.gnu.org/licenses/>.
-*/
+ * Copyright (C) 2020 by Savoir-faire Linux
+ * Authors: William Enright <william.enright@savoirfairelinux.com>
+ *          Ndeye Anna Ndiaye <anna.ndiaye@savoirfairelinux.com>
+ *          Johnny Flores <johnny.flores@savoirfairelinux.com>
+ *          Mohammed Raza <mohammed.raza@savoirfairelinux.com>
+ *          Felix Sidokhine <felix.sidokhine@savoirfairelinux.com>
+ *
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
 package net.jami.jams.server.servlets;
 
 import com.jsoniter.JsonIterator;
@@ -51,14 +51,6 @@ public class LoginServlet extends HttpServlet {
      * @apiParam {body} [LoginRequest] username/password sent to server as JSON object
      *
      * @apiSuccess (200) {body} AuthTokenResponse the 0Auth authentication token
-     * @apiSuccessExample {json} Success-Response:
-     * {
-     *     "token_type":"Bearer",
-     *     "access_token":"JWT representation of the token",
-     *     "expires_in":180000,
-     *     "scope":"USER"
-     * }
-     *
      * @apiError (403) {null} null The user is unauthorized
      */
     @Override
diff --git a/jams-server/src/main/java/net/jami/jams/server/servlets/api/install/CreateServerSettingsServlet.java b/jams-server/src/main/java/net/jami/jams/server/servlets/api/install/CreateServerSettingsServlet.java
index 9c55af40..a67fc61f 100644
--- a/jams-server/src/main/java/net/jami/jams/server/servlets/api/install/CreateServerSettingsServlet.java
+++ b/jams-server/src/main/java/net/jami/jams/server/servlets/api/install/CreateServerSettingsServlet.java
@@ -44,7 +44,7 @@ public class CreateServerSettingsServlet extends HttpServlet {
     }
 
     @Override
-    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         CertificateAuthorityConfig config = JsonIterator.deserialize(
                 req.getInputStream().readAllBytes(),CertificateAuthorityConfig.class);
         CachedObjects.certificateAuthorityConfig = config;
diff --git a/jams-server/src/main/resources/webapp/js/api.js b/jams-server/src/main/resources/webapp/js/api.js
index 029b7302..6e0977a6 100644
--- a/jams-server/src/main/resources/webapp/js/api.js
+++ b/jams-server/src/main/resources/webapp/js/api.js
@@ -37,7 +37,7 @@ var ca_setup_page = 'ca-setup.html';
 var identity_management_page = 'identity-management.html';
 var server_parameters_page = 'server-parameters.html';
 var api_path_post_install_admin = '/api/install/start';
-var api_path_post_auth_login = '/api/auth/login';
+var api_path_post_auth_login = '/api/login';
 var api_path_post_install_ca = '/api/install/ca';
 var api_path_post_install_auth = '/api/install/auth';
 var api_path_post_install_server = '/api/install/settings';
@@ -54,15 +54,16 @@ var api_path_get_logout = '/api/auth/logout';
 var api_path_get_post_configuration_auth_service = '/api/configuration/authservice';
 var api_path_get_post_configuration_global_settings = '/api/configuration/globalsettings';
 var api_path_post_configuration_change_password = '/api/configuration/changepassword';
-var api_path_post_configuration_register_license = '/api/configuration/registerlicense';
+var api_path_post_configuration_register_license = '/api/subscription';
 var api_path_get_auth_license = '/api/auth/license';
-var api_path_get_auth_services = '/api/auth/authservices';
-var api_path_get_needs_update = '/api/checkupdate';
-var api_path_get_start_update = '/api/startupdate';
+var api_path_get_directories = '/api/auth/directories';
+var api_path_get_needs_update = '/api/update';
+var api_path_get_start_update = '/api/update';
 var api_path_post_create_user = '/api/user/create';
 var api_path_post_update_user = '/api/user/update';
 var api_path_post_exists_user = '/api/user/exists';
-var api_path_get_user_extended_data ='/api/auth/user/extendeddata';
+var api_path_get_user_directory_search ='/api/auth/directory/search';
+var api_path_get_user_directory_entry ='/api/auth/directory/entry';
 var api_path_get_user_needs_reset ='/api/user/needsreset';
 
 
@@ -90,8 +91,6 @@ function ajaxApiCall(api_path, request_type, data, credentials, callBackFunction
     if (window.localStorage.getItem('access_token')) {
 
         var jwt = localStorage.getItem('access_token');
-        console.log(jwt);
-        console.log(data);
 
         ajax['headers'] =  {
             "Bearer": jwt,
@@ -100,7 +99,7 @@ function ajaxApiCall(api_path, request_type, data, credentials, callBackFunction
 
     // pass data in the header
     if (data) {
-        if (api_path == api_path_get_user_extended_data || api_path == api_path_get_auth_user_search || api_path == api_path_get_user_needs_reset)
+        if (api_path == api_path_get_user_directory_search || api_path == api_path_get_auth_user_search || api_path == api_path_get_user_needs_reset)
             isSearch = true;
 
         // search dataType
@@ -110,7 +109,6 @@ function ajaxApiCall(api_path, request_type, data, credentials, callBackFunction
         else {
 
             if (window.localStorage.getItem('access_token')) {
-
                 var jwt = localStorage.getItem('access_token');
                 console.log(jwt);
 
diff --git a/jams-server/src/main/resources/webapp/js/cookies-manager.js b/jams-server/src/main/resources/webapp/js/cookies-manager.js
index e3e23f66..98580988 100644
--- a/jams-server/src/main/resources/webapp/js/cookies-manager.js
+++ b/jams-server/src/main/resources/webapp/js/cookies-manager.js
@@ -25,6 +25,7 @@ function setCookie(key, value, expiry) {
 }
 
 function setJWT(value) {
+    window.localStorage.removeItem('access_token');
     window.localStorage.setItem('access_token', value.access_token);
 }
 
diff --git a/jams-server/src/main/resources/webapp/js/identity-management.js b/jams-server/src/main/resources/webapp/js/identity-management.js
index 53e5e2e6..7416c09c 100644
--- a/jams-server/src/main/resources/webapp/js/identity-management.js
+++ b/jams-server/src/main/resources/webapp/js/identity-management.js
@@ -72,6 +72,7 @@ function setLDAPParametersData(form) {
   settings['fieldMappings']['facsimileTelephoneNumber'] = "FaxNumber";
   settings['fieldMappings']['extensionName'] = "PhoneNumberExtension";
   settings['fieldMappings']['o'] = "Organization";
+  settings['fieldMappings']['uid'] = "Username";
 
   authSource['type'] = 'LDAP';
   authSource['ldapSettings'] = settings;
diff --git a/jams-server/src/main/resources/webapp/js/search.js b/jams-server/src/main/resources/webapp/js/search.js
index 0053d32c..2fd27c79 100644
--- a/jams-server/src/main/resources/webapp/js/search.js
+++ b/jams-server/src/main/resources/webapp/js/search.js
@@ -25,8 +25,13 @@ var userEdit = '';
 var userRevoke = '';
 
 $(document).ready(function() {
+
+    var data = {
+        'queryString': "*"
+    }
+
     // load all users
-    ajaxApiCall(api_path_get_user_extended_data, 'GET', null, null, listAllUsers);
+    ajaxApiCall(api_path_get_user_directory_search, 'GET', data, null, listAllUsers);
 
     setTimeout(function() {
         ajaxApiCall(api_path_get_needs_update, 'GET', null, null, handleUpdate, false);
@@ -118,7 +123,6 @@ function listAllUsers(data, statusCode, jqXHR)
     for (i = 0; i < resultSet.length; i++) {
       var firstName = '';
       var lastName = '';
-      console.log("listAllUsers");
       console.log(resultSet[i]);
 
       if (resultSet[i].firstName)
@@ -154,7 +158,7 @@ function listAllUsers(data, statusCode, jqXHR)
   });
 
     setTimeout(function() {
-        ajaxApiCall(api_path_get_auth_services, 'GET', null, null, ishsqlConfig);
+        ajaxApiCall(api_path_get_directories, 'GET', null, null, ishsqlConfig);
     }, 100);
     addListenersForActions();
 }
@@ -317,13 +321,13 @@ function handleUpdate(data, statusCode, jqXHR) {
         var resultSet = data;
         if (resultSet.updateAvailable == true) {
             $('#update-title-div').show();
-            $('#update-title').text("UPGRADE TO JAMS " + resultSet.newVersions.substring(resultSet.newVersions.indexOf("=") + 1, resultSet.newVersions.length - 1));
+            $('#update-title').text("UPGRADE TO JAMS " + resultSet.remoteVersions.substring(resultSet.remoteVersions.indexOf("=") + 1, resultSet.remoteVersions.length - 1));
 
             document.getElementsByClassName("update-title")[0].addEventListener('click', function(){
                 if (isActive == true) {
                     $('#downloadUpdateModalCenter').modal('show');
                     document.getElementsByClassName("auto-update")[0].addEventListener('click', function() {
-                        ajaxApiCall(api_path_get_start_update, 'GET', null, null, handleUpdateStart, false);
+                        ajaxApiCall(api_path_get_start_update, 'POST', null, null, handleUpdateStart, false);
                     });
                 }
                 else
diff --git a/jams-server/src/main/resources/webapp/js/user.js b/jams-server/src/main/resources/webapp/js/user.js
index 12b31d2b..3749ae93 100644
--- a/jams-server/src/main/resources/webapp/js/user.js
+++ b/jams-server/src/main/resources/webapp/js/user.js
@@ -160,7 +160,7 @@ $(document).ready(function() {
         if (searchAccess) {
             // set User information fields
             setTimeout(function() {
-                ajaxApiCall(api_path_get_auth_services, 'GET', null, null, ishsqlConfig);
+                ajaxApiCall(api_path_get_directories, 'GET', null, null, ishsqlConfig);
                 ajaxApiCall(api_path_get_auth_user_search, 'GET', searchData, null, setUserInfoDataSource);
                 ajaxApiCall(api_path_get_user_extended_data, 'GET', searchData, null, setUserExtendedData);
             }, 300);
-- 
GitLab