From 4f0ea1f15b91e3192385ec12963a39ca9f8e97c1 Mon Sep 17 00:00:00 2001 From: rxbn_ Date: Thu, 1 Oct 2020 21:58:02 +0200 Subject: [PATCH] SSO module rewrite --- bin/sso/module.js | 54 ++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/bin/sso/module.js b/bin/sso/module.js index 0c3b84c..8ceaf87 100644 --- a/bin/sso/module.js +++ b/bin/sso/module.js @@ -6,42 +6,38 @@ * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -var functions = {}; -var request = require('request'); -var querystring = require('querystring'); +const methods = {}; +const bent = require('bent'); +const querystring = require('querystring'); + +const cfg = require(global['__dirname']+'/bin/config'); /** * authenticate user * @author Ruben Meyer - * @param {Object} obj obj (userId, userToken, appId, appSecret) - * @param {Function} callback obj (err, access => true, false) + * @async + * @param {Object} obj obj (userId, token, appId, appSecret) + * @return {Object} callback obj (err, access => {true, false}) */ -functions.authenticateUser = (obj, callback) => { - if(typeof callback !== 'function') callback = function() {}; +methods.authenticateUser = async (obj, callback) => { + let request = bent(cfg.sso.providerApi, 'POST', 'json', 200); - request({ - method: 'POST', - uri: global['gds'].cfg.sso.provider, - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ + try { + let post = await request('', { applicationId: obj.appId, applicationSecret: obj.appSecret, userId: obj.userId, - token: obj.userToken - }) - }, (err, res, body) => { - console.log('error:', err); // Print the error if one occurred - console.log('statusCode:', res && res.statusCode); // Print the response status code if a response was received - console.log('body:', body); // Print the HTML - try { - if(typeof body !== "object") body = JSON.parse(body); - return callback(err, body.access); - } catch(e) { - callback(new Error("Body is misformed")); - } - }); + token: obj.token + }); + + if(post.status == 200 && post.message == "msg.auth.authentication.successful") + return true; + } catch(err) { + // something went wrong + console.log(err); + return false; + } + }; /** @@ -51,12 +47,12 @@ functions.authenticateUser = (obj, callback) => { * @param {Object} obj obj(url, appId) * @param {Function} callback string(url) */ -functions.createAuthentication = (obj) => { +methods.createAuthentication = (obj) => { let nUrl = { redirectUrl: obj.url, appId: obj.appId }; - return global['gds'].cfg.sso.provider+"?"+querystring.stringify(nUrl); + return cfg.sso.providerUI+"?"+querystring.stringify(nUrl); }; ////////////////////////////////////////////////////////////////////////////////////////////////////////////////