|
| 1 | +"use server"; |
| 2 | + |
| 3 | +import { recoveryCodeBucket, resetUser2FAWithRecoveryCode, totpBucket } from "@/lib/server/2fa"; |
| 4 | +import { RefillingTokenBucket } from "@/lib/server/rate-limit"; |
| 5 | +import { getCurrentSession, setSessionAs2FAVerified } from "@/lib/server/session"; |
| 6 | +import { getUserTOTPKey, updateUserTOTPKey } from "@/lib/server/user"; |
| 7 | +import { decodeBase64 } from "@oslojs/encoding"; |
| 8 | +import { verifyTOTP } from "@oslojs/otp"; |
| 9 | +import { redirect } from "next/navigation"; |
| 10 | + |
| 11 | +export async function verify2FAAction(_prev: ActionResult, formData: FormData): Promise<ActionResult> { |
| 12 | + const { session, user } = getCurrentSession(); |
| 13 | + if (session === null) { |
| 14 | + return { |
| 15 | + message: "Not authenticated" |
| 16 | + }; |
| 17 | + } |
| 18 | + if (!user.emailVerified) { |
| 19 | + return { |
| 20 | + message: "Forbidden" |
| 21 | + }; |
| 22 | + } |
| 23 | + if (!user.registered2FA) { |
| 24 | + return { |
| 25 | + message: "Forbidden" |
| 26 | + }; |
| 27 | + } |
| 28 | + if (!totpBucket.check(user.id, 1)) { |
| 29 | + return { |
| 30 | + message: "Too many requests" |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + const code = formData.get("code"); |
| 35 | + if (typeof code !== "string") { |
| 36 | + return { |
| 37 | + message: "Invalid or missing fields" |
| 38 | + }; |
| 39 | + } |
| 40 | + if (code === "") { |
| 41 | + return { |
| 42 | + message: "Enter your code" |
| 43 | + }; |
| 44 | + } |
| 45 | + if (!totpBucket.consume(user.id, 1)) { |
| 46 | + return { |
| 47 | + message: "Too many requests" |
| 48 | + }; |
| 49 | + } |
| 50 | + const totpKey = getUserTOTPKey(user.id); |
| 51 | + if (totpKey === null) { |
| 52 | + return { |
| 53 | + message: "Forbidden" |
| 54 | + }; |
| 55 | + } |
| 56 | + if (!verifyTOTP(totpKey, 30, 6, code)) { |
| 57 | + return { |
| 58 | + message: "Invalid code" |
| 59 | + }; |
| 60 | + } |
| 61 | + totpBucket.reset(user.id); |
| 62 | + setSessionAs2FAVerified(session.id); |
| 63 | + return redirect("/"); |
| 64 | +} |
| 65 | + |
| 66 | +const totpUpdateBucket = new RefillingTokenBucket<number>(3, 60 * 10); |
| 67 | + |
| 68 | +export async function setup2FAAction(_prev: ActionResult, formData: FormData): Promise<ActionResult> { |
| 69 | + const { session, user } = getCurrentSession(); |
| 70 | + if (session === null) { |
| 71 | + return { |
| 72 | + message: "Not authenticated" |
| 73 | + }; |
| 74 | + } |
| 75 | + if (!user.emailVerified) { |
| 76 | + return { |
| 77 | + message: "Forbidden" |
| 78 | + }; |
| 79 | + } |
| 80 | + if (user.registered2FA && !session.twoFactorVerified) { |
| 81 | + return { |
| 82 | + message: "Forbidden" |
| 83 | + }; |
| 84 | + } |
| 85 | + if (!totpUpdateBucket.check(user.id, 1)) { |
| 86 | + return { |
| 87 | + message: "Too many requests" |
| 88 | + }; |
| 89 | + } |
| 90 | + |
| 91 | + const encodedKey = formData.get("key"); |
| 92 | + const code = formData.get("code"); |
| 93 | + if (typeof encodedKey !== "string" || typeof code !== "string") { |
| 94 | + return { |
| 95 | + message: "Invalid or missing fields" |
| 96 | + }; |
| 97 | + } |
| 98 | + if (code === "") { |
| 99 | + return { |
| 100 | + message: "Please enter your code" |
| 101 | + }; |
| 102 | + } |
| 103 | + if (encodedKey.length !== 28) { |
| 104 | + return { |
| 105 | + message: "Please enter your code" |
| 106 | + }; |
| 107 | + } |
| 108 | + let key: Uint8Array; |
| 109 | + try { |
| 110 | + key = decodeBase64(encodedKey); |
| 111 | + } catch { |
| 112 | + return { |
| 113 | + message: "Invalid key" |
| 114 | + }; |
| 115 | + } |
| 116 | + if (key.byteLength !== 20) { |
| 117 | + return { |
| 118 | + message: "Invalid key" |
| 119 | + }; |
| 120 | + } |
| 121 | + if (!totpUpdateBucket.consume(user.id, 1)) { |
| 122 | + return { |
| 123 | + message: "Too many requests" |
| 124 | + }; |
| 125 | + } |
| 126 | + if (!verifyTOTP(key, 30, 6, code)) { |
| 127 | + return { |
| 128 | + message: "Invalid code" |
| 129 | + }; |
| 130 | + } |
| 131 | + updateUserTOTPKey(session.userId, key); |
| 132 | + setSessionAs2FAVerified(session.id); |
| 133 | + return redirect("/recovery-code"); |
| 134 | +} |
| 135 | + |
| 136 | +export async function reset2FAAction(_prev: ActionResult, formData: FormData): Promise<ActionResult> { |
| 137 | + const { session, user } = getCurrentSession(); |
| 138 | + if (session === null) { |
| 139 | + return { |
| 140 | + message: "Not authenticated" |
| 141 | + }; |
| 142 | + } |
| 143 | + if (!user.emailVerified) { |
| 144 | + return { |
| 145 | + message: "Forbidden" |
| 146 | + }; |
| 147 | + } |
| 148 | + if (!user.registered2FA) { |
| 149 | + return { |
| 150 | + message: "Forbidden" |
| 151 | + }; |
| 152 | + } |
| 153 | + if (!recoveryCodeBucket.check(user.id, 1)) { |
| 154 | + return { |
| 155 | + message: "Too many requests" |
| 156 | + }; |
| 157 | + } |
| 158 | + |
| 159 | + const code = formData.get("code"); |
| 160 | + if (typeof code !== "string") { |
| 161 | + return { |
| 162 | + message: "Invalid or missing fields" |
| 163 | + }; |
| 164 | + } |
| 165 | + if (code === "") { |
| 166 | + return { |
| 167 | + message: "Please enter your code" |
| 168 | + }; |
| 169 | + } |
| 170 | + if (!recoveryCodeBucket.consume(user.id, 1)) { |
| 171 | + return { |
| 172 | + message: "Too many requests" |
| 173 | + }; |
| 174 | + } |
| 175 | + const valid = resetUser2FAWithRecoveryCode(user.id, code); |
| 176 | + if (!valid) { |
| 177 | + return { |
| 178 | + message: "Invalid recovery code" |
| 179 | + }; |
| 180 | + } |
| 181 | + recoveryCodeBucket.reset(user.id); |
| 182 | + return redirect("/2fa/setup"); |
| 183 | +} |
| 184 | + |
| 185 | +interface ActionResult { |
| 186 | + message: string; |
| 187 | +} |
0 commit comments