UserServlet.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.auth.user;

  18. import static net.jami.jams.server.Server.certificateAuthority;
  19. import static net.jami.jams.server.Server.dataStore;

  20. import com.google.gson.Gson;

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

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

  31. import java.io.IOException;
  32. import java.util.Optional;

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

  36.     // User can "read" himself.
  37.     /**
  38.      * @apiVersion 1.0.0
  39.      * @api {get} /api/auth/user Get JAMS user info
  40.      * @apiName getUser
  41.      * @apiGroup User
  42.      * @apiSuccess (200) {body} User json user object representation
  43.      * @apiSuccessExample {json} Success-Response: { "username":"jdoe", "password":null,
  44.      *     "userType":"AD", "realm":"savoirfairelinux", "accessLevel":"USER",
  45.      *     "needsPasswordReset":false, "ethAddress":"8272773ac", "ethKey":"192938ae72772ab",
  46.      *     "jamiId":"6e3552723df", "certificate":"pem_formatted_certificate",
  47.      *     "privateKey":"pem_formatted_key", "revoked":false }
  48.      * @apiError (500) {null} null was unable to fetch user information
  49.      */
  50.     @Override
  51.     @ScopedServletMethod(securityGroups = {AccessLevel.USER})
  52.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  53.             throws ServletException, IOException {
  54.         String username = req.getAttribute("username").toString();
  55.         Optional<User> result = dataStore.getUserDao().getByUsername(username);
  56.         if (result.isEmpty()) {
  57.             resp.sendError(404, "User was not found!");
  58.             return;
  59.         }

  60.         User user = result.get();
  61.         if (certificateAuthority.getLatestCRL().get() != null) {
  62.             user.setRevoked(
  63.                     certificateAuthority
  64.                                     .getLatestCRL()
  65.                                     .get()
  66.                                     .getRevokedCertificate(user.getCertificate().getSerialNumber())
  67.                             != null);
  68.         } else user.setRevoked(false);
  69.         resp.setStatus(200);
  70.         resp.getOutputStream().write(gson.toJson(user).getBytes());
  71.         resp.flushBuffer();
  72.     }

  73.     // The user can update 3 fields: password,privatekey,publickey
  74.     // For now we do not consider the possibility for privatekey, publickey for other reasons.
  75.     /**
  76.      * @apiVersion 1.0.0
  77.      * @api {put} /api/auth/user Modify the user's info (for now just the password)
  78.      * @apiName putUser
  79.      * @apiGroup User
  80.      * @apiParam {query} password new password
  81.      * @apiSuccess (200) {null} null password changed successfully
  82.      * @apiError (500) {null} null was unable to change password
  83.      */
  84.     @Override
  85.     @ScopedServletMethod(securityGroups = AccessLevel.USER)
  86.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  87.         String username = req.getAttribute("username").toString();

  88.         // Check if he is AD/LDAP - then return a 403, because we are unable to set such password.
  89.         User user = dataStore.getUserDao().getByUsername(username).orElseThrow();
  90.         if (user.getUserType() != AuthenticationSourceType.LOCAL) {
  91.             resp.sendError(403, "Unable to change user data as user is not a local user.");
  92.             return;
  93.         }

  94.         String password = req.getParameter("password");

  95.         if (dataStore.getUserDao().updateObject(password, username)) resp.setStatus(200);
  96.         else
  97.             resp.sendError(
  98.                     500, "An error occurred while attempting to update the user's data field.");
  99.     }
  100. }