/**
* 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'
const { getSidKey } = require('./common')
module.exports = function (component) {
component.service('$sesalabSso', function () {
return {
/**
* Efface tous les authTokens en session
* @param context
*/
flushAuthTokens: (context) => {
context.session.authTokensByOrigin = {}
},
/**
* Retourne un tableau de tous les authBundles (baseUrl + token pour s'authentifier dessus)
* @param {Context} context
* @returns {AuthToken[]}
*/
getAuthBundles: (context) => {
if (!context.session.authTokensByOrigin) return []
const origins = Object.keys(context.session.authTokensByOrigin)
if (!origins.length) return []
return origins.map(origin => ({
baseUrl: origin,
token: context.session.authTokensByOrigin[origin]
}))
},
/**
* Sur un serveur, retourne le authToken du user courant pour une origine donnée (baseUrl)
* @param {Context} context
* @param {string} origin Le baseUrl de l'appli cliente sesalabSso concernée
* @returns {string|undefined} Le token
*/
getAuthToken: (context, origin) => {
return context.session.authTokensByOrigin && context.session.authTokensByOrigin[origin]
},
/**
* Retourne un objet façon hashTable avec une liste de {[baseUrl]: token}
* @param {Context} context
* @returns {Object}
*/
getAuthTokensByOrigin: (context) => Object.assign({}, context.session.authTokensByOrigin),
getSidKey,
/**
* Met en session un token
* @param context
* @param {string|object} baseUrl on peut passer un authBundle avec {baseUrl, token}
* @param {string} token
*/
setAuthToken: (context, baseUrl, token) => {
if (!context) throw new Error('contexte absent')
if (!baseUrl) throw new Error('paramètres invalides')
if (typeof baseUrl === 'object') {
if (baseUrl.baseUrl && baseUrl.token) {
token = baseUrl.token
baseUrl = baseUrl.baseUrl
} else {
throw new Error('paramètres invalides')
}
}
if (typeof baseUrl === 'string' && typeof token === 'string') {
// On pourrait vérifier que baseUrl est bien dans une liste de trucs connus,
// mais ici c'est lui qui nous autorise à passer des requêtes authentifiées chez lui,
// pas grand risque pour nous.
if (!context.session.authTokensByOrigin) context.session.authTokensByOrigin = {}
if (baseUrl.substr(-1) !== '/') baseUrl += '/'
context.session.authTokensByOrigin[baseUrl] = token
}
}
}
})
}
/**
* @typedef AuthToken
* @property {string} baseUrl
* @property {string} token Le token permettant faire des appels authentifié sur l'api de baseUrl
*/