This commit is contained in:
syuilo
2018-04-13 06:06:18 +09:00
parent a3bd4ba426
commit 3368fe8552
20 changed files with 582 additions and 609 deletions

View File

@ -1,4 +1,4 @@
import * as express from 'express';
import * as Koa from 'koa';
import * as bcrypt from 'bcryptjs';
import * as speakeasy from 'speakeasy';
import User, { ILocalUser } from '../../../models/user';
@ -7,26 +7,26 @@ import event from '../../../publishers/stream';
import signin from '../common/signin';
import config from '../../../config';
export default async (req: express.Request, res: express.Response) => {
res.header('Access-Control-Allow-Origin', config.url);
res.header('Access-Control-Allow-Credentials', 'true');
export default async (ctx: Koa.Context) => {
ctx.set('Access-Control-Allow-Origin', config.url);
ctx.set('Access-Control-Allow-Credentials', 'true');
const username = req.body['username'];
const password = req.body['password'];
const token = req.body['token'];
const username = ctx.body['username'];
const password = ctx.body['password'];
const token = ctx.body['token'];
if (typeof username != 'string') {
res.sendStatus(400);
ctx.status = 400;
return;
}
if (typeof password != 'string') {
res.sendStatus(400);
ctx.status = 400;
return;
}
if (token != null && typeof token != 'string') {
res.sendStatus(400);
ctx.status = 400;
return;
}
@ -37,12 +37,12 @@ export default async (req: express.Request, res: express.Response) => {
}, {
fields: {
data: false,
'profile': false
profile: false
}
}) as ILocalUser;
if (user === null) {
res.status(404).send({
ctx.throw(404, {
error: 'user not found'
});
return;
@ -60,17 +60,17 @@ export default async (req: express.Request, res: express.Response) => {
});
if (verified) {
signin(res, user, false);
signin(ctx, user, false);
} else {
res.status(400).send({
ctx.throw(400, {
error: 'invalid token'
});
}
} else {
signin(res, user, false);
signin(ctx, user, false);
}
} else {
res.status(400).send({
ctx.throw(400, {
error: 'incorrect password'
});
}
@ -79,8 +79,8 @@ export default async (req: express.Request, res: express.Response) => {
const record = await Signin.insert({
createdAt: new Date(),
userId: user._id,
ip: req.ip,
headers: req.headers,
ip: ctx.ip,
headers: ctx.headers,
success: same
});

View File

@ -1,5 +1,5 @@
import * as uuid from 'uuid';
import * as express from 'express';
import * as Koa from 'koa';
import * as bcrypt from 'bcryptjs';
import { generate as generateKeypair } from '../../../crypto_key';
import recaptcha = require('recaptcha-promise');
@ -33,30 +33,30 @@ const home = {
]
};
export default async (req: express.Request, res: express.Response) => {
export default async (ctx: Koa.Context) => {
// Verify recaptcha
// ただしテスト時はこの機構は障害となるため無効にする
if (process.env.NODE_ENV !== 'test') {
const success = await recaptcha(req.body['g-recaptcha-response']);
const success = await recaptcha(ctx.body['g-recaptcha-response']);
if (!success) {
res.status(400).send('recaptcha-failed');
ctx.throw(400, 'recaptcha-failed');
return;
}
}
const username = req.body['username'];
const password = req.body['password'];
const username = ctx.body['username'];
const password = ctx.body['password'];
// Validate username
if (!validateUsername(username)) {
res.sendStatus(400);
ctx.status = 400;
return;
}
// Validate password
if (!validatePassword(password)) {
res.sendStatus(400);
ctx.status = 400;
return;
}
@ -71,7 +71,7 @@ export default async (req: express.Request, res: express.Response) => {
// Check username already used
if (usernameExist !== 0) {
res.sendStatus(400);
ctx.status = 400;
return;
}
@ -143,5 +143,5 @@ export default async (req: express.Request, res: express.Response) => {
});
// Response
res.send(await pack(account));
ctx.body = await pack(account);
};