aboutsummaryrefslogtreecommitdiff
path: root/src/FileRouter.ts
diff options
context:
space:
mode:
authorjw2552018-03-03 14:53:31 +0000
committerjw2552018-03-03 14:53:31 +0000
commit0caebd4bfdfd9c8d7840fc379412d5d1f867575f (patch)
treeab0cd04004080d97bb85bde517f49d32eac9aedf /src/FileRouter.ts
parentfixed compilation issues _2 (diff)
downloadmedicine-0caebd4bfdfd9c8d7840fc379412d5d1f867575f.tar.gz
medicine-0caebd4bfdfd9c8d7840fc379412d5d1f867575f.zip

pulled latest files from trh/payload-checker

Diffstat (limited to 'src/FileRouter.ts')
-rw-r--r--src/FileRouter.ts617
1 files changed, 359 insertions, 258 deletions
diff --git a/src/FileRouter.ts b/src/FileRouter.ts
index 7d3d549..8d13401 100644
--- a/src/FileRouter.ts
+++ b/src/FileRouter.ts
@@ -1,6 +1,20 @@
+import { ContributorGroupFullInfo, default as ContributorGroup
+ } from './db/model/ContributorGroup';
+import { default as File } from './db/model/File';
+import { ProjectFullInfo, default as Project } from './db/model/Project';
+import { UserFullInfo, default as User } from './db/model/User';
+import { default as UserGroup } from './db/model/UserGroup';
+
import { NextFunction, Request, Response, Router } from 'express';
-import { Property, default as serverConfig,
- propertyExists } from './serverConfig';
+
+import { RequestError } from './errors/errorware';
+import { files } from './files';
+import { logger } from './logger';
+import { Property, default as serverConfig } from './serverConfig';
+import { uuid } from './uuid';
+
+// TODO: figure this out
+const BASE_FILE_STORAGE: string = 'TODO: figure this out';
/**
* The type of an Express middleware callback.
@@ -8,415 +22,502 @@ import { Property, default as serverConfig,
type Middleware =
(req: Request, res: Response, next: NextFunction) => void;
-/*
+/**
* Returns the supported Protocols
*
* The server's unwrapped response MUST match:
-
- exactly({
- "supported": array(string),
- "required": array(string)
- })
+ *
+ * exactly({
+ * "supported": array(string),
+ * "required": array(string)
+ * })
*/
const getProtocols: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- const returnValue: {
- supported: string[];
- required: string[];
- } = {
- supported: [
- 'BE50'
- ],
+ res.locals.data = {
+ supported: [],
required: [
'BE01'
]
};
- res.send(JSON.stringify(returnValue));
+ next();
};
-/*
+/**
* Logging Endpoint for all components:
*
* with a request matching
-
- array({
- "component": string,
- "level": alternative([
- "info", "security", "warning", "error", "critical"
- ]),
- "value": anything
- })
+ *
+ * array({
+ * "component": string,
+ * "level": alternative([
+ * "info",
+ * "security",
+ * "warning",
+ * "error",
+ * "critical"
+ * ]),
+ * "value": anything
+ * })
*/
const postLog: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- res.status(501)
- .send({error: 'logging_not_enabled'});
+ if (!req.user.hasPrivilege('logging')) {
+ next(new RequestError(400, 'invalid_privilege'));
+ return;
+ } else if (!logger.isEnabled()) {
+ next(new RequestError(501, 'logging_not_enabled'));
+ return;
+ }
+ for (const item of req.body) {
+ logger.log(item.level, item.value, {
+ component: item.component,
+ user: req.user.username,
+ external: true
+ });
+ }
+ next();
};
-/*
+/**
* Clients authorised as a user with the admin privilege can access
*
* http://backend.endpoint/log?before=<datetime>&after=<datetime>&level=<level>
*
* the server response should match
-
- array({
- "component": string,
- "level": alternative(
- ["info", "security", "warning", "error", "critical"
- ]),
- "value": anything,
- "username": string,
- "timestamp": string
- })
+ *
+ * array({
+ * "component": string,
+ * "level": alternative([
+ * "info",
+ * "security",
+ * "warning",
+ * "error",
+ * "critical"
+ * ]),
+ * "value": anything,
+ * "username": string,
+ * "timestamp": string
+ * })
*/
const getLog: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- res.status(501)
- .send({error: 'logging_not_enabled'});
+ if (!req.user.hasPrivilege('admin')) {
+ next(new RequestError(400, 'invalid_privilege'));
+ }
+ res.locals.data = logger.fetch(
+ req.params.level, {
+ before: new Date(req.params.before),
+ after: new Date(req.params.after)
+ }
+ );
+ next();
};
-/*
+/**
* Any authenticated client can access
*
* http://backend.endpoint/properties
*
* the server SHOULD respond with a list of properties,
* with the unwrapped response matching
-
- array({
- "id": string,
- "display": optional({
- "category": string,
- "group": string,
- "display_name": string,
- "description": string
- }),
- "read_only": boolean,
- "type": alternative(["string", "integer", "boolean"]),
- "value": alternative([string, integer, boolean])
- })
+ *
+ * array({
+ * "id": string,
+ * "display": optional({
+ * "category": string,
+ * "group": string,
+ * "display_name": string,
+ * "description": string
+ * }),
+ * "read_only": boolean,
+ * "type": alternative(["string", "integer", "boolean"]),
+ * "value": alternative([string, integer, boolean])
+ * })
*/
const getProperties: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- res.send(serverConfig);
+ res.locals.data = serverConfig;
+ next();
};
-/*
+/**
* To update properties the client POSTs to
*
* http://backend.endpoint/properties?action=update
*
* with body
-
- array({
- "id": string,
- "value": alternative([string, integer, boolean])
- })
+ *
+ * array({
+ * "id": string,
+ * "value": alternative([string, integer, boolean])
+ * })
*/
const postProperties: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- let success: boolean = true;
-
- // For each property to update
- let prop: Property;
- for (prop of req.body) {
-
- // Check that the property has an ID and a value
- if (!prop.hasOwnProperty('id') || !prop.hasOwnProperty('value')) {
- res.status(400)
- .send({
- error: 'invalid_request',
- error_description: 'A supplied property is missing an id or value'
- });
- success = false;
- break;
+ for (const newProp of req.body) {
+ const prop: Property | undefined = serverConfig.find(
+ (p: Property): boolean => p.id === newProp.id
+ );
+ if (prop === undefined) {
+ next(new RequestError(400, 'invalid_property', undefined, newProp.id));
+ return;
+ } else if (prop.readOnly) {
+ next(new RequestError(400, 'invalid_property', undefined, newProp.id));
+ return;
}
-
- // Find out if the property exists in the config and if so, at which index
- const index: number = propertyExists(prop.id);
-
- // Check some of the properties values to ensure that it can be updated
- if (index === -1 || serverConfig[index].readOnly) {
- /* TODO Implement an or, using Tom's type checker to ensure
- * the value is "valid" based on the type of the property */
- const description: string =
- index === -1
- ? 'Property not found'
- : 'Property is Read_Only';
- res.status(400)
- .send({
- error: 'invalid_property',
- error_data: prop.id,
- error_description: description
- });
- success = false;
- break;
+ try {
+ prop.value = newProp.value;
+ } catch (err) {
+ // FIXME: Assuming it's a value-type error, but it might not be!
+ next(new RequestError(400, 'invalid_property_value', undefined, newProp.id));
+ return;
}
-
- // Make the update to the property
- serverConfig[index].value = prop.value;
- }
-
- // Tell the client that everything was okay;
- if (success) {
- res.status(200)
- .send();
}
+ next();
};
-/*
+/**
* Listing available privileges
*
* And the server's response, unwrapped, must match
-
- array({
- "privilege": string,
- "description": string,
- "internal": boolean
- })
+ *
+ * array({
+ * "privilege": string,
+ * "description": string,
+ * "internal": boolean
+ * })
*/
-const getUserPriveleges: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+const getUserPrivileges: Middleware =
+ async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+ const usergroups: UserGroup[] = await UserGroup.findAll();
+ res.locals.data = usergroups.map(
+ (ug: UserGroup) => ug.getPrivilege()
+ );
+ next();
};
-/*
+/**
* Listing users
*
* If the client is authorised as a user with the admin privilege
* the server MUST respond with a list of users,
* with the unwrapped response matching
-
- array({
- "username": string,
- "privileges": array(string),
- "projects": array({
- "project_name": string,
- "access_level": string
- }),
- "public_user_metadata": metadata,
- "private_user_metadata": metadata,
- "public_admin_metadata": metadata,
- "private_admin_metadata": metadata
- })
+ *
+ * array({
+ * "username": string,
+ * "privileges": array(string),
+ * "projects": array({
+ * "project_name": string,
+ * "access_level": string
+ * }),
+ * "public_user_metadata": metadata,
+ * "private_user_metadata": metadata,
+ * "public_admin_metadata": metadata,
+ * "private_admin_metadata": metadata
+ * })
*/
const getUsers: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
-
+ 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;
+ }
+ );
+ next();
};
-/*
- * expose additional configuration options for individual users
- *
- *
+/**
+ * Expose additional configuration options for individual users
*/
const getUserProperties: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ res.locals.data = res.locals.user.properties;
+ next();
};
-/*
+/**
* Listing a particular User
*
* If the client is authorised as a user with the admin privilege
- * the server MUST respond with details of the particular user
- * if they exist (unwrapped response):
-
- {
- "username": string,
- "privileges": array(string),
- "projects": array({
- "project_name": string,
- "access_level": string
- }),
- "public_user_metadata": metadata,
- "private_user_metadata": metadata,
- "public_admin_metadata": metadata,
- "private_admin_metadata": metadata
- }
+ * the server MUST respond with details of the particular user if they exist
+ * (unwrapped response):
+ *
+ * {
+ * "username": string,
+ * "privileges": array(string),
+ * "projects": array({
+ * "project_name": string,
+ * "access_level": string
+ * }),
+ * "public_user_metadata": metadata,
+ * "private_user_metadata": metadata,
+ * "public_admin_metadata": metadata,
+ * "private_admin_metadata": metadata
+ * }
*/
-const getUsername:
- (req: Request, res: Response, next: NextFunction, user?: string) => void =
- (req: Request, res: Response, next: NextFunction, user?: string): void => {
- res.status(500)
- .send();
+const getUsername: Middleware =
+ (req: Request, res: Response, next: NextFunction): void => {
+ if (res.locals.user === null) {
+ next(new RequestError(404, 'user_not_found'));
+ return;
+ }
+ res.locals.data = res.locals.user.fullInfo;
+ next();
};
const getCurUser: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
- const curUser: string = '';
- getUsername(req, res, next, curUser);
+ res.locals.data = req.user.fullInfo;
+ next();
};
-/*
+/**
* Updating the current user
*
* http://backend.endpoint/current_user?action=update
*
* with body matching
-
- {
- "password": optional({
- "old": string,
- "new": string
- }),
- "public_user_metadata": optional(metadata),
- "private_user_metadata": optional(metadata),
- "public_admin_metadata": not_present,
- "private_admin_metadata": not_present
- }
-*/
+ *
+ * {
+ * "password": optional({
+ * "old": string,
+ * "new": string
+ * }),
+ * "public_user_metadata": optional(metadata),
+ * "private_user_metadata": optional(metadata),
+ * "public_admin_metadata": not_present,
+ * "private_admin_metadata": not_present
+ * }
+ */
const postCurUser: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+ try {
+ await req.user.updateInfo(req.body);
+ await req.user.save();
+ } catch (err) {
+ next(err);
+ return;
+ }
+ next();
};
-/*
+/**
* Creating a User/updating a user
*
* http://backend.endpoint/users/<username>?action=create
*
* with body matching
-
- {
- "privileges": array(string),
- "password": string,
- "public_user_metadata": optional(metadata),
- "private_user_metadata": optional(metadata),
- "public_admin_metadata": optional(metadata),
- "private_admin_metadata": optional(metadata)
- }
+ *
+ * {
+ * "privileges": array(string),
+ * "password": string,
+ * "public_user_metadata": optional(metadata),
+ * "private_user_metadata": optional(metadata),
+ * "public_admin_metadata": optional(metadata),
+ * "private_admin_metadata": optional(metadata)
+ * }
*/
const postUsername: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+ let user: User | null | undefined = res.locals.user;
+ if (user == null) {
+ user = new User({
+ username: req.params.username,
+ password: req.params.password
+ });
+ } else {
+ user.password = req.params.password;
+ }
+ user.metadata = req.body;
+ next();
};
-/*
+/**
* Listing available Roles
*
* The servers response, unwrapped, must match
-
- array({
- "role": string,
- "description": string,
- "internal": boolean
- })
+ *
+ * array({
+ * "role": string,
+ * "description": string,
+ * "internal": boolean
+ * })
*/
const getProjectRoles: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+ const roles: ContributorGroup[] = await ContributorGroup.findAll();
+ res.locals.data = roles.filter(
+ (cg: ContributorGroup): boolean => !cg.isInternal
+ ).map(
+ (cg: ContributorGroup): ContributorGroupFullInfo => cg.fullInfo
+ );
+ next();
};
-/*
+/**
* Listing Projects
*
* The server MUST respond with a list of projects,
* with the unwrapped response matching
-
- array({
- "project_name": string,
- "users": array({
- "username": string,
- "access_level": string
- }),
- "public_metadata": metadata,
- "private_metadata": optional(metadata),
- "admin_metadata": optional(metadata)
- })
+ *
+ * array({
+ * "project_name": string,
+ * "users": array({
+ * "username": string,
+ * "access_level": string
+ * }),
+ * "public_metadata": metadata,
+ * "private_metadata": optional(metadata),
+ * "admin_metadata": optional(metadata)
+ * })
*/
const getProjects: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ async(req: Request, res: Response, next: NextFunction): Promise<void> => {
+ const projects: Project[] = await Project.findAll({
+ include: [
+ User
+ ]
+ });
+ res.locals.data = projects.map(
+ (p: Project): ProjectFullInfo => p.fullInfo
+ );
+ next();
};
-/*
+/**
* Listing a specific Project
*
* The server MUST respond with details of the particular project
* if it exists (unwrapped response):
-
- {
- "project_name": string,
- "users": array({
- "username": string,
- "access_level": string
- }),
- "public_metadata": metadata,
- "private_metadata": metadata,
- "admin_metadata": optional(metadata)
- }
+ *
+ * {
+ * "project_name": string,
+ * "users": array({
+ * "username": string,
+ * "access_level": string
+ * }),
+ * "public_metadata": metadata,
+ * "private_metadata": metadata,
+ * "admin_metadata": optional(metadata)
+ * }
*/
const getProjectName: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ 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'));
+ } else {
+ res.locals.data = project.fullInfo;
+ next();
+ }
};
-/*
+/**
* Creating a Project / updating a project / deleting / Project grants
*
* http://backend.endpoint/projects/<project_name>?action=create
*
* with body matching
-
- {
- "public_metadata": optional(metadata),
- "private_metadata": optional(metadata),
- "admin_metadata": optional(metadata)
- }
+ *
+ * {
+ * "public_metadata": optional(metadata),
+ * "private_metadata": optional(metadata),
+ * "admin_metadata": optional(metadata)
+ * }
*/
const postProjectName: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ 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) {
+ const file: File = new File({
+ mimetype: 'inode/directory'
+ });
+ promise = file.save();
+ project = new Project({
+ uuid: uuid.generate(),
+ name: req.params.project_name,
+ rootFolder: file
+ });
+ }
+ project.metadata = req.body;
+ // tslint:disable-next-line:await-promise
+ await promise;
+ await project.save();
+ next();
};
-/*
+/**
* additional configuration options for individual projects
*/
const getProjectProperties: Middleware =
(req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ if (res.locals.project == null) {
+ next(new RequestError(404, 'project_not_found'));
+ } else {
+ res.locals.data = res.locals.project.properties;
+ next();
+ }
};
/**
* GET a File.
*/
const getFilePath: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ 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;
+ }
+ res.locals.function = res.sendFile;
+ res.locals.data =
+ `${BASE_FILE_STORAGE}/${res.locals.project.name}/${file.uuid}`;
+ next();
};
/**
* GET a File.
*/
const getFileId: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ (req: Request, res: Response, next: NextFunction): void => {
+ const file: File | null = res.locals.file;
+ if (file === null) {
+ next(new RequestError(404, 'file_not_found'));
+ return;
+ }
+ res.locals.function = res.sendFile;
+ res.locals.data = files.path(req.params.id, res.locals.project.name);
+ next();
};
/**
* Post a File. / delete / move / etc
*/
const postFilePath: Middleware =
- (req: Request, res: Response, next: NextFunction): void => {
- res.status(500)
- .send();
+ (req: Request, res: Response, next: NextFunction): void => {
+ if (res.locals.file != null) {
+ next(new RequestError(400, 'file_exists'));
+ }
+ const file: File = new File({
+ uuid: uuid.generate(),
+ name: res.locals.filename,
+ parentFolder: res.locals.parentFolder
+ });
+ req.pipe(files.writableStream(file.uuid, res.locals.project.name));
+ next();
};
export class FileRouter {
@@ -436,13 +537,13 @@ export class FileRouter {
*/
public init(): void {
// General
- this.router.get ('/_supported_protocols_', getProtocols);
+ this.router.get ('/_supported_protocols_', getProtocols);
this.router.get ('/log', getLog);
this.router.post('/log', postLog);
this.router.get ('/properties', getProperties);
this.router.post('/properties', postProperties);
// Users
- this.router.get ('/user_privileges', getUserPriveleges);
+ this.router.get ('/user_privileges', getUserPrivileges);
this.router.get ('/users', getUsers);
this.router.get ('/users/:username', getUsername);
this.router.post('/users/:username', postUsername);