All files / src/server/middleware sof-scope.middleware.js

96.15% Statements 25/26
97.05% Branches 33/34
100% Functions 4/4
96.15% Lines 25/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 836x 6x 6x 6x                   4x               2x         1x   1x                       4x   4x       4x             6x 734x   734x 726x         8x 5x       3x   4x 4x 4x   4x   4x 3x     1x      
const scopeChecker = require('@asymmetrik/sof-scope-checker');
const noOpMiddleware = require('./noop.middleware.js');
const { INTERACTIONS } = require('../../constants');
const errors = require('../utils/error.utils');
 
/**
 * @name deriveActionFromInteraction
 * @summary Given an interaction, what type of action will be performed
 * on this particular route, either read, write, or *
 * @param {String} interaction
 * @return {String} action needed to access a route
 */
function deriveActionFromInteraction(interaction) {
  switch (interaction) {
    case INTERACTIONS.SEARCH:
    case INTERACTIONS.HISTORY:
    case INTERACTIONS.SEARCH_BY_ID:
    case INTERACTIONS.EXPAND_BY_ID:
    case INTERACTIONS.HISTORY_BY_ID:
    case INTERACTIONS.SEARCH_BY_VID:
    case INTERACTIONS.OPERATIONS_GET:
      return 'read';
    case INTERACTIONS.CREATE:
    case INTERACTIONS.UPDATE:
    case INTERACTIONS.DELETE:
    case INTERACTIONS.OPERATIONS_POST:
      return 'write';
    default:
      return '*';
  }
}
 
/**
 * @name parseScopes
 * @summary Parse scopes from a user context
 * @param {Object} user
 * @param {String} scopeKey
 * @return {Array<String>} scopes assigned to a particular user
 */
function parseScopes(user = {}, scopeKey = 'scope') {
  let scopes = user[scopeKey];
 
  Iif (Array.isArray(scopes)) {
    return scopes;
  }
 
  return typeof scopes === 'string' ? scopes.split(/[, ]/) : [];
}
 
/**
 * @name exports
 * @summary SOF Scope Middleware function
 */
module.exports = function sofScopeCheckMiddleware(options = {}) {
  let { route = {}, name = '', auth = {} } = options;
  // Disable validation when in a test environment
  if (process.env.NODE_ENV === 'test') {
    return noOpMiddleware;
  }
  // If we do not have Smart on FHIR Authorization enabled,
  // disable this middleware. If you are using some other system for scopes,
  // feel free to remove this and add your own checks
  if (auth.type !== 'smart' || auth.strategy === undefined) {
    return noOpMiddleware;
  }
 
  // At this point, we have determined we want Smart on FHIR authentication
  return function sofScopeMiddleware(req, res, next) {
    // name is lowercased, we want upper, foo -> Foo
    let resource = name.slice(0, 1).toUpperCase() + name.slice(1);
    let action = deriveActionFromInteraction(route.interaction);
    let scopes = parseScopes(req && req.user, auth.customScopeKey);
    // Check if they have permission
    let { error } = scopeChecker(resource, action, scopes);
 
    if (error) {
      return next(errors.unauthorized(error.message, req.params && req.params.version));
    }
 
    return next();
  };
};