CreateAuthSourceServlet.java

  1. /*
  2.  * Copyright (C) 2020-2024 by Savoir-faire Linux
  3.  *
  4.  * This program is free software; you can redistribute it and/or modify
  5.  * it under the terms of the GNU General Public License as published by
  6.  * the Free Software Foundation; either version 3 of the License, or
  7.  * (at your option) any later version.
  8.  *
  9.  * This program is distributed in the hope that it will be useful,
  10.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  * GNU General Public License for more details.
  13.  *
  14.  * You should have received a copy of the GNU General Public License
  15.  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  16.  */
  17. package net.jami.jams.server.servlets.api.install;

  18. import static net.jami.jams.server.Server.userAuthenticationModule;

  19. import com.google.gson.Gson;

  20. import jakarta.servlet.ServletException;
  21. import jakarta.servlet.annotation.WebServlet;
  22. import jakarta.servlet.http.HttpServlet;
  23. import jakarta.servlet.http.HttpServletRequest;
  24. import jakarta.servlet.http.HttpServletResponse;

  25. import lombok.extern.slf4j.Slf4j;

  26. import net.jami.jams.common.annotations.ScopedServletMethod;
  27. import net.jami.jams.common.authentication.AuthenticationSourceType;
  28. import net.jami.jams.common.objects.requests.CreateAuthSourceRequest;
  29. import net.jami.jams.common.objects.user.AccessLevel;
  30. import net.jami.jams.common.serialization.adapters.GsonFactory;

  31. import java.io.IOException;

  32. @WebServlet("/api/install/auth")
  33. @Slf4j
  34. public class CreateAuthSourceServlet extends HttpServlet {
  35.     private final Gson gson = GsonFactory.createGson();

  36.     @Override
  37.     @ScopedServletMethod(securityGroups = {AccessLevel.ADMIN})
  38.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  39.             throws ServletException, IOException {
  40.         super.doGet(req, resp);
  41.     }

  42.     @Override
  43.     @ScopedServletMethod(securityGroups = {AccessLevel.ADMIN})
  44.     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
  45.             throws ServletException, IOException {
  46.         CreateAuthSourceRequest authSourceRequest =
  47.                 gson.fromJson(req.getReader(), CreateAuthSourceRequest.class);
  48.         CachedObjects.localAuthSettings = null;
  49.         CachedObjects.activeDirectorySettings = null;
  50.         CachedObjects.ldapSettings = null;
  51.         boolean error = false;
  52.         switch (authSourceRequest.getType()) {
  53.             case LOCAL:
  54.                 CachedObjects.localAuthSettings = authSourceRequest.getLocalAuthSettings();
  55.                 break;
  56.             case LDAP:
  57.                 if (userAuthenticationModule.testModuleConfiguration(
  58.                         AuthenticationSourceType.LDAP,
  59.                         gson.toJson(authSourceRequest.getLdapSettings()))) {
  60.                     CachedObjects.ldapSettings = authSourceRequest.getLdapSettings();
  61.                 } else error = true;
  62.                 break;
  63.             case AD:
  64.                 if (userAuthenticationModule.testModuleConfiguration(
  65.                         AuthenticationSourceType.AD,
  66.                         gson.toJson(authSourceRequest.getActiveDirectorySettings()))) {
  67.                     CachedObjects.activeDirectorySettings =
  68.                             authSourceRequest.getActiveDirectorySettings();
  69.                 } else error = true;
  70.                 break;
  71.         }
  72.         if (error)
  73.             resp.sendError(
  74.                     500,
  75.                     "The supplied configuration is invalid or the connectivity tests has failed");
  76.         else CachedObjects.endpoint = "/api/install/settings";
  77.     }
  78. }