wip
This commit is contained in:
		
							parent
							
								
									61f21594a9
								
							
						
					
					
						commit
						22d2f2051c
					
				
					 10 changed files with 40 additions and 26 deletions
				
			
		| 
						 | 
					@ -444,9 +444,10 @@ export default class MiOS extends EventEmitter {
 | 
				
			||||||
		// Append a credential
 | 
							// Append a credential
 | 
				
			||||||
		if (this.isSignedIn) (data as any).i = this.i.token;
 | 
							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) => {
 | 
							return new Promise((resolve, reject) => {
 | 
				
			||||||
 | 
								const viaStream = this.stream.hasConnection &&
 | 
				
			||||||
 | 
									(localStorage.getItem('apiViaStream') ? localStorage.getItem('apiViaStream') == 'true' : true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if (viaStream) {
 | 
								if (viaStream) {
 | 
				
			||||||
				const stream = this.stream.borrow();
 | 
									const stream = this.stream.borrow();
 | 
				
			||||||
				const id = Math.random().toString();
 | 
									const id = Math.random().toString();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -25,11 +25,21 @@ export default async (endpoint: Endpoint, ctx: Koa.Context) => {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Authentication
 | 
						// Authentication
 | 
				
			||||||
	try {
 | 
						try {
 | 
				
			||||||
		[user, app] = await authenticate(ctx.body['i']);
 | 
							[user, app] = await authenticate(ctx.request.body['i']);
 | 
				
			||||||
	} catch (e) {
 | 
						} catch (e) {
 | 
				
			||||||
		return reply(403, 'AUTHENTICATION_FAILED');
 | 
							reply(403, 'AUTHENTICATION_FAILED');
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						let res;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// API invoking
 | 
						// 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);
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -226,7 +226,7 @@ if (config.line_bot) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// シグネチャ比較
 | 
							// シグネチャ比較
 | 
				
			||||||
		if (sig1 === sig2) {
 | 
							if (sig1 === sig2) {
 | 
				
			||||||
			ctx.body.events.forEach(ev => {
 | 
								ctx.request.body.events.forEach(ev => {
 | 
				
			||||||
				handler.emit('event', ev);
 | 
									handler.emit('event', ev);
 | 
				
			||||||
			});
 | 
								});
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,11 +6,9 @@ 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(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;
 | 
						const isSecure = user != null && app == null;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	//console.log(endpoint, user, app, data);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	const ep = typeof endpoint == 'string' ? endpoints.find(e => e.name == endpoint) : endpoint;
 | 
						const ep = typeof endpoint == 'string' ? endpoints.find(e => e.name == endpoint) : endpoint;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (ep.secure && !isSecure) {
 | 
						if (ep.secure && !isSecure) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,7 +13,9 @@ 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
 | 
				
			||||||
 | 
					}));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Init multer instance
 | 
					// Init multer instance
 | 
				
			||||||
const upload = multer({
 | 
					const upload = multer({
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -11,9 +11,9 @@ export default async (ctx: Koa.Context) => {
 | 
				
			||||||
	ctx.set('Access-Control-Allow-Origin', config.url);
 | 
						ctx.set('Access-Control-Allow-Origin', config.url);
 | 
				
			||||||
	ctx.set('Access-Control-Allow-Credentials', 'true');
 | 
						ctx.set('Access-Control-Allow-Credentials', 'true');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const username = ctx.body['username'];
 | 
						const username = ctx.request.body['username'];
 | 
				
			||||||
	const password = ctx.body['password'];
 | 
						const password = ctx.request.body['password'];
 | 
				
			||||||
	const token = ctx.body['token'];
 | 
						const token = ctx.request.body['token'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (typeof username != 'string') {
 | 
						if (typeof username != 'string') {
 | 
				
			||||||
		ctx.status = 400;
 | 
							ctx.status = 400;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -37,7 +37,7 @@ export default async (ctx: Koa.Context) => {
 | 
				
			||||||
	// Verify recaptcha
 | 
						// Verify recaptcha
 | 
				
			||||||
	// ただしテスト時はこの機構は障害となるため無効にする
 | 
						// ただしテスト時はこの機構は障害となるため無効にする
 | 
				
			||||||
	if (process.env.NODE_ENV !== 'test') {
 | 
						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) {
 | 
							if (!success) {
 | 
				
			||||||
			ctx.throw(400, 'recaptcha-failed');
 | 
								ctx.throw(400, 'recaptcha-failed');
 | 
				
			||||||
| 
						 | 
					@ -45,8 +45,8 @@ export default async (ctx: Koa.Context) => {
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const username = ctx.body['username'];
 | 
						const username = ctx.request.body['username'];
 | 
				
			||||||
	const password = ctx.body['password'];
 | 
						const password = ctx.request.body['password'];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Validate username
 | 
						// Validate username
 | 
				
			||||||
	if (!validateUsername(username)) {
 | 
						if (!validateUsername(username)) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -35,10 +35,14 @@ if (config.github_bot != null) {
 | 
				
			||||||
	const secret = config.github_bot.hook_secret;
 | 
						const secret = config.github_bot.hook_secret;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	router.post('/hooks/github', ctx => {
 | 
						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 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)) {
 | 
							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;
 | 
								ctx.status = 204;
 | 
				
			||||||
		} else {
 | 
							} else {
 | 
				
			||||||
			ctx.status = 400;
 | 
								ctx.status = 400;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -13,6 +13,11 @@ import sendDriveFile from './send-drive-file';
 | 
				
			||||||
const app = new Koa();
 | 
					const app = new Koa();
 | 
				
			||||||
app.use(cors());
 | 
					app.use(cors());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app.use(async (ctx, next) => {
 | 
				
			||||||
 | 
						ctx.set('Cache-Control', 'max-age=31536000, immutable');
 | 
				
			||||||
 | 
						await next();
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Init router
 | 
					// Init router
 | 
				
			||||||
const router = new Router();
 | 
					const router = new Router();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -27,7 +32,7 @@ router.get('/app-default.jpg', ctx => {
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
router.get('/:id', sendDriveFile);
 | 
					router.get('/:id', sendDriveFile);
 | 
				
			||||||
router.get('/:id/:name', sendDriveFile);
 | 
					router.get('/:id/*', sendDriveFile);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Register router
 | 
					// Register router
 | 
				
			||||||
app.use(router.routes());
 | 
					app.use(router.routes());
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,12 +83,6 @@ export default function(readable: stream.Readable, type: string, ctx: Koa.Contex
 | 
				
			||||||
		ctx.set('Content-Disposition', 'attachment');
 | 
							ctx.set('Content-Disposition', 'attachment');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	ctx.set('Cache-Control', 'max-age=31536000, immutable');
 | 
					 | 
				
			||||||
	ctx.set('Content-Type', data.contentType);
 | 
						ctx.set('Content-Type', data.contentType);
 | 
				
			||||||
 | 
						ctx.body = data.stream;
 | 
				
			||||||
	data.stream.pipe(ctx.res);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	data.stream.on('end', () => {
 | 
					 | 
				
			||||||
		ctx.res.end();
 | 
					 | 
				
			||||||
	});
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue