Skip to content

Commit

Permalink
- Fix compilation (remove used import on gson)
Browse files Browse the repository at this point in the history
- user event : use by default generated keyring, to compute hash and signature on user event
  • Loading branch information
blavenie committed Jan 10, 2017
1 parent e4b568d commit c97f1b6
Show file tree
Hide file tree
Showing 17 changed files with 47 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.google.gson.GsonBuilder;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.NetworkPeering;
import org.duniter.core.util.json.JsonArrayParser;
import org.duniter.core.util.json.JsonAttributeParser;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.util.json.JsonSyntaxException;

import java.io.IOException;

Expand All @@ -22,7 +22,7 @@ public BlockchainBlock.Certification deserialize(JsonParser jp, DeserializationC

String[] parts = certificationStr.split(":");
if (parts.length != 4) {
throw new JsonParseException(String.format("Bad format for BlockchainBlock.Certification. Should have 4 parts, but found %s.", parts.length));
throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Certification. Should have 4 parts, but found %s.", parts.length));
}

BlockchainBlock.Certification result = new BlockchainBlock.Certification();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.duniter.core.client.model.bma.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.conn.util.InetAddressUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.EndpointProtocol;
import org.duniter.core.client.model.bma.NetworkPeering;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.duniter.core.client.model.bma.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.util.json.JsonSyntaxException;

import java.io.IOException;

Expand All @@ -24,7 +23,7 @@ public BlockchainBlock.Identity deserialize(JsonParser jp, DeserializationContex

String[] identityParts = identityStr.split(":");
if (identityParts.length != 4) {
throw new JsonParseException(String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", identityParts.length));
throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", identityParts.length));
}

BlockchainBlock.Identity result = new BlockchainBlock.Identity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.NetworkPeering;
import org.duniter.core.client.model.bma.gson.JsonArrayParser;
import org.duniter.core.client.model.bma.gson.JsonAttributeParser;
import org.duniter.core.util.json.JsonArrayParser;
import org.duniter.core.util.json.JsonAttributeParser;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.duniter.core.client.model.bma.jackson;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.util.json.JsonSyntaxException;

import java.io.IOException;

Expand All @@ -23,7 +22,7 @@ public BlockchainBlock.Joiner deserialize(JsonParser jp, DeserializationContext

String[] identityParts = identityStr.split(":");
if (identityParts.length != 5) {
throw new JsonParseException(String.format("Bad format for BlockchainBlock.Identity. Should have 5 parts, but found %s.", identityParts.length));
throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Identity. Should have 5 parts, but found %s.", identityParts.length));
}

BlockchainBlock.Joiner result = new BlockchainBlock.Joiner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.google.gson.JsonParseException;
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.util.json.JsonSyntaxException;

import java.io.IOException;

Expand All @@ -22,7 +22,7 @@ public BlockchainBlock.Revoked deserialize(JsonParser jp, DeserializationContext

String[] parts = str.split(":");
if (parts.length != 2) {
throw new JsonParseException(String.format("Bad format for BlockchainBlock.Revoked. Should have 2 parts, but found %s.", parts.length));
throw new JsonSyntaxException(String.format("Bad format for BlockchainBlock.Revoked. Should have 2 parts, but found %s.", parts.length));
}

BlockchainBlock.Revoked result = new BlockchainBlock.Revoked();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.http.message.BasicNameValuePair;
import org.duniter.core.client.config.Configuration;
import org.duniter.core.client.model.bma.*;
import org.duniter.core.client.model.bma.gson.JsonArrayParser;
import org.duniter.core.util.json.JsonArrayParser;
import org.duniter.core.client.model.local.Identity;
import org.duniter.core.client.model.local.Peer;
import org.duniter.core.client.model.local.Wallet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* #L%
*/

import org.duniter.core.util.json.JsonArrayParser;
import org.junit.Assert;
import org.junit.Test;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public interface CryptoService extends Bean {
*/
KeyPair getKeyPairFromSeed(byte[] seed);

KeyPair getRandomKeypair();

String sign(String message, byte[] secretKey);

String sign(String message, String secretKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public KeyPair getKeyPairFromSeed(byte[] seed) {
return new KeyPair(publicKey, secretKey);
}

@Override
public KeyPair getRandomKeypair() {
return getKeyPairFromSeed(String.valueOf(System.currentTimeMillis()).getBytes());
}

@Override
public String sign(String message, byte[] secretKey) {
byte[] messageBinary = CryptoUtils.decodeUTF8(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.duniter.core.client.model.bma.gson;
package org.duniter.core.util.json;

/*
* #%L
Expand All @@ -22,7 +22,6 @@
* #L%
*/

import com.google.gson.JsonParseException;
import org.duniter.core.util.CollectionUtils;

import java.util.ArrayList;
Expand All @@ -40,15 +39,15 @@ enum ParserState {
READING_ARRAY
}

public String[] getValuesAsArray(String jsonArray) throws JsonParseException {
public String[] getValuesAsArray(String jsonArray) throws JsonSyntaxException {
List<String> result = getValuesAsList(jsonArray);
if (CollectionUtils.isEmpty(result)) {
return null;
}
return result.toArray(new String[result.size()]);
}

public List<String> getValuesAsList(String jsonArray) throws JsonParseException {
public List<String> getValuesAsList(String jsonArray) throws JsonSyntaxException {
ParserState state = ParserState.READING_ARRAY;
List<String> result = new ArrayList<String>();
StringBuilder currentObject = null;
Expand All @@ -67,7 +66,7 @@ public List<String> getValuesAsList(String jsonArray) throws JsonParseException
}
case '}': {
if (state == ParserState.READING_ARRAY) {
throw new JsonParseException("unexpected '}' at " + i);
throw new JsonSyntaxException("unexpected '}' at " + i);
} else {
currentObject.append(c);
parenthesisBalance--;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.duniter.core.client.model.bma.gson;
package org.duniter.core.util.json;

/*
* #%L
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.duniter.core.client.service.exception;
package org.duniter.core.util.json;

/*
* #%L
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.BlockchainParameters;
import org.duniter.core.client.model.bma.EndpointProtocol;
import org.duniter.core.client.model.bma.gson.JsonAttributeParser;
import org.duniter.core.util.json.JsonAttributeParser;
import org.duniter.core.client.model.bma.jackson.JacksonUtils;
import org.duniter.core.client.model.local.Peer;
import org.duniter.core.client.service.bma.BlockchainRemoteService;
import org.duniter.core.client.service.bma.NetworkRemoteService;
import org.duniter.core.client.service.exception.BlockNotFoundException;
import org.duniter.core.client.service.exception.JsonSyntaxException;
import org.duniter.core.util.json.JsonSyntaxException;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.model.NullProgressionModel;
import org.duniter.core.model.ProgressionModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@
import org.duniter.core.service.CryptoService;
import org.duniter.core.util.CollectionUtils;
import org.duniter.core.util.websocket.WebsocketClientEndpoint;
import org.duniter.elasticsearch.user.PluginSettings;
import org.duniter.elasticsearch.user.service.AbstractService;
import org.duniter.elasticsearch.service.BlockchainService;
import org.duniter.elasticsearch.service.changes.ChangeEvent;
import org.duniter.elasticsearch.service.changes.ChangeService;
import org.duniter.elasticsearch.service.changes.ChangeSource;
import org.duniter.elasticsearch.user.PluginSettings;
import org.duniter.elasticsearch.user.model.UserEvent;
import org.duniter.elasticsearch.user.model.UserEventCodes;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.nuiton.i18n.I18n;

import java.io.IOException;
import java.util.*;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Created by Benoit on 30/03/2015.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ public ListenableActionFuture<IndexResponse> indexEvent(Locale locale, UserEvent
signedEvent.setSignature(signature);
eventJson = toJson(signedEvent);
} else {
logger.debug("Could not generate hash for new user event (no keyring)");
// Node has not keyring: do NOT sign it
// TODO : autogen a key pair ?
eventJson = event.toJson(locale);
}

Expand Down Expand Up @@ -377,14 +377,23 @@ private void sendEmail(String recipients, String subject, String textContent) {
}

private KeyPair getNodeKeyPairOrNull(PluginSettings pluginSettings) {

KeyPair result;
if (StringUtils.isNotBlank(pluginSettings.getKeyringSalt()) &&
StringUtils.isNotBlank(pluginSettings.getKeyringPassword())) {
return cryptoService.getKeyPair(pluginSettings.getKeyringSalt(),
result = cryptoService.getKeyPair(pluginSettings.getKeyringSalt(),
pluginSettings.getKeyringPassword());
}
else {
// Use a ramdom keypair
result = cryptoService.getRandomKeypair();
logger.warn(String.format("No keyring in config. salt/password (or keyring) is need to signed user event documents. Will use a generated key [%s]", getNodePubKey(result)));
if (logger.isDebugEnabled()) {
logger.debug(String.format(" salt: " + pluginSettings.getKeyringSalt().replaceAll(".", "*")));
logger.debug(String.format("password: " + pluginSettings.getKeyringPassword().replaceAll(".", "*")));
}
}

return null;
return result;
}

private String getNodePubKey(KeyPair nodeKeyPair) {
Expand Down

0 comments on commit c97f1b6

Please sign in to comment.