forked from calcom/cal.diy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogin.2fa.e2e.ts
More file actions
183 lines (151 loc) · 6.95 KB
/
login.2fa.e2e.ts
File metadata and controls
183 lines (151 loc) · 6.95 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { Page } from "@playwright/test";
import { expect } from "@playwright/test";
import { authenticator } from "otplib";
import { symmetricDecrypt } from "@calcom/lib/crypto";
import { totpAuthenticatorCheck } from "@calcom/lib/totp";
import { prisma } from "@calcom/prisma";
import { test } from "./lib/fixtures";
test.describe.configure({ mode: "parallel" });
// TODO: add more backup code tests, e.g. login + disabling 2fa with backup
// a test to logout requires both a successful login as logout, to prevent
// a doubling of tests failing on logout & logout, we can group them.
test.describe("2FA Tests", async () => {
test.afterEach(async ({ users }) => {
await users.deleteAll();
});
test("should allow a user to enable 2FA and login using 2FA", async ({ page, users }) => {
// log in trail user
const user = await test.step("Enable 2FA", async () => {
const user = await users.create();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userPassword = user.username!;
await user.apiLogin();
// expects the home page for an authorized user
await page.goto("/settings/security/two-factor-auth");
await page.click(`[data-testid=two-factor-switch]`);
await page.fill('input[name="password"]', userPassword);
await page.press('input[name="password"]', "Enter");
const secret = await page.locator(`[data-testid=two-factor-secret]`).textContent();
expect(secret).toHaveLength(32);
await page.click('[data-testid="goto-otp-screen"]');
/**
* Try a wrong code and test that wrong code is rejected.
*/
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await fillOtp({ page, secret: "123456", noRetry: true });
await expect(page.locator('[data-testid="error-submitting-code"]')).toBeVisible();
await removeOtpInput(page);
await fillOtp({
page,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
secret: secret!,
});
await expect(page.getByTestId("backup-codes-download")).toBeVisible();
return user;
});
await test.step("Logout", async () => {
await users.logout();
});
await test.step("Login with 2FA enabled", async () => {
await user.login();
const userWith2FaSecret = await prisma.user.findFirst({
where: {
id: user.id,
},
});
const secret = symmetricDecrypt(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
userWith2FaSecret!.twoFactorSecret!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
process.env.CALENDSO_ENCRYPTION_KEY!
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await fillOtp({ page, secret: secret! });
await Promise.all([
page.press('input[name="2fa6"]', "Enter"),
page.waitForResponse("**/api/auth/callback/credentials**"),
]);
const shellLocator = page.locator(`[data-testid=dashboard-shell]`);
await expect(shellLocator).toBeVisible();
});
});
test("should allow a user to disable 2FA", async ({ page, users }) => {
// log in trail user
const user = await test.step("Enable 2FA", async () => {
const user = await users.create();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userPassword = user.username!;
await user.apiLogin();
// expects the home page for an authorized user
await page.goto("/settings/security/two-factor-auth");
await page.click(`[data-testid=two-factor-switch][data-state="unchecked"]`);
await page.fill('input[name="password"]', userPassword);
await page.press('input[name="password"]', "Enter");
const secret = await page.locator(`[data-testid=two-factor-secret]`).textContent();
expect(secret).toHaveLength(32);
await page.click('[data-testid="goto-otp-screen"]');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await fillOtp({ page, secret: secret! });
// backup codes are now showing, so run a few tests
// click download button
const promise = page.waitForEvent("download");
await page.getByTestId("backup-codes-download").click();
const download = await promise;
expect(download.suggestedFilename()).toBe("cal-backup-codes.txt");
// TODO: check file content
// click copy button
await page.getByTestId("backup-codes-copy").click();
await page.getByTestId("toast-success").waitFor();
// TODO: check clipboard content
// close backup code dialog
await page.getByTestId("backup-codes-close").click();
await expect(page.locator(`[data-testid=two-factor-switch][data-state="checked"]`)).toBeVisible();
return user;
});
await test.step("Disable 2FA", async () => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userPassword = user.username!;
// expects the home page for an authorized user
await page.goto("/settings/security/two-factor-auth");
await page.click(`[data-testid=two-factor-switch][data-state="checked"]`);
await page.fill('input[name="password"]', userPassword);
const userWith2FaSecret = await prisma.user.findFirst({
where: {
id: user.id,
},
});
const secret = symmetricDecrypt(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
userWith2FaSecret!.twoFactorSecret!,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
process.env.CALENDSO_ENCRYPTION_KEY!
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
await fillOtp({ page, secret: secret! });
await page.click('[data-testid="disable-2fa"]');
await expect(page.locator(`[data-testid=two-factor-switch][data-state="unchecked"]`)).toBeVisible();
return user;
});
});
});
async function removeOtpInput(page: Page) {
await page.locator('input[name="2fa6"]').waitFor({ state: "visible", timeout: 30_000 });
// Remove one OTP input
await page.locator('input[name="2fa6"]').focus();
await page.keyboard.press("Backspace");
}
async function fillOtp({ page, secret, noRetry }: { page: Page; secret: string; noRetry?: boolean }) {
let token = authenticator.generate(secret);
if (!noRetry && !totpAuthenticatorCheck(token, secret)) {
logger.log("Token expired, Renerating.");
// Maybe token was just about to expire, try again just once more
token = authenticator.generate(secret);
}
await page.locator('input[name="2fa1"]').waitFor({ state: "visible", timeout: 60_000 });
await page.fill('input[name="2fa1"]', token[0]);
await page.fill('input[name="2fa2"]', token[1]);
await page.fill('input[name="2fa3"]', token[2]);
await page.fill('input[name="2fa4"]', token[3]);
await page.fill('input[name="2fa5"]', token[4]);
await page.fill('input[name="2fa6"]', token[5]);
}