verify.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import {
  2. getCryptoString,
  3. makeErrorResponse,
  4. makeSuccessResponse,
  5. } from "utils/server.ts";
  6. import { define } from "utils/state.ts";
  7. import { find } from "utils/db.ts";
  8. import { setCookie } from "@std/http";
  9. export const handler = define.handlers({
  10. async POST(ctx) {
  11. const req = ctx.req;
  12. const reqJson = await req.json();
  13. const id = reqJson.id;
  14. const password = reqJson.password || "";
  15. if (id && password) {
  16. const post = find("Post", { id, shared: 1 }, [
  17. "share_password",
  18. ]);
  19. if (post.length > 0) {
  20. const storedHash = post[0]["share_password"] as string;
  21. if (storedHash) {
  22. const inputHash = await getCryptoString(password, "MD5");
  23. if (inputHash === storedHash) {
  24. const token = await getCryptoString(
  25. id + storedHash,
  26. "MD5",
  27. );
  28. const resp = makeSuccessResponse(true);
  29. setCookie(resp.headers, {
  30. name: `pd-share-${id}`,
  31. value: token,
  32. path: "/",
  33. });
  34. return resp;
  35. }
  36. }
  37. }
  38. }
  39. return makeErrorResponse();
  40. },
  41. });