-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.dart
50 lines (46 loc) · 1.42 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import 'dart:convert';
import 'dart:io';
import 'package:cedar/cedar.dart';
import 'package:cedar_ffi/cedar_ffi.dart';
Future<void> main() async {
final root = Platform.script.resolve('cedar/');
final schemaJson =
File.fromUri(root.resolve('example.cedarschema.json')).readAsStringSync();
final policiesCedar =
File.fromUri(root.resolve('example.cedar')).readAsStringSync();
final cedar = CedarEngine(
schema: CedarSchema.fromJson(
jsonDecode(schemaJson) as Map<String, Object?>,
),
policies: CedarPolicySetFfi.fromCedar(policiesCedar),
);
final app = CedarEntity(
id: CedarEntityId('Application', 'TinyTodo'),
);
final user = CedarEntity(
id: CedarEntityId('User', 'alice'),
parents: [app.id],
attributes: {
'name': CedarValueJson.string('Alice'),
},
);
final canCreateTodo = cedar.isAuthorized(
CedarAuthorizationRequest(
principal: user.id,
action: CedarEntityId('Action', 'CreateList'),
resource: app.id,
),
entities: [app, user],
);
switch (canCreateTodo) {
case CedarAuthorizationResponse(decision: CedarAuthorizationDecision.allow):
print('Alice can create the todo list!');
case CedarAuthorizationResponse(
:final errorMessages,
:final reasons,
):
print('Alice cannot create the todo list');
print('Contributing policies: $reasons');
print('Error messages: $errorMessages');
}
}