Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup code #92

Merged
merged 8 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/main/java/org/biscuitsec/biscuit/crypto/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public PublicKey(Algorithm algorithm, EdDSAPublicKey public_key) {

public PublicKey(Algorithm algorithm, byte[] data) {
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(data, ed25519);
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec);
this.key = pubKey;
this.key = new EdDSAPublicKey(pubKeySpec);
this.algorithm = algorithm;
}

Expand All @@ -38,8 +37,7 @@ public String toHex() {
public PublicKey(Algorithm algorithm, String hex) {
byte[] data = Utils.hexStringToByteArray(hex);
EdDSAPublicKeySpec pubKeySpec = new EdDSAPublicKeySpec(data, ed25519);
EdDSAPublicKey pubKey = new EdDSAPublicKey(pubKeySpec);
this.key = pubKey;
this.key = new EdDSAPublicKey(pubKeySpec);
this.algorithm = algorithm;
}

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/biscuitsec/biscuit/datalog/FactSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,14 @@ public Stream stream(TrustedOrigins blockIds) {
})
.flatMap(entry -> entry.getValue()
.stream()
.map(fact -> new Tuple2(entry.getKey(), fact)));
.map(fact -> new Tuple2<>(entry.getKey(), fact)));
}

public Stream<Fact> stream() {
return facts.entrySet()
.stream()
.flatMap(entry -> entry.getValue()
.stream()
.map(fact -> fact));
.stream());
}


Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/biscuitsec/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public Stream<Either<Error, Tuple2<Origin, Fact>>> apply(
if (term instanceof Term.Bool) {
Term.Bool b = (Term.Bool) term;
if (!b.value()) {
return Either.right(new Tuple3(origin, generatedVariables, false));
return Either.right(new Tuple3<>(origin, generatedVariables, false));
}
// continue evaluating if true
} else {
Expand All @@ -72,11 +72,11 @@ public Stream<Either<Error, Tuple2<Origin, Fact>>> apply(
}

}
return Either.right(new Tuple3(origin, generatedVariables, true));
return Either.right(new Tuple3<>(origin, generatedVariables, true));
})
// sometimes we need to make the compiler happy
.filter((java.util.function.Predicate<? super Either<? extends Object, ? extends Object>>)
res -> res.isRight() & ((Tuple3<Origin, Map<Long, Term>, Boolean>) res.get())._3.booleanValue()).map(res -> {
res -> res.isRight() & ((Tuple3<Origin, Map<Long, Term>, Boolean>) res.get())._3).map(res -> {
Tuple3<Origin, Map<Long, Term>, Boolean> t = (Tuple3<Origin, Map<Long, Term>, Boolean>) res.get();
Origin origin = t._1;
Map<Long, Term> generatedVariables = t._2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public String fromEpochIsoDate(long epochSec) {
}

/**
* Due to https://github.com/biscuit-auth/biscuit/blob/master/SPECIFICATIONS.md#symbol-table,
* According to <a href="https://github.com/biscuit-auth/biscuit/blob/master/SPECIFICATIONS.md#symbol-table">the specification</a>,
* We need two symbols tables:
* * one for the defaults symbols indexed from 0 et 1023 in <code>defaultSymbols</code> list
* * one for the usages symbols indexed from 1024 in <code>symbols</code> list
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/org/biscuitsec/biscuit/datalog/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public void run(RunLimits limits, final SymbolTable symbols) throws Error {
Tuple2<Origin, Fact> t2 = res.get();
newFacts.add(t2._1, t2._2);
} else {
Error e = res.getLeft();
throw e;
throw res.getLeft();
}
}
}
Expand Down Expand Up @@ -97,8 +96,7 @@ public final FactSet query_rule(final Rule rule, Long origin, TrustedOrigins sco
Tuple2<Origin, Fact> t2 = res.get();
newFacts.add(t2._1, t2._2);
} else {
Error e = res.getLeft();
throw e;
throw res.getLeft();
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/biscuitsec/biscuit/token/Authorizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -641,15 +641,15 @@ public RuleSet rules() {
public List<Tuple2<Long, List<Check>>> checks() {
List<Tuple2<Long, List<Check>>> allChecks = new ArrayList<>();
if(!this.checks.isEmpty()) {
allChecks.add(new Tuple2(Long.MAX_VALUE, this.checks));
allChecks.add(new Tuple2<>(Long.MAX_VALUE, this.checks));
}

List<Check> authorityChecks = new ArrayList<>();
for(org.biscuitsec.biscuit.datalog.Check check: this.token.authority.checks) {
authorityChecks.add(Check.convert_from(check, this.token.symbols));
}
if(!authorityChecks.isEmpty()) {
allChecks.add(new Tuple2((long) 0, authorityChecks));
allChecks.add(new Tuple2<>((long) 0, authorityChecks));
}

long count = 1;
Expand All @@ -667,7 +667,7 @@ public List<Tuple2<Long, List<Check>>> checks() {
}
}
if(!blockChecks.isEmpty()) {
allChecks.add(new Tuple2(count, blockChecks));
allChecks.add(new Tuple2<>(count, blockChecks));
}
count += 1;
}
Expand All @@ -678,4 +678,4 @@ public List<Tuple2<Long, List<Check>>> checks() {
public List<Policy> policies() {
return this.policies;
}
}
}
6 changes: 2 additions & 4 deletions src/main/java/org/biscuitsec/biscuit/token/Biscuit.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ static private Biscuit make(final SecureRandom rng, final KeyPair root, final Op

Either<Error.FormatError, SerializedBiscuit> container = SerializedBiscuit.make(root, root_key_id, authority, next);
if (container.isLeft()) {
Error.FormatError e = container.getLeft();
throw e;
throw container.getLeft();
} else {
SerializedBiscuit s = container.get();
List<byte[]> revocation_ids = s.revocation_identifiers();
Expand Down Expand Up @@ -332,8 +331,7 @@ public Biscuit attenuate(final SecureRandom rng, final KeyPair keypair, Block bl

Either<Error.FormatError, SerializedBiscuit> containerRes = copiedBiscuit.serializedBiscuit.append(keypair, block);
if (containerRes.isLeft()) {
Error.FormatError error = containerRes.getLeft();
throw error;
throw containerRes.getLeft();
}
SerializedBiscuit container = containerRes.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ public UnverifiedBiscuit attenuate(final SecureRandom rng, final KeyPair keypair

Either<Error.FormatError, SerializedBiscuit> containerRes = copiedBiscuit.serializedBiscuit.append(keypair, block);
if (containerRes.isLeft()) {
Error.FormatError error = containerRes.getLeft();
throw error;
throw containerRes.getLeft();
}
SerializedBiscuit container = containerRes.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public Predicate clone(){
String name = this.name;
List<Term> terms = new ArrayList<Term>(this.terms.size());
terms.addAll(this.terms);
Predicate p = new Predicate(name, terms);
return p;
return new Predicate(name, terms);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -368,18 +368,18 @@ public static Either<Error, Tuple2<String, List<Scope>>> scopes(String s) {
}
}

return Either.right(new Tuple2(s, scopes));
return Either.right(new Tuple2<>(s, scopes));
}

public static Either<Error, Tuple2<String, Scope>> scope(String s) {
if (s.startsWith("authority")) {
s = s.substring("authority".length());
return Either.right(new Tuple2(s, Scope.authority()));
return Either.right(new Tuple2<>(s, Scope.authority()));
}

if (s.startsWith("previous")) {
s = s.substring("previous".length());
return Either.right(new Tuple2(s, Scope.previous()));
return Either.right(new Tuple2<>(s, Scope.previous()));
}

if (0 < s.length() && s.charAt(0) == '{') {
Expand All @@ -390,7 +390,7 @@ public static Either<Error, Tuple2<String, Scope>> scope(String s) {
}
Tuple2<String, String> t = res.get();
if (0 < s.length() && s.charAt(0) == '}') {
return Either.right(new Tuple2(t._1, Scope.parameter(t._2)));
return Either.right(new Tuple2<>(t._1, Scope.parameter(t._2)));
} else {
return Either.left(new Error(s, "unrecognized parameter end"));
}
Expand All @@ -401,7 +401,7 @@ public static Either<Error, Tuple2<String, Scope>> scope(String s) {
return Either.left(new Error(s, "unrecognized public key"));
}
Tuple2<String, PublicKey> t = res2.get();
return Either.right(new Tuple2(t._1, Scope.publicKey(t._2)));
return Either.right(new Tuple2<>(t._1, Scope.publicKey(t._2)));
}

public static Either<Error, Tuple2<String, PublicKey>> publicKey(String s) {
Expand All @@ -411,7 +411,7 @@ public static Either<Error, Tuple2<String, PublicKey>> publicKey(String s) {

s = s.substring("ed25519/".length());
Tuple2<String, byte[]> t = hex(s);
return Either.right(new Tuple2(t._1, new PublicKey(Schema.PublicKey.Algorithm.Ed25519, t._2)));
return Either.right(new Tuple2<>(t._1, new PublicKey(Schema.PublicKey.Algorithm.Ed25519, t._2)));
}

public static Either<Error, Tuple2<String, Predicate>> fact_predicate(String s) {
Expand Down Expand Up @@ -470,43 +470,43 @@ public static Either<Error, Tuple2<String, Term>> term(String s) {
Either<Error, Tuple2<String, Term.Variable>> res5 = variable(s);
if (res5.isRight()) {
Tuple2<String, Term.Variable> t = res5.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Str>> res2 = string(s);
if (res2.isRight()) {
Tuple2<String, Term.Str> t = res2.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Set>> res7 = set(s);
if (res7.isRight()) {
Tuple2<String, Term.Set> t = res7.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Bool>> res6 = bool(s);
if (res6.isRight()) {
Tuple2<String, Term.Bool> t = res6.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Date>> res4 = date(s);
if (res4.isRight()) {
Tuple2<String, Term.Date> t = res4.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Integer>> res3 = integer(s);
if (res3.isRight()) {
Tuple2<String, Term.Integer> t = res3.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Bytes>> res8 = bytes(s);
if (res8.isRight()) {
Tuple2<String, Term.Bytes> t = res8.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

return Either.left(new Error(s, "unrecognized value"));
Expand All @@ -520,37 +520,37 @@ public static Either<Error, Tuple2<String, Term>> fact_term(String s) {
Either<Error, Tuple2<String, Term.Str>> res2 = string(s);
if (res2.isRight()) {
Tuple2<String, Term.Str> t = res2.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Set>> res7 = set(s);
if (res7.isRight()) {
Tuple2<String, Term.Set> t = res7.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Bool>> res6 = bool(s);
if (res6.isRight()) {
Tuple2<String, Term.Bool> t = res6.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Date>> res4 = date(s);
if (res4.isRight()) {
Tuple2<String, Term.Date> t = res4.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Integer>> res3 = integer(s);
if (res3.isRight()) {
Tuple2<String, Term.Integer> t = res3.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

Either<Error, Tuple2<String, Term.Bytes>> res8 = bytes(s);
if (res8.isRight()) {
Tuple2<String, Term.Bytes> t = res8.get();
return Either.right(new Tuple2(t._1, t._2));
return Either.right(new Tuple2<>(t._1, t._2));
}

return Either.left(new Error(s, "unrecognized value"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,8 @@ static SerializedBiscuit from_bytes_inner(Schema.Biscuit data, org.biscuitsec.bi

Either<Error, Void> res = b.verify(root);
if (res.isLeft()) {
Error e = res.getLeft();
//System.out.println("verification error: "+e.toString());
throw e;
throw res.getLeft();
} else {
return b;
}
Expand Down Expand Up @@ -164,8 +163,7 @@ static private SerializedBiscuit deserialize(Schema.Biscuit data) throws Error.F
}
Proof proof = new Proof(secretKey, signature);

SerializedBiscuit b = new SerializedBiscuit(authority, blocks, proof);
return b;
return new SerializedBiscuit(authority, blocks, proof);
}


Expand Down Expand Up @@ -219,8 +217,7 @@ public byte[] serialize() throws Error.FormatError.SerializationError {
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
biscuit.writeTo(stream);
byte[] data = stream.toByteArray();
return data;
return stream.toByteArray();
} catch (IOException e) {
throw new Error.FormatError.SerializationError(e.toString());
}
Expand Down Expand Up @@ -462,8 +459,7 @@ public Tuple3<Block, ArrayList<Block>, HashMap<Long, List<Long>>> extractBlocks(
ArrayList<Option<org.biscuitsec.biscuit.crypto.PublicKey>> blockExternalKeys = new ArrayList<>();
Either<Error.FormatError, Block> authRes = Block.from_bytes(this.authority.block, Option.none());
if (authRes.isLeft()) {
Error e = authRes.getLeft();
throw e;
throw authRes.getLeft();
}
Block authority = authRes.get();
for(org.biscuitsec.biscuit.crypto.PublicKey pk: authority.publicKeys()) {
Expand All @@ -483,8 +479,7 @@ public Tuple3<Block, ArrayList<Block>, HashMap<Long, List<Long>>> extractBlocks(
}
Either<Error.FormatError, Block> blockRes = Block.from_bytes(bdata.block, externalKey);
if (blockRes.isLeft()) {
Error e = blockRes.getLeft();
throw e;
throw blockRes.getLeft();
}
Block block = blockRes.get();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void testRuleWithScope() {
Arrays.asList(
Utils.pred("resource", List.of(Utils.string("file1")))
),
new ArrayList(),
new ArrayList<>(),
Arrays.asList(
Scope.publicKey(new PublicKey(Schema.PublicKey.Algorithm.Ed25519, "6e9e6d5a75cf0c0e87ec1256b4dfed0ca3ba452912d213fcc70f8516583db9db")),
Scope.authority()
Expand Down Expand Up @@ -511,4 +511,4 @@ void testDatalogRemoveComment() throws org.biscuitsec.biscuit.error.Error.Parser
Either<Map<Integer, List<Error>>, Block> output = Parser.datalog(1, toParse);
assertTrue(output.isRight());
}
}
}
12 changes: 5 additions & 7 deletions src/test/java/org/biscuitsec/biscuit/token/SamplesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -382,18 +382,16 @@ public World(Authorizer authorizer) {

this.rules = rulesets;

List<CheckSet> checksets = authorizer.checks().stream()
this.checks = authorizer.checks().stream()
.map((Tuple2<Long, List<Check>> t) -> {
List<String> checks = t._2.stream().map(c -> c.toString()).collect(Collectors.toList());
Collections.sort(checks);
List<String> checks1 = t._2.stream().map(c -> c.toString()).collect(Collectors.toList());
Collections.sort(checks1);
if(t._1 == null) {
return new CheckSet(checks);
return new CheckSet(checks1);
} else {
return new CheckSet(t._1, checks);
return new CheckSet(t._1, checks1);
}
}).collect(Collectors.toList());

this.checks = checksets;
this.policies = authorizer.policies().stream().map(p -> p.toString()).collect(Collectors.toList());
Collections.sort(this.rules);
Collections.sort(this.checks);
Expand Down
Loading