From 4919f028c884a041da7ff098abb02389b4eac598 Mon Sep 17 00:00:00 2001 From: 简律纯 Date: Tue, 18 Apr 2023 03:02:17 +0800 Subject: ✨add envshare docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- envshare/app/unseal/page.tsx | 159 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 envshare/app/unseal/page.tsx (limited to 'envshare/app/unseal') diff --git a/envshare/app/unseal/page.tsx b/envshare/app/unseal/page.tsx new file mode 100644 index 0000000..0b63e6b --- /dev/null +++ b/envshare/app/unseal/page.tsx @@ -0,0 +1,159 @@ +"use client"; +import React, { Fragment, useState, useEffect } from "react"; +import { ClipboardDocumentCheckIcon, ClipboardDocumentIcon, Cog6ToothIcon } from "@heroicons/react/24/outline"; + +import { Title } from "@components/title"; + +import { decodeCompositeKey } from "pkg/encoding"; +import { decrypt } from "pkg/encryption"; +import Link from "next/link"; +import { ErrorMessage } from "@components/error"; + +export default function Unseal() { + const [compositeKey, setCompositeKey] = useState(""); + useEffect(() => { + if (typeof window !== "undefined") { + setCompositeKey(window.location.hash.replace(/^#/, "")); + } + }, []); + + const [text, setText] = useState(null); + const [loading, setLoading] = useState(false); + const [remainingReads, setRemainingReads] = useState(null); + const [error, setError] = useState(null); + const [copied, setCopied] = useState(false); + + const onSubmit = async () => { + try { + setError(null); + setText(null); + setLoading(true); + + if (!compositeKey) { + throw new Error("No id provided"); + } + + const { id, encryptionKey, version } = decodeCompositeKey(compositeKey); + const res = await fetch(`/api/v1/load?id=${id}`); + if (!res.ok) { + throw new Error(await res.text()); + } + const json = (await res.json()) as { + iv: string; + encrypted: string; + remainingReads: number | null; + }; + setRemainingReads(json.remainingReads); + + const decrypted = await decrypt(json.encrypted, encryptionKey, json.iv, version); + + setText(decrypted); + } catch (e) { + console.error(e); + setError((e as Error).message); + } finally { + setLoading(false); + } + }; + + return ( +
+ {error ? : null} + {text ? ( +
+ {remainingReads !== null ? ( +
+ {remainingReads > 0 ? ( +

+ This document can be read {remainingReads} more times. +

+ ) : ( +

+ This was the last time this document could be read. It was deleted from storage. +

+ )} +
+ ) : null} +
+            
+ +
+
+                  {text}
+                
+
+
+
+ +
+ + Share another + + +
+
+ ) : ( +
{ + e.preventDefault(); + onSubmit(); + }} + > + Decrypt a document + +
+ + setCompositeKey(e.target.value)} + /> +
+ + +
+ )} +
+ ); +} -- cgit v1.2.3-70-g09d2