File size: 559 Bytes
755dd12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { Context, Next } from 'koa';

export function whiteListMiddleware() {
  return async (ctx: Context, next: Next) => {
    const host = ctx.request.host;
    const whiteList = process.env.WHITELIST_DOMAINS;
    const list = whiteList ? whiteList.split(',') : [];

    console.log('[whiteListMiddleware]', list, host);

    if (!list.length) {
      return await next();
    }

    if (list.some(item => host.includes(item.trim()))) {
      await next();
    } else {
      ctx.res.statusCode = 401;
      ctx.body = 'Unauthorized domain.';
    }
  };
}