wip
This commit is contained in:
parent
22d2f2051c
commit
e2e7babee0
5 changed files with 14 additions and 10 deletions
|
@ -7,6 +7,8 @@ import { IUser } from '../../models/user';
|
|||
import { IApp } from '../../models/app';
|
||||
|
||||
export default async (endpoint: Endpoint, ctx: Koa.Context) => {
|
||||
const body = ctx.is('multipart/form-data') ? (ctx.req as any).body : ctx.request.body;
|
||||
|
||||
const reply = (x?: any, y?: any) => {
|
||||
if (x === undefined) {
|
||||
ctx.status = 204;
|
||||
|
@ -25,7 +27,7 @@ export default async (endpoint: Endpoint, ctx: Koa.Context) => {
|
|||
|
||||
// Authentication
|
||||
try {
|
||||
[user, app] = await authenticate(ctx.request.body['i']);
|
||||
[user, app] = await authenticate(body['i']);
|
||||
} catch (e) {
|
||||
reply(403, 'AUTHENTICATION_FAILED');
|
||||
return;
|
||||
|
@ -35,7 +37,7 @@ export default async (endpoint: Endpoint, ctx: Koa.Context) => {
|
|||
|
||||
// API invoking
|
||||
try {
|
||||
res = await call(endpoint, user, app, ctx.request.body, ctx.req);
|
||||
res = await call(endpoint, user, app, body, (ctx.req as any).file);
|
||||
} catch (e) {
|
||||
reply(400, e);
|
||||
return;
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
import * as http from 'http';
|
||||
import * as multer from 'koa-multer';
|
||||
|
||||
import endpoints, { Endpoint } from './endpoints';
|
||||
|
@ -6,7 +5,7 @@ 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<any>(async (ok, rej) => {
|
||||
export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any, file?: any) => new Promise<any>(async (ok, rej) => {
|
||||
const isSecure = user != null && app == null;
|
||||
|
||||
const ep = typeof endpoint == 'string' ? endpoints.find(e => e.name == endpoint) : endpoint;
|
||||
|
@ -36,8 +35,8 @@ export default (endpoint: string | Endpoint, user: IUser, app: IApp, data: any,
|
|||
|
||||
let exec = require(`${__dirname}/endpoints/${ep.name}`);
|
||||
|
||||
if (ep.withFile && req) {
|
||||
exec = exec.bind(null, (req as multer.MulterIncomingMessage).file);
|
||||
if (ep.withFile && file) {
|
||||
exec = exec.bind(null, file);
|
||||
}
|
||||
|
||||
let res;
|
||||
|
|
|
@ -3,7 +3,7 @@ import * as Koa from 'koa';
|
|||
import config from '../../../config';
|
||||
import { ILocalUser } from '../../../models/user';
|
||||
|
||||
export default function(ctx: Koa.Context, user: ILocalUser, redirect: boolean) {
|
||||
export default function(ctx: Koa.Context, user: ILocalUser, redirect = false) {
|
||||
const expires = 1000 * 60 * 60 * 24 * 365; // One Year
|
||||
ctx.cookies.set('i', user.token, {
|
||||
path: '/',
|
||||
|
@ -16,5 +16,7 @@ export default function(ctx: Koa.Context, user: ILocalUser, redirect: boolean) {
|
|||
|
||||
if (redirect) {
|
||||
ctx.redirect(config.url);
|
||||
} else {
|
||||
ctx.status = 204;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,8 @@ const handler = require('./api-handler').default;
|
|||
// Init app
|
||||
const app = new Koa();
|
||||
app.use(bodyParser({
|
||||
detectJSON: () => true
|
||||
// リクエストが multipart/form-data でない限りはJSONだと見なす
|
||||
detectJSON: ctx => !ctx.is('multipart/form-data')
|
||||
}));
|
||||
|
||||
// Init multer instance
|
||||
|
|
|
@ -60,14 +60,14 @@ export default async (ctx: Koa.Context) => {
|
|||
});
|
||||
|
||||
if (verified) {
|
||||
signin(ctx, user, false);
|
||||
signin(ctx, user);
|
||||
} else {
|
||||
ctx.throw(400, {
|
||||
error: 'invalid token'
|
||||
});
|
||||
}
|
||||
} else {
|
||||
signin(ctx, user, false);
|
||||
signin(ctx, user);
|
||||
}
|
||||
} else {
|
||||
ctx.throw(400, {
|
||||
|
|
Loading…
Reference in a new issue