<commit_before>/**
 * Copyright (c) 2016, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or
 * https://opensource.org/licenses/BSD-3-Clause
 */

/**
 * view/loadView.js
 *
 * Maps url to jade files, which are relative to the views directory
 */

'use strict'; // eslint-disable-line strict

const SSOConfig = require('../db/index').SSOConfig;
const User = require('../db/index').User;
const Profile = require('../db/index').Profile;
const SamlStrategy = require('passport-saml').Strategy;
const viewConfig = require('../viewConfig');
const jwtUtil = require('../utils/jwtUtil');
const httpStatus = require('./constants').httpStatus;
const url = require('url');

// protected urls
const viewmap = {
  '/aspects': 'admin',
  '/aspects/:key': 'admin',
  '/aspects/:key/edit': 'admin',
  '/subjects': 'admin',
  '/subjects/:key': 'admin',
  '/subjects/:key/edit': 'admin',
  '/samples': 'admin',
  '/samples/:key': 'admin',
  '/samples/:key/edit': 'admin',
  '/perspectives': 'perspective/perspective',
  '/perspectives/:key': 'perspective/perspective',
  '/tokens/new': 'tokens/new',
  '/rooms': 'rooms/list',
  '/rooms/types': 'rooms/types',
  '/rooms/types/:key': 'rooms/type',
  '/rooms/:key': 'rooms',
};

/**
 * Checks if the user is authenticated and and there is a valid session
 * @param  {Object}   req  Request object
 * @param  {Object}   res  Response object
 * @param  {Function} next The next middleware function in the stack
 * @returns {Function}  next function if authenticated, else redirect to login
 * after setting redirect url query parameter.
 */
function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }

  return res.redirect('/login?ru=' + encodeURIComponent(
    req.protocol + '://' + req.get('host') + req.originalUrl)
  );
}

/**
 * Authentication for validation SAML responses
 * Used in SAML SSO Stategy
 * Will provision user if no matching user is found
 *
 * @param  {Object}   userProfile - User profile parameters
 * @param  {Function} done - Callback function
 */
function samlAuthentication(userProfile, done) {
  User.findOne({ where: { email: userProfile.email } })
  .then((user) => {
    if (!user) {
      return Profile.findOrCreate({ where: { name: 'RefocusSSOUser' } });
    }

    // profile already attached - default scope applied on find
    return done(null, user);
  })
  .spread((profile) => {

    /**
     * default scope not applied on create, so we use User.find after this to
     * get profile attached to user.
     */
    return User.create({
      email: userProfile.email,
      profileId: profile.id,
      name: userProfile.email,
      password: <PASSWORD>,
      sso: true,
    });
  })
  .then((createdUser) =>
    User.findById(createdUser.id) // to get profile name with user object
  )<commit_msg>do not use findOrCreate (#691)<commit_after>/**
 * Copyright (c) 2016, salesforce.com, inc.
 * All rights reserved.
 * Licensed under the BSD 3-Clause license.
 * For full license text, see LICENSE.txt file in the repo root or
 * https://opensource.org/licenses/BSD-3-Clause
 */

/**
 * view/loadView.js
 *
 * Maps url to jade files, which are relative to the views directory
 */

'use strict'; // eslint-disable-line strict

const SSOConfig = require('../db/index').SSOConfig;
const User = require('../db/index').User;
const Profile = require('../db/index').Profile;
const SamlStrategy = require('passport-saml').Strategy;
const viewConfig = require('../viewConfig');
const jwtUtil = require('../utils/jwtUtil');
const httpStatus = require('./constants').httpStatus;
const url = require('url');

// protected urls
const viewmap = {
  '/aspects': 'admin',
  '/aspects/:key': 'admin',
  '/aspects/:key/edit': 'admin',
  '/subjects': 'admin',
  '/subjects/:key': 'admin',
  '/subjects/:key/edit': 'admin',
  '/samples': 'admin',
  '/samples/:key': 'admin',
  '/samples/:key/edit': 'admin',
  '/perspectives': 'perspective/perspective',
  '/perspectives/:key': 'perspective/perspective',
  '/tokens/new': 'tokens/new',
  '/rooms': 'rooms/list',
  '/rooms/types': 'rooms/types',
  '/rooms/types/:key': 'rooms/type',
  '/rooms/:key': 'rooms',
};

/**
 * Checks if the user is authenticated and and there is a valid session
 * @param  {Object}   req  Request object
 * @param  {Object}   res  Response object
 * @param  {Function} next The next middleware function in the stack
 * @returns {Function}  next function if authenticated, else redirect to login
 * after setting redirect url query parameter.
 */
function ensureAuthenticated(req, res, next) {
  if (req.isAuthenticated()) {
    return next();
  }

  return res.redirect('/login?ru=' + encodeURIComponent(
    req.protocol + '://' + req.get('host') + req.originalUrl)
  );
}

/**
 * Authentication for validation SAML responses
 * Used in SAML SSO Stategy
 * Will provision user if no matching user is found
 *
 * @param  {Object}   userProfile - User profile parameters
 * @param  {Function} done - Callback function
 */
function samlAuthentication(userProfile, done) {
  User.findOne({ where: { email: userProfile.email } })
  .then((user) => {
    if (!user) {
      return Profile.findOne({ where: { name: 'RefocusSSOUser' } });
    }

    // profile already attached - default scope applied on find
    return done(null, user);
  })
  .then((foundProfile) => {
    if (foundProfile) {
      return foundProfile;
    }

    return Profile.create({ name: 'RefocusSSOUser' });
  })
  .then((profile) => {

    /**
     * default scope not applied on create, so we use User.find after this to
     * get profile attached to user.
     */
    return User.create({
      email: userProfile.email,
      profileId: profile.id,
      name: userProfile.email,
      password: <PASSWORD>,
      sso: true,
    });
  })
  .then((createdUser) =>
    User.findById(createdUser.id) // to get profile name with user object
  )
{'fraction_non_alphanumeric': 0.15803541597059806, 'fraction_numerical': 0.002505846976277982, 'mean_word_length': 3.4054451802796173, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4465055', 'n_tokens_mistral': 1860, 'n_tokens_neox': 1852, 'n_words': 541}