Skip to content

Commit

Permalink
Merge pull request #394 from hms-dbmi-cellenics/1903-add-two-checkbox…
Browse files Browse the repository at this point in the history
…es-for-compliance

1903 add two checkboxes for compliance
  • Loading branch information
cosa65 authored Jul 19, 2022
2 parents 683c14f + 81de9bc commit 010588b
Show file tree
Hide file tree
Showing 8 changed files with 242 additions and 22 deletions.
37 changes: 29 additions & 8 deletions src/api.v2/middlewares/authMiddlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ const { CacheMissError } = require('../../cache/cache-utils');
const { UnauthorizedError, UnauthenticatedError } = require('../../utils/responses');

const UserAccess = require('../model/UserAccess');
const NotAgreedToTermsError = require('../../utils/responses/NotAgreedToTermsError');
const { BIOMAGE_DOMAIN_NAMES } = require('../../utils/constants');

// Throws if the user isnt authenticated
const checkUserAuthenticated = (req, next) => {
if (!req.user) {
next(new UnauthenticatedError('The request does not contain an authentication token.'));
return false;
}

return true;
};

// Throws if the user hasnt agreed to the privacy policy yet
const checkForPrivacyPolicyAgreement = (req, next) => {
const isBiomageDeployment = BIOMAGE_DOMAIN_NAMES.includes(config.domainName) || config.clusterEnv === 'development';

if (req.user['custom:agreed_terms'] !== 'true' && isBiomageDeployment) {
next(new NotAgreedToTermsError('The user hasnt agreed to the privacy policy yet.'));
return false;
}

return true;
};

/**
* General authorization middleware. Resolves with nothing on
Expand Down Expand Up @@ -51,10 +75,8 @@ const authorize = async (userId, resource, method, experimentId) => {
* Calls `authorize()` internally.
*/
const expressAuthorizationMiddleware = async (req, res, next) => {
if (!req.user) {
next(new UnauthenticatedError('The request does not contain an authentication token.'));
return;
}
if (!checkUserAuthenticated(req, next)) return;
if (!checkForPrivacyPolicyAgreement(req, next)) return;

try {
await authorize(req.user.sub, req.url, req.method, req.params.experimentId);
Expand All @@ -65,10 +87,9 @@ const expressAuthorizationMiddleware = async (req, res, next) => {
};

const expressAuthenticationOnlyMiddleware = async (req, res, next) => {
if (!req.user) {
next(new UnauthenticatedError('The request does not contain an authentication token.'));
return;
}
if (!checkUserAuthenticated(req, next)) return;
if (!checkForPrivacyPolicyAgreement(req, next)) return;

next();
};

Expand Down
4 changes: 2 additions & 2 deletions src/config/test-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ module.exports = {
clusterEnv: 'test',
awsAccountId: '000000000000',
awsRegion: 'eu-west-1',
corsOriginUrl: 'https://scp.mockDomainName.com',
domainName: 'scp.mockDomainName.com',
corsOriginUrl: 'https://scp.biomage.net',
domainName: 'scp.biomage.net',
podName: 'test',
sandboxId: 'default',
adminSub: 'mockAdminSub',
Expand Down
Loading

0 comments on commit 010588b

Please sign in to comment.