initial commit with imported utils from old github project
This commit is contained in:
commit
66bb463e23
39 files changed
+1944
No files matched your search
@@ -0,0 +1,43 @@
|
||||
"use server";
|
||||
import { error } from "console";
|
||||
import { subtle } from "crypto";
|
||||
import { cookies } from "next/headers";
|
||||
|
||||
export async function storeAccessKey_Cookie(key: CryptoKey) {
|
||||
if (key.extractable) {
|
||||
try {
|
||||
const exported = await subtle.exportKey("raw", key);
|
||||
const exportedKeyBuffer = new Uint8Array(exported);
|
||||
const key_base64 = Buffer.from(exportedKeyBuffer).toString("base64");
|
||||
(await cookies()).set("user_identity", key_base64);
|
||||
} catch (err) {
|
||||
throw error.apply(err);
|
||||
}
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeAccessKey_Cookie() {
|
||||
(await cookies()).set("user_identity", "");
|
||||
}
|
||||
|
||||
export async function retreiveAccessKey_Cookie() {
|
||||
const cookieStore = await cookies();
|
||||
try {
|
||||
const key_base64 = cookieStore.get("user_identity");
|
||||
if (key_base64 == null || key_base64.value.length < 1) {
|
||||
return null;
|
||||
}
|
||||
const key_raw = Uint8Array.from(atob(key_base64.value), (c) =>
|
||||
c.charCodeAt(0),
|
||||
);
|
||||
const key = subtle.importKey("raw", key_raw, "AES-GCM", true, [
|
||||
"encrypt",
|
||||
"decrypt",
|
||||
]);
|
||||
return key;
|
||||
} catch (err) {
|
||||
return error.apply(err);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user