Ft/hook 18/fix gen webhook key (#9)
* HOOK-18: fixed bug with webhook generating webhook key * fixed hookDaoImpl * fix test and minor improving code
This commit is contained in:
parent
4865b6ca20
commit
7679cd5066
@ -16,6 +16,7 @@ import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.jdbc.support.GeneratedKeyHolder;
|
||||
import org.springframework.jdbc.support.KeyHolder;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
@ -162,8 +163,8 @@ public class HookDaoImpl implements HookDao {
|
||||
@Override
|
||||
@Transactional
|
||||
public Hook create(Hook hook) {
|
||||
KeyPair keyPair = createPairKey(hook.getPartyId());
|
||||
hook.setPubKey(keyPair.getPublKey());
|
||||
String pubKey = createOrGetPubKey(hook.getPartyId());
|
||||
hook.setPubKey(pubKey);
|
||||
hook.setEnabled(true);
|
||||
|
||||
final String sql = "INSERT INTO hook.webhook(party_id, url) " +
|
||||
@ -259,24 +260,27 @@ public class HookDaoImpl implements HookDao {
|
||||
@Autowired
|
||||
Signer signer;
|
||||
|
||||
private KeyPair createPairKey(String partyId) {
|
||||
private String createOrGetPubKey(String partyId) {
|
||||
final String sql = "INSERT INTO hook.party_key(party_id, priv_key, pub_key) " +
|
||||
"VALUES (:party_id, :priv_key, :pub_key) " +
|
||||
"ON CONFLICT(party_id) DO NOTHING";
|
||||
"ON CONFLICT(party_id) DO UPDATE SET party_id=:party_id RETURNING pub_key";
|
||||
|
||||
KeyPair keyPair = signer.generateKeys();
|
||||
MapSqlParameterSource params = new MapSqlParameterSource()
|
||||
.addValue("party_id", partyId)
|
||||
.addValue("priv_key", keyPair.getPrivKey())
|
||||
.addValue("pub_key", keyPair.getPublKey());
|
||||
String pubKey = null;
|
||||
try {
|
||||
jdbcTemplate.update(sql, params);
|
||||
} catch (DataAccessException e) {
|
||||
log.warn("WebhookKeyDaoImpl.createPairKey error", e);
|
||||
KeyHolder keyHolder = new GeneratedKeyHolder();
|
||||
jdbcTemplate.update(sql, params, keyHolder);
|
||||
pubKey = (String) keyHolder.getKeys().get("pub_key");
|
||||
} catch (DataAccessException | NullPointerException | ClassCastException e) {
|
||||
log.warn("WebhookKeyDaoImpl.createOrGetPubKey error", e);
|
||||
throw new DaoException(e);
|
||||
}
|
||||
log.info("Key with party_id = {} added to table", partyId);
|
||||
return keyPair;
|
||||
log.info("Key with party_id = {} added to table. PubKey {}", partyId, pubKey);
|
||||
return pubKey;
|
||||
}
|
||||
|
||||
|
||||
|
@ -44,6 +44,7 @@ public class HookDaoImplTest extends AbstractIntegrationTest {
|
||||
eventFilterByCode.getInvoice().setShopId(1);
|
||||
WebhookParams webhookParams = new WebhookParams("123", eventFilterByCode, "https://google.com");
|
||||
Hook hook = hookDao.create(HookConverter.convert(webhookParams));
|
||||
String pubKey1 = hook.getPubKey();
|
||||
ids.add(hook.getId());
|
||||
webhookAdditionalFilters.clear();
|
||||
webhookAdditionalFilters.add(new WebhookAdditionalFilter(EventType.INVOICE_STATUS_CHANGED, 78, "unpaid", null));
|
||||
@ -55,7 +56,9 @@ public class HookDaoImplTest extends AbstractIntegrationTest {
|
||||
webhookAdditionalFilters.add(new WebhookAdditionalFilter(EventType.INVOICE_STATUS_CHANGED));
|
||||
webhookParams = new WebhookParams("123", EventFilterUtils.getEventFilter(webhookAdditionalFilters), "https://2ch.hk/b");
|
||||
hook = hookDao.create(HookConverter.convert(webhookParams));
|
||||
String pubKey2 = hook.getPubKey();
|
||||
ids.add(hook.getId());
|
||||
Assert.assertEquals(pubKey1, pubKey2);
|
||||
}
|
||||
|
||||
@After
|
||||
|
Loading…
Reference in New Issue
Block a user