Source: component/common.js

/**
 * This file is part of Sesatheque.
 *   Copyright 2014-2015, Association Sésamath
 *
 * Sesatheque is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License version 3
 * as published by the Free Software Foundation.
 *
 * Sesatheque is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Sesatheque (LICENCE.txt).
 * @see http://www.gnu.org/licenses/agpl.txt
 *
 *
 * Ce fichier fait partie de l'application Sésathèque, créée par l'association Sésamath.
 *
 * Sésathèque est un logiciel libre ; vous pouvez le redistribuer ou le modifier suivant
 * les termes de la GNU Affero General Public License version 3 telle que publiée par la
 * Free Software Foundation.
 * Sésathèque est distribué dans l'espoir qu'il sera utile, mais SANS AUCUNE GARANTIE ;
 * sans même la garantie tacite de QUALITÉ MARCHANDE ou d'ADÉQUATION à UN BUT PARTICULIER.
 * Consultez la GNU Affero General Public License pour plus de détails.
 * Vous devez avoir reçu une copie de la GNU General Public License en même temps que Sésathèque
 * (cf LICENCE.txt et http://vvlibri.org/fr/Analyse/gnu-affero-general-public-license-v3-analyse
 * pour une explication en français)
 */
'use strict'
// un module avec nos constantes et qq méthodes qui les utilisent

/**
 * Préfixe des routes, des clés de cache ou du header Authorization
 */
const defaultPrefix = 'sesalabSso'

/**
 * Pour obtenir une clé de cache, avec un nomble variable d'arguments
 */
function getKey (type, token) {
  if (!type || !token) throw new Error('Arguments invalides')
  const key = Array.prototype.slice.call(arguments).filter(arg => arg).join(':')
  return `${defaultPrefix}:${key}`
}

module.exports = {
  defaultPrefix,
  /**
   * Un préfixe pour le header Authorization, pour passer le token lors de l'appel du validate
   */
  xFerPrefix: defaultPrefix + 'Xfer',
  /**
   * timeout pour valider les tickets, en s
   */
  defaultTimeout: 10,
  /**
   * Retourne la clé de cache pour récupérer un authBundle à partir d'un token d'url
   * @param {string} token
   * @return {string} La clé de cache
   */
  getAuthBundleKey: (token) => getKey('authBundle', token),
  /**
   * Retourne le token de cette requête
   * @param req l'objet request d'express
   * @returns {string} Le token du header authorization s'il existe et que c'est un sesalabSso
   */
  getAuthToken: (req) => {
    const auth = req.get('authorization')
    if (auth) {
      const chunks = auth.split(' ')
      if (chunks[0] === defaultPrefix && chunks[1]) return chunks[1]
    }
  },
  /**
   * Retourne la clé de cache pour récupérer un sessionId à partir d'un authToken
   * @param {string} authToken
   * @return {string} La clé de cache
   */
  getSidKey: (authToken) => getKey('sid', authToken),
  /**
   * Retourne la clé de cache pour récupérer un user à partir d'un token d'url
   * @param {string} token
   * @return {string} La clé de cache
   */
  getUserKey: (token) => getKey('user', token)
}