-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathlogin.handler.ts
144 lines (128 loc) · 3.73 KB
/
login.handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import AdminJS from "adminjs";
import { Router } from "express";
import type {
AuthenticationContext,
AuthenticationMaxRetriesOptions,
AuthenticationOptions,
} from "../types.js";
const getLoginPath = (admin: AdminJS): string => {
const { loginPath, rootPath } = admin.options;
// since we are inside already namespaced router we have to replace login and logout routes that
// they don't have rootUrl inside. So changing /admin/login to just /login.
// but there is a case where user gives / as a root url and /login becomes `login`. We have to
// fix it by adding / in front of the route
const normalizedLoginPath = loginPath.replace(rootPath, "");
return normalizedLoginPath.startsWith("/")
? normalizedLoginPath
: `/${normalizedLoginPath}`;
};
class Retry {
private static retriesContainer: Map<string, Retry> = new Map();
private lastRetry: Date | undefined;
private retriesCount = 0;
constructor(ip: string) {
const existing = Retry.retriesContainer.get(ip);
if (existing) {
return existing;
}
Retry.retriesContainer.set(ip, this);
}
public canLogin(
maxRetries: number | AuthenticationMaxRetriesOptions | undefined
): boolean {
if (maxRetries === undefined) {
return true;
} else if (typeof maxRetries === "number") {
maxRetries = {
count: maxRetries,
duration: 60,
};
} else if (maxRetries.count <= 0) {
return true;
}
if (
!this.lastRetry ||
new Date().getTime() - this.lastRetry.getTime() >
maxRetries.duration * 1000
) {
this.lastRetry = new Date();
this.retriesCount = 1;
return true;
} else {
this.lastRetry = new Date();
this.retriesCount++;
return this.retriesCount <= maxRetries.count;
}
}
}
export const withLogin = (
router: Router,
admin: AdminJS,
auth: AuthenticationOptions
): void => {
const { rootPath } = admin.options;
const loginPath = getLoginPath(admin);
const { provider } = auth;
const providerProps = provider?.getUiProps?.() ?? {};
router.get(loginPath, async (req, res) => {
const baseProps = {
action: admin.options.loginPath,
errorMessage: null,
};
const login = await admin.renderLogin({
...baseProps,
...providerProps,
});
return res.send(login);
});
router.post(loginPath, async (req, res, next) => {
if (!new Retry(req.ip).canLogin(auth.maxRetries)) {
const login = await admin.renderLogin({
action: admin.options.loginPath,
errorMessage: "tooManyRequests",
...providerProps,
});
return res.send(login);
}
const context: AuthenticationContext = { req, res };
let adminUser;
if (provider) {
adminUser = await provider.handleLogin(
{
headers: req.headers,
query: req.query,
params: req.params,
data: req.fields ?? {},
},
context
);
} else {
const { email, password } = req.fields as {
email: string;
password: string;
};
// "auth.authenticate" must always be defined if "auth.provider" isn't
adminUser = await auth.authenticate!(email, password, context);
}
if (adminUser) {
req.session.adminUser = adminUser;
req.session.save((err) => {
if (err) {
return next(err);
}
if (req.session.redirectTo) {
return res.redirect(302, req.session.redirectTo);
} else {
return res.redirect(302, rootPath);
}
});
} else {
const login = await admin.renderLogin({
action: admin.options.loginPath,
errorMessage: "invalidCredentials",
...providerProps,
});
return res.send(login);
}
});
};