All files / src/components/settings SecurityTab.tsx

100% Statements 69/69
100% Branches 24/24
90% Functions 9/10
100% Lines 69/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280              299x 299x 299x 299x 299x 299x     299x 299x 299x 299x 299x 299x 299x 299x 299x     299x 15x 15x         299x 4x 4x 1x 1x   3x 1x 1x   2x 2x 2x 2x 2x 1x 1x 1x 1x   1x   2x       299x 6x 6x 6x 6x 6x 5x   5x 5x   1x   6x       299x 2x 2x 2x 2x 1x 1x 1x 1x   1x   2x       299x 2x 2x       2x 2x 2x 2x     299x                                                                                                                                                       264x                           3x                                                                                                                                                                          
import { useEffect, useState } from "react";
import { api } from "../../api";
import { PasswordStrengthMeter } from "../PasswordStrengthMeter";
import { FormField } from "./FormField";
 
export function SecurityTab() {
	// Change password state
	const [currentPassword, setCurrentPassword] = useState("");
	const [newPassword, setNewPassword] = useState("");
	const [confirmNewPassword, setConfirmNewPassword] = useState("");
	const [changePwLoading, setChangePwLoading] = useState(false);
	const [changePwError, setChangePwError] = useState<string | null>(null);
	const [changePwSuccess, setChangePwSuccess] = useState(false);
 
	// Rotate recovery key state
	const [rotatePassword, setRotatePassword] = useState("");
	const [rotateLoading, setRotateLoading] = useState(false);
	const [rotateError, setRotateError] = useState<string | null>(null);
	const [newMnemonic, setNewMnemonic] = useState<string | null>(null);
	const [rotateAcknowledged, setRotateAcknowledged] = useState(false);
	const [confirmLoading, setConfirmLoading] = useState(false);
	const [confirmError, setConfirmError] = useState<string | null>(null);
	const [rotateConfirmPassword, setRotateConfirmPassword] = useState("");
	const [hasPendingRotation, setHasPendingRotation] = useState(false);
 
	// Check for pending rotation on mount (e.g. power failed before confirmation)
	useEffect(() => {
		api.encryption.recoveryRotationStatus().then(
			({ pending }) => setHasPendingRotation(pending),
			() => {}, // ignore errors (locked state, etc.)
		);
	}, []);
 
	const handleChangePassword = async (e: React.FormEvent) => {
		e.preventDefault();
		if (newPassword !== confirmNewPassword) {
			setChangePwError("New passwords do not match.");
			return;
		}
		if (newPassword.length < 12) {
			setChangePwError("New password must be at least 12 characters.");
			return;
		}
		setChangePwLoading(true);
		setChangePwError(null);
		setChangePwSuccess(false);
		try {
			await api.encryption.changePassword(currentPassword, newPassword);
			setChangePwSuccess(true);
			setCurrentPassword("");
			setNewPassword("");
			setConfirmNewPassword("");
		} catch (err) {
			setChangePwError((err as Error).message);
		} finally {
			setChangePwLoading(false);
		}
	};
 
	const handleRotateRecoveryKey = async (e: React.FormEvent) => {
		e.preventDefault();
		setRotateLoading(true);
		setRotateError(null);
		try {
			const { recoveryMnemonic } = await api.encryption.rotateRecoveryKey(rotatePassword);
			setNewMnemonic(recoveryMnemonic);
			// Pre-fill confirm password since we just verified it
			setRotateConfirmPassword(rotatePassword);
			setRotatePassword("");
		} catch (err) {
			setRotateError((err as Error).message);
		} finally {
			setRotateLoading(false);
		}
	};
 
	const handleConfirmRotation = async () => {
		setConfirmLoading(true);
		setConfirmError(null);
		try {
			await api.encryption.confirmRecoveryRotation(rotateConfirmPassword);
			setNewMnemonic(null);
			setRotateAcknowledged(false);
			setRotateConfirmPassword("");
			setHasPendingRotation(false);
		} catch (err) {
			setConfirmError((err as Error).message);
		} finally {
			setConfirmLoading(false);
		}
	};
 
	const handleCancelRotation = async () => {
		try {
			await api.encryption.cancelRecoveryRotation();
		} catch {
			// best effort
		}
		setNewMnemonic(null);
		setRotateAcknowledged(false);
		setRotateConfirmPassword("");
		setHasPendingRotation(false);
	};
 
	return (
		<div className="space-y-8 p-4 sm:p-6">
			<h3 className="text-sm font-medium text-gray-900 dark:text-gray-100">Security</h3>
 
			{/* Change Password */}
			<form onSubmit={handleChangePassword} className="space-y-4">
				<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300">Change Password</h4>
 
				{changePwError && (
					<div className="text-sm text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20 px-3 py-2 rounded-md">
						{changePwError}
					</div>
				)}
				{changePwSuccess && (
					<div className="text-sm text-green-600 dark:text-green-400 bg-green-50 dark:bg-green-900/20 px-3 py-2 rounded-md">
						Password changed successfully.
					</div>
				)}
 
				<FormField
					label="Current Password"
					value={currentPassword}
					onChange={setCurrentPassword}
					type="password"
					placeholder="Your current encryption password"
					required
				/>
				<div>
					<FormField
						label="New Password"
						value={newPassword}
						onChange={setNewPassword}
						type="password"
						placeholder="At least 12 characters"
						required
					/>
					<PasswordStrengthMeter password={newPassword} />
				</div>
				<FormField
					label="Confirm New Password"
					value={confirmNewPassword}
					onChange={setConfirmNewPassword}
					type="password"
					placeholder="Repeat your new password"
					required
				/>
 
				<button
					type="submit"
					disabled={changePwLoading}
					className="px-4 py-1.5 bg-stork-600 hover:bg-stork-700 disabled:opacity-50 text-white rounded-md text-sm font-medium transition-colors"
				>
					{changePwLoading ? "Changing\u2026" : "Change Password"}
				</button>
			</form>
 
			<hr className="border-gray-200 dark:border-gray-700" />
 
			{/* Rotate Recovery Key */}
			{newMnemonic ? (
				<div className="space-y-4">
					<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300">
						New Recovery Phrase
					</h4>
					<div className="text-sm text-amber-700 dark:text-amber-400 bg-amber-50 dark:bg-amber-900/20 px-3 py-2 rounded-md">
						Your old recovery phrase still works until you confirm below. Write down the new phrase,
						then confirm to complete the rotation.
					</div>
 
					<div className="bg-amber-50 dark:bg-amber-900/20 border border-amber-200 dark:border-amber-800 rounded-lg p-4">
						<p className="text-xs font-semibold text-amber-700 dark:text-amber-400 uppercase tracking-wider mb-3">
							Recovery Phrase
						</p>
						<div className="grid grid-cols-4 gap-2">
							{newMnemonic.split(/\s+/).map((word, i) => (
								// biome-ignore lint/suspicious/noArrayIndexKey: order-dependent list
								<div key={i} className="flex items-center gap-1.5">
									<span className="text-xs text-amber-500 dark:text-amber-600 w-5 text-right flex-shrink-0">
										{i + 1}.
									</span>
									<span className="text-sm font-mono text-gray-800 dark:text-gray-200">{word}</span>
								</div>
							))}
						</div>
					</div>
 
					<label className="flex items-start gap-3 cursor-pointer">
						<input
							type="checkbox"
							checked={rotateAcknowledged}
							onChange={(e) => setRotateAcknowledged(e.target.checked)}
							className="mt-0.5 rounded border-gray-300 dark:border-gray-600"
						/>
						<span className="text-sm text-gray-600 dark:text-gray-400">
							I've written down my new recovery phrase and stored it safely.
						</span>
					</label>
 
					{confirmError && (
						<div className="text-sm text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20 px-3 py-2 rounded-md">
							{confirmError}
						</div>
					)}
 
					<div className="flex gap-3">
						<button
							type="button"
							disabled={!rotateAcknowledged || confirmLoading}
							onClick={handleConfirmRotation}
							className="px-4 py-1.5 bg-stork-600 hover:bg-stork-700 disabled:opacity-50 disabled:cursor-not-allowed text-white rounded-md text-sm font-medium transition-colors"
						>
							{confirmLoading ? "Confirming\u2026" : "Confirm \u2014 Invalidate Old Phrase"}
						</button>
						<button
							type="button"
							onClick={handleCancelRotation}
							className="px-4 py-1.5 bg-gray-200 hover:bg-gray-300 dark:bg-gray-700 dark:hover:bg-gray-600 text-gray-700 dark:text-gray-300 rounded-md text-sm font-medium transition-colors"
						>
							Cancel
						</button>
					</div>
				</div>
			) : (
				<div className="space-y-4">
					{hasPendingRotation && (
						<div className="text-sm text-amber-700 dark:text-amber-400 bg-amber-50 dark:bg-amber-900/20 px-3 py-2 rounded-md">
							A recovery key rotation was started but not confirmed. Your old recovery phrase still
							works.{" "}
							<button
								type="button"
								onClick={handleCancelRotation}
								className="underline hover:no-underline font-medium"
							>
								Cancel the pending rotation
							</button>
						</div>
					)}
 
					<form onSubmit={handleRotateRecoveryKey} className="space-y-4">
						<h4 className="text-sm font-medium text-gray-700 dark:text-gray-300">
							Rotate Recovery Key
						</h4>
						<p className="text-sm text-gray-500 dark:text-gray-400">
							Generate a new 24-word recovery phrase. Your old phrase will continue to work until
							you confirm the rotation.
						</p>
 
						{rotateError && (
							<div className="text-sm text-red-600 dark:text-red-400 bg-red-50 dark:bg-red-900/20 px-3 py-2 rounded-md">
								{rotateError}
							</div>
						)}
 
						<FormField
							label="Current Password"
							value={rotatePassword}
							onChange={setRotatePassword}
							type="password"
							placeholder="Confirm your encryption password"
							required
						/>
 
						<button
							type="submit"
							disabled={rotateLoading}
							className="px-4 py-1.5 bg-amber-600 hover:bg-amber-700 disabled:opacity-50 text-white rounded-md text-sm font-medium transition-colors"
						>
							{rotateLoading ? "Generating\u2026" : "Rotate Recovery Key"}
						</button>
					</form>
				</div>
			)}
		</div>
	);
}