Properly indent all source files
This commit is contained in:
@ -9,111 +9,111 @@ import globals from '../globals';
|
||||
// req.authDetails returns eg. {methods: ['basic'], username: 'johndoe', level: 'write'}
|
||||
|
||||
module.exports = async (req, res, next) => {
|
||||
let givenMethod = ''; // authorization method given by client, basic taken preferred
|
||||
let user = {name: '', level: '', id: '', location: '', models: []}; // user object
|
||||
let givenMethod = ''; // authorization method given by client, basic taken preferred
|
||||
let user = {name: '', level: '', id: '', location: '', models: []}; // user object
|
||||
|
||||
// test authentications
|
||||
const userBasic = await basic(req, next);
|
||||
// test authentications
|
||||
const userBasic = await basic(req, next);
|
||||
|
||||
if (userBasic) { // basic available
|
||||
givenMethod = 'basic';
|
||||
user = userBasic;
|
||||
}
|
||||
else { // if basic not available, test key
|
||||
const userKey = await key(req, next);
|
||||
if (userKey) {
|
||||
givenMethod = 'key';
|
||||
user = userKey;
|
||||
}
|
||||
}
|
||||
if (userBasic) { // basic available
|
||||
givenMethod = 'basic';
|
||||
user = userBasic;
|
||||
}
|
||||
else { // if basic not available, test key
|
||||
const userKey = await key(req, next);
|
||||
if (userKey) {
|
||||
givenMethod = 'key';
|
||||
user = userKey;
|
||||
}
|
||||
}
|
||||
|
||||
req.auth = (res, levels, method = 'all') => {
|
||||
if (givenMethod === method || (method === 'all' && givenMethod !== '')) { // method is available
|
||||
if (levels.indexOf(user.level) > -1) { // level is available
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
res.status(403).json({status: 'Forbidden'});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.status(401).json({status: 'Unauthorized'});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
req.auth = (res, levels, method = 'all') => {
|
||||
if (givenMethod === method || (method === 'all' && givenMethod !== '')) { // method is available
|
||||
if (levels.indexOf(user.level) > -1) { // level is available
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
res.status(403).json({status: 'Forbidden'});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else {
|
||||
res.status(401).json({status: 'Unauthorized'});
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
req.authDetails = {
|
||||
method: givenMethod,
|
||||
username: user.name,
|
||||
level: user.level,
|
||||
id: user.id,
|
||||
location: user.location,
|
||||
models: user.models
|
||||
};
|
||||
req.authDetails = {
|
||||
method: givenMethod,
|
||||
username: user.name,
|
||||
level: user.level,
|
||||
id: user.id,
|
||||
location: user.location,
|
||||
models: user.models
|
||||
};
|
||||
|
||||
next();
|
||||
next();
|
||||
}
|
||||
|
||||
|
||||
function basic (req, next): any { // checks basic auth and returns changed user object
|
||||
return new Promise(resolve => {
|
||||
const auth = basicAuth(req);
|
||||
if (auth !== undefined) { // basic auth available
|
||||
UserModel.find({name: auth.name, status: globals.status.new}).lean().exec( (err, data: any) => { // find user
|
||||
if (err) return next(err);
|
||||
if (data.length === 1) { // one user found
|
||||
bcrypt.compare(auth.pass, data[0].pass, (err, res) => { // check password
|
||||
if (err) return next(err);
|
||||
if (res === true) { // password correct
|
||||
resolve({
|
||||
level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
|
||||
name: data[0].name,
|
||||
id: data[0]._id.toString(),
|
||||
location: data[0].location,
|
||||
models: data[0].models
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
return new Promise(resolve => {
|
||||
const auth = basicAuth(req);
|
||||
if (auth !== undefined) { // basic auth available
|
||||
UserModel.find({name: auth.name, status: globals.status.new}).lean().exec( (err, data: any) => { // find user
|
||||
if (err) return next(err);
|
||||
if (data.length === 1) { // one user found
|
||||
bcrypt.compare(auth.pass, data[0].pass, (err, res) => { // check password
|
||||
if (err) return next(err);
|
||||
if (res === true) { // password correct
|
||||
resolve({
|
||||
level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
|
||||
name: data[0].name,
|
||||
id: data[0]._id.toString(),
|
||||
location: data[0].location,
|
||||
models: data[0].models
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function key (req, next): any { // checks API key and returns changed user object
|
||||
return new Promise(resolve => {
|
||||
if (req.query.key !== undefined) { // key available
|
||||
UserModel.find({key: req.query.key, status: globals.status.new}).lean().exec( (err, data: any) => { // find user
|
||||
if (err) return next(err);
|
||||
if (data.length === 1) { // one user found
|
||||
resolve({
|
||||
level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
|
||||
name: data[0].name,
|
||||
id: data[0]._id.toString(),
|
||||
location: data[0].location,
|
||||
models: data[0].models
|
||||
});
|
||||
if (!/^\/api/m.test(req.url)){
|
||||
delete req.query.key; // delete query parameter to avoid interference with later validation
|
||||
}
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
return new Promise(resolve => {
|
||||
if (req.query.key !== undefined) { // key available
|
||||
UserModel.find({key: req.query.key, status: globals.status.new}).lean().exec( (err, data: any) => { // find user
|
||||
if (err) return next(err);
|
||||
if (data.length === 1) { // one user found
|
||||
resolve({
|
||||
level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
|
||||
name: data[0].name,
|
||||
id: data[0]._id.toString(),
|
||||
location: data[0].location,
|
||||
models: data[0].models
|
||||
});
|
||||
if (!/^\/api/m.test(req.url)){
|
||||
delete req.query.key; // delete query parameter to avoid interference with later validation
|
||||
}
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
else {
|
||||
resolve(null);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -2,8 +2,8 @@ import {parseAsync} from 'json2csv';
|
||||
import flatten from './flatten';
|
||||
|
||||
export default function csv(input: any[], f: (err, data) => void) { // parse JSON to CSV
|
||||
parseAsync(input.map(e => flatten(e)), {includeEmptyRows: true})
|
||||
.then(csv => f(null, csv))
|
||||
.catch(err => f(err, null));
|
||||
parseAsync(input.map(e => flatten(e)), {includeEmptyRows: true})
|
||||
.then(csv => f(null, csv))
|
||||
.catch(err => f(err, null));
|
||||
}
|
||||
|
||||
|
@ -1,42 +1,42 @@
|
||||
import globals from '../globals';
|
||||
|
||||
export default function flatten (data, keepArray = false) { // flatten object: {a: {b: true}} -> {a.b: true}
|
||||
const result = {};
|
||||
function recurse (cur, prop) {
|
||||
if (Object(cur) !== cur || Object.keys(cur).length === 0) { // simple value
|
||||
result[prop] = cur;
|
||||
}
|
||||
else if (prop === `${globals.spectrum.spectrum}.${globals.spectrum.dpt}`) { // convert spectrum for ML
|
||||
result[prop + '.labels'] = cur.map(e => parseFloat(e[0]));
|
||||
result[prop + '.values'] = cur.map(e => parseFloat(e[1]));
|
||||
}
|
||||
else if (Array.isArray(cur)) {
|
||||
if (keepArray) {
|
||||
result[prop] = cur;
|
||||
}
|
||||
else { // array to string
|
||||
if (cur.length && (Object(cur[0]) !== cur || Object.keys(cur[0]).length === 0)) { // array of non-objects
|
||||
result[prop] = '[' + cur.join(', ') + ']';
|
||||
}
|
||||
else {
|
||||
let l = 0;
|
||||
for(let i = 0, l = cur.length; i < l; i++)
|
||||
recurse(cur[i], prop + "[" + i + "]");
|
||||
if (l == 0)
|
||||
result[prop] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
else { // object
|
||||
let isEmpty = true;
|
||||
for (let p in cur) {
|
||||
isEmpty = false;
|
||||
recurse(cur[p], prop ? prop+"."+p : p);
|
||||
}
|
||||
if (isEmpty && prop)
|
||||
result[prop] = {};
|
||||
}
|
||||
}
|
||||
recurse(data, '');
|
||||
return result;
|
||||
}
|
||||
const result = {};
|
||||
function recurse (cur, prop) {
|
||||
if (Object(cur) !== cur || Object.keys(cur).length === 0) { // simple value
|
||||
result[prop] = cur;
|
||||
}
|
||||
else if (prop === `${globals.spectrum.spectrum}.${globals.spectrum.dpt}`) { // convert spectrum for ML
|
||||
result[prop + '.labels'] = cur.map(e => parseFloat(e[0]));
|
||||
result[prop + '.values'] = cur.map(e => parseFloat(e[1]));
|
||||
}
|
||||
else if (Array.isArray(cur)) {
|
||||
if (keepArray) {
|
||||
result[prop] = cur;
|
||||
}
|
||||
else { // array to string
|
||||
if (cur.length && (Object(cur[0]) !== cur || Object.keys(cur[0]).length === 0)) { // array of non-objects
|
||||
result[prop] = '[' + cur.join(', ') + ']';
|
||||
}
|
||||
else {
|
||||
let l = 0;
|
||||
for(let i = 0, l = cur.length; i < l; i++)
|
||||
recurse(cur[i], prop + "[" + i + "]");
|
||||
if (l == 0)
|
||||
result[prop] = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
else { // object
|
||||
let isEmpty = true;
|
||||
for (let p in cur) {
|
||||
isEmpty = false;
|
||||
recurse(cur[p], prop ? prop+"."+p : p);
|
||||
}
|
||||
if (isEmpty && prop)
|
||||
result[prop] = {};
|
||||
}
|
||||
}
|
||||
recurse(data, '');
|
||||
return result;
|
||||
}
|
||||
|
@ -4,86 +4,86 @@ import axios from 'axios';
|
||||
|
||||
export default class Mail{
|
||||
|
||||
static readonly address = 'definma@bosch-iot.com'; // email address
|
||||
static uri: string; // mail API URI
|
||||
static auth = {username: '', password: ''}; // mail API credentials
|
||||
static mailPass: string; // mail API generates password
|
||||
static readonly address = 'definma@bosch-iot.com'; // email address
|
||||
static uri: string; // mail API URI
|
||||
static auth = {username: '', password: ''}; // mail API credentials
|
||||
static mailPass: string; // mail API generates password
|
||||
|
||||
static init() {
|
||||
if (process.env.NODE_ENV === 'production') { // only send mails in production
|
||||
this.mailPass = Array(64).fill(0).map(() => Math.floor(Math.random() * 10)).join('');
|
||||
this.uri = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.uri;
|
||||
this.auth.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username;
|
||||
this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password;
|
||||
axios({ // get registered mail addresses
|
||||
method: 'get',
|
||||
url: this.uri + '/management/userDomainMapping',
|
||||
auth: this.auth
|
||||
}).then(res => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
if (res.data.addresses.indexOf(this.address) < 0) { // mail address not registered
|
||||
if (res.data.addresses.length) { // delete wrong registered mail address
|
||||
await axios({
|
||||
method: 'delete',
|
||||
url: this.uri + '/management/mailAddresses/' + res.data.addresses[0],
|
||||
auth: this.auth
|
||||
});
|
||||
}
|
||||
await axios({ // register right mail address
|
||||
method: 'post',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address,
|
||||
auth: this.auth
|
||||
});
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}).then(() => {
|
||||
return axios({ // set new mail password
|
||||
method: 'put',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address + '/password/' + this.mailPass,
|
||||
auth: this.auth
|
||||
});
|
||||
}).then(() => { // init done successfully
|
||||
console.info('Mail service established successfully');
|
||||
}).catch(err => { // somewhere an error occurred
|
||||
console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`,
|
||||
err.response.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
static init() {
|
||||
if (process.env.NODE_ENV === 'production') { // only send mails in production
|
||||
this.mailPass = Array(64).fill(0).map(() => Math.floor(Math.random() * 10)).join('');
|
||||
this.uri = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.uri;
|
||||
this.auth.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username;
|
||||
this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password;
|
||||
axios({ // get registered mail addresses
|
||||
method: 'get',
|
||||
url: this.uri + '/management/userDomainMapping',
|
||||
auth: this.auth
|
||||
}).then(res => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
if (res.data.addresses.indexOf(this.address) < 0) { // mail address not registered
|
||||
if (res.data.addresses.length) { // delete wrong registered mail address
|
||||
await axios({
|
||||
method: 'delete',
|
||||
url: this.uri + '/management/mailAddresses/' + res.data.addresses[0],
|
||||
auth: this.auth
|
||||
});
|
||||
}
|
||||
await axios({ // register right mail address
|
||||
method: 'post',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address,
|
||||
auth: this.auth
|
||||
});
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}).then(() => {
|
||||
return axios({ // set new mail password
|
||||
method: 'put',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address + '/password/' + this.mailPass,
|
||||
auth: this.auth
|
||||
});
|
||||
}).then(() => { // init done successfully
|
||||
console.info('Mail service established successfully');
|
||||
}).catch(err => { // somewhere an error occurred
|
||||
console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`,
|
||||
err.response.data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
static send (mailAddress, subject, content, f: (x?) => void = () => {}) { // callback executed empty or with error
|
||||
if (process.env.NODE_ENV === 'production') { // only send mails in production
|
||||
axios({
|
||||
method: 'post',
|
||||
url: this.uri + '/email',
|
||||
auth: this.auth,
|
||||
data: {
|
||||
recipients: [{to: mailAddress}],
|
||||
subject: {content: subject},
|
||||
body: {
|
||||
content: content,
|
||||
contentType: "text/html"
|
||||
},
|
||||
from: {
|
||||
eMail: this.address,
|
||||
password: this.mailPass
|
||||
}
|
||||
}
|
||||
}).then(() => {
|
||||
f();
|
||||
}).catch((err) => {
|
||||
f(err);
|
||||
});
|
||||
}
|
||||
else { // dev dummy replacement
|
||||
console.info('Sending mail to ' + mailAddress + ': -- ' + subject + ' -- ' + content);
|
||||
f();
|
||||
}
|
||||
}
|
||||
static send (mailAddress, subject, content, f: (x?) => void = () => {}) { // callback executed empty or with error
|
||||
if (process.env.NODE_ENV === 'production') { // only send mails in production
|
||||
axios({
|
||||
method: 'post',
|
||||
url: this.uri + '/email',
|
||||
auth: this.auth,
|
||||
data: {
|
||||
recipients: [{to: mailAddress}],
|
||||
subject: {content: subject},
|
||||
body: {
|
||||
content: content,
|
||||
contentType: "text/html"
|
||||
},
|
||||
from: {
|
||||
eMail: this.address,
|
||||
password: this.mailPass
|
||||
}
|
||||
}
|
||||
}).then(() => {
|
||||
f();
|
||||
}).catch((err) => {
|
||||
f(err);
|
||||
});
|
||||
}
|
||||
else { // dev dummy replacement
|
||||
console.info('Sending mail to ' + mailAddress + ': -- ' + subject + ' -- ' + content);
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user