diff --git a/requests.js b/requests.js index 7034ef026ec871f00546d64771f09936b33f66fb..c2d8579b319a542c994b6a8572a07bf245f8bf18 100644 --- a/requests.js +++ b/requests.js @@ -2,7 +2,7 @@ const rp = require('request-promise'); // Modules propres -var { modifyChan } = require('./connection-db'); +var { modifyChan, getChanByState } = require('./connection-db'); // Configurations const config = require('./config'); @@ -54,6 +54,37 @@ function getGroupById(token, id) { }).catch(err => { console.error(err) }) } +// Récupération d'un token +function getFirstToken(code, state) { + + const options = { + url: 'https://auth.viarezo.fr/oauth/token', + method: 'POST', + headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, + form: { + grant_type: 'authorization_code', + code: code, + redirect_uri: config.website.protocol + '://' + config.website.hostname + '/auth', + client_id: config.oauth2.clientid, + client_secret: config.oauth2.secretid + } + } + + return Promise.all([ + rp(options), + getChanByState(state) + ]).then(([body, chan]) => { + if (!chan) return + rep = JSON.parse(body); + chan.token = rep.access_token; + chan.refresh = rep.refresh_token; + chan.expiration = rep.expires_at; + chan.state = ''; + return modifyChan(chan) + }) +} + + // Récupération d'un nouveau token function getNewToken(chan) { @@ -79,4 +110,4 @@ function getNewToken(chan) { }) }; -module.exports = { getBirthdays, sendRequest, searchGroups, getGroupById, getNewToken }; \ No newline at end of file +module.exports = { getBirthdays, sendRequest, searchGroups, getGroupById, getFirstToken, getNewToken }; \ No newline at end of file diff --git a/schedule.js b/schedule.js new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/website.js b/website.js index bc608cbcd699d48caea40658ff180c3e015d06dd..77cbe157314a6740a784c3f737ff60b154ae3be7 100644 --- a/website.js +++ b/website.js @@ -1,16 +1,16 @@ // Modules extérieurs var app = require('express')(); -var rp = require('request-promise'); // Modules propres var bot = require('./telegram'); var { modifyChan, getChanByState } = require('./connection-db'); +var { getFirstToken } = require('./requests'); // Configurations const config = require('./config'); -app.listen(80, '127.0.0.1', () => { +app.listen(config.website.port, '127.0.0.1', () => { console.log(`[express] Website is up and accessible on ${config.website.protocol}://${config.website.hostname}/`); }) @@ -41,30 +41,7 @@ app.get('/auth', function (req, res) { if (!req.query.code || !req.query.state) return res.sendFile(`${__dirname}/auth.html`) - const options = { - url: 'https://auth.viarezo.fr/oauth/token', - method: 'POST', - headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, - form: { - grant_type: 'authorization_code', - code: req.query.code, - redirect_uri: config.website.protocol + '://' + config.website.hostname + '/auth', - client_id: config.oauth2.clientid, - client_secret: config.oauth2.secretid - } - } - - return rp(options).then(body => { - return Promise.all([body, getChanByState(req.query.state)]) - }).then(([body, chan]) => { - if (!chan) return - rep = JSON.parse(body); - chan.token = rep.access_token; - chan.refresh = rep.refresh_token; - chan.expiration = rep.expires_at; - chan.state = ''; - return modifyChan(chan) - }).then(chan => { +return getFirstToken(req.query.code, req.query.state).then(chan => { bot.sendMessage(chan.chatId, `@${chan.username} s'est connecté à OAuth2, shall we begin?`) res.redirect(301, `${config.website.protocol}://${config.website.hostname}/auth`) })