User.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.common.objects.user;

  18. import lombok.AllArgsConstructor;
  19. import lombok.Getter;
  20. import lombok.NoArgsConstructor;
  21. import lombok.Setter;

  22. import net.jami.jams.common.authentication.AuthenticationSourceType;
  23. import net.jami.jams.common.objects.roots.BlockchainEntity;
  24. import net.jami.jams.common.objects.roots.X509Entity;
  25. import net.jami.jams.common.serialization.database.DatabaseObject;
  26. import net.jami.jams.common.utils.X509Utils;

  27. import java.sql.PreparedStatement;
  28. import java.sql.ResultSet;

  29. @AllArgsConstructor
  30. @NoArgsConstructor
  31. @Getter
  32. @Setter
  33. public class User extends X509Entity implements BlockchainEntity, DatabaseObject {

  34.     private String username;
  35.     private transient String password;
  36.     private AuthenticationSourceType userType;
  37.     private String realm; // sort of the domain.
  38.     private AccessLevel accessLevel = AccessLevel.USER;
  39.     private Boolean needsPasswordReset = false;
  40.     private transient String salt;

  41.     private String ethAddress;
  42.     private transient String ethKey;
  43.     private String jamiId;

  44.     public User(ResultSet rs) throws Exception {
  45.         this.username = rs.getString("username");
  46.         this.password = rs.getString("password");
  47.         this.userType = AuthenticationSourceType.valueOf(rs.getString("userType"));
  48.         this.accessLevel = AccessLevel.valueOf(rs.getString("accessLevel"));
  49.         this.needsPasswordReset = Boolean.parseBoolean(rs.getString("needsPasswordReset"));
  50.         this.salt = rs.getString("salt");
  51.         this.realm = rs.getString("realm");
  52.         this.ethAddress = rs.getString("ethAddress");
  53.         this.ethKey = rs.getString("ethKey");
  54.         this.jamiId = rs.getString("jamiId");
  55.         if (rs.getString("certificate") != null && !rs.getString("certificate").isBlank()) {
  56.             this.setCertificate(X509Utils.getCertificateFromPEMString(rs.getString("certificate")));
  57.         }
  58.         if (rs.getString("privatekey") != null && !rs.getString("privatekey").isBlank()) {
  59.             this.setPrivateKey(X509Utils.getKeyFromPEMString(rs.getString("privatekey")));
  60.         }
  61.     }

  62.     @Override
  63.     public String getAddress() {
  64.         return this.ethAddress;
  65.     }

  66.     @Override
  67.     public void setAddress(String address) {
  68.         this.ethAddress = address;
  69.     }

  70.     @Override
  71.     public String getKey() {
  72.         return ethKey;
  73.     }

  74.     public String getAccessLevelName() {
  75.         return this.accessLevel.name();
  76.     }

  77.     @Override
  78.     public void setKey(String key) {
  79.         this.ethKey = key;
  80.     }

  81.     @Override
  82.     public PreparedStatement getInsert(PreparedStatement ps) throws Exception {
  83.         ps.setString(1, username);
  84.         // We don't store the user's password if he is remote.
  85.         if (userType != null && userType.equals(AuthenticationSourceType.LOCAL))
  86.             ps.setString(2, password);
  87.         else ps.setString(2, null);
  88.         if (userType != null) ps.setString(3, userType.toString());
  89.         else ps.setString(3, null);
  90.         ps.setString(4, realm);
  91.         ps.setString(5, ethAddress);
  92.         ps.setString(6, ethKey);
  93.         ps.setString(7, jamiId);
  94.         if (this.getCertificate() != null) {
  95.             ps.setString(8, X509Utils.getPEMStringFromCertificate(this.getCertificate()));
  96.         } else {
  97.             ps.setString(8, "");
  98.         }
  99.         if (this.getPrivateKey() != null) {
  100.             ps.setString(9, X509Utils.getPEMStringFromPrivateKey(this.getPrivateKey()));
  101.         } else {
  102.             ps.setString(9, "");
  103.         }
  104.         ps.setString(10, accessLevel.toString());
  105.         ps.setString(11, needsPasswordReset.toString());
  106.         ps.setString(12, salt);
  107.         return ps;
  108.     }

  109.     @Override
  110.     public PreparedStatement getDelete(PreparedStatement ps) throws Exception {
  111.         return null;
  112.     }

  113.     @Override
  114.     public PreparedStatement getUpdate(PreparedStatement ps) throws Exception {
  115.         return null;
  116.     }

  117.     public void setPassword(char[] password) {
  118.         this.password = new String(password);
  119.     }

  120.     public void setPassword(String password) {
  121.         this.password = password;
  122.     }
  123. }