This commit is contained in:
syuilo 2018-04-13 09:44:00 +09:00
parent 61f21594a9
commit 22d2f2051c
10 changed files with 40 additions and 26 deletions

View file

@ -444,9 +444,10 @@ export default class MiOS extends EventEmitter {
// Append a credential
if (this.isSignedIn) (data as any).i = this.i.token;
const viaStream = localStorage.getItem('apiViaStream') ? localStorage.getItem('apiViaStream') == 'true' : true;
return new Promise((resolve, reject) => {
const viaStream = this.stream.hasConnection &&
(localStorage.getItem('apiViaStream') ? localStorage.getItem('apiViaStream') == 'true' : true);
if (viaStream) {
const stream = this.stream.borrow();
const id = Math.random().toString();

View file

@ -25,11 +25,21 @@ export default async (endpoint: Endpoint, ctx: Koa.Context) => {
// Authentication
try {
[user, app] = await authenticate(ctx.body['i']);
[user, app] = await authenticate(ctx.request.body['i']);
} catch (e) {
return reply(403, 'AUTHENTICATION_FAILED');
reply(403, 'AUTHENTICATION_FAILED');
return;
}
let res;
// API invoking
call(endpoint, user, app, ctx.body, ctx.req).then(reply).catch(e => reply(400, e));
try {
res = await call(endpoint, user, app, ctx.request.body, ctx.req);
} catch (e) {
reply(400, e);
return;
}
reply(res);
};

View file

@ -226,7 +226,7 @@ if (config.line_bot) {
// シグネチャ比較
if (sig1 === sig2) {
ctx.body.events.forEach(ev => {
ctx.request.body.events.forEach(ev => {
handler.emit('event', ev);
});
} else {

View file

@ -6,11 +6,9 @@ import limitter from './limitter';
import { IUser } from '../../models/user';
import { IApp } from '../../models/app';
export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any, req?: http.IncomingMessage) => new Promise(async (ok, rej) => {
export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any, req?: http.IncomingMessage) => new Promise<any>(async (ok, rej) => {
const isSecure = user != null && app == null;
//console.log(endpoint, user, app, data);
const ep = typeof endpoint == 'string' ? endpoints.find(e => e.name == endpoint) : endpoint;
if (ep.secure && !isSecure) {

View file

@ -13,7 +13,9 @@ const handler = require('./api-handler').default;
// Init app
const app = new Koa();
app.use(bodyParser);
app.use(bodyParser({
detectJSON: () => true
}));
// Init multer instance
const upload = multer({

View file

@ -11,9 +11,9 @@ export default async (ctx: Koa.Context) => {
ctx.set('Access-Control-Allow-Origin', config.url);
ctx.set('Access-Control-Allow-Credentials', 'true');
const username = ctx.body['username'];
const password = ctx.body['password'];
const token = ctx.body['token'];
const username = ctx.request.body['username'];
const password = ctx.request.body['password'];
const token = ctx.request.body['token'];
if (typeof username != 'string') {
ctx.status = 400;

View file

@ -37,7 +37,7 @@ export default async (ctx: Koa.Context) => {
// Verify recaptcha
// ただしテスト時はこの機構は障害となるため無効にする
if (process.env.NODE_ENV !== 'test') {
const success = await recaptcha(ctx.body['g-recaptcha-response']);
const success = await recaptcha(ctx.request.body['g-recaptcha-response']);
if (!success) {
ctx.throw(400, 'recaptcha-failed');
@ -45,8 +45,8 @@ export default async (ctx: Koa.Context) => {
}
}
const username = ctx.body['username'];
const password = ctx.body['password'];
const username = ctx.request.body['username'];
const password = ctx.request.body['password'];
// Validate username
if (!validateUsername(username)) {

View file

@ -35,10 +35,14 @@ if (config.github_bot != null) {
const secret = config.github_bot.hook_secret;
router.post('/hooks/github', ctx => {
const body = JSON.stringify(ctx.request.body);
const hash = crypto.createHmac('sha1', secret).update(body).digest('hex');
const sig1 = new Buffer(ctx.headers['x-hub-signature']);
const sig2 = new Buffer(`sha1=${crypto.createHmac('sha1', secret).update(JSON.stringify(ctx.body)).digest('hex')}`);
const sig2 = new Buffer(`sha1=${hash}`);
// シグネチャ比較
if (sig1.equals(sig2)) {
handler.emit(ctx.headers['x-github-event'], ctx.body);
handler.emit(ctx.headers['x-github-event'], ctx.request.body);
ctx.status = 204;
} else {
ctx.status = 400;

View file

@ -13,6 +13,11 @@ import sendDriveFile from './send-drive-file';
const app = new Koa();
app.use(cors());
app.use(async (ctx, next) => {
ctx.set('Cache-Control', 'max-age=31536000, immutable');
await next();
});
// Init router
const router = new Router();
@ -27,7 +32,7 @@ router.get('/app-default.jpg', ctx => {
});
router.get('/:id', sendDriveFile);
router.get('/:id/:name', sendDriveFile);
router.get('/:id/*', sendDriveFile);
// Register router
app.use(router.routes());

View file

@ -83,12 +83,6 @@ export default function(readable: stream.Readable, type: string, ctx: Koa.Contex
ctx.set('Content-Disposition', 'attachment');
}
ctx.set('Cache-Control', 'max-age=31536000, immutable');
ctx.set('Content-Type', data.contentType);
data.stream.pipe(ctx.res);
data.stream.on('end', () => {
ctx.res.end();
});
ctx.body = data.stream;
}