Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly create new keys #64

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/run_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ clean)
kill_nodes
kill_backend
kill_db
rm -f bin/*
rm -rf bin nodes
exit
;;

Expand Down
27 changes: 13 additions & 14 deletions web/backend/src/authManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,21 @@ the connection string has the following format:
postgres://username:password@host:port/database
the migrate option is used to create the tables if they don't exist, we set it to false because we create the tables manually
*/
async function initEnforcer() {
const dbAdapter = await SequelizeAdapter.newAdapter({
dialect: 'postgres',
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT || '5432', 10),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: 'casbin',
});
return newEnforcer('src/model.conf', dbAdapter);
export async function initEnforcer(): Promise<Enforcer> {
if (authEnforcer === undefined) {
const dbAdapter = await SequelizeAdapter.newAdapter({
dialect: 'postgres',
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT || '5432', 10),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: 'casbin',
});
authEnforcer = await newEnforcer('src/model.conf', dbAdapter);
}
return authEnforcer;
}

Promise.all([initEnforcer()]).then((createdEnforcer) => {
[authEnforcer] = createdEnforcer;
});

export function isAuthorized(sciper: number | undefined, subject: string, action: string): boolean {
return authEnforcer.enforceSync(sciper, subject, action);
}
Expand Down
17 changes: 1 addition & 16 deletions web/backend/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,12 @@ Backend CLI, currently providing 3 commands for user management:
*/

import { Command, InvalidArgumentError } from 'commander';
import { SequelizeAdapter } from 'casbin-sequelize-adapter';
import { newEnforcer } from 'casbin';
import { curve } from '@dedis/kyber';
import * as fs from 'fs';
import { PERMISSIONS, readSCIPER } from './authManager';
import { PERMISSIONS, readSCIPER, initEnforcer } from './authManager';

const program = new Command();

async function initEnforcer() {
const dbAdapter = await SequelizeAdapter.newAdapter({
dialect: 'postgres',
host: process.env.DATABASE_HOST,
port: parseInt(process.env.DATABASE_PORT || '5432', 10),
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: 'casbin',
});

return newEnforcer('src/model.conf', dbAdapter);
}

program
.command('addAdmin')
.description('Given a SCIPER number, the owner would gain full admin permissions')
Expand Down
4 changes: 3 additions & 1 deletion web/backend/src/controllers/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import express from 'express';
import axios, { AxiosError } from 'axios';
import { sciper2sess } from '../session';
import { getUserPermissions, readSCIPER, setMapAuthorization } from '../authManager';
import { initEnforcer, getUserPermissions, readSCIPER, setMapAuthorization } from '../authManager';

export const authenticationRouter = express.Router();

initEnforcer().catch((e) => console.error(`Couldn't initialize enforcerer: ${e}`));

authenticationRouter.get('/get_dev_login/:userId', (req, res) => {
if (process.env.REACT_APP_DEV_LOGIN !== 'true') {
const err = `/get_dev_login can only be called with REACT_APP_DEV_LOGIN===true: ${process.env.REACT_APP_DEV_LOGIN}`;
Expand Down
3 changes: 3 additions & 0 deletions web/backend/src/controllers/dela.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ import axios, { AxiosError, Method } from 'axios';
import xss from 'xss';
import {
assignUserPermissionToOwnElection,
initEnforcer,
isAuthorized,
PERMISSIONS,
revokeUserPermissionToOwnElection,
} from '../authManager';

export const delaRouter = express.Router();

initEnforcer().catch((e) => console.error(`Couldn't initialize enforcerer: ${e}`));

// get payload creates a payload with a signature on it
function getPayload(dataStr: string) {
let dataStrB64 = Buffer.from(dataStr).toString('base64url');
Expand Down
4 changes: 3 additions & 1 deletion web/backend/src/controllers/proxies.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import express from 'express';
import lmdb from 'lmdb';
import { isAuthorized, PERMISSIONS } from '../authManager';
import { initEnforcer, isAuthorized, PERMISSIONS } from '../authManager';

export const proxiesRouter = express.Router();

initEnforcer().catch((e) => console.error(`Couldn't initialize enforcerer: ${e}`));

const proxiesDB = lmdb.open<string, string>({ path: `${process.env.DB_PATH}proxies` });
proxiesRouter.post('', (req, res) => {
if (!isAuthorized(req.session.userId, PERMISSIONS.SUBJECTS.PROXIES, PERMISSIONS.ACTIONS.POST)) {
Expand Down
4 changes: 3 additions & 1 deletion web/backend/src/controllers/users.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import express from 'express';

import { addPolicy, isAuthorized, PERMISSIONS } from '../authManager';
import { addPolicy, initEnforcer, isAuthorized, PERMISSIONS } from '../authManager';

export const usersRouter = express.Router();

initEnforcer().catch((e) => console.error(`Couldn't initialize enforcerer: ${e}`));

// This call allows a user that is admin to get the list of the people that have
// a special role (not a voter).
usersRouter.get('/user_rights', (req, res) => {
Expand Down
Loading