aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/FileRouter.ts40
-rw-r--r--src/app.ts16
-rw-r--r--src/auth.ts33
-rw-r--r--src/db/model/Project.ts4
-rw-r--r--src/db/model/User.ts1
-rw-r--r--src/errors/errorware.ts8
-rw-r--r--src/files.ts5
-rw-r--r--src/logger.ts5
8 files changed, 79 insertions, 33 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts
index b3b1a38..4d6eb16 100644
--- a/src/FileRouter.ts
+++ b/src/FileRouter.ts
@@ -217,24 +217,23 @@ const getUserPrivileges: Middleware =
* "private_admin_metadata": metadata
* })
*/
-const getUsers: Middleware =
- async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+const getUsers: Middleware = async(
+ req: Request, res: Response, next: NextFunction
+): Promise<void> => {
const users: User[] = await User.findAll({
include: [
UserGroup,
Project
]
});
- res.locals.data = users.map<UserFullInfo>(
- (u: User) => {
- const info: UserFullInfo = u.fullInfo;
- if (!req.user.hasPrivilege('admin')) {
- delete info.private_user_metadata;
- delete info.private_admin_metadata;
- }
- return info;
+ res.locals.data = users.map((u: User): UserFullInfo => {
+ const info: UserFullInfo = u.fullInfo;
+ if (!req.user.hasPrivilege('admin')) {
+ delete info.private_user_metadata;
+ delete info.private_admin_metadata;
}
- );
+ return info;
+ });
next();
};
@@ -416,8 +415,8 @@ const getProjects: Middleware =
const getProjectName: Middleware =
async(req: Request, res: Response, next: NextFunction): Promise<void> => {
const project: Project | null = res.locals.project;
- if (project === null) {
- next(new RequestError(404, 'project_not_found'));
+ if (project == null) {
+ return next(new RequestError(404, 'project_not_found'));
} else {
res.locals.data = project.fullInfo;
next();
@@ -441,7 +440,7 @@ const postProjectName: Middleware =
async(req: Request, res: Response, next: NextFunction): Promise<void> => {
let project: Project | null = res.locals.project;
let promise: PromiseLike<File> | null = null;
- if (project === null) {
+ if (project == null) {
const file: File = new File({
mimetype: 'inode/directory'
});
@@ -474,12 +473,15 @@ const getProjectProperties: Middleware =
/**
* GET a File.
*/
-const getFilePath: Middleware =
- async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+const getFilePath: Middleware = async(
+ req: Request, res: Response, next: NextFunction
+): Promise<void> => {
const file: File | null = res.locals.file;
- if (file === null) {
- next(new RequestError(404, 'file_not_found'));
- return;
+ const project: Project | null = res.locals.project;
+ if (file == null) {
+ return next(new RequestError(404, 'file_not_found'));
+ } else if (project == null) {
+ return next(new RequestError(404, 'project_not_found'));
}
res.locals.function = res.sendFile;
res.locals.data =
diff --git a/src/app.ts b/src/app.ts
index ad00105..c627027 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -12,6 +12,7 @@ import FileRouter from './FileRouter';
import { default as File } from './db/model/File';
import { default as Project } from './db/model/Project';
import { default as User } from './db/model/User';
+import { default as UserGroup } from './db/model/UserGroup';
/**
* Configuration of the Express server app, including installation of
@@ -67,6 +68,21 @@ class App {
] })
);
this.express.use(passport.initialize());
+ this.express.use(async(
+ req: ex.Request, res: ex.Response, next: ex.NextFunction
+ ): Promise<void> => {
+ if (req.user != null) {
+ req.user = await User.findOne({
+ where: {
+ username: req.user.object.username
+ },
+ include: [
+ UserGroup
+ ]
+ });
+ }
+ next();
+ });
if (this.logEnabled) {
this.express.use(logger('combined'));
}
diff --git a/src/auth.ts b/src/auth.ts
index 368f130..8a6b248 100644
--- a/src/auth.ts
+++ b/src/auth.ts
@@ -38,6 +38,7 @@ const configStrategy: () => void = (): void => {
*
* @param req The Express http request.
* @param res The Express http response.
+ * @param next The next Express middleware.
*/
const checkErr: Middleware = (
req: Request, res: Response, next: NextFunction
@@ -89,9 +90,10 @@ const authenticate: Middleware = (
*
* @param req The Express http request.
* @param res The Express http response.
+ * @param next The next Express middleware - only used for errors.
*/
const genToken: Middleware = async(
- req: Request, res: Response
+ req: Request, res: Response, next: NextFunction
): Promise<void> => {
const user: User | null = await User.findOne({
where: {
@@ -102,7 +104,11 @@ const genToken: Middleware = async(
if (user === null) {
// Whoa, what an error!
- return;
+ return next(new RequestError(
+ 400,
+ 'invalid_grant',
+ 'Invalid username'
+ ));
}
const token: string = jwt.sign(
@@ -155,6 +161,22 @@ export class AuthRouter {
}
}
+interface UnauthorizedError {
+ name: 'UnauthorizedError';
+ message: string;
+ code: string;
+ status: number;
+}
+
+const isUnauthorizedError: (item: object) => item is UnauthorizedError = (
+ item: object
+): item is UnauthorizedError => {
+ return (item as Partial<UnauthorizedError>).name === 'UnauthorizedError'
+ && (item as Partial<UnauthorizedError>).message !== undefined
+ && (item as Partial<UnauthorizedError>).code !== undefined
+ && (item as Partial<UnauthorizedError>).status !== undefined;
+};
+
/**
* Error-handling middleware used to handle authentication errors.
*
@@ -168,10 +190,11 @@ export class AuthRouter {
*/
export const unauthorisedErr: Errorware =
(err: Error, req: Request, res: Response, next: NextFunction): void => {
- if (err.name === 'UnauthorizedError') {
+ if (isUnauthorizedError(err)) {
return next(new RequestError(
- 401, 'not_authorised',
- 'user does not have correct authorisation for task'
+ err.status,
+ err.code,
+ err.message
));
}
next(err);
diff --git a/src/db/model/Project.ts b/src/db/model/Project.ts
index 3618f03..d57eaaa 100644
--- a/src/db/model/Project.ts
+++ b/src/db/model/Project.ts
@@ -107,9 +107,7 @@ export default class Project extends Model<Project> {
return {
project_name: this.name,
users: this.contributors
- .map<UserInfo>(
- (u: User) => this.getUserInfo(u)
- ),
+ .map((u: User): UserInfo => this.getUserInfo(u)),
public_metadata: {
creation_date: this.creationDate
},
diff --git a/src/db/model/User.ts b/src/db/model/User.ts
index 7315393..171e899 100644
--- a/src/db/model/User.ts
+++ b/src/db/model/User.ts
@@ -110,7 +110,6 @@ export default class User extends Model<User> {
}
public hasPrivilege(privilege: string): boolean {
- // FIXME: Fetch these things if they aren't already present
if (this.userGroups === undefined) {
throw new Error('usergroups undefined');
}
diff --git a/src/errors/errorware.ts b/src/errors/errorware.ts
index 05c714d..6d87bcb 100644
--- a/src/errors/errorware.ts
+++ b/src/errors/errorware.ts
@@ -28,7 +28,7 @@ export class RequestError extends Error {
* @returns `true` if the object is a [[RequestError]]; `false`
* otherwise.
*/
- public static is(test: Object): test is RequestError {
+ public static is(test: object): test is RequestError {
return (test as Partial<RequestError>).code !== undefined
&& (test as Partial<RequestError>).name !== undefined
&& (test as Partial<RequestError>).responseBlock !== undefined;
@@ -110,9 +110,11 @@ export type Errorware =
* @param err The Error to act on.
* @param req The Express http request.
* @param res The Express http response.
+ * @param next The next Express middleware function.
*/
-export const errorHandler: Errorware =
- (err: Error, req: Request, res: Response): void => {
+export const errorHandler: Errorware = (
+ err: Error, req: Request, res: Response, next: NextFunction
+): void => {
if (RequestError.is(err)) {
res.status(err.code)
.json(err.responseBlock());
diff --git a/src/files.ts b/src/files.ts
index fb22013..644d24e 100644
--- a/src/files.ts
+++ b/src/files.ts
@@ -15,8 +15,9 @@ const writableStream:
};
const logPath:
- (type: string, projectName?: string) => string =
- (type: string, projectName?: string): string => {
+(type: string, projectName?: string) => string = (
+ type: string, projectName?: string
+): string => {
return projectName
? `${LOG_BASE_DIRECTORY}/projects/${projectName}/${type}`
: `${LOG_BASE_DIRECTORY}/general/${type}`;
diff --git a/src/logger.ts b/src/logger.ts
index 953a341..bbb0a73 100644
--- a/src/logger.ts
+++ b/src/logger.ts
@@ -31,6 +31,8 @@ interface LoggingMetadata {
[key: string]: any;
}
+export let enabled: boolean = true;
+
const consoleTransport: winston.TransportInstance =
new winston.transports.Console({
name: 'consoleTransport',
@@ -162,6 +164,9 @@ const makeLogFunction: (level: LogLevel) => LogFunction = (
return (
message: string, meta: LoggingMetadata = {}
): void => {
+ if (!enabled) {
+ return;
+ }
meta.user = meta.user || '_BE4_system';
meta.component = meta.component || 'core';
basicLogger.log(level, message, meta);