58 lines
1.7 KiB
JavaScript
58 lines
1.7 KiB
JavaScript
|
|
const express = require('express')
|
|
const fs = require('fs')
|
|
const https = require('https')
|
|
const passport = require('passport')
|
|
const TwitterStrategy = require('passport-twitter').Strategy
|
|
let config = require('./config.js')
|
|
let storage = require('./storage.js')
|
|
|
|
|
|
let oauth = {
|
|
start: function() {
|
|
let app = express()
|
|
|
|
app.use(require('express-session')({
|
|
secret: storage.data.sess_secret,
|
|
resave: true,
|
|
saveUninitialized: true
|
|
}));
|
|
|
|
app.use(passport.initialize())
|
|
app.use(passport.session({ secret: storage.data.sess_secret }))
|
|
|
|
passport.use(new TwitterStrategy({
|
|
consumerKey: config.twitter_consumer_key,
|
|
consumerSecret: config.twitter_consumer_secret,
|
|
callbackURL: `${config.callback_url}${config.callback_path}`
|
|
},
|
|
(token, tokenSecret, profile, cb) => {
|
|
storage.data.token = token
|
|
storage.data.token_secret = tokenSecret
|
|
storage.save()
|
|
console.log("[INFO] Got tokens, restart!")
|
|
}
|
|
))
|
|
|
|
console.log('[INFO] Starting HTTPS webserver for OAuth')
|
|
|
|
app.get('/', passport.authenticate('twitter'))
|
|
|
|
app.get(config.callback_path, passport.authenticate('twitter', { failureRedirect: '/failed' }), (req, res) => {
|
|
res.redirect("/success")
|
|
})
|
|
|
|
app.get('/failed', (req, res) => res.send('Authentication failed'))
|
|
app.get('/success', (req, res) => res.send('Authentication success. Restart XTweet when it stops'))
|
|
|
|
https.createServer({
|
|
key: fs.readFileSync(config.key),
|
|
cert: fs.readFileSync(config.cert),
|
|
passphrase: config.passphrase
|
|
}, app).listen(config.port, config.bind_ip)
|
|
|
|
console.log(`[INFO] Server listening on ${config.bind_ip}:${config.port}`)
|
|
}
|
|
}
|
|
|
|
module.exports = oauth;
|