-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move cedar_common/corks to main repo
- Loading branch information
Showing
98 changed files
with
18,132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# https://dart.dev/guides/libraries/private-files | ||
# Created by `dart pub` | ||
.dart_tool/ | ||
|
||
# Avoid committing pubspec.lock for library packages; see | ||
# https://dart.dev/guides/libraries/private-files#pubspeclock. | ||
pubspec.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 0.1.0 | ||
|
||
- Initial version. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# cedar_common | ||
|
||
Base types and utilities of the Cedar language in Dart. | ||
|
||
This is separate from `package:cedar_dart` so that the types can be used without bundling the native assets of `package:cedar_dart`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file configures the static analysis results for your project (errors, | ||
# warnings, and lints). | ||
# | ||
# This enables the 'recommended' set of lints from `package:lints`. | ||
# This set helps identify many issues that may lead to problems when running | ||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic | ||
# style and format. | ||
# | ||
# If you want a smaller set of lints you can change this to specify | ||
# 'package:lints/core.yaml'. These are just the most critical lints | ||
# (the recommended set includes the core lints). | ||
# The core lints are also what is used by pub.dev for scoring packages. | ||
|
||
include: package:lints/recommended.yaml | ||
|
||
# Uncomment the following section to specify additional rules. | ||
|
||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** | ||
|
||
# For more information about the core and recommended set of lints, see | ||
# https://dart.dev/go/core-lints | ||
|
||
# For additional information about configuring this file, see | ||
# https://dart.dev/guides/language/analysis-options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// Base types and utilities of the Cedar language in Dart. | ||
/// | ||
/// This is separate from `package:cedar_dart` so that the types can be used | ||
/// without bundling the native assets of `package:cedar_dart`. | ||
library; | ||
|
||
export 'src/ast/cedar_entity.dart'; | ||
export 'src/ast/cedar_entity_id.dart'; | ||
export 'src/ast/cedar_node.dart'; | ||
export 'src/ast/cedar_schema.dart'; | ||
export 'src/policy/cedar_policy.dart'; | ||
export 'src/policy/cedar_policy_set.dart'; | ||
export 'src/policy/json_expr.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'package:cedar_common/src/ast/cedar_entity_id.dart'; | ||
import 'package:cedar_common/src/ast/cedar_node.dart'; | ||
import 'package:cedar_common/src/policy/json_expr.dart'; | ||
|
||
final class CedarEntity implements CedarNode { | ||
const CedarEntity({ | ||
required this.id, | ||
this.parents = const [], | ||
this.attributes = const {}, | ||
}); | ||
|
||
factory CedarEntity.fromJson(Map<String, Object?> json) => CedarEntity( | ||
id: CedarEntityId.fromJson(json['uid'] as Map<String, Object?>), | ||
parents: (json['parents'] as List<Object?>) | ||
.map((e) => CedarEntityId.fromJson(e as Map<String, Object?>)) | ||
.toList(), | ||
attributes: (json['attrs'] as Map<Object?, Object?>) | ||
.cast<String, Object?>() | ||
.map((key, value) => MapEntry(key, CedarValueJson.fromJson(value))), | ||
); | ||
|
||
final CedarEntityId id; | ||
final List<CedarEntityId> parents; | ||
final Map<String, CedarValueJson> attributes; | ||
|
||
@override | ||
Map<String, Object?> toJson() => { | ||
'uid': id.toJson(), | ||
'parents': parents.map((e) => e.toJson()).toList(), | ||
'attrs': attributes.map((key, value) => MapEntry(key, value.toJson())), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import 'package:cedar_common/src/ast/cedar_node.dart'; | ||
|
||
final class CedarEntityId implements CedarNode { | ||
const CedarEntityId(this.type, this.id); | ||
|
||
factory CedarEntityId.fromJson(Map<String, Object?> json) { | ||
switch (json) { | ||
case {'type': final String type, 'id': final String id} || | ||
{'__entity': {'type': final String type, 'id': final String id}}: | ||
return CedarEntityId(type, id); | ||
default: | ||
throw FormatException('Invalid entity ID JSON: $json'); | ||
} | ||
} | ||
|
||
final String type; | ||
final String id; | ||
|
||
/// Returns a normalized version of this entity ID. | ||
/// | ||
/// Cedar prohibits whitespace in entity IDs, so this method removes all | ||
/// whitespace from the [type] and [id]. | ||
/// | ||
/// See [RFC 9](https://github.com/cedar-policy/rfcs/blob/main/text/0009-disallow-whitespace-in-entityuid.md) | ||
/// for more information. | ||
CedarEntityId get normalized => CedarEntityId( | ||
type, | ||
String.fromCharCodes( | ||
id.runes.expand((char) { | ||
return switch (char) { | ||
0 => '\\0'.codeUnits, | ||
0x9 => '\\t'.codeUnits, | ||
0xa => '\\n'.codeUnits, | ||
0xd => '\\r'.codeUnits, | ||
0x22 => '\\"'.codeUnits, | ||
0x27 => "\\'".codeUnits, | ||
< 0x20 || | ||
0x7f || // Delete | ||
0x96 || // Non-breaking space | ||
> 0xffff => | ||
'\\u{${char.toRadixString(16)}}'.codeUnits, | ||
_ => [char], | ||
}; | ||
}), | ||
), | ||
); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is CedarEntityId && type == other.type && id == other.id; | ||
|
||
@override | ||
int get hashCode => Object.hash(type, id); | ||
|
||
@override | ||
String toString() => '$type::"$id"'; | ||
|
||
@override | ||
Map<String, Object?> toJson() => { | ||
'type': type, | ||
'id': id, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
abstract interface class CedarNode { | ||
Map<String, Object?> toJson(); | ||
} |
Oops, something went wrong.