RevokeUserFlow.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.core.workflows;

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

  20. import lombok.extern.slf4j.Slf4j;

  21. import net.jami.jams.common.objects.requests.RevocationRequest;
  22. import net.jami.jams.common.objects.requests.RevocationType;
  23. import net.jami.jams.common.objects.responses.DeviceRevocationResponse;
  24. import net.jami.jams.common.objects.user.User;

  25. import java.math.BigInteger;

  26. @Slf4j
  27. public class RevokeUserFlow {

  28.     public static DeviceRevocationResponse revokeUser(String username) {
  29.         DeviceRevocationResponse response = new DeviceRevocationResponse();
  30.         try {
  31.             User user = dataStore.getUserDao().getByUsername(username).get();
  32.             if (user == null) {
  33.                 log.error("Unable to find user!");
  34.                 return null;
  35.             }
  36.             BigInteger serialNumber = user.getCertificate().getSerialNumber();
  37.             RevocationRequest request = new RevocationRequest();
  38.             request.setRevocationType(RevocationType.USER);
  39.             request.setIdentifier(serialNumber);
  40.             certificateAuthority.revokeCertificate(request);

  41.             // Wait for the CRL worker to complete certificate revocation
  42.             certificateAuthority.waitForRevokeCompletion();

  43.             // Check if the certificate has been revoked
  44.             if (certificateAuthority.getLatestCRL().get().getRevokedCertificate(serialNumber)
  45.                     != null) {
  46.                 // Certificate revoked successfully
  47.                 response.setSuccess(true);
  48.                 return response;
  49.             } else {
  50.                 // Certificate not yet revoked
  51.                 log.error(
  52.                         "The certificate has not appeared in the CRL. Operation may have failed.");
  53.                 response.setSuccess(false);
  54.                 return response;
  55.             }
  56.         } catch (Exception e) {
  57.             log.error("An error occurred while revoking device: {}", e.getMessage());
  58.             response.setSuccess(false);
  59.             response.setErrorDetails(e.getMessage());
  60.             return response;
  61.         }
  62.     }
  63. }