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