This commit is contained in:
syuilo
2017-11-23 13:25:33 +09:00
parent d680b6bd9b
commit 767a292dbd
6 changed files with 128 additions and 55 deletions

19
src/api/common/signin.ts Normal file
View File

@ -0,0 +1,19 @@
import config from '../../conf';
export default function(res, user, redirect: boolean) {
const expires = 1000 * 60 * 60 * 24 * 365; // One Year
res.cookie('i', user.token, {
path: '/',
domain: `.${config.host}`,
secure: config.url.substr(0, 5) === 'https',
httpOnly: false,
expires: new Date(Date.now() + expires),
maxAge: expires
});
if (redirect) {
res.redirect(config.url);
} else {
res.sendStatus(204);
}
}

View File

@ -4,7 +4,7 @@ import { default as User, IUser } from '../models/user';
import Signin from '../models/signin';
import serialize from '../serializers/signin';
import event from '../event';
import config from '../../conf';
import signin from '../common/signin';
export default async (req: express.Request, res: express.Response) => {
res.header('Access-Control-Allow-Credentials', 'true');
@ -43,17 +43,7 @@ export default async (req: express.Request, res: express.Response) => {
const same = await bcrypt.compare(password, user.password);
if (same) {
const expires = 1000 * 60 * 60 * 24 * 365; // One Year
res.cookie('i', user.token, {
path: '/',
domain: `.${config.host}`,
secure: config.url.substr(0, 5) === 'https',
httpOnly: false,
expires: new Date(Date.now() + expires),
maxAge: expires
});
res.sendStatus(204);
signin(res, user, false);
} else {
res.status(400).send({
error: 'incorrect password'

View File

@ -1,4 +1,6 @@
import * as express from 'express';
import * as cookie from 'cookie';
import * as uuid from 'uuid';
// import * as Twitter from 'twitter';
// const Twitter = require('twitter');
import autwh from 'autwh';
@ -7,6 +9,7 @@ import User from '../models/user';
import serialize from '../serializers/user';
import event from '../event';
import config from '../../conf';
import signin from '../common/signin';
module.exports = (app: express.Application) => {
app.get('/disconnect/twitter', async (req, res): Promise<any> => {
@ -30,8 +33,13 @@ module.exports = (app: express.Application) => {
if (config.twitter == null) {
app.get('/connect/twitter', (req, res) => {
res.send('現在Twitterへ接続できません');
res.send('現在Twitterへ接続できません (このインスタンスではTwitterはサポートされていません)');
});
app.get('/signin/twitter', (req, res) => {
res.send('現在Twitterへ接続できません (このインスタンスではTwitterはサポートされていません)');
});
return;
}
@ -41,6 +49,12 @@ module.exports = (app: express.Application) => {
callbackUrl: `${config.api_url}/tw/cb`
});
const twAuthSignin = autwh({
consumerKey: config.twitter.consumer_key,
consumerSecret: config.twitter.consumer_secret,
callbackUrl: `${config.api_url}/signin/twitter/cb`
});
app.get('/connect/twitter', async (req, res): Promise<any> => {
if (res.locals.user == null) return res.send('plz signin');
const ctx = await twAuth.begin();
@ -56,15 +70,15 @@ module.exports = (app: express.Application) => {
const user = await User.findOneAndUpdate({
token: res.locals.user
}, {
$set: {
twitter: {
access_token: result.accessToken,
access_token_secret: result.accessTokenSecret,
user_id: result.userId,
screen_name: result.screenName
}
$set: {
twitter: {
access_token: result.accessToken,
access_token_secret: result.accessTokenSecret,
user_id: result.userId,
screen_name: result.screenName
}
});
}
});
res.send(`Twitter: @${result.screenName} を、Misskey: @${user.username} に接続しました!`);
@ -75,4 +89,50 @@ module.exports = (app: express.Application) => {
}));
});
});
app.get('/signin/twitter', async (req, res): Promise<any> => {
const ctx = await twAuthSignin.begin();
const sessid = uuid();
redis.set(sessid, JSON.stringify(ctx));
const expires = 1000 * 60 * 60; // 1h
res.cookie('signin_with_twitter_session_id', sessid, {
path: '/',
domain: `.${config.host}`,
secure: config.url.substr(0, 5) === 'https',
httpOnly: true,
expires: new Date(Date.now() + expires),
maxAge: expires
});
res.redirect(ctx.url);
});
app.get('/signin/twitter/cb', (req, res): any => {
// req.headers['cookie'] は常に string ですが、型定義の都合上
// string | string[] になっているので string を明示しています
const cookies = cookie.parse((req.headers['cookie'] as string || ''));
const sessid = cookies['signin_with_twitter_session_id'];
if (sessid == undefined) {
res.status(400).send('invalid session');
}
redis.get(sessid, async (_, ctx) => {
const result = await twAuthSignin.done(JSON.parse(ctx), req.query.oauth_verifier);
const user = await User.findOne({
'twitter.user_id': result.userId
});
if (user == null) {
res.status(404).send(`@${result.screenName}と連携しているMisskeyアカウントはありませんでした...`);
}
signin(res, user, true);
});
});
};

View File

@ -157,6 +157,7 @@
</h1>
<mk-signin ref="signin"/>
</div>
<a href={ _API_URL_ + '/signin/twitter' }>Twitterでサインイン</a>
<div class="divider"><span>or</span></div>
<button class="signup" onclick={ parent.signup }>新規登録</button><a class="introduction" onclick={ introduction }>Misskeyについて</a>
<style>

View File

@ -1,5 +1,6 @@
<mk-entrance-signin>
<mk-signin/>
<a href={ _API_URL_ + '/signin/twitter' }>Twitterでサインイン</a>
<div class="divider"><span>or</span></div>
<button class="signup" onclick={ parent.signup }>%i18n:mobile.tags.mk-entrance-signin.signup%</button><a class="introduction" onclick={ parent.introduction }>%i18n:mobile.tags.mk-entrance-signin.about%</a>
<style>