Skip to content

Commit 5c63301

Browse files
committed
Adding roles to user updates; adding auth restrictions to pw updates; moving limiting to middleware decl
1 parent e21c403 commit 5c63301

3 files changed

Lines changed: 30 additions & 11 deletions

File tree

src/index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ app.use(cors());
3232
app.use(helmet());
3333
app.use(express.json());
3434
app.use(express.urlencoded({ extended: true }));
35+
app.use(apiLimiter);
3536

3637
// Custom middleware
3738
app.use(async (req, res, next) => {
@@ -43,14 +44,14 @@ app.use(async (req, res, next) => {
4344
});
4445

4546
// Helper endpoints
46-
app.get('/', apiLimiter, (req, res) => {
47+
app.get('/', (req, res) => {
4748
res.redirect('/api-docs/');
4849
});
49-
app.get('/help', apiLimiter, (req, res) => {
50+
app.get('/help', (req, res) => {
5051
res.redirect('/api-docs/');
5152
})
5253
app.use('/api-docs', apiLimiter, swaggerUi.serve, swaggerUi.setup(swaggerDocument, swaggerOptions));
53-
app.use('/health', apiLimiter, (req, res) => {
54+
app.use('/health', (req, res) => {
5455
res.status(200).json({
5556
uptime: utils.formatTime(process.uptime()),
5657
environment: process.env.NODE_ENV || 'n/a',
@@ -61,16 +62,16 @@ app.use('/health', apiLimiter, (req, res) => {
6162

6263
// Routes
6364
Object.entries(routes).forEach(([key, value]) => {
64-
app.use(`/${key}`, apiLimiter, value);
65+
app.use(`/${key}`, value);
6566
});
6667

6768
// Handle 404
68-
app.use(apiLimiter, (req, res) => {
69+
app.use((req, res) => {
6970
return res.status(404).send("404: Not Found.");
7071
});
7172

7273
// Handle 503
73-
app.use(apiLimiter, (error, req, res, next) => {
74+
app.use((error, req, res, next) => {
7475
console.error(error);
7576
return res.status(503).send("503: Service Unavailable");
7677
});

src/routes/user.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,25 @@ router.put('/', utils.authMiddleware, async (req, res) => {
183183
}
184184
});
185185
/** @todo add ability to change email */
186+
186187

187188
/** @todo when roles are added make sure only admin or relevant user can change password */
188-
user.password = password;
189+
const e = await utils.loadCasbin();
190+
const roles = await e.getRolesForUser(req.context.me.email);
191+
192+
if (password) {
193+
if (req.context.me.email === email || roles.includes('admin')) {
194+
user.password = password;
195+
}
196+
}
197+
198+
/** @todo this is half-baked. Once updating users is available through the front-end this should be revisited. */
199+
if (roles !== undefined) {
200+
const e = await utils.loadCasbin();
201+
for (const role of roles) {
202+
await e.addRoleForUser(email.toLowerCase(), role);
203+
}
204+
}
189205

190206
user.displayName = (displayName) ? displayName : user.displayName;
191207
user.phone = (phone) ? phone : user.phone;

src/utils/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const validateToken = async (req) => {
7070
* @return {Boolean}
7171
*/
7272
const validateRoles = async (req) => {
73-
const e = await req.context.casbinEnforcer;
73+
const e = await loadCasbin();
7474
const { originalUrl: path, method } = req;
7575

7676
const isAllowed = await e.enforce(req.context.me.email, path, method);
@@ -84,15 +84,17 @@ const validateRoles = async (req) => {
8484
* @param {*} res the response object
8585
* @param {*} next the next handler in the chain
8686
*/
87-
const authMiddleware = async (req, res, next) => {
87+
const authMiddleware = async (req, res, next) => {
8888
let authed = false;
8989

9090
if (process.env.BYPASS_LOGIN) {
9191
authed = process.env.BYPASS_LOGIN;
9292
} else {
9393
authed = await validateToken(req);
94-
authed = await validateRoles(req);
95-
}
94+
if (authed) {
95+
authed = await validateRoles(req);
96+
}
97+
}
9698

9799
if (authed) {
98100
next();

0 commit comments

Comments
 (0)