[bot] break out RPC and shared packages for bot service breakout

This commit is contained in:
41666 2019-04-04 11:13:34 -05:00
parent 544ae65c58
commit 50b5e334a3
No known key found for this signature in database
GPG key ID: BC51D07640DC10AF
31 changed files with 233 additions and 111 deletions

View file

@ -1,15 +0,0 @@
class RPCError extends Error {
constructor (msg, code, ...extra) {
super(msg)
this.code = code
this.extra = extra
}
static fromResponse (body, status) {
const e = new RPCError(body.msg, status)
e.remoteStack = body.trace
return e
}
}
module.exports = RPCError

View file

@ -1,7 +1,7 @@
// @flow
import { type AppContext } from '../Roleypoly'
import { type Context } from 'koa'
import RPCError from './_error'
import RPCError from '@roleypoly/rpc-client/error'
import logger from '../logger'
const log = logger(__filename)
@ -122,6 +122,29 @@ export const any = (
silent: boolean = false
) => (...args: any) => fn(...args)
export const bot = (
$: AppContext,
fn: (ctx: Context, ...args: any[]) => any,
silent: boolean = false
) => (
ctx: Context,
...args: any
) => {
const authToken: ?string = ctx.request.headers['Authorization']
if (authToken != null && authToken.startsWith('Bot ')) {
if (authToken === `Bot ${$.config.sharedSecret}`) {
return fn(ctx, ...args)
}
}
if (!silent) {
log.info('RPC bot check failed', logFacts(ctx))
}
throw PermissionError
}
type Handler = (ctx: Context, ...args: any[]) => any
type Strategy = (
$: AppContext,

View file

@ -1,7 +1,7 @@
// @flow
import fnv from 'fnv-plus'
import autoloader from './_autoloader'
import RPCError from './_error'
import RPCError from '@roleypoly/rpc-client/error'
import type Roleypoly, { Router } from '../Roleypoly'
import type { Context } from 'koa'
// import logger from '../logger'
@ -121,7 +121,6 @@ export default class RPCServer {
if (err instanceof RPCError || err.constructor.name === 'RPCError') {
// $FlowFixMe
ctx.status = err.code
console.log(err.code)
}
}

View file

@ -3,7 +3,7 @@ import { type AppContext } from '../Roleypoly'
import { type Context } from 'koa'
import { type Guild } from 'eris'
import * as secureAs from './_security'
import RPCError from './_error'
import RPCError from '@roleypoly/rpc-client/error'
export default ($: AppContext) => ({

View file

@ -6,7 +6,8 @@ import * as secureAs from './_security'
export default ($: AppContext) => ({
getCurrentUser: secureAs.authed($, async (ctx: Context) => {
return $.discord.getUserPartial(ctx.session.userId)
const u = await $.discord.getUserPartial(ctx.session.userId)
return u
}),
isRoot: secureAs.root($, () => {