Commit 4a21d9ac authored by Guillaume Vagner's avatar Guillaume Vagner
Browse files

divide website

parent cad98ae2
......@@ -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
// 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`)
})
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment