Skip to content

Instantly share code, notes, and snippets.

View francium-lupe's full-sized avatar

francium-lupe

View GitHub Profile
@francium-lupe
francium-lupe / authz_fundamentals
Created June 28, 2023 18:26
The core components of authorization
def update_post(user, post)
if not user.admin?
raise Forbidden, "you must be an admin to update this post"
end
...
end
@francium-lupe
francium-lupe / RBAC_vehicles.js
Last active August 29, 2023 13:59
Rental Car Platform
const requiredRoles = {
'/vehicle/lock-vehicle': new Set(['admin', 'manager', 'booked-user']),
'/vehicle/unlock-vehicle': new Set(['admin', 'manager', 'booked-user']),
'/vehicle/update': new Set(['admin', 'manager']),
'/vehicle/update-authorized-users': new Set(['admin'])
};
@francium-lupe
francium-lupe / Roles_RBAC.js
Last active August 29, 2023 13:58
requiredRoles Mapping
if (endpoint && endpoint in requiredRoles) {
if (!roles.find(role => requiredRoles[endpoint].has(role))) {
throw new BadRequest(
`User not authorized for action ${actionName}, has roles "${roles}"`
);
}
}
actor User {}
# "admin" is a global role that applies to all vehicles
global {
roles = ["admin"];
}
resource Vehicle {
permissions = ["security", "update", "updateAuthorizedUsers"];
roles = ["admin", "manager", "booked-user"];
actor User {}
resource Vehicle {
permissions = ["security", "update", "updateAuthorizedUsers"];
roles = ["admin", "manager", "booked-user"];
"security" if "booked-user";
"update" if "manager";
"updateAuthorizedUsers" if "admin";
import { Oso } from 'oso';
class User {
constructor(roles) {
this.roles = roles;
}
}
class Vehicle {
constructor(id) {
this.id = id;
function getRolesForUser(user) {
if (user._id.toString() === this.userId.toString()) {
return ['booked-user'];
}
return [];
}
actor User {}
global {
roles = ["admin"];
}
resource Vehicle {
permissions = ["security", "update", "updateAuthorizedUsers"];
roles = ["admin", "manager", "driver"];
relations = { currentlyBookedBy: User };
@francium-lupe
francium-lupe / Express_endpoints.js
Created September 11, 2023 22:25
Threads API endpoints for creating a new user, creating a thread, and retrieving a list of all threads
const express = require('express');
const app = express();
app.use(express.json());
app.use(function(req, res, next) {
console.log(new Date(), req.method, req.url);
next();
});
const threads = [];
const users = [];
@francium-lupe
francium-lupe / auth_fundamentals.rb
Last active September 18, 2023 14:27
authorization fundamentals - venn diagram
def update_post(user, post)
if not user.admin?
raise Forbidden, "you must be an admin to update this post"
end
...
end