From 42268f0fd482cb6923526153f170c657f64c4ba8 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 21 Jan 2017 13:17:35 +0900 Subject: [PATCH] wip --- package.json | 1 + src/web/server.ts | 5 ++++- src/web/service/twitter.ts | 32 ++++++++++++++++++++++++++++++++ src/web/service/twitter/begin.ts | 21 --------------------- 4 files changed, 37 insertions(+), 22 deletions(-) create mode 100644 src/web/service/twitter.ts delete mode 100644 src/web/service/twitter/begin.ts diff --git a/package.json b/package.json index 1516daf333..9a573fa88a 100644 --- a/package.json +++ b/package.json @@ -65,6 +65,7 @@ "@types/websocket": "0.0.32", "accesses": "1.2.0", "argv": "0.0.2", + "autwh": "0.0.0", "babel-core": "6.22.1", "babel-polyfill": "6.20.0", "babel-preset-es2015": "6.22.0", diff --git a/src/web/server.ts b/src/web/server.ts index 6bbc662525..9c44807f94 100644 --- a/src/web/server.ts +++ b/src/web/server.ts @@ -28,6 +28,9 @@ app.use(compression()); */ app.use((req, res, next) => { res.header('X-Frame-Options', 'DENY'); + + res.locals.user = (req.headers['cookie'].match(/i=(!\w+)/) || [null, null])[1]; + next(); }); @@ -48,7 +51,7 @@ app.get(/\/api:meta/, require('./meta')); app.get(/\/api:url/, require('./service/url-preview')); app.post(/\/api:rss/, require('./service/rss-proxy')); -app.get(/\/connect:twitter/, require('./service/twitter/begin')); +require('./service/twitter')(app); /** * Subdomain diff --git a/src/web/service/twitter.ts b/src/web/service/twitter.ts new file mode 100644 index 0000000000..634b6aed7c --- /dev/null +++ b/src/web/service/twitter.ts @@ -0,0 +1,32 @@ +import * as express from 'express'; +//import * as Twitter from 'twitter'; +//const Twitter = require('twitter'); +import autwh from 'autwh'; +import redis from '../../db/redis'; +import config from '../../conf'; + +module.exports = (app: express.Application) => { + if (config.twitter == null) return; + + const twAuth = autwh({ + consumerKey: config.twitter.consumer_key, + consumerSecret: config.twitter.consumer_secret, + callbackUrl: config.url + '/tw/cb' + }); + + app.get('/twitter:connect', async (req, res): Promise => { + if (res.locals.user == null) return res.send('plz signin'); + const ctx = await twAuth.begin(); + redis.set(res.locals.user, JSON.stringify(ctx)); + res.redirect(ctx.url); + }); + + app.get('/twitter/callback', (req, res): any => { + if (res.locals.user == null) return res.send('plz signin'); + redis.get(res.locals.user, async (_, ctx) => { + const tokens = await twAuth.done(JSON.parse(ctx), req.query.oauth_verifier); + console.log(tokens); + res.send('Authorized!'); + }) + }); +}; diff --git a/src/web/service/twitter/begin.ts b/src/web/service/twitter/begin.ts deleted file mode 100644 index 2998b30c96..0000000000 --- a/src/web/service/twitter/begin.ts +++ /dev/null @@ -1,21 +0,0 @@ -import * as express from 'express'; -//import * as Twitter from 'twitter'; -const Twitter = require('twitter'); -import config from '../../../conf'; - -module.exports = (req: express.Request, res: express.Response) => { - if (config.twitter) { - const client = new Twitter({ - consumer_key: config.twitter.consumer_key, - consumer_secret: config.twitter.consumer_secret - }); - - client.post('oauth/request_token', { - oauth_callback: config.url + '/tw/cb' - }, (x, y, z) => { - console.log(x); - console.log(y); - console.log(z); - }); - } -};