From f42fef72b7adf463cbfa70c41adfa8ff2378e456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6nd=C3=B6r?= Date: Wed, 29 Mar 2017 11:37:37 +0200 Subject: [PATCH 1/3] code cleanup updated spring --- pom.xml | 14 +- .../Controller/RestService.java | 5 +- .../globalregistry/configuration/Config.java | 10 +- .../globalregistry/dht/DHTManager.java | 2 +- .../globalregistry/model/AbstractDataset.java | 242 ++++++------- .../rethink/globalregistry/model/Dataset.java | 3 +- .../globalregistry/model/Dataset_V1.java | 259 +++++++------- .../globalregistry/model/Dataset_V2.java | 312 +++++++++-------- .../eu/rethink/globalregistry/model/GUID.java | 214 ++++++------ .../globalregistry/model/schema_v1.json | 31 +- .../globalregistry/model/schema_v2.json | 181 +++++----- .../globalregistry/tool/DatasetTool.java | 6 +- .../tool/GlobalRegistryAPI.java | 324 +++++++++--------- .../util/ECDSAKeyPairManager.java | 167 +++++---- .../util/IntegrityException.java | 17 +- .../globalregistry/tests/DatasetTest.java | 46 ++- 16 files changed, 946 insertions(+), 887 deletions(-) diff --git a/pom.xml b/pom.xml index 204a956..a0ac8e6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,12 +6,12 @@ eu.rethink.globalregistry ReThinkGlobalRegistry - 0.3.2 + 0.3.3 org.springframework.boot spring-boot-starter-parent - 1.5.1.RELEASE + 1.5.2.RELEASE @@ -65,16 +65,16 @@ commons-daemon 1.0.15 - - org.springframework.boot - spring-boot-starter-test - test - commons-codec commons-codec 1.10 + + org.springframework.boot + spring-boot-starter-test + test + junit junit diff --git a/src/main/java/eu/rethink/globalregistry/Controller/RestService.java b/src/main/java/eu/rethink/globalregistry/Controller/RestService.java index 163888d..2e9b199 100644 --- a/src/main/java/eu/rethink/globalregistry/Controller/RestService.java +++ b/src/main/java/eu/rethink/globalregistry/Controller/RestService.java @@ -72,7 +72,6 @@ public ResponseEntity index() throws URISyntaxException response.put("connectedNodes", connectedNodes); return new ResponseEntity(response.toString(), HttpStatus.OK); - } //STATUS: Index Function is working absolutely fine. @@ -553,10 +552,11 @@ public ResponseEntity putdata(@RequestBody String jwt, @PathVariable("GU /** * DELETE + * @throws JSONException * */ @RequestMapping(value = "guid/{GUID}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) - public ResponseEntity deletedata(@PathVariable("GUID") String GUID) throws URISyntaxException + public ResponseEntity deleteData(@PathVariable("GUID") String GUID) throws URISyntaxException { LOGGER.error("Incoming request: DELETE /guid/" + GUID); @@ -589,6 +589,7 @@ public ResponseEntity deletedata(@PathVariable("GUID") String GUID) thro /** * Easteregg. Just returning "I'm a teapot" as of RFC #2324 + * @throws JSONException * */ @RequestMapping(value = "teapot", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) diff --git a/src/main/java/eu/rethink/globalregistry/configuration/Config.java b/src/main/java/eu/rethink/globalregistry/configuration/Config.java index 81307ad..bb55f72 100644 --- a/src/main/java/eu/rethink/globalregistry/configuration/Config.java +++ b/src/main/java/eu/rethink/globalregistry/configuration/Config.java @@ -7,17 +7,17 @@ * Configuration class of the GlobalRegistry. Using singleton pattern. * * @author Sebastian Göndör - * @version 3 - * @date 13.02.2017 + * @version 4 + * @date 29.03.2017 */ public class Config { private static Config _singleton = null; - private static final String versionName = "0.3.2"; - private static final int versionNumber = 1569; + private static final String versionName = "0.3.3"; + private static final int versionNumber = 1578; private static final String versionCode = "springified"; - private static final String versionDate = "2017-02-14"; + private static final String versionDate = "2017-03-29"; private static final String productName = "reTHINK Global Registry"; private static final String productNameShort = "gReg"; diff --git a/src/main/java/eu/rethink/globalregistry/dht/DHTManager.java b/src/main/java/eu/rethink/globalregistry/dht/DHTManager.java index ca85deb..5bafc9a 100644 --- a/src/main/java/eu/rethink/globalregistry/dht/DHTManager.java +++ b/src/main/java/eu/rethink/globalregistry/dht/DHTManager.java @@ -57,7 +57,7 @@ public DHTManager initDHT() throws IOException peer = new PeerBuilderDHT(new PeerBuilder(new Number160(rand)).ports(Config.getInstance().getPortDHT()).start()).start(); new IndirectReplication(peer).start(); - + InetAddress address = Inet4Address.getByName(Config.getInstance().getConnectNode()); FutureDiscover futureDiscover = peer.peer().discover().inetAddress(address).ports(Config.getInstance().getPortDHT()).start(); futureDiscover.awaitUninterruptibly(); diff --git a/src/main/java/eu/rethink/globalregistry/model/AbstractDataset.java b/src/main/java/eu/rethink/globalregistry/model/AbstractDataset.java index fcaa817..40cb9cb 100644 --- a/src/main/java/eu/rethink/globalregistry/model/AbstractDataset.java +++ b/src/main/java/eu/rethink/globalregistry/model/AbstractDataset.java @@ -1,120 +1,132 @@ package eu.rethink.globalregistry.model; import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; -public abstract class AbstractDataset { - - protected String guid; - protected String salt; - protected JSONArray userIDs; - protected String lastUpdate; - protected String timeout; - protected String publicKey; - protected int active; - protected int revoked; - protected JSONObject defaults; - protected int schemaVersion; - protected JSONArray legacyIDs; - - - public String getGUID() { - return guid; - } - - - public void setGUID(String guid) { - this.guid = guid; - } - - - public int getSchemaVersion() { - return schemaVersion; - } - - - public void setSchemaVersion(int schemaVersion) { - this.schemaVersion = schemaVersion; - } - - - public String getSalt() { - return salt; - } - - public void setSalt(String salt) { - this.salt = salt; - } - - - public JSONArray getUserIDs() { - return userIDs; - } - - public JSONObject getUserIDs(int index) { - return userIDs.getJSONObject(index); - } - - - public void setUserIDs(JSONArray userIDs) { - this.userIDs = userIDs; - } - - - public String getLastUpdate() { - return lastUpdate; - } - - - public void setLastUpdate(String lastUpdate) { - this.lastUpdate = lastUpdate; - } - - public String getTimeout() { - return timeout; - } - - - public void setTimeout(String timeout) { - this.timeout = timeout; - } - - - public String getPublicKey() { - return publicKey; - } - - - public void setPublicKey(String publicKey) { - this.publicKey = publicKey; - } - - - public int getActive() { - return active; - } - - - public void setActive(int active) { - this.active = active; - } - - public int getRevoked() { - return revoked; - } - - public void setRevoked(int revoked) { - this.revoked = revoked; - } - - - public JSONObject getDefaults() { - return defaults; - } - - - public void setDefaults(JSONObject defaults) { - this.defaults = defaults; - } - -} +public abstract class AbstractDataset +{ + protected String guid; + protected String salt; + protected JSONArray userIDs; + protected String lastUpdate; + protected String timeout; + protected String publicKey; + protected int active; + protected int revoked; + protected JSONObject defaults; + protected int schemaVersion; + protected JSONArray legacyIDs; + + public String getGUID() + { + return guid; + } + + public void setGUID(String guid) + { + this.guid = guid; + } + + public int getSchemaVersion() + { + return schemaVersion; + } + + public void setSchemaVersion(int schemaVersion) + { + this.schemaVersion = schemaVersion; + } + + public String getSalt() + { + return salt; + } + + public void setSalt(String salt) + { + this.salt = salt; + } + + public JSONArray getUserIDs() + { + return userIDs; + } + + public JSONObject getUserIDs(int index) + { + try + { + return userIDs.getJSONObject(index); + } + catch(JSONException e) + { + return new JSONObject(); + } + } + + public void setUserIDs(JSONArray userIDs) + { + this.userIDs = userIDs; + } + + public String getLastUpdate() + { + return lastUpdate; + } + + public void setLastUpdate(String lastUpdate) + { + this.lastUpdate = lastUpdate; + } + + public String getTimeout() + { + return timeout; + } + + public void setTimeout(String timeout) + { + this.timeout = timeout; + } + + public String getPublicKey() + { + return publicKey; + } + + public void setPublicKey(String publicKey) + { + this.publicKey = publicKey; + } + + public int getActive() + { + return active; + } + + public void setActive(int active) + { + this.active = active; + } + + public int getRevoked() + { + return revoked; + } + + public void setRevoked(int revoked) + { + this.revoked = revoked; + } + + public JSONObject getDefaults() + { + return defaults; + } + + public void setDefaults(JSONObject defaults) + { + this.defaults = defaults; + } +} \ No newline at end of file diff --git a/src/main/java/eu/rethink/globalregistry/model/Dataset.java b/src/main/java/eu/rethink/globalregistry/model/Dataset.java index 460a26d..c0dbf14 100644 --- a/src/main/java/eu/rethink/globalregistry/model/Dataset.java +++ b/src/main/java/eu/rethink/globalregistry/model/Dataset.java @@ -211,8 +211,7 @@ public void setLegacyIDs(JSONArray legacyIDs) { this.legacyIDs = legacyIDs; } - - + //validate schema is for checking the given datset against its schema format and structure. public boolean validateSchema() throws DatasetIntegrityException { int version = this.schemaVersion; diff --git a/src/main/java/eu/rethink/globalregistry/model/Dataset_V1.java b/src/main/java/eu/rethink/globalregistry/model/Dataset_V1.java index 9981094..64708cf 100644 --- a/src/main/java/eu/rethink/globalregistry/model/Dataset_V1.java +++ b/src/main/java/eu/rethink/globalregistry/model/Dataset_V1.java @@ -5,140 +5,139 @@ import org.everit.json.schema.Schema; import org.everit.json.schema.ValidationException; import org.everit.json.schema.loader.SchemaLoader; +import org.json.JSONException; import org.json.JSONObject; import org.json.JSONTokener; import java.io.IOException; import java.io.InputStream; - -public class Dataset_V1 extends AbstractDataset { - - public Dataset_V1(){ - super(); - } - - - public static Dataset_V1 createFromJSONObject(JSONObject json) { - Dataset_V1 datasetV1 = new Dataset_V1(); - - datasetV1.setSchemaVersion(json.getInt("schemaVersion")); - datasetV1.setActive(json.getInt("active")); - datasetV1.setRevoked(json.getInt("revoked")); - datasetV1.setGUID(json.getString("guid")); - datasetV1.setPublicKey(json.getString("publicKey")); - datasetV1.setSalt(json.getString("salt")); - datasetV1.setUserIDs(json.getJSONArray("userIDs")); - datasetV1.setLastUpdate(json.getString("lastUpdate")); - datasetV1.setTimeout(json.getString("timeout")); - datasetV1.setDefaults(json.getJSONObject("defaults")); - - /* - try - { - datasetV1.validateSchema(json); - datasetV1.checkIntegrity(json); - } - catch (DatasetIntegrityException e) - { - // TODO handle errors like this - return null; - } - */ - return datasetV1; - } - - public static JSONObject exportJSONObject(Dataset_V1 datasetV1) - { - JSONObject json = new JSONObject(); - - json.put("schemaVersion", datasetV1.schemaVersion); - json.put("salt", datasetV1.salt); - json.put("userIDs", datasetV1.userIDs); - json.put("lastUpdate", datasetV1.lastUpdate); - json.put("timeout", datasetV1.timeout); - json.put("publicKey", datasetV1.publicKey); - json.put("active", datasetV1.active); - json.put("revoked", datasetV1.revoked); - json.put("guid", datasetV1.guid); - json.put("defaults", datasetV1.defaults); - //json.put("legacyIDs", datasetV1.legacyIDs); - - return json; - } - - public boolean validateSchema(JSONObject json) throws DatasetIntegrityException { - - try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("schema_v1.json")) { - JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream)); - Schema schema = SchemaLoader.load(rawSchema); - schema.validate(json); - } catch (IOException e) { - e.printStackTrace(); - } catch (ValidationException e) { - throw new DatasetIntegrityException("Dataset does not validate against JSON Schema"); - } - return true; - } - - //checking integrity is for checking the value of each variable inside dataset, if its correct or not. - public boolean checkIntegrity(JSONObject json) throws DatasetIntegrityException - { - Dataset_V1 datasetV1 = Dataset_V1.createFromJSONObject(json); - if (datasetV1.getGUID().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'guid' missing"); - if (!datasetV1.getGUID().equals(GUID.createGUID(datasetV1.getPublicKey(), datasetV1.getSalt()))) - throw new DatasetIntegrityException("illegal parameter value..."); - - if (datasetV1.getLastUpdate().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'lastUpdate' missing"); - if (!XSDDateTime.validateXSDDateTime(datasetV1.getLastUpdate())) - throw new DatasetIntegrityException("invalid 'DateTime' format..."); - - if(datasetV1.getTimeout().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'timeout' missing"); - if (!XSDDateTime.validateXSDDateTime(datasetV1.getTimeout())) - throw new DatasetIntegrityException("invalid 'DateTime' format..."); - - if (datasetV1.getPublicKey().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'publicKey' missing"); - String stringtobechecked = datasetV1.getPublicKey().substring(26, datasetV1.getPublicKey().length()-24); - if (!Base64.isArrayByteBase64(stringtobechecked.getBytes())) - throw new DatasetIntegrityException("invalid 'PublicKey' character set..."); - - if (datasetV1.getSalt().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'salt' missing"); - if (!Base64.isArrayByteBase64(datasetV1.getSalt().getBytes())) - throw new DatasetIntegrityException("invalid 'Salt' character set..."); - - String isactive = Integer.toString(datasetV1.getActive()); - if(isactive.isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'Active' missing"); - if(datasetV1.getActive() != 0 && datasetV1.getActive() != 1) - throw new DatasetIntegrityException("invalid 'Active' value..."); - - String isrevoked = Integer.toString(datasetV1.getRevoked()); - if(isrevoked.isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'revoked' missing"); - if(datasetV1.getRevoked() != 0 && datasetV1.getRevoked() != 1) - throw new DatasetIntegrityException("invalid 'Revoked' value..."); - - /*if(getUserIDs().length() == 0) - throw new DatasetIntegrityException("mandatory parameter 'userIDs' missing"); - - String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; - Pattern pattern = Pattern.compile(regex); - for(int n = 0; n < datasetV1.getUserIDs().length(); n++) - { - JSONObject object = datasetV1.getUserIDs().getJSONObject(n); - String uid = object.getString("uID"); - String domain = object.getString("domain"); - String userID = uid + "@" + domain; - Matcher matcher = pattern.matcher(userID); - if(!matcher.matches()) - throw new DatasetIntegrityException("invalid 'UserID' value for " + userID); - } - */ - return true; - } +public class Dataset_V1 extends AbstractDataset +{ + public Dataset_V1() + { + super(); + } + + public static Dataset_V1 createFromJSONObject(JSONObject json) + { + Dataset_V1 datasetV1 = new Dataset_V1(); + + datasetV1.setSchemaVersion(json.getInt("schemaVersion")); + datasetV1.setActive(json.getInt("active")); + datasetV1.setRevoked(json.getInt("revoked")); + datasetV1.setGUID(json.getString("guid")); + datasetV1.setPublicKey(json.getString("publicKey")); + datasetV1.setSalt(json.getString("salt")); + datasetV1.setUserIDs(json.getJSONArray("userIDs")); + datasetV1.setLastUpdate(json.getString("lastUpdate")); + datasetV1.setTimeout(json.getString("timeout")); + datasetV1.setDefaults(json.getJSONObject("defaults")); + + /* + * try { datasetV1.validateSchema(json); datasetV1.checkIntegrity(json); + * } catch (DatasetIntegrityException e) { // TODO handle errors like + * this return null; } + */ + return datasetV1; + } + + public static JSONObject exportJSONObject(Dataset_V1 datasetV1) + { + JSONObject json = new JSONObject(); + + json.put("schemaVersion", datasetV1.schemaVersion); + json.put("salt", datasetV1.salt); + json.put("userIDs", datasetV1.userIDs); + json.put("lastUpdate", datasetV1.lastUpdate); + json.put("timeout", datasetV1.timeout); + json.put("publicKey", datasetV1.publicKey); + json.put("active", datasetV1.active); + json.put("revoked", datasetV1.revoked); + json.put("guid", datasetV1.guid); + json.put("defaults", datasetV1.defaults); + // json.put("legacyIDs", datasetV1.legacyIDs); + + return json; + } + + public boolean validateSchema(JSONObject json) throws DatasetIntegrityException + { + + try(InputStream inputStream = getClass().getClassLoader().getResourceAsStream("schema_v1.json")) + { + JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream)); + Schema schema = SchemaLoader.load(rawSchema); + schema.validate(json); + } + catch(IOException e) + { + e.printStackTrace(); + } + catch(ValidationException e) + { + throw new DatasetIntegrityException("Dataset does not validate against JSON Schema"); + } + return true; + } + + // checking integrity is for checking the value of each variable inside + // dataset, if its correct or not. + public boolean checkIntegrity(JSONObject json) throws DatasetIntegrityException + { + Dataset_V1 datasetV1 = Dataset_V1.createFromJSONObject(json); + if(datasetV1.getGUID().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'guid' missing"); + if(!datasetV1.getGUID().equals(GUID.createGUID(datasetV1.getPublicKey(), datasetV1.getSalt()))) + throw new DatasetIntegrityException("illegal parameter value..."); + + if(datasetV1.getLastUpdate().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'lastUpdate' missing"); + if(!XSDDateTime.validateXSDDateTime(datasetV1.getLastUpdate())) + throw new DatasetIntegrityException("invalid 'DateTime' format..."); + + if(datasetV1.getTimeout().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'timeout' missing"); + if(!XSDDateTime.validateXSDDateTime(datasetV1.getTimeout())) + throw new DatasetIntegrityException("invalid 'DateTime' format..."); + + if(datasetV1.getPublicKey().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'publicKey' missing"); + String stringtobechecked = datasetV1.getPublicKey().substring(26, datasetV1.getPublicKey().length() - 24); + if(!Base64.isArrayByteBase64(stringtobechecked.getBytes())) + throw new DatasetIntegrityException("invalid 'PublicKey' character set..."); + + if(datasetV1.getSalt().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'salt' missing"); + if(!Base64.isArrayByteBase64(datasetV1.getSalt().getBytes())) + throw new DatasetIntegrityException("invalid 'Salt' character set..."); + + String isactive = Integer.toString(datasetV1.getActive()); + if(isactive.isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'Active' missing"); + if(datasetV1.getActive() != 0 && datasetV1.getActive() != 1) + throw new DatasetIntegrityException("invalid 'Active' value..."); + + String isrevoked = Integer.toString(datasetV1.getRevoked()); + if(isrevoked.isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'revoked' missing"); + if(datasetV1.getRevoked() != 0 && datasetV1.getRevoked() != 1) + throw new DatasetIntegrityException("invalid 'Revoked' value..."); + + /* + * if(getUserIDs().length() == 0) throw new + * DatasetIntegrityException("mandatory parameter 'userIDs' missing"); + * + * String regex = + * "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + * Pattern pattern = Pattern.compile(regex); for(int n = 0; n < + * datasetV1.getUserIDs().length(); n++) { JSONObject object = + * datasetV1.getUserIDs().getJSONObject(n); String uid = + * object.getString("uID"); String domain = object.getString("domain"); + * String userID = uid + "@" + domain; Matcher matcher = + * pattern.matcher(userID); if(!matcher.matches()) throw new + * DatasetIntegrityException("invalid 'UserID' value for " + userID); } + */ + return true; + } } diff --git a/src/main/java/eu/rethink/globalregistry/model/Dataset_V2.java b/src/main/java/eu/rethink/globalregistry/model/Dataset_V2.java index d788c39..7f679c1 100644 --- a/src/main/java/eu/rethink/globalregistry/model/Dataset_V2.java +++ b/src/main/java/eu/rethink/globalregistry/model/Dataset_V2.java @@ -17,161 +17,159 @@ /** * Created by Half-Blood on 2/6/2017. */ -public class Dataset_V2 extends AbstractDataset { - - public Dataset_V2(){ - super(); - } - - public JSONArray getLegacyIDs() { - return legacyIDs; - } - - - public void setLegacyIDs(JSONArray legacyIDs) { - this.legacyIDs = legacyIDs; - } - - public static Dataset_V2 createFromJSONObject(JSONObject json) - { - Dataset_V2 datasetV2 = new Dataset_V2(); - - datasetV2.setSchemaVersion(json.getInt("schemaVersion")); - datasetV2.setActive(json.getInt("active")); - datasetV2.setRevoked(json.getInt("revoked")); - datasetV2.setGUID(json.getString("guid")); - datasetV2.setPublicKey(json.getString("publicKey")); - datasetV2.setSalt(json.getString("salt")); - datasetV2.setUserIDs(json.getJSONArray("userIDs")); - datasetV2.setLastUpdate(json.getString("lastUpdate")); - datasetV2.setTimeout(json.getString("timeout")); - datasetV2.setDefaults(json.getJSONObject("defaults")); - datasetV2.setLegacyIDs(json.getJSONArray("legacyIDs")); - - /* - try - { - datasetV2.validateSchema(json); - datasetV2.checkIntegrity(json); - } - catch (DatasetIntegrityException e) - { - // TODO handle errors like this - return null; - } - */ - return datasetV2; - } - - public static JSONObject exportJSONObject(Dataset_V2 datasetV2) - { - JSONObject json = new JSONObject(); - - json.put("schemaVersion", datasetV2.schemaVersion); - json.put("salt", datasetV2.salt); - json.put("userIDs", datasetV2.userIDs); - json.put("lastUpdate", datasetV2.lastUpdate); - json.put("timeout", datasetV2.timeout); - json.put("publicKey", datasetV2.publicKey); - json.put("active", datasetV2.active); - json.put("revoked", datasetV2.revoked); - json.put("guid", datasetV2.guid); - json.put("defaults", datasetV2.defaults); - json.put("legacyIDs", datasetV2.legacyIDs); - - return json; - } - - public boolean validateSchema(JSONObject json) throws DatasetIntegrityException { - - try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("schema_v2.json")) { - JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream)); - Schema schema = SchemaLoader.load(rawSchema); - schema.validate(json); - } catch (IOException e) { - e.printStackTrace(); - } catch (ValidationException e) { - throw new DatasetIntegrityException("Dataset does not validate against JSON Schema"); - } - return true; - } - - //checking integrity is for checking the value of each variable inside dataset, if its correct or not. - public boolean checkIntegrity(JSONObject json) throws DatasetIntegrityException - { - Dataset_V2 datasetV2 = createFromJSONObject(json); - if (datasetV2.getGUID().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'guid' missing"); - if (!datasetV2.getGUID().equals(GUID.createGUID(datasetV2.getPublicKey(), datasetV2.getSalt()))) - throw new DatasetIntegrityException("illegal parameter value..."); - - if (datasetV2.getLastUpdate().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'lastUpdate' missing"); - if (!XSDDateTime.validateXSDDateTime(datasetV2.getLastUpdate())) - throw new DatasetIntegrityException("invalid 'DateTime' format..."); - - if(datasetV2.getTimeout().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'timeout' missing"); - if (!XSDDateTime.validateXSDDateTime(datasetV2.getTimeout())) - throw new DatasetIntegrityException("invalid 'DateTime' format..."); - - if (datasetV2.getPublicKey().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'publicKey' missing"); - String stringtobechecked = datasetV2.getPublicKey().substring(26, datasetV2.getPublicKey().length()-24); - if (!Base64.isArrayByteBase64(stringtobechecked.getBytes())) - throw new DatasetIntegrityException("invalid 'PublicKey' character set..."); - - if (datasetV2.getSalt().isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'salt' missing"); - if (!Base64.isArrayByteBase64(datasetV2.getSalt().getBytes())) - throw new DatasetIntegrityException("invalid 'Salt' character set..."); - - String isactive = Integer.toString(datasetV2.getActive()); - if(isactive.isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'Active' missing"); - if(datasetV2.getActive() != 0 && datasetV2.getActive() != 1) - throw new DatasetIntegrityException("invalid 'Active' value..."); - - String isrevoked = Integer.toString(datasetV2.getRevoked()); - if(isrevoked.isEmpty()) - throw new DatasetIntegrityException("mandatory parameter 'revoked' missing"); - if(datasetV2.getRevoked() != 0 && datasetV2.getRevoked() != 1) - throw new DatasetIntegrityException("invalid 'Revoked' value..."); - - /*if(getUserIDs().length() == 0) +public class Dataset_V2 extends AbstractDataset +{ + public Dataset_V2() + { + super(); + } + + public JSONArray getLegacyIDs() + { + return legacyIDs; + } + + public void setLegacyIDs(JSONArray legacyIDs) + { + this.legacyIDs = legacyIDs; + } + + public static Dataset_V2 createFromJSONObject(JSONObject json) + { + Dataset_V2 datasetV2 = new Dataset_V2(); + + datasetV2.setSchemaVersion(json.getInt("schemaVersion")); + datasetV2.setActive(json.getInt("active")); + datasetV2.setRevoked(json.getInt("revoked")); + datasetV2.setGUID(json.getString("guid")); + datasetV2.setPublicKey(json.getString("publicKey")); + datasetV2.setSalt(json.getString("salt")); + datasetV2.setUserIDs(json.getJSONArray("userIDs")); + datasetV2.setLastUpdate(json.getString("lastUpdate")); + datasetV2.setTimeout(json.getString("timeout")); + datasetV2.setDefaults(json.getJSONObject("defaults")); + datasetV2.setLegacyIDs(json.getJSONArray("legacyIDs")); + + /* + * try { datasetV2.validateSchema(json); datasetV2.checkIntegrity(json); + * } catch (DatasetIntegrityException e) { // TODO handle errors like + * this return null; } + */ + return datasetV2; + } + + public static JSONObject exportJSONObject(Dataset_V2 datasetV2) + { + JSONObject json = new JSONObject(); + + json.put("schemaVersion", datasetV2.schemaVersion); + json.put("salt", datasetV2.salt); + json.put("userIDs", datasetV2.userIDs); + json.put("lastUpdate", datasetV2.lastUpdate); + json.put("timeout", datasetV2.timeout); + json.put("publicKey", datasetV2.publicKey); + json.put("active", datasetV2.active); + json.put("revoked", datasetV2.revoked); + json.put("guid", datasetV2.guid); + json.put("defaults", datasetV2.defaults); + json.put("legacyIDs", datasetV2.legacyIDs); + + return json; + } + + public boolean validateSchema(JSONObject json) throws DatasetIntegrityException + { + + try(InputStream inputStream = getClass().getClassLoader().getResourceAsStream("schema_v2.json")) + { + JSONObject rawSchema = new JSONObject(new JSONTokener(inputStream)); + Schema schema = SchemaLoader.load(rawSchema); + schema.validate(json); + } + catch(IOException e) + { + e.printStackTrace(); + } + catch(ValidationException e) + { + throw new DatasetIntegrityException("Dataset does not validate against JSON Schema"); + } + return true; + } + + // checking integrity is for checking the value of each variable inside + // dataset, if its correct or not. + public boolean checkIntegrity(JSONObject json) throws DatasetIntegrityException + { + Dataset_V2 datasetV2 = createFromJSONObject(json); + if(datasetV2.getGUID().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'guid' missing"); + if(!datasetV2.getGUID().equals(GUID.createGUID(datasetV2.getPublicKey(), datasetV2.getSalt()))) + throw new DatasetIntegrityException("illegal parameter value..."); + + if(datasetV2.getLastUpdate().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'lastUpdate' missing"); + if(!XSDDateTime.validateXSDDateTime(datasetV2.getLastUpdate())) + throw new DatasetIntegrityException("invalid 'DateTime' format..."); + + if(datasetV2.getTimeout().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'timeout' missing"); + if(!XSDDateTime.validateXSDDateTime(datasetV2.getTimeout())) + throw new DatasetIntegrityException("invalid 'DateTime' format..."); + + if(datasetV2.getPublicKey().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'publicKey' missing"); + String stringtobechecked = datasetV2.getPublicKey().substring(26, datasetV2.getPublicKey().length() - 24); + if(!Base64.isArrayByteBase64(stringtobechecked.getBytes())) + throw new DatasetIntegrityException("invalid 'PublicKey' character set..."); + + if(datasetV2.getSalt().isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'salt' missing"); + if(!Base64.isArrayByteBase64(datasetV2.getSalt().getBytes())) + throw new DatasetIntegrityException("invalid 'Salt' character set..."); + + String isactive = Integer.toString(datasetV2.getActive()); + if(isactive.isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'Active' missing"); + if(datasetV2.getActive() != 0 && datasetV2.getActive() != 1) + throw new DatasetIntegrityException("invalid 'Active' value..."); + + String isrevoked = Integer.toString(datasetV2.getRevoked()); + if(isrevoked.isEmpty()) + throw new DatasetIntegrityException("mandatory parameter 'revoked' missing"); + if(datasetV2.getRevoked() != 0 && datasetV2.getRevoked() != 1) + throw new DatasetIntegrityException("invalid 'Revoked' value..."); + + /* + * if(getUserIDs().length() == 0) throw new + * DatasetIntegrityException("mandatory parameter 'userIDs' missing"); + * + * String regex = + * "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; + * Pattern pattern = Pattern.compile(regex); for(int n = 0; n < + * datasetV2.getUserIDs().length(); n++) { JSONObject object = + * datasetV2.getUserIDs().getJSONObject(n); String uid = + * object.getString("uID"); String domain = object.getString("domain"); + * String userID = uid + "@" + domain; Matcher matcher = + * pattern.matcher(userID); if(!matcher.matches()) throw new + * DatasetIntegrityException("invalid 'UserID' value for " + userID); } + */ + + if(datasetV2.getLegacyIDs().length() == 0) throw new DatasetIntegrityException("mandatory parameter 'userIDs' missing"); - - String regex = "^[\\w!#$%&'*+/=?`{|}~^-]+(?:\\.[\\w!#$%&'*+/=?`{|}~^-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,6}$"; - Pattern pattern = Pattern.compile(regex); - for(int n = 0; n < datasetV2.getUserIDs().length(); n++) - { - JSONObject object = datasetV2.getUserIDs().getJSONObject(n); - String uid = object.getString("uID"); - String domain = object.getString("domain"); - String userID = uid + "@" + domain; - Matcher matcher = pattern.matcher(userID); - if(!matcher.matches()) - throw new DatasetIntegrityException("invalid 'UserID' value for " + userID); - } - */ - - if(datasetV2.getLegacyIDs().length() == 0) - throw new DatasetIntegrityException("mandatory parameter 'userIDs' missing"); - for(int n = 0; n < datasetV2.getLegacyIDs().length(); n++) - { - JSONObject object = datasetV2.getLegacyIDs().getJSONObject(n); - String type = object.getString("type"); - String category = object.getString("category"); - String description = object.getString("description"); - String id = object.getString("id"); - if ((!Base64.isArrayByteBase64(type.getBytes())) && - (!Base64.isArrayByteBase64(category.getBytes())) && - (!Base64.isArrayByteBase64(description.getBytes())) && - (!Base64.isArrayByteBase64(id.getBytes()))) - throw new DatasetIntegrityException("invalid 'LegacyID' character set..."); - } - - return true; - } - -} + for(int n = 0; n < datasetV2.getLegacyIDs().length(); n++) + { + JSONObject object = datasetV2.getLegacyIDs().getJSONObject(n); + String type = object.getString("type"); + String category = object.getString("category"); + String description = object.getString("description"); + String id = object.getString("id"); + if((!Base64.isArrayByteBase64(type.getBytes())) && (!Base64.isArrayByteBase64(category.getBytes())) + && (!Base64.isArrayByteBase64(description.getBytes())) + && (!Base64.isArrayByteBase64(id.getBytes()))) + throw new DatasetIntegrityException("invalid 'LegacyID' character set..."); + } + + return true; + } +} \ No newline at end of file diff --git a/src/main/java/eu/rethink/globalregistry/model/GUID.java b/src/main/java/eu/rethink/globalregistry/model/GUID.java index a3d1a5e..30d5fda 100644 --- a/src/main/java/eu/rethink/globalregistry/model/GUID.java +++ b/src/main/java/eu/rethink/globalregistry/model/GUID.java @@ -17,108 +17,112 @@ * @version 1 * @author Sebastian Göndör */ -public class GUID { - private static int ITERATIONS = 10000; - private static String HASH_ALGORITHM = "HmacSHA256"; - - public static String createGUID(String key, String salt) - { - byte[] hash = null; - - key = ECDSAKeyPairManager.stripKey(key); - - try - { - hash = PBKDF2.deriveKey(key.getBytes(), salt.getBytes(), ITERATIONS, 32); // 32 bytes == 256 bit - } - catch (NoSuchAlgorithmException e) - { - e.printStackTrace(); - } - catch (InvalidKeyException e) - { - e.printStackTrace(); - } - //return (new BigInteger(DatatypeConverter.printHexBinary(hash), 16)).toString(36).toUpperCase(); //<-- base36 encoding - return new String(Base64UrlCodec.BASE64URL.encode(hash)); - } - - private static class PBKDF2 - { - public static byte[] deriveKey(byte[] password, byte[] salt, int iterationCount, int dkLen) throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException - { - SecretKeySpec keyspec = new SecretKeySpec(password, HASH_ALGORITHM); - Mac prf = Mac.getInstance(HASH_ALGORITHM); - prf.init(keyspec); - - // Note: hLen, dkLen, l, r, T, F, etc. are horrible names for - // variables and functions in this day and age, but they - // reflect the terse symbols used in RFC 2898 to describe - // the PBKDF2 algorithm, which improves validation of the - // code vs. the RFC. - // - // dklen is expressed in bytes. (16 for a 128-bit key) - - int hLen = prf.getMacLength(); // 20 for SHA1 - int l = Math.max(dkLen, hLen); // 1 for 128bit (16-byte) keys - int r = dkLen - (l - 1) * hLen; // 16 for 128bit (16-byte) keys - byte T[] = new byte[l * hLen]; - int ti_offset = 0; - - for(int i=1; i<=l; i++) - { - F(T, ti_offset, prf, salt, iterationCount, i); - ti_offset += hLen; - } - - if(r < hLen) - { - // Incomplete last block - byte DK[] = new byte[dkLen]; - System.arraycopy(T, 0, DK, 0, dkLen); - return DK; - } - - return T; - } - - - private static void F(byte[] dest, int offset, Mac prf, byte[] S, int c, int blockIndex) - { - final int hLen = prf.getMacLength(); - byte U_r[] = new byte[hLen]; - // U0 = S || INT (i); - byte U_i[] = new byte[S.length + 4]; - - System.arraycopy(S, 0, U_i, 0, S.length); - INT(U_i, S.length, blockIndex); - - for(int i=0; i Date: Wed, 29 Mar 2017 11:54:35 +0200 Subject: [PATCH 2/3] updated Dockerfile --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index d79eb06..49ff361 100644 --- a/Dockerfile +++ b/Dockerfile @@ -13,7 +13,7 @@ WORKDIR /build #ADD *.private.der /build/ #ADD rethink-ca /build/rethink-ca #ADD docker-entrypoint.sh / -ADD target/ReThinkGlobalRegistry-0.3.2.jar app.jar +ADD target/ReThinkGlobalRegistry-0.3.3.jar app.jar #RUN ["mvn", "dependency:resolve"] #RUN ["mvn", "verify"] From ac6d7388be0cf57713a5327fa1d0e23a71a9ef67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20G=C3=B6nd=C3=B6r?= Date: Wed, 29 Mar 2017 11:55:00 +0200 Subject: [PATCH 3/3] fixed small bug --- src/test/java/eu/rethink/globalregistry/tests/DatasetTest.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/test/java/eu/rethink/globalregistry/tests/DatasetTest.java b/src/test/java/eu/rethink/globalregistry/tests/DatasetTest.java index 80ffffe..cb683c2 100644 --- a/src/test/java/eu/rethink/globalregistry/tests/DatasetTest.java +++ b/src/test/java/eu/rethink/globalregistry/tests/DatasetTest.java @@ -43,8 +43,6 @@ public void setUp() @Test public void createDatasetFromJSON() { - data = new JSONObject(); - // TODO create dataset from JSON if(data.getInt("schemaVersion") == 1) {