Checkpoint · Lesson 6.1
The first Cedar decision: checkpoint files
Compare your folder with the reference using Git, if you cloned it: git diff lesson/m06-l01 -- src.
Files changed in this lesson
package-lock.json changed
Generated by npm from package.json. Do not edit it by hand; run the npm commands from the lesson.
package.json changed
package.json{
"name": "caretaker",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "node --env-file=.env.test --test --test-concurrency=1 --test-reporter=spec \"test/**/*.test.js\"",
"start": "node --env-file=.env src/server.js",
"dev": "node --env-file=.env --watch src/server.js",
"db:migrate": "node --env-file=.env scripts/migrate.js",
"db:seed": "node --env-file=.env scripts/seed.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"@cedar-policy/cedar-wasm": "4.14.0",
"argon2": "0.45.2",
"connect-pg-simple": "10.0.0",
"ejs": "7.0.2",
"express": "5.3.0",
"express-rate-limit": "8.7.4",
"express-session": "1.19.0",
"pg": "8.23.1"
},
"devDependencies": {
"supertest": "7.3.2"
}
}
{ "name": "caretaker", "version": "1.0.0", "description": "", "scripts": { "test": "node --env-file=.env.test --test --test-concurrency=1 --test-reporter=spec \"test/**/*.test.js\"", "start": "node --env-file=.env src/server.js", "dev": "node --env-file=.env --watch src/server.js", "db:migrate": "node --env-file=.env scripts/migrate.js", "db:seed": "node --env-file=.env scripts/seed.js" }, "keywords": [], "author": "", "license": "ISC", "type": "module", "dependencies": { "@cedar-policy/cedar-wasm": "4.14.0", "argon2": "0.45.2", "connect-pg-simple": "10.0.0", "ejs": "7.0.2", "express": "5.3.0", "express-rate-limit": "8.7.4", "express-session": "1.19.0", "pg": "8.23.1" }, "devDependencies": { "supertest": "7.3.2" }}scripts/cedar-hello.js added
scripts/cedar-hello.js// The smallest real Cedar decision. Usage: node scripts/cedar-hello.js [userId] [issueId]
import * as cedar from '@cedar-policy/cedar-wasm/nodejs';
const [userId = '1', issueId = '101'] = process.argv.slice(2);
// A reference to an entity: its type and its id. Ids are always strings.
const ref = (type, id) => ({ type: `Caretaker::${type}`, id: String(id) });
// Policies: Cedar source text, keyed by an id that we choose.
const policies = {
'view-issue': `
permit (
principal,
action == Caretaker::Action::"ViewIssue",
resource
)
when { principal.issueViewerBuildings.contains(resource.building) };
`,
};
// Entities: the facts Cedar may look at. Here they are typed by hand.
// In the app they will be built from database rows.
const entities = [
{ uid: ref('User', 1), attrs: { issueViewerBuildings: [ref('Building', 10), ref('Building', 20)] }, parents: [] },
{ uid: ref('User', 2), attrs: { issueViewerBuildings: [ref('Building', 30)] }, parents: [] },
{ uid: ref('Issue', 101), attrs: { building: ref('Building', 10) }, parents: [] },
{ uid: ref('Issue', 105), attrs: { building: ref('Building', 30) }, parents: [] },
];
// The request: who, what, which, and any extra facts about this request.
const call = {
principal: ref('User', userId),
action: { type: 'Caretaker::Action', id: 'ViewIssue' },
resource: ref('Issue', issueId),
context: {},
policies: { staticPolicies: policies },
entities,
};
const answer = cedar.isAuthorized(call);
console.log('REQUEST', JSON.stringify({ principal: call.principal, action: call.action, resource: call.resource, context: call.context }));
console.log('ANSWER ', JSON.stringify(answer, null, 2));
// The smallest real Cedar decision. Usage: node scripts/cedar-hello.js [userId] [issueId]import * as cedar from '@cedar-policy/cedar-wasm/nodejs'; const [userId = '1', issueId = '101'] = process.argv.slice(2); // A reference to an entity: its type and its id. Ids are always strings.const ref = (type, id) => ({ type: `Caretaker::${type}`, id: String(id) }); // Policies: Cedar source text, keyed by an id that we choose.const policies = { 'view-issue': ` permit ( principal, action == Caretaker::Action::"ViewIssue", resource ) when { principal.issueViewerBuildings.contains(resource.building) }; `,}; // Entities: the facts Cedar may look at. Here they are typed by hand.// In the app they will be built from database rows.const entities = [ { uid: ref('User', 1), attrs: { issueViewerBuildings: [ref('Building', 10), ref('Building', 20)] }, parents: [] }, { uid: ref('User', 2), attrs: { issueViewerBuildings: [ref('Building', 30)] }, parents: [] }, { uid: ref('Issue', 101), attrs: { building: ref('Building', 10) }, parents: [] }, { uid: ref('Issue', 105), attrs: { building: ref('Building', 30) }, parents: [] },]; // The request: who, what, which, and any extra facts about this request.const call = { principal: ref('User', userId), action: { type: 'Caretaker::Action', id: 'ViewIssue' }, resource: ref('Issue', issueId), context: {}, policies: { staticPolicies: policies }, entities,}; const answer = cedar.isAuthorized(call); console.log('REQUEST', JSON.stringify({ principal: call.principal, action: call.action, resource: call.resource, context: call.context }));console.log('ANSWER ', JSON.stringify(answer, null, 2));Project tree at this checkpoint
.env.example
.env.test
.gitignore
.npmrc
compose.yaml
db/init/01-create-test-database.sql
migrations/001_create_organisations_buildings_issues.sql
migrations/002_create_inspections.sql
migrations/003_create_users.sql
migrations/004_create_session_table.sql
migrations/005_create_memberships_and_building_roles.sql
package-lock.json
package.json
public/css/style.css
public/js/app.js
scripts/cedar-hello.js
scripts/migrate.js
scripts/seed.js
src/app.js
src/authz/rules.js
src/config.js
src/db.js
src/lib/http.js
src/lib/ids.js
src/lib/validation.js
src/middleware/auth.js
src/middleware/csrf.js
src/middleware/flash.js
src/middleware/rate-limit.js
src/migrate.js
src/models/access.js
src/models/buildings.js
src/models/dashboard.js
src/models/inspections.js
src/models/issues.js
src/models/organisations.js
src/models/scope.js
src/models/users.js
src/routes/account.js
src/routes/auth.js
src/routes/buildings.js
src/routes/inspections.js
src/routes/issues.js
src/routes/organisations.js
src/seed.js
src/server.js
test/access-matrix.test.js
test/access.test.js
test/auth.test.js
test/buildings.test.js
test/csrf.test.js
test/dashboard.test.js
test/helpers.js
test/inspections.test.js
test/isolation.test.js
test/issue-authz.test.js
test/issue-list.test.js
test/issues.test.js
test/organisations.test.js
test/rate-limit.test.js
test/require-login.test.js
test/resource-authz.test.js
views/account.ejs
views/auth/login.ejs
views/auth/register.ejs
views/buildings/form.ejs
views/buildings/show.ejs
views/dashboard.ejs
views/error.ejs
views/forbidden.ejs
views/inspections/form.ejs
views/inspections/index.ejs
views/inspections/show.ejs
views/issues/form.ejs
views/issues/index.ejs
views/issues/show.ejs
views/not-found.ejs
views/organisations/form.ejs
views/organisations/index.ejs
views/organisations/show.ejs
views/partials/foot.ejs
views/partials/head.ejs
views/partials/nav.ejs