Change first character of all end-of-line comments to upper case
This commit is contained in:
@ -4,22 +4,22 @@ import UserModel from '../models/user';
|
||||
import globals from '../globals';
|
||||
|
||||
|
||||
// appends req.auth(res, ['levels'], method = 'all')
|
||||
// which returns sends error message and returns false if unauthorized, otherwise true
|
||||
// req.authDetails returns eg. {methods: ['basic'], username: 'johndoe', level: 'write'}
|
||||
// Appends req.auth(res, ['levels'], method = 'all')
|
||||
// Which returns sends error message and returns false if unauthorized, otherwise true
|
||||
// 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
|
||||
// Test authentications
|
||||
const userBasic = await basic(req, next);
|
||||
|
||||
if (userBasic) { // basic available
|
||||
if (userBasic) { // Basic available
|
||||
givenMethod = 'basic';
|
||||
user = userBasic;
|
||||
}
|
||||
else { // if basic not available, test key
|
||||
else { // If basic not available, test key
|
||||
const userKey = await key(req, next);
|
||||
if (userKey) {
|
||||
givenMethod = 'key';
|
||||
@ -28,8 +28,8 @@ module.exports = async (req, res, next) => {
|
||||
}
|
||||
|
||||
req.auth = (res, levels, method = 'all') => {
|
||||
if (givenMethod === method || (method === 'all' && givenMethod !== '')) { // method is available
|
||||
if (levels.indexOf(user.level) > -1) { // level is available
|
||||
if (givenMethod === method || (method === 'all' && givenMethod !== '')) { // Method is available
|
||||
if (levels.indexOf(user.level) > -1) { // Level is available
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
@ -56,16 +56,16 @@ module.exports = async (req, res, next) => {
|
||||
}
|
||||
|
||||
|
||||
function basic (req, next): any { // checks basic auth and returns changed user object
|
||||
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 (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 (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
|
||||
if (res === true) { // Password correct
|
||||
resolve({
|
||||
level: Object.entries(globals.levels).find(e => e[1] === data[0].level)[0],
|
||||
name: data[0].name,
|
||||
@ -90,12 +90,12 @@ function basic (req, next): any { // checks basic auth and returns changed user
|
||||
});
|
||||
}
|
||||
|
||||
function key (req, next): any { // checks API key and returns changed user object
|
||||
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 (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
|
||||
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,
|
||||
@ -104,7 +104,7 @@ function key (req, next): any { // checks API key and returns changed user obje
|
||||
models: data[0].models
|
||||
});
|
||||
if (!/^\/api/m.test(req.url)){
|
||||
delete req.query.key; // delete query parameter to avoid interference with later validation
|
||||
delete req.query.key; // Delete query parameter to avoid interference with later validation
|
||||
}
|
||||
}
|
||||
else {
|
||||
|
@ -1,7 +1,7 @@
|
||||
import {parseAsync} from 'json2csv';
|
||||
import flatten from './flatten';
|
||||
|
||||
export default function csv(input: any[], f: (err, data) => void) { // parse JSON to CSV
|
||||
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));
|
||||
|
@ -1,12 +1,12 @@
|
||||
import globals from '../globals';
|
||||
|
||||
export default function flatten (data, keepArray = false) { // flatten object: {a: {b: true}} -> {a.b: true}
|
||||
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
|
||||
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
|
||||
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]));
|
||||
}
|
||||
@ -14,8 +14,8 @@ export default function flatten (data, keepArray = false) { // flatten object:
|
||||
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
|
||||
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 {
|
||||
@ -27,7 +27,7 @@ export default function flatten (data, keepArray = false) { // flatten object:
|
||||
}
|
||||
}
|
||||
}
|
||||
else { // object
|
||||
else { // Object
|
||||
let isEmpty = true;
|
||||
for (let p in cur) {
|
||||
isEmpty = false;
|
||||
|
@ -1,36 +1,36 @@
|
||||
import axios from 'axios';
|
||||
|
||||
// sends an email using the BIC service
|
||||
// Sends an email using the BIC service
|
||||
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
await axios({ // Register right mail address
|
||||
method: 'post',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address,
|
||||
auth: this.auth
|
||||
@ -43,22 +43,22 @@ export default class Mail{
|
||||
}
|
||||
});
|
||||
}).then(() => {
|
||||
return axios({ // set new mail password
|
||||
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
|
||||
}).then(() => { // Init done successfully
|
||||
console.info('Mail service established successfully');
|
||||
}).catch(err => { // somewhere an error occurred
|
||||
}).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
|
||||
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',
|
||||
@ -81,7 +81,7 @@ export default class Mail{
|
||||
f(err);
|
||||
});
|
||||
}
|
||||
else { // dev dummy replacement
|
||||
else { // Dev dummy replacement
|
||||
console.info('Sending mail to ' + mailAddress + ': -- ' + subject + ' -- ' + content);
|
||||
f();
|
||||
}
|
||||
|
Reference in New Issue
Block a user