Commit 100540d6 authored by Larbi Gharib's avatar Larbi Gharib
Browse files

Added update certificate DAO method

Change-Id: I82fab30582dceca4884c8f611865036d4f0385eb
parent 77ce12b0
......@@ -29,7 +29,9 @@ import net.jami.jams.common.dao.StatementList;
import net.jami.jams.common.dao.connectivity.SQLConnection;
import net.jami.jams.common.objects.user.User;
import java.security.cert.Certificate;
import java.sql.PreparedStatement;
import java.util.Objects;
@Slf4j
public class UserDao extends AbstractDao<User> {
......@@ -56,18 +58,12 @@ public class UserDao extends AbstractDao<User> {
}
}
@Override
public boolean updateObject(StatementList update, StatementList constraints) {
if(update.getStatements().get(0).getColumn() == "certificate"){
public boolean updateUserCertificate(String username, String certificate) {
SQLConnection connection = DataStore.connectionPool.getConnection();
try {
String certificate = update.getStatements().get(0).getValue();
String user = constraints.getStatements().get(0).getValue();
PreparedStatement ps = connection.getConnection().prepareStatement("UPDATE users SET certificate = ? WHERE username = ?");
ps.setString(1, certificate);
ps.setString(2, user);
ps.setString(2, username);
return ps.executeUpdate() != 0;
} catch (Exception e) {
log.error("An error has occurred while trying to update a user certificate: " + e.toString());
......@@ -75,8 +71,10 @@ public class UserDao extends AbstractDao<User> {
} finally {
DataStore.connectionPool.returnConnection(connection);
}
}
}
@Override
public boolean updateObject(StatementList update, StatementList constraints) {
String pw = update.getStatements().get(0).getValue();
String salt = "";
......
......@@ -166,15 +166,7 @@ public class DataStore implements AuthenticationSource {
}
public boolean updateUserCertificate(User user) {
StatementList update = new StatementList();
StatementList constraints = new StatementList();
update.addStatement(new StatementElement("certificate","=", X509Utils.getPEMStringFromCertificate(user.getCertificate()),""));
constraints.addStatement(new StatementElement("username","=", user.getUsername(),""));
return userDao.updateObject(update, constraints);
return userDao.updateUserCertificate(user.getUsername(), X509Utils.getPEMStringFromCertificate(user.getCertificate()));
}
@Override
......
......@@ -22,6 +22,7 @@
*/
package net.jami.datastore.dao;
import lombok.extern.slf4j.Slf4j;
import net.jami.datastore.main.DataStore;
import net.jami.jams.common.authentication.AuthenticationSourceType;
import net.jami.jams.common.dao.StatementElement;
......@@ -41,11 +42,14 @@ import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.SecureRandom;
@Slf4j
class DAOTest {
static String strPrivateKey;
static String strCertificate;
static String strPkcs10Request;
static String generatedCertificate;
static String refreshedCertificate;
@BeforeAll
static void setUp() throws Exception{
......@@ -59,6 +63,10 @@ class DAOTest {
strCertificate = new String(path.readAllBytes());
path = classLoader.getResourceAsStream("pkcs10request.txt");
strPkcs10Request = new String(path.readAllBytes());
path = classLoader.getResourceAsStream("cert_a.crt");
generatedCertificate = new String(path.readAllBytes());
path = classLoader.getResourceAsStream("cert_b.crt");
refreshedCertificate = new String(path.readAllBytes());
}
@Test
......@@ -84,6 +92,34 @@ class DAOTest {
Assertions.assertEquals(user1.getAccessLevel(),AccessLevel.ADMIN);
}
@Test
void refreshUserCertificate() throws Exception {
User user = new User();
user.setUsername("TestUser");
user.setUserType(AuthenticationSourceType.LOCAL);
SecureRandom random = new SecureRandom();
byte[] salt = new byte[16];
random.nextBytes(salt);
user.setSalt(Base64.encodeBase64String(salt));
user.setPassword(hashPassword("abc123", salt));
user.setAccessLevel(AccessLevel.USER);
user.setCertificate(X509Utils.getCertificateFromPEMString(generatedCertificate));
UserDao userDAO = new UserDao();
userDAO.storeObject(user);
StatementList statementList = new StatementList();
statementList.addStatement(new StatementElement("username","=","TestUser",""));
User user1 = userDAO.getObjects(statementList).get(0);
Assertions.assertNotNull(user1);
Assertions.assertEquals(user1.getAccessLevel(),AccessLevel.USER);
userDAO.updateUserCertificate("TestUser", X509Utils.getPEMStringFromCertificate(X509Utils.getCertificateFromPEMString(refreshedCertificate)));
User user2 = userDAO.getObjects(statementList).get(0);
Assertions.assertNotEquals(user1.getCertificate(), user2.getCertificate());
}
@Test
void storeDevice(){
Device device = new Device();
......
-----BEGIN CERTIFICATE-----
MIIFgDCCA2igAwIBAgIRALCoDS70cDjZ/NfSny9ADnIwDQYJKoZIhvcNAQENBQAwHz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vm2tbqWJkkRvMQzw2mKkeGvxWK2pPoFPf51bgrpYsCxlb5v+jOUDSFhp+zED6w0Ggvabnv2QH9+4zKAk6hs2G0PqCUOz4viOfG7EzuWT23JYOMg6Nxqfd/VUx8+CV23f/sMNPNyA0tvxx7sNpyX1s0gEB2xGxVJFrCSOkz1no8qaPvvWqUVSeC8oDuZv5wd2Xw+pr+x1xHN5TjWDOvq5EHNNB6JcTDXRZGVDyhNDKqnsxv1CUuQVclucAlI8hOUfKLrPxAA7l2G1T23aDEQubURd1i4vZtECekBsvsuZ4JRYaymPB6f8U4kJGy+ZE6GsieXTfNLf1WG+Gy56M1zDc/ZJu3p/335BUukKjsGZteZv9VDAXX6WeaORW/sQSyTLvnqVqv0i153GYGjWhT8rOwwIIVZkWegYDbum4xsbCrQKnIwbvD6jqfcjxzgezc5xErl+BwEjtk5rlPHzwS/Sv5RKuCwrIqTLSds0YQAd1mT+mnno2yDAaOR9JaHFs4DWch+U66N1uEpsFDYcTouhLZGVunv/CLg==
-----END CERTIFICATE-----
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment