Skip to content

Commit

Permalink
Merge pull request #31 from camicroscope/develop
Browse files Browse the repository at this point in the history
For 3.7.3
  • Loading branch information
birm authored May 1, 2020
2 parents 08a3209 + 5338521 commit ef869eb
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 21 deletions.
3 changes: 1 addition & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
FROM node:12-alpine
RUN apk add git
RUN mkdir /root/src
RUN npm install -g forever
COPY . /root/src
WORKDIR /root/src
RUN npm install
ARG viewer
RUN if [ -z ${viewer} ]; then git clone https://github.com/camicroscope/camicroscope.git; else git clone https://github.com/camicroscope/camicroscope.git --branch=$viewer; fi
EXPOSE 8010
CMD forever caracal.js
CMD node caracal.js
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Conslidated Backend, Auth, and Security Services
|MONGO_URI | mongo connection uri | mongodb://localhost |
|MONGO_DB | mongo db to use, default camic |
|GENERATE_KEY_IF_MISSING | automatic generate key in server in not found | false |
|ENABLE_SECURITY_AT| time at which to enable security; [see parsable times](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse)| (not active) |

## files used
key/key and key/key.pub are used for internal jwts for this service. You can use key/make_key.sh to generate these, or otherwise add your own
Expand Down
6 changes: 5 additions & 1 deletion caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ app.use(function(req, res, next) {
// auth related services
app.get('/auth/Token/check', auth.jwkTokenTrade(auth.CLIENT, auth.PRIKEY, userFunction));
app.get('/auth/Token/renew', auth.tokenTrade(auth.PUBKEY, auth.PRIKEY, userFunction));
app.get('/auth/Token/proto', auth.firstSetupUserSignupExists());


// public files, don't use login handler here
app.use(express.static('static'));
Expand Down Expand Up @@ -161,11 +163,13 @@ app.use('/data', function(req, res, next) {

// error handler
app.use(function(err, req, res, next) {
console.error(JSON.stringify(err));
let statusCode = err.statusCode || 500;
// wrap strings in a json
if (typeof err === 'string' || err instanceof String) {
err = {'error': err};
console.error(err)
} else {
console.error(err.error || err.message || err.toString());
}
res.status(statusCode).json(err);
});
Expand Down
36 changes: 28 additions & 8 deletions handlers/authHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ const {execSync} = require('child_process');
const preCommand = "openssl req -subj ";
const postCommand = " -x509 -nodes -newkey rsa:2048 -keyout ./keys/key -out ./keys/key.pub";
var JWK_URL = process.env.JWK_URL;
var DISABLE_SEC = process.env.DISABLE_SEC || false;
var DISABLE_SEC = (process.env.DISABLE_SEC === 'true') || false;
var AUD = process.env.AUD || false;
var ISS = process.env.ISS || false;
var EXPIRY = process.env.EXPIRY || '1d';
var DEFAULT_USER_TYPE = process.env.DEFAULT_USER_TYPE || 'Null';
var PUBKEY;
var PRIKEY;
var GENERATE_KEY_IF_MISSING = process.env.GENERATE_KEY_IF_MISSING || false;
var CLIENT;
var GENERATE_KEY_IF_MISSING = (process.env.GENERATE_KEY_IF_MISSING === 'true') || false;
var ENABLE_SECURITY_AT = (process.env.ENABLE_SECURITY_AT ? process.env.ENABLE_SECURITY_AT : "") || false;

if (!fs.existsSync('./keys/key') && !fs.existsSync('./keys/key.pub') && GENERATE_KEY_IF_MISSING) {
try {
Expand All @@ -31,7 +33,7 @@ try {
if (fs.existsSync(prikeyPath)) {
PRIKEY = fs.readFileSync(prikeyPath, 'utf8');
} else {
if (DISABLE_SEC) {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
PRIKEY = '';
console.warn('prikey null since DISABLE_SEC and no prikey provided');
} else {
Expand All @@ -47,7 +49,7 @@ try {
if (fs.existsSync(prikeyPath)) {
var PUBKEY = fs.readFileSync(prikeyPath, 'utf8');
} else {
if (DISABLE_SEC) {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
PUBKEY = '';
console.warn('pubkey null since DISABLE_SEC and no prikey provided');
} else {
Expand All @@ -57,18 +59,20 @@ try {
} catch (err) {
console.error(err);
}

if (DISABLE_SEC && !JWK_URL) {
var CLIENT = jwksClient({
CLIENT = jwksClient({
jwksUri: 'https://www.googleapis.com/oauth2/v3/certs', // a default value
});
} else if (JWK_URL) {
var CLIENT = jwksClient({
CLIENT = jwksClient({
jwksUri: JWK_URL,
});
} else {
console.error('need JWKS URL (JWK_URL)');
process.exit(1);
}
}

const getToken = function(req) {
if (req.headers.authorization &&
req.headers.authorization.split(' ')[0] === 'Bearer') { // Authorization: Bearer g1jipjgi1ifjioj
Expand Down Expand Up @@ -168,7 +172,7 @@ function tokenTrade(checkKey, signKey, userFunction) {

function loginHandler(checkKey) {
return function(req, res, next) {
if (DISABLE_SEC) {
if (DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
let token = jwt.decode(getToken(req)) || {};
req.tokenInfo = token;
req.userType = token.userType || DEFAULT_USER_TYPE || 'Null';
Expand Down Expand Up @@ -240,12 +244,28 @@ function editHandler(dataField, filterField, attrField) {
};
}

function firstSetupUserSignupExists() {
return function(req, res) {
if (ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
res.send({
'exists': true,
});
} else {
res.send({
'exists': false,
});
}
};
}


auth = {};
auth.jwkTokenTrade = jwkTokenTrade;
auth.tokenTrade = tokenTrade;
auth.filterHandler = filterHandler;
auth.loginHandler = loginHandler;
auth.editHandler = editHandler;
auth.firstSetupUserSignupExists = firstSetupUserSignupExists;
auth.CLIENT = CLIENT;
auth.PRIKEY = PRIKEY;
auth.PUBKEY = PUBKEY;
Expand Down
3 changes: 2 additions & 1 deletion handlers/dataHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ var mongo = require('mongodb');

var MONGO_URI = process.env.MONGO_URI || 'mongodb://localhost';
var MONGO_DB = process.env.MONGO_DB || 'camic';
var DISABLE_SEC = process.env.DISABLE_SEC || false;
var DISABLE_SEC = (process.env.DISABLE_SEC === 'true') || false;


function mongoFind(collection, query) {
return new Promise(function(res, rej) {
Expand Down
41 changes: 35 additions & 6 deletions handlers/filterFunction.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
function filterFunction(filter, data, attr, wildcard) {
// make filter an array
if (!Array.isArray(filter)) {
filter = [filter];
if (typeof filter==="string") {
try {
filter = JSON.parse(filter.replace(/'/g, '"'));
} catch (err) {
filter=[filter]; // make an array of the filter
}
}
if (filter.indexOf(wildcard) == -1) {
// is data an array?
if (Array.isArray(data)) {
// remove ones where does not match
data = data.filter((x) => (!x[attr] || filter.indexOf(x[attr]) >= 0) );
data = data.filter((x) => {
let list;
if (!x[attr]) {
return true;
}
try {
list=JSON.parse(x[attr].replace(/'/g, '"'));
return list.some((e) =>filter.indexOf(e) >= 0);
} catch (err) { // when list is not an array, but a string
list=x[attr];
return filter.indexOf(x[attr]) >= 0;
}
});
} else {
if (!data[attr] || filter.indexOf(data[attr]) >= 0) {
if (!data[attr]) {
data = data;
} else {
data = {};
let list;
try {
list=JSON.parse(data[attr].replace(/'/g, '"'));
if (list.some((e) => filter.indexOf(e) >= 0)) {
return data;
} else {
return {};
}
} catch (err) { // when list is not an array, but a string
if (filter.indexOf(data[attr]) >= 0) {
return data;
} else {
return {};
}
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions handlers/permssionHandler.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
var DISABLE_SEC = process.env.DISABLE_SEC || false;
var DISABLE_SEC = (process.env.DISABLE_SEC === 'true') || false;
var ENABLE_SECURITY_AT = (process.env.ENABLE_SECURITY_AT ? process.env.ENABLE_SECURITY_AT : "") || false;

function permissionHandler(permissionList, test=false) {
return function(req, res, next) {
if (!test &&DISABLE_SEC) {
if (!test && DISABLE_SEC || ENABLE_SECURITY_AT && Date.parse(ENABLE_SECURITY_AT) > Date.now()) {
req.permission_ok = true;
next();
} else {
Expand Down
2 changes: 1 addition & 1 deletion handlers/userFunction.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var ALLOW_PUBLIC = process.env.ALLOW_PUBLIC;
var ALLOW_PUBLIC = (process.env.ALLOW_PUBLIC === 'true');

var dataHandlers = require('./dataHandlers.js');
// userFunction -- used for login given id provider token
Expand Down

0 comments on commit ef869eb

Please sign in to comment.