summary refs log tree commit diff
path: root/src/api/routes/users/@me/mfa/webauthn/credentials/index.ts
blob: f3de9c377c5cc615b137be8bc8c2ba7504fc7488 (plain) (blame)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Fosscord and Fosscord Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { route } from "@fosscord/api";
import {
	CreateWebAuthnCredentialSchema,
	DiscordApiErrors,
	FieldErrors,
	GenerateWebAuthnCredentialsSchema,
	generateWebAuthnTicket,
	SecurityKey,
	User,
	verifyWebAuthnToken,
	WebAuthn,
	WebAuthnPostSchema,
} from "@fosscord/util";
import bcrypt from "bcrypt";
import { Request, Response, Router } from "express";
import { ExpectedAttestationResult } from "fido2-lib";
import { HTTPError } from "lambert-server";
const router = Router();

const isGenerateSchema = (
	body: WebAuthnPostSchema,
): body is GenerateWebAuthnCredentialsSchema => {
	return "password" in body;
};

const isCreateSchema = (
	body: WebAuthnPostSchema,
): body is CreateWebAuthnCredentialSchema => {
	return "credential" in body;
};

function toArrayBuffer(buf: Buffer) {
	const ab = new ArrayBuffer(buf.length);
	const view = new Uint8Array(ab);
	for (let i = 0; i < buf.length; ++i) {
		view[i] = buf[i];
	}
	return ab;
}

router.get("/", route({}), async (req: Request, res: Response) => {
	const securityKeys = await SecurityKey.find({
		where: {
			user_id: req.user_id,
		},
	});

	return res.json(
		securityKeys.map((key) => ({
			id: key.id,
			name: key.name,
		})),
	);
});

router.post(
	"/",
	route({ body: "WebAuthnPostSchema" }),
	async (req: Request, res: Response) => {
		if (!WebAuthn.fido2) {
			// TODO: I did this for typescript and I can't use !
			throw new Error("WebAuthn not enabled");
		}

		const user = await User.findOneOrFail({
			where: {
				id: req.user_id,
			},
			select: [
				"data",
				"id",
				"disabled",
				"deleted",
				"totp_secret",
				"mfa_enabled",
				"username",
			],
			relations: ["settings"],
		});

		if (isGenerateSchema(req.body)) {
			const { password } = req.body;
			const same_password = await bcrypt.compare(
				password,
				user.data.hash || "",
			);
			if (!same_password) {
				throw FieldErrors({
					password: {
						message: req.t("auth:login.INVALID_PASSWORD"),
						code: "INVALID_PASSWORD",
					},
				});
			}

			const registrationOptions =
				await WebAuthn.fido2.attestationOptions();
			const challenge = JSON.stringify({
				publicKey: {
					...registrationOptions,
					challenge: Buffer.from(
						registrationOptions.challenge,
					).toString("base64"),
					user: {
						id: user.id,
						name: user.username,
						displayName: user.username,
					},
				},
			});

			const ticket = await generateWebAuthnTicket(challenge);

			return res.json({
				ticket: ticket,
				challenge,
			});
		} else if (isCreateSchema(req.body)) {
			const { credential, name, ticket } = req.body;

			const verified = await verifyWebAuthnToken(ticket);
			if (!verified) throw new HTTPError("Invalid ticket", 400);

			const clientAttestationResponse = JSON.parse(credential);

			if (!clientAttestationResponse.rawId)
				throw new HTTPError("Missing rawId", 400);

			const rawIdBuffer = Buffer.from(
				clientAttestationResponse.rawId,
				"base64",
			);
			clientAttestationResponse.rawId = toArrayBuffer(rawIdBuffer);

			const attestationExpectations: ExpectedAttestationResult =
				JSON.parse(
					Buffer.from(
						clientAttestationResponse.response.clientDataJSON,
						"base64",
					).toString(),
				);

			const regResult = await WebAuthn.fido2.attestationResult(
				clientAttestationResponse,
				{
					...attestationExpectations,
					factor: "second",
				},
			);

			const authnrData = regResult.authnrData;
			const keyId = Buffer.from(authnrData.get("credId")).toString(
				"base64",
			);
			const counter = authnrData.get("counter");
			const publicKey = authnrData.get("credentialPublicKeyPem");

			const securityKey = SecurityKey.create({
				name,
				counter,
				public_key: publicKey,
				user_id: req.user_id,
				key_id: keyId,
			});

			await Promise.all([
				securityKey.save(),
				User.update({ id: req.user_id }, { webauthn_enabled: true }),
			]);

			return res.json({
				name,
				id: securityKey.id,
			});
		} else {
			throw DiscordApiErrors.INVALID_AUTHENTICATION_TOKEN;
		}
	},
);

export default router;