/* Copyright 2017-2021 Norman Breau Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import * as express from 'express'; import { IncomingHttpHeaders } from 'http'; import {Writable} from 'stream'; import { Fields, Files } from 'formidable'; import formidable from 'formidable'; import {IFormData} from './IFormData'; import { IAuthTokenData } from './IAuthTokenData'; import { getInstance } from './instance'; import { Token } from './Token'; import { JWTError } from './JWTError'; import { ResponseData } from './ResponseData'; import {StatusCode} from './StatusCode'; import { InternalError } from './InternalError'; // eslint-disable-next-line @typescript-eslint/no-require-imports import IncomingForm = require('formidable/Formidable'); import { IDatabasePosition } from './IDatabasePosition'; export type IQueryParamValue = undefined | string | IQueryParams | (string | IQueryParams)[]; export interface IQueryParams { [key: string]: IQueryParamValue; } export interface IParameterMap { [key: string]: string | string[]; } export class Request { private $request: express.Request; public constructor(request: express.Request) { this.$request = request; } public getBody(): TBody { return this.$request.body; } public getForm(): Promise { return new Promise((resolve, reject) => { let r: express.Request = this.getRequestSource(); let form: IncomingForm = formidable({ hashAlgorithm: 'md5' }); form.parse(r, (error: any, fields: Fields, files: Files): any => { if (error) { return reject(error); } return resolve({ fields, files: files }); }); }); } public getHeaders(): IncomingHttpHeaders { return this.$request.headers; } public getHeader(name: string): string { let value: string | string[] = this.$request.headers[name.toLowerCase()]; if (typeof value === 'string') { return value; } else { return value && value[0] ? value[0] : null; } } public getQueryVariables(): IQueryParams { return this.$request.query; } public getQueryVariable(name: string): IQueryParamValue { return this.$request.query[name]; } public getQuerySingleVariable(name: string): string { let v: IQueryParamValue = this.getQueryVariable(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return v; } if (Array.isArray(v)) { v = v[0]; if (typeof v === 'string') { return v; } } return null; // Not parseable } public getQueryMultiVariable(name: string): string[] { let v: IQueryParamValue = this.getQueryVariable(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return [v]; } if (Array.isArray(v)) { let out: string[] = []; for (let i: number = 0; i < v.length; i++) { if (typeof v[i] === 'string') { out.push(v[i] as string); } } return out; } return null; // Not parseable } public getParams(): IParameterMap { return this.$request.params; } public getParam(name: string): string | string[] { return this.$request.params[name]; } /** * Gets a URL parameter as string. If the value is multiple * then only the first value is returned. * This is a convenience method over `getParam` which returns a union. */ public getURLSingleParam(name: string): string { let v: string | string[] = this.getParam(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return v; } else { return v[0]; } } /** * Gets a URL parameter as string[]. * This is a convenience method over `getParam` which returns a union. */ public getURLMultiParam(name: string): string[] { let v: string | string[] = this.getParam(name); if (v === undefined || v === null) return null; if (typeof v === 'string') { return [v]; } else { return v; } } public getIP(): string { return this.$request.ip; } public getForwardedIP(): string { return this.getHeader('X-Forwarded-For'); } public getHostname(): string { return this.$request.hostname; } public getMethod(): string { return this.$request.method; } public getURL(): string { return this.$request.originalUrl; } public isSecure(): boolean { return this.$request.secure; } public pipe(destination: Writable): Writable { return this.$request.pipe(destination); } public unpipe(source: Writable): void { this.$request.unpipe(source); } public getRequestSource(): express.Request { return this.$request; } /** * Reads the Page/Position DB headers if present * Will return null if headers are not parseable. * * @since 9.1.0 * @returns {IDatabasePosition} */ public getDatabasePosition(): IDatabasePosition { let pageStr: string = this.getHeader('X-DATABASE-PAGE'); let positionStr: string = this.getHeader('X-DATABASE-POSITION'); let page: number = null; let position: number = null; let out: IDatabasePosition = null; if (pageStr && positionStr) { page = parseInt(pageStr); position = parseInt(positionStr); if (isNaN(page) || isNaN(position)) { page = null; position = null; } } if (page && position) { out = { page, position }; } return out; } public async getAuthenticationToken(): Promise { let authHeader: string = getInstance().getConfig().authentication_header; let tdata: TAuthToken = null; try { tdata = (await getInstance().getTokenManager().verify(new Token(this.getHeader(authHeader)))) as TAuthToken; } catch (ex) { let error: ResponseData = null; if (ex && ex.name) { switch (ex.name) { case JWTError.ERR_EXPIRED: error = new ResponseData(StatusCode.ERR_UNAUTHORIZED, { code: ex.name, reason: ex.message }); break; case JWTError.ERR_GENERIC: error = new ResponseData(StatusCode.ERR_UNAUTHORIZED, { code: ex.name, reason : ex.message }); break; } } if (error === null) { let ie: InternalError = new InternalError(ex); error = new ResponseData(ie.getHTTPCode(), { code: ie.getCode(), reason: ie.getMessage() }); } throw error; } return tdata; } }