Skip to content

Commit

Permalink
feat(server): support experimental token config
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Oct 18, 2023
1 parent 2f5931a commit b386cca
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@satorijs/server",
"description": "Basic API server for Satori protocol",
"version": "1.0.4",
"version": "2.1.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"exports": {
Expand Down
15 changes: 15 additions & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export interface WebhookConfig {

export interface Config {
path?: string
token?: string
api?: ApiConfig
websocket?: WebSocketConfig
webhook?: WebhookConfig
}

export const Config: Schema<Config> = Schema.object({
path: Schema.string().default('/satori'),
token: Schema.string().experimental(),
api: Schema.object({
// enabled: Schema.boolean().default(true),
}),
Expand Down Expand Up @@ -63,6 +65,13 @@ export function apply(ctx: Context, config: Config) {
return koa.status = 404
}

if (config.token) {
if (koa.request.headers.authorization !== `Bearer ${config.token}`) {
koa.body = 'invalid token'
return koa.status = 403
}
}

const json = koa.request.body
const selfId = koa.request.headers['x-self-id']
const platform = koa.request.headers['x-platform']
Expand Down Expand Up @@ -121,6 +130,12 @@ export function apply(ctx: Context, config: Config) {
}

if (payload.op === Universal.Opcode.IDENTIFY) {
if (config.token) {
if (payload.body?.token !== config.token) {
return socket.close(4004, 'invalid token')
}
}

client.authorized = true
socket.send(JSON.stringify({
op: Universal.Opcode.READY,
Expand Down

0 comments on commit b386cca

Please sign in to comment.