src/app.ts   A
last analyzed

Complexity

Total Complexity 6
Complexity/F 3

Size

Lines of Code 84
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 70
mnd 4
bc 4
fnc 2
dl 0
loc 84
rs 10
bpm 2
cpm 3
noi 0
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B app.ts ➔ findUser 0 30 5
A app.ts ➔ createApp 0 33 1
1
// eslint-disable-next-line import/no-unassigned-import
2
import "reflect-metadata";
3
import { Action, createExpressServer } from "routing-controllers";
4
import { getCustomRepository } from "typeorm";
5
import { AuthController } from "./controllers/AuthController";
6
import { isProduction, loadEnv } from "./utils/global";
7
import { CompressionMiddleware } from "./middlewares/CompressionMiddleware";
8
import { DatabaseMiddleware } from "./middlewares/DatabaseMiddleware";
9
import { RateLimitMiddleware } from "./middlewares/RateLimitMiddleware";
10
import { SecurityHstsMiddleware } from "./middlewares/SecurityHstsMiddleware";
11
import { SecurityMiddleware } from "./middlewares/SecurityMiddleware";
12
import { SecurityNoCacheMiddleware } from "./middlewares/SecurityNoCacheMiddleware";
13
import { UserRepository } from "./repositories/UserRepository";
14
import { checkJwt, createJwt } from "./utils/jwt";
15
import { User } from "./entities/User";
16
import omit from "lodash.omit";
17
18
async function findUser(action: Action): Promise<User | false> {
19
  const request = action.request;
20
  const response = action.response;
21
22
  const authorization = request.headers["authorization"];
23
  const apiKey = request.query.key || request.headers["x-api-key"];
24
  const repository = getCustomRepository(UserRepository);
25
26
  if (authorization) {
27
    try {
28
      const payload = checkJwt(authorization);
29
      const user = await repository.findOne({ username: payload.username });
30
31
      if (user) {
32
        const token = createJwt(omit(payload, ["exp", "iat"]));
33
        response.setHeader("token", `Bearer ${token}`);
34
      }
35
36
      return user;
37
    } catch {
38
      return false;
39
    }
40
  }
41
42
  if (apiKey) {
43
    return await repository.findOneByApiKey(apiKey);
44
  }
45
46
  return false;
47
}
48
49
export function createApp() {
50
  loadEnv();
51
52
  return createExpressServer({
53
    development: !isProduction(),
54
    validation: false,
55
    cors: {
56
      origin: true,
57
      allowedHeaders: [
58
        "accept",
59
        "content-type",
60
        "x-requested-with",
61
        "x-api-key",
62
      ],
63
    },
64
    controllers: [
65
      AuthController,
66
    ],
67
    middlewares: [
68
      CompressionMiddleware,
69
      DatabaseMiddleware,
70
      SecurityHstsMiddleware,
71
      SecurityMiddleware,
72
      SecurityNoCacheMiddleware,
73
      RateLimitMiddleware,
74
    ],
75
    currentUserChecker: async (action: Action) => {
76
      return await findUser(action);
77
    },
78
    authorizationChecker: async (action: Action, roles: string[]) => {
79
      const user = await findUser(action);
80
      return user && !roles.length;
81
    },
82
  });
83
}
84