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:
Inal Arsanukaev 2017-04-26 15:16:06 +03:00 committed by GitHub
parent 4865b6ca20
commit 7679cd5066
2 changed files with 16 additions and 9 deletions

View File

@ -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;
}

View File

@ -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