This commit is contained in:
syuilo 2017-01-21 13:17:35 +09:00
parent 464bbecede
commit 66f55a2b1c
4 changed files with 37 additions and 22 deletions

View file

@ -65,6 +65,7 @@
"@types/websocket": "0.0.32", "@types/websocket": "0.0.32",
"accesses": "1.2.0", "accesses": "1.2.0",
"argv": "0.0.2", "argv": "0.0.2",
"autwh": "0.0.0",
"babel-core": "6.22.1", "babel-core": "6.22.1",
"babel-polyfill": "6.20.0", "babel-polyfill": "6.20.0",
"babel-preset-es2015": "6.22.0", "babel-preset-es2015": "6.22.0",

View file

@ -28,6 +28,9 @@ app.use(compression());
*/ */
app.use((req, res, next) => { app.use((req, res, next) => {
res.header('X-Frame-Options', 'DENY'); res.header('X-Frame-Options', 'DENY');
res.locals.user = (req.headers['cookie'].match(/i=(!\w+)/) || [null, null])[1];
next(); next();
}); });
@ -48,7 +51,7 @@ app.get(/\/api:meta/, require('./meta'));
app.get(/\/api:url/, require('./service/url-preview')); app.get(/\/api:url/, require('./service/url-preview'));
app.post(/\/api:rss/, require('./service/rss-proxy')); app.post(/\/api:rss/, require('./service/rss-proxy'));
app.get(/\/connect:twitter/, require('./service/twitter/begin')); require('./service/twitter')(app);
/** /**
* Subdomain * Subdomain

View file

@ -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<any> => {
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!');
})
});
};

View file

@ -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);
});
}
};