Skip to content

Commit

Permalink
Merge pull request #2 from JavaWebStack/dev
Browse files Browse the repository at this point in the history
Release 1.0.1
  • Loading branch information
JanHolger authored Oct 7, 2022
2 parents 7778c20 + 1d6819c commit 134cfef
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target
dependency-reduced-pom.xml
.idea
*.iml
*.iml
.env
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ JWS Utilties
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>web-utils</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
</dependency>
```
11 changes: 3 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<buildVersion>1.0.0-SNAPSHOT</buildVersion>
<buildVersion>1.0.1-SNAPSHOT</buildVersion>
</properties>

<groupId>org.javawebstack</groupId>
Expand Down Expand Up @@ -44,22 +44,17 @@
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>orm</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.javawebstack</groupId>
<artifactId>http-server</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.1</version>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
54 changes: 41 additions & 13 deletions src/main/java/org/javawebstack/webutils/config/Config.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package org.javawebstack.webutils.config;

import org.javawebstack.abstractdata.AbstractElement;
import org.javawebstack.abstractdata.AbstractObject;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Config {

Expand All @@ -11,42 +17,41 @@ public Config set(String path, String key, String value) {
return set(prefix + key.toLowerCase(Locale.ROOT), value);
}

public Config add(String path, Map<String, String> data, Map<String, String> mapping) {
public Config add(String path, Map<String, String> data, Function<String, String> mapping) {
data.forEach((key, value) -> {
if(mapping != null && mapping.containsKey(key))
key = mapping.get(key);
set(path, key, value);
key = mapping.apply(key);
if(key != null)
set(path, key, value);
});
return this;
}

public Config add(Map<String, String> data, Map<String, String> mapping) {
return add(null, data, null);
public Config add(Map<String, String> data, Function<String, String> mapping) {
return add(null, data, mapping);
}

public Config add(String path, Map<String, String> data) {
return add(path, data, null);
return add(path, data, k -> k);
}

public Config add(Map<String, String> data) {
return add(null, data, null);
return add(null, data, k -> k);
}

public Config add(String path, EnvFile envFile, Map<String, String> mapping) {
public Config add(String path, EnvFile envFile, Function<String, String> mapping) {
return add(path, envFile.getValues(), mapping);
}

public Config add(String path, EnvFile envFile) {
return add(path, envFile, null);
return add(path, envFile, k -> k);
}

public Config add(EnvFile envFile, Map<String, String> mapping) {
public Config add(EnvFile envFile, Function<String, String> mapping) {
return add(null, envFile, mapping);

}

public Config add(EnvFile envFile) {
return add(null, envFile, null);
return add(null, envFile, k -> k);
}

public Config set(String key, String value) {
Expand All @@ -64,6 +69,21 @@ public String get(String key) {
return get(key, null);
}

public AbstractObject getObject(String key) {
String prefix = key.length() > 0 ? (key + ".") : "";
Set<String> s = config.keySet().stream().filter(k -> k.startsWith(prefix)).collect(Collectors.toSet());
if(s.size() == 0)
return null;
return AbstractElement.fromTree(s.stream().collect(Collectors.toMap(k -> k.substring(prefix.length()).split("\\."), config::get))).object();
}

public Config set(String key, AbstractObject object) {
String prefix = key.length() > 0 ? (key + ".") : "";
config.keySet().stream().filter(k -> k.startsWith(prefix)).forEach(config::remove);
object.toTree().forEach((k, v) -> config.put(prefix + String.join(".", k), v.toString()));
return this;
}

public int getInt(String key, int defaultValue) {
String value = get(key);
if (value == null)
Expand All @@ -88,4 +108,12 @@ public boolean has(String key) {
return config.containsKey(key);
}

public Set<String> keys() {
return config.keySet();
}

public static String basicEnvMapping(String k) {
return k.toLowerCase(Locale.ROOT).replace("_", ".");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.javawebstack.webutils.config;

import java.util.Locale;
import java.util.function.Function;

public class EnvAssocMapping implements Function<String, String> {

private final String prefix;
private final Function<String, String> subMapping;

public EnvAssocMapping(String prefix, Function<String, String> subMapping) {
this.prefix = prefix;
this.subMapping = subMapping;
}

public String apply(String s) {
if(!s.startsWith(prefix+ "_"))
return null;
String[] spl = s.substring(prefix.length() + 1).split("_", 2);
if(spl.length != 2)
return null;
String sk = subMapping.apply(spl[1]);
if(sk == null)
return null;
return prefix.toLowerCase(Locale.ROOT) + "." + spl[0].toLowerCase(Locale.ROOT) + "." + sk;
}

}
26 changes: 26 additions & 0 deletions src/main/java/org/javawebstack/webutils/config/Mapping.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.javawebstack.webutils.config;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class Mapping implements Function<String, String> {

private final Map<String, String> mapping = new HashMap<>();
private boolean failIfNotFound = false;

public Mapping failIfNotFound(boolean failIfNotFound) {
this.failIfNotFound = failIfNotFound;
return this;
}

public Mapping map(String from, String to) {
mapping.put(from, to);
return this;
}

public String apply(String s) {
return mapping.getOrDefault(s, failIfNotFound ? null : s);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.javawebstack.webutils.config;

import java.util.function.Function;

public class MappingPipeline implements Function<String, String> {

private final Function<String, String>[] pipeline;

public MappingPipeline(Function<String, String>... pipeline) {
this.pipeline = pipeline;
}

public String apply(String s) {
String k = s;
for(Function<String, String> mapping : pipeline)
k = mapping.apply(k);
return k == null ? s : k;
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/javawebstack/webutils/config/MappingTryout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.javawebstack.webutils.config;

import java.util.function.Function;

public class MappingTryout implements Function<String, String> {

private final Function<String, String>[] tryout;
private boolean failIfEqual = true;

public MappingTryout(Function<String, String>... tryout) {
this.tryout = tryout;
}

public MappingTryout failIfEqual(boolean failIfEqual) {
this.failIfEqual = failIfEqual;
return this;
}

public String apply(String s) {
for(Function<String, String> mapping : tryout) {
String k = mapping.apply(s);
if(k != null && (!failIfEqual || !k.equals(s)))
return k;
}
return null;
}

}
23 changes: 11 additions & 12 deletions src/main/java/org/javawebstack/webutils/crypt/Crypt.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.javawebstack.webutils.crypt;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import org.javawebstack.abstractdata.AbstractElement;
import org.javawebstack.abstractdata.AbstractObject;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
Expand Down Expand Up @@ -84,11 +83,11 @@ public String encrypt(byte[] data) {
cipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] value = cipher.doFinal(data);
byte[] mac = mac(Base64.getEncoder().encode(iv), Base64.getEncoder().encode(value));
Map<String, String> cryptData = new HashMap<>();
cryptData.put("iv", new String(Base64.getEncoder().encode(iv)));
cryptData.put("value", new String(Base64.getEncoder().encode(value)));
cryptData.put("mac", toHex(mac));
return new String(Base64.getEncoder().encode(new GsonBuilder().disableHtmlEscaping().create().toJson(cryptData).getBytes(StandardCharsets.UTF_8)));
AbstractObject cryptData = new AbstractObject();
cryptData.set("iv", new String(Base64.getEncoder().encode(iv)));
cryptData.set("value", new String(Base64.getEncoder().encode(value)));
cryptData.set("mac", toHex(mac));
return new String(Base64.getEncoder().encode(cryptData.toJsonString().getBytes(StandardCharsets.UTF_8)));
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException ex) {
throw new SecurityException(ex.getMessage());
}
Expand Down Expand Up @@ -120,11 +119,11 @@ public String decryptString(String data) {

public byte[] decrypt(String data) {
try {
JsonObject json = new Gson().fromJson(new String(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8), JsonObject.class);
if (!toHex(mac(json.get("iv").getAsString().getBytes(StandardCharsets.UTF_8), json.get("value").getAsString().getBytes(StandardCharsets.UTF_8))).equals(json.get("mac").getAsString()))
AbstractObject json = AbstractElement.fromJson(new String(Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)), StandardCharsets.UTF_8)).object();
if (!toHex(mac(json.string("iv").getBytes(StandardCharsets.UTF_8), json.string("value").getBytes(StandardCharsets.UTF_8))).equals(json.string("mac")))
throw new SecurityException("Invalid MAC");
byte[] iv = Base64.getDecoder().decode(json.get("iv").getAsString().getBytes(StandardCharsets.UTF_8));
byte[] value = Base64.getDecoder().decode(json.get("value").getAsString().getBytes(StandardCharsets.UTF_8));
byte[] iv = Base64.getDecoder().decode(json.string("iv").getBytes(StandardCharsets.UTF_8));
byte[] value = Base64.getDecoder().decode(json.string("value").getBytes(StandardCharsets.UTF_8));
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.javawebstack.webutils.middleware;

import org.javawebstack.httpserver.Exchange;
import org.javawebstack.httpserver.HTTPMethod;
import org.javawebstack.httpserver.handler.RequestInterceptor;
import org.javawebstack.httpserver.helper.HttpMethod;

public class CORSPolicy implements RequestInterceptor {
private final String allowedOrigin;
Expand All @@ -13,7 +13,7 @@ public CORSPolicy(String allowedOrigin) {

public boolean intercept(Exchange exchange) {
exchange.header("Access-Control-Allow-Origin", allowedOrigin);
if (exchange.getMethod() == HttpMethod.OPTIONS) {
if (exchange.getMethod() == HTTPMethod.OPTIONS) {
exchange.header("Access-Control-Allow-Headers", "*");
exchange.header("Access-Control-Allow-Methods", "*");
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public MultipartPolicy(String location) {
}

public boolean intercept(Exchange exchange) {
/*
if (location != null)
exchange.enableMultipart(location);
*/
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public RateLimitMiddleware(long millis){
}

public Object handle(Exchange exchange) {
String ip = exchange.rawRequest().getRemoteAddr();
String ip = exchange.socket().getRemoteAddress();
if (exchange.header("X-Forwarded-For") != null)
ip = exchange.header("X-Forwarded-For");
RateLimit rateLimit = null;
Expand Down

0 comments on commit 134cfef

Please sign in to comment.