DeviceServlet.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.device;

  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 com.google.gson.JsonObject;

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

  27. import net.jami.jams.common.annotations.ScopedServletMethod;
  28. import net.jami.jams.common.objects.devices.Device;
  29. import net.jami.jams.common.objects.requests.DeviceRegistrationRequest;
  30. import net.jami.jams.common.objects.responses.DeviceRegistrationResponse;
  31. import net.jami.jams.common.objects.responses.DeviceRevocationResponse;
  32. import net.jami.jams.common.objects.user.AccessLevel;
  33. import net.jami.jams.common.serialization.adapters.GsonFactory;
  34. import net.jami.jams.common.serialization.tomcat.TomcatCustomErrorHandler;
  35. import net.jami.jams.server.core.workflows.RegisterDeviceFlow;
  36. import net.jami.jams.server.core.workflows.RevokeDeviceFlow;

  37. import java.io.IOException;

  38. @WebServlet("/api/auth/device/*")
  39. public class DeviceServlet extends HttpServlet {
  40.     private final Gson gson = GsonFactory.createGson();

  41.     /**
  42.      * @apiVersion 1.0.0
  43.      * @api {get} /api/auth/device Get device info
  44.      * @apiName getDevice
  45.      * @apiGroup Device
  46.      * @apiParam {path} deviceId id of the device
  47.      * @apiSuccess (200) {body} Device device information
  48.      * @apiSuccessExample {json} Success-Response: { "certificate":"pem_encoded_certificate",
  49.      *     "displayName":"My Galaxy S8", "deviceId":"6aec6252ad", "revoked":true }
  50.      * @apiError (500) {null} null Device was unable to be retrieved
  51.      */
  52.     @Override
  53.     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
  54.             throws ServletException, IOException {
  55.         String username = req.getAttribute("username").toString();
  56.         String deviceId = req.getPathInfo().replace("/", "");

  57.         Device device =
  58.                 dataStore.getDeviceDao().getByDeviceIdAndOwner(deviceId, username).orElseThrow();

  59.         if (certificateAuthority.getLatestCRL().get() != null)
  60.             device.setRevoked(
  61.                     certificateAuthority
  62.                                     .getLatestCRL()
  63.                                     .get()
  64.                                     .getRevokedCertificate(
  65.                                             device.getCertificate().getSerialNumber())
  66.                             != null);
  67.         else device.setRevoked(false);

  68.         resp.getOutputStream().write(gson.toJson(device).getBytes());
  69.         resp.flushBuffer();
  70.     }

  71.     /**
  72.      * @apiVersion 1.0.0
  73.      * @api {post} /api/auth/device Enroll a device
  74.      * @apiName postDevice
  75.      * @apiGroup Device
  76.      * @apiParam {body} DeviceRegistrationRequest device registration request
  77.      * @apiParamExample {json} Request-Example: { "csr":"pem_encoded_csr", "deviceName":"My Galaxy
  78.      *     S8" }
  79.      * @apiSuccess (200) {body} DeviceRegistrationResponse registration response
  80.      * @apiError (500) {null} null Device was unable to be enrolled
  81.      * @apiSuccessExample {json} Success-Response: {
  82.      *     "certificateChain":"pem_encoded_certificate_chain", "displayName":"John Doe",
  83.      *     "nameServer":"https://mydomain.com", "deviceReceipt": "device_receipt_object",
  84.      *     "receiptSignature":"receipt_signature_object", "userPhoto":"base64_encoded_photo" }
  85.      */
  86.     @Override
  87.     @ScopedServletMethod(securityGroups = {AccessLevel.USER})
  88.     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
  89.         Gson gson = GsonFactory.createGson();
  90.         String username = req.getAttribute("username").toString();

  91.         DeviceRegistrationRequest request =
  92.                 gson.fromJson(req.getReader(), DeviceRegistrationRequest.class);
  93.         DeviceRegistrationResponse devResponse =
  94.                 RegisterDeviceFlow.registerDevice(username, request);

  95.         if (devResponse == null) {
  96.             TomcatCustomErrorHandler.sendCustomError(
  97.                     resp, 500, "A server error occurred while enrolling the device.");
  98.             return;
  99.         }

  100.         String filteredJson = gson.toJson(devResponse);
  101.         JsonObject obj = gson.fromJson(filteredJson, JsonObject.class);

  102.         renameKeys(obj);

  103.         resp.getOutputStream().write((obj.toString()).getBytes());
  104.         resp.flushBuffer();
  105.     }

  106.     /**
  107.      * Modifies in place obj to rename a key
  108.      *
  109.      * @param obj
  110.      * @param oldKey
  111.      * @param newKey
  112.      */
  113.     private static void renameKey(JsonObject obj, String oldKey, String newKey) {
  114.         if (obj.get(oldKey) != null) {
  115.             obj.add(newKey, obj.get(oldKey));
  116.             obj.remove(oldKey);
  117.         }
  118.     }

  119.     /**
  120.      * Modifies in place obj to rename keys
  121.      *
  122.      * @param obj
  123.      */
  124.     public static void renameKeys(JsonObject obj) {
  125.         renameKey(obj, "videoEnabled", "Account.videoEnabled");
  126.         renameKey(obj, "publicInCalls", "DHTRelay.PublicInCalls");
  127.         renameKey(obj, "autoAnswer", "Account.autoAnswer");
  128.         renameKey(obj, "peerDiscovery", "Account.peerDiscovery");
  129.         renameKey(obj, "accountDiscovery", "Account.accountDiscovery");
  130.         renameKey(obj, "accountPublish", "Account.accountPublish");

  131.         renameKey(obj, "rendezVous", "Account.rendezVous");
  132.         renameKey(obj, "upnpEnabled", "Account.upnpEnabled");

  133.         renameKey(obj, "turnEnabled", "TURN.enable");
  134.         renameKey(obj, "turnServer", "TURN.server");
  135.         renameKey(obj, "turnServerUserName", "TURN.username");
  136.         renameKey(obj, "turnServerPassword", "TURN.password");
  137.         renameKey(obj, "proxyEnabled", "Account.proxyEnabled");
  138.         renameKey(obj, "proxyServer", "Account.proxyServer");
  139.         renameKey(obj, "dhtProxyListUrl", "Account.dhtProxyListUrl");
  140.         renameKey(obj, "displayName", "Account.displayName");
  141.         renameKey(obj, "defaultModerators", "Account.defaultModerators");
  142.         renameKey(obj, "uiCustomization", "Account.uiCustomization");
  143.     }

  144.     /**
  145.      * @apiVersion 1.0.0
  146.      * @api {put} /api/auth/device Change the name of a device
  147.      * @apiName putDevice
  148.      * @apiGroup Device
  149.      * @apiParam {path} deviceId id of the device
  150.      * @apiParam {query} deviceName new name for the device
  151.      * @apiSuccess (200) {null} null name changed successfully
  152.      * @apiError (500) {null} null device name was unable to be changed
  153.      */
  154.     @Override
  155.     protected void doPut(HttpServletRequest req, HttpServletResponse resp)
  156.             throws ServletException, IOException {
  157.         String username = req.getAttribute("username").toString();
  158.         String deviceId = req.getPathInfo().replace("/", "");
  159.         String deviceName = req.getParameter("deviceName");

  160.         if (dataStore.getDeviceDao().updateObject(deviceName, username, deviceId))
  161.             resp.setStatus(200);
  162.         else
  163.             TomcatCustomErrorHandler.sendCustomError(
  164.                     resp, 500, "A server error occurred while updating the device information.");
  165.     }

  166.     /**
  167.      * @apiVersion 1.0.0
  168.      * @api {delete} /api/auth/device Deactivate a device
  169.      * @apiName deleteDevice
  170.      * @apiGroup Device
  171.      * @apiParam {path} deviceId id of the device
  172.      * @apiSuccess (200) {null} null device deactivated successfully
  173.      * @apiError (500) {null} null device was unable to be deactivated
  174.      */
  175.     @Override
  176.     @ScopedServletMethod(securityGroups = {AccessLevel.USER})
  177.     protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
  178.             throws ServletException, IOException {
  179.         String deviceId = req.getPathInfo().replace("/", "");
  180.         // If the device does not belong to the user throw a 403
  181.         String owner = req.getAttribute("username").toString();

  182.         long deviceIdCount =
  183.                 dataStore.getDeviceDao().getByOwner(owner).stream()
  184.                         .filter(device -> device.getDeviceId().equals(deviceId))
  185.                         .count();

  186.         if (deviceIdCount == 0) {
  187.             TomcatCustomErrorHandler.sendCustomError(
  188.                     resp, 403, "You do not have sufficient rights to revoke this device!");
  189.             return;
  190.         }

  191.         DeviceRevocationResponse devResponse = RevokeDeviceFlow.revokeDevice(owner, deviceId);
  192.         if (devResponse != null) {
  193.             resp.getOutputStream().write(gson.toJson(devResponse).getBytes());
  194.             resp.flushBuffer();
  195.         } else
  196.             TomcatCustomErrorHandler.sendCustomError(
  197.                     resp, 500, "A server error occurred while revoking the device.");
  198.     }
  199. }