Skip to content

Commit

Permalink
parse and display scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
Geal committed Jan 1, 2024
1 parent 834f588 commit 35781d6
Show file tree
Hide file tree
Showing 8 changed files with 252 additions and 30 deletions.
5 changes: 5 additions & 0 deletions src/main/java/com/clevercloud/biscuit/crypto/PublicKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,9 @@ public boolean equals(Object o) {
public int hashCode() {
return key.hashCode();
}

@Override
public String toString() {
return "ed25519/" + toHex();
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/clevercloud/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ public boolean check_match_all(final Set<Fact> facts, SymbolTable symbols) {
}
}

public Rule(final Predicate head, final List<Predicate> body, final List<Expression> expressions,
public Rule(final Predicate head, final List<Predicate> body, final List<Expression> expressions) {
this.head = head;
this.body = body;
this.expressions = expressions;
this.scopes = new ArrayList<>();
}

public Rule(final Predicate head, final List<Predicate> body, final List<Expression> expressions,
final List<Scope> scopes) {
this.head = head;
this.body = body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public String toString() {
res += ", "+ String.join(", ", e);
}

if(!q.scopes.isEmpty()) {
final List<String> e = q.scopes.stream().map((scope) -> scope.toString()).collect(Collectors.toList());
res += " trusting " + String.join(", ", e);
}

return res;
}).collect(Collectors.toList());

Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/clevercloud/biscuit/token/builder/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,14 @@ public Either<String, Rule> validate_variables() {
return Stream.of(((Term.Variable) t).value);
} else return Stream.empty();
}).collect(Collectors.toSet());

for(Expression e: this.expressions) {
e.gatherVariables(free_variables);
}
if (free_variables.isEmpty()) {
return Either.right(this);
}

for (Predicate p : this.body) {
for (Term term : p.terms) {
if (term instanceof Term.Variable) {
Expand All @@ -122,6 +127,7 @@ public Either<String, Rule> validate_variables() {
}
}
}

return Either.left("rule head or expressions contains variables that are not used in predicates of the rule's body: " + free_variables.toString());
}

Expand Down Expand Up @@ -181,6 +187,7 @@ public boolean equals(Object o) {

if (head != null ? !head.equals(rule.head) : rule.head != null) return false;
if (body != null ? !body.equals(rule.body) : rule.body != null) return false;
if (scopes != null ? !scopes.equals(rule.scopes) : rule.scopes != null) return false;
return expressions != null ? expressions.equals(rule.expressions) : rule.expressions == null;
}

Expand All @@ -189,6 +196,7 @@ public int hashCode() {
int result = head != null ? head.hashCode() : 0;
result = 31 * result + (body != null ? body.hashCode() : 0);
result = 31 * result + (expressions != null ? expressions.hashCode() : 0);
result = 31 * result + (scopes != null ? scopes.hashCode() : 0);
return result;
}

Expand All @@ -204,6 +212,11 @@ public String toString() {
res += ", " + String.join(", ", e);
}

if(!r.scopes.isEmpty()) {
final List<String> e = r.scopes.stream().map((scope) -> scope.toString()).collect(Collectors.toList());
res += " trusting " + String.join(", ", e);
}

return res;
}
}
49 changes: 47 additions & 2 deletions src/main/java/com/clevercloud/biscuit/token/builder/Scope.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@


import java.util.ArrayList;
import java.util.Objects;

import static io.vavr.API.Left;
import static io.vavr.API.Right;
Expand Down Expand Up @@ -52,17 +53,25 @@ public static Scope publicKey(PublicKey publicKey) {
return new Scope(Kind.PublicKey, publicKey);
}

public static Scope parameter(String parameter) {
return new Scope(Kind.Parameter, parameter);
}

public com.clevercloud.biscuit.datalog.Scope convert(SymbolTable symbols) {
switch (this.kind) {
case Authority:
return com.clevercloud.biscuit.datalog.Scope.authority();
case Previous:
return com.clevercloud.biscuit.datalog.Scope.previous();
case Parameter:
throw new Exception("Remaining parameter: "+this.parameter);
//FIXME
return null;
//throw new Exception("Remaining parameter: "+this.parameter);
case PublicKey:
return com.clevercloud.biscuit.datalog.Scope.publicKey(symbols.insert(this.publicKey));
}
//FIXME
return null;
}

public static Scope convert_from(com.clevercloud.biscuit.datalog.Scope scope, SymbolTable symbols) {
Expand All @@ -77,7 +86,43 @@ public static Scope convert_from(com.clevercloud.biscuit.datalog.Scope scope, Sy
}

//FIXME error management should bubble up here
throw new Exception("panic");
//throw new Exception("panic");
return null;

}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Scope scope = (Scope) o;

if (kind != scope.kind) return false;
if (!Objects.equals(publicKey, scope.publicKey)) return false;
return Objects.equals(parameter, scope.parameter);
}

@Override
public int hashCode() {
int result = kind.hashCode();
result = 31 * result + (publicKey != null ? publicKey.hashCode() : 0);
result = 31 * result + (parameter != null ? parameter.hashCode() : 0);
return result;
}

@Override
public String toString() {
switch (this.kind) {
case Authority:
return "authority";
case Previous:
return "previous";
case Parameter:
return "{"+this.parameter+"}";
case PublicKey:
return this.publicKey.toString();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static Term set(HashSet<Term> s) {
return new Term.Set(s);
}

private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
public static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
Expand All @@ -69,6 +69,7 @@ public static String byteArrayToHexString(byte[] bytes) {
}

public static byte[] hexStringToByteArray(String hex) {
hex = hex.toUpperCase();
int l = hex.length();
byte[] data = new byte[l/2];
for (int i = 0; i < l; i += 2) {
Expand Down
Loading

0 comments on commit 35781d6

Please sign in to comment.