From 5de53e23eaefdc1fda1474a49acb63babefdd22e Mon Sep 17 00:00:00 2001 From: Silas Date: Sat, 4 Dec 2021 16:57:47 -0500 Subject: [PATCH] implement step 2 stuff --- assets/js/definitions/index.d.ts | 3 ++ assets/js/pages/AuthPage.tsx | 16 ++++++++++ assets/js/pages/ForPage.tsx | 32 ++++++++++++++++++- assets/js/pages/JustPage.tsx | 13 +++++--- assets/js/pages/YouPage.tsx | 30 ++++++++++++++--- assets/tsconfig.json | 3 +- lib/entendu/links.ex | 1 + lib/entendu/links/link.ex | 6 +++- .../controllers/auth_controller.ex | 1 - .../controllers/link_controller.ex | 19 ++++++++--- lib/entendu_web/live/live_helpers.ex | 4 +-- lib/entendu_web/templates/link/just.html.eex | 2 +- lib/entendu_web/views/link_view.ex | 1 - ...315_add_service_and_recipient_to_links.exs | 10 ++++++ 14 files changed, 121 insertions(+), 20 deletions(-) create mode 100644 assets/js/definitions/index.d.ts create mode 100644 assets/js/pages/AuthPage.tsx create mode 100644 priv/repo/migrations/20211204212315_add_service_and_recipient_to_links.exs diff --git a/assets/js/definitions/index.d.ts b/assets/js/definitions/index.d.ts new file mode 100644 index 0000000..98200a5 --- /dev/null +++ b/assets/js/definitions/index.d.ts @@ -0,0 +1,3 @@ +interface Link { + id: string +} \ No newline at end of file diff --git a/assets/js/pages/AuthPage.tsx b/assets/js/pages/AuthPage.tsx new file mode 100644 index 0000000..97013f8 --- /dev/null +++ b/assets/js/pages/AuthPage.tsx @@ -0,0 +1,16 @@ +// import HexMix from "../utils/hexmix"; +// const fragmentData = window.location.hash.split('.'); +// if (fragmentData.length <= 0) { +// alert("No key found in fragment URI"); +// return; +// } +// const key = HexMix.hexToUint8(fragmentData[0]); +// const iv = HexMix.hexToUint8(fragmentData[1]); + +// const importedKey = await window.crypto.subtle.importKey( +// 'raw', +// key, +// 'AES-GCM', +// true, +// ['encrypt', 'decrypt'] +// ); \ No newline at end of file diff --git a/assets/js/pages/ForPage.tsx b/assets/js/pages/ForPage.tsx index 737fba2..4e1e4e2 100644 --- a/assets/js/pages/ForPage.tsx +++ b/assets/js/pages/ForPage.tsx @@ -2,6 +2,7 @@ import React, { useState } from "react"; import { ProgressIndicator, Header2, Button, IconArrow, Label, Input, Select, CenteredContainer, SpaceBetweenContainer, Spacer, TextAlignWrapper, GlobalStyle } from "@intended/intended-ui"; + const ForPage = () => { const [recipientInput, setRecipientInput] = useState(""); const [serviceSelect, setServiceSelect] = useState(""); @@ -16,6 +17,35 @@ const ForPage = () => { setServiceSelect(e.target.value); }; + const postContacts = async () => { + const fragmentData = window.location.hash.split('.'); + if (fragmentData.length <= 0) { + alert("No key found in fragment URI"); + return; + } + + const linkId = sessionStorage.getItem("link_id"); + if (linkId == null || linkId == "") { + alert("No created link found in storage"); + return; + } + + const formData = new FormData(); + formData.append('recipient', recipientInput); + formData.append('service', serviceSelect); + formData.append("link_id", linkId); + + try { + await fetch(`${window.location.origin}/just/for`, { + body: formData, + method: "POST" + }); + window.location.href = `${window.location.origin}/just/for/you`; + } catch (err: any) { + alert(err.message); + } + }; + return ( @@ -50,7 +80,7 @@ const ForPage = () => { - + diff --git a/assets/js/pages/JustPage.tsx b/assets/js/pages/JustPage.tsx index 9fb0f1a..e2db81e 100644 --- a/assets/js/pages/JustPage.tsx +++ b/assets/js/pages/JustPage.tsx @@ -3,7 +3,7 @@ import React, { useState } from "react"; import { ProgressIndicator, Header2, Button, IconArrow, Label, FileInput, TextArea, CenteredContainer, Spacer, TextAlignWrapper, GlobalStyle } from '@intended/intended-ui'; import HexMix from "../utils/hexmix"; -const JustPage = () => { +const JustPage = (props) => { const [secretInput, setSecretInput] = useState(""); const [fileInput, setFileInput] = useState(null); const [fileName, setFileName] = useState(""); @@ -55,12 +55,17 @@ const JustPage = () => { formData.append('filename', 'secret.txt'); try { - await fetch(`${window.location.origin}/just`, { + const link: unknown = await fetch(`${window.location.origin}/just`, { + headers: { + "X-CSRF-Token": props.csrf + }, body: formData, method: "POST" }); - const url = `${window.location.origin}/just/for#${keyHex}.${ivHex}`; - window.location.href = url + sessionStorage.setItem("link_id", (link as Link).id); + sessionStorage.setItem("key_hex", keyHex); + sessionStorage.setItem("iv_hex", ivHex); + window.location.href = `${window.location.origin}/just/for`; } catch (err: any) { alert(err.message); } diff --git a/assets/js/pages/YouPage.tsx b/assets/js/pages/YouPage.tsx index 615b323..b503757 100644 --- a/assets/js/pages/YouPage.tsx +++ b/assets/js/pages/YouPage.tsx @@ -1,8 +1,27 @@ -import React from "react"; +import React, { useState } from "react"; import { ProgressIndicator, Header2, Button, IconArrow, InputButtonWithIcon, Label, Input, CenteredContainer, SpaceBetweenContainer, Spacer, TextAlignWrapper, GlobalStyle } from "@intended/intended-ui"; const YouPage = () => { + const calculateUrl = () => { + const linkId = sessionStorage.getItem("link_id"); + const keyHex = sessionStorage.getItem("key_hex"); + const ivHex = sessionStorage.getItem("iv_hex"); + + `${window.location.origin}/just/for/you/${linkId}#${keyHex}.${ivHex}` + return ""; + }; + + const [url, setUrl] = useState(calculateUrl()); + + const copyUrl = async () => { + try { + navigator.clipboard.writeText(url); + } catch (err: any) { + alert("Could not copy url to clipboard."); + } + } + return ( @@ -18,8 +37,8 @@ const YouPage = () => { {}} - value="https://intended.link/for/you/MWUzZjg4YmEtZmNmNy00M..." + onClick={copyUrl} + value={url} variant="copy" /> @@ -39,7 +58,10 @@ const YouPage = () => { - + diff --git a/assets/tsconfig.json b/assets/tsconfig.json index 55f0b15..9ab7787 100644 --- a/assets/tsconfig.json +++ b/assets/tsconfig.json @@ -11,7 +11,8 @@ "forceConsistentCasingInFileNames": true, "noUnusedLocals": true, "noUnusedParameters": true, - "lib": ["es2015", "dom"] + "lib": ["es2015", "dom"], + "typeRoots": ["./js/definitions"] }, "exclude": [ "/node_modules/**/*", diff --git a/lib/entendu/links.ex b/lib/entendu/links.ex index 01fa9c0..6e9de59 100644 --- a/lib/entendu/links.ex +++ b/lib/entendu/links.ex @@ -36,6 +36,7 @@ defmodule Entendu.Links do """ def get_link!(id), do: Repo.get!(Link, id) + def get_link(id), do: Repo.get(Link, id) @doc """ Creates a link. diff --git a/lib/entendu/links/link.ex b/lib/entendu/links/link.ex index d939945..f36d468 100644 --- a/lib/entendu/links/link.ex +++ b/lib/entendu/links/link.ex @@ -11,6 +11,8 @@ defmodule Entendu.Links.Link do field :filetype, :string field :text_content, :string field :file_content, :string + field :recipient, :string + field :service, :string timestamps() end @@ -24,7 +26,9 @@ defmodule Entendu.Links.Link do :filename, :filetype, :text_content, - :file_content + :file_content, + :recipient, + :service ]) end end diff --git a/lib/entendu_web/controllers/auth_controller.ex b/lib/entendu_web/controllers/auth_controller.ex index a92017b..531e44f 100644 --- a/lib/entendu_web/controllers/auth_controller.ex +++ b/lib/entendu_web/controllers/auth_controller.ex @@ -7,7 +7,6 @@ defmodule EntenduWeb.AuthController do plug Ueberauth - alias Ueberauth.Strategy.Helpers alias Entendu.UserFromAuth def delete(conn, _params) do diff --git a/lib/entendu_web/controllers/link_controller.ex b/lib/entendu_web/controllers/link_controller.ex index f804274..981daa7 100644 --- a/lib/entendu_web/controllers/link_controller.ex +++ b/lib/entendu_web/controllers/link_controller.ex @@ -33,8 +33,6 @@ defmodule EntenduWeb.LinkController do link_params <- Params.to_map(changeset), {:ok, %Link{} = link} <- Links.create_link(link_params) do conn - |> put_status(:created) - |> assign(:link, link) |> render("show_authorized.json", %{link: link}) end end @@ -43,8 +41,21 @@ defmodule EntenduWeb.LinkController do render(conn, "for.html") end - def for(_conn, %{username: _username, service: _service}) do - {:error, "not implemented"} + defparams( + second_step(%{ + service: :string, + recipient: :string + }) + ) + + def for(conn, %{link_id: link_id} = params) do + with %Changeset{valid?: true} = changeset <- first_step(params), + link_params <- Params.to_map(changeset), + %Link{} = link <- Links.get_link(link_id), + Links.update_link(link, link_params) do + conn + |> render("show_authorized.json", %{link: link}) + end end def you_page(conn, _params) do diff --git a/lib/entendu_web/live/live_helpers.ex b/lib/entendu_web/live/live_helpers.ex index ff5df47..122cfaa 100644 --- a/lib/entendu_web/live/live_helpers.ex +++ b/lib/entendu_web/live/live_helpers.ex @@ -15,9 +15,9 @@ defmodule EntenduWeb.LiveHelpers do link: @link, return_to: Routes.link_index_path(@socket, :index) %> """ - def live_modal(socket, component, opts) do + def live_modal(_socket, component, opts) do path = Keyword.fetch!(opts, :return_to) modal_opts = [id: :modal, return_to: path, component: component, opts: opts] - live_component(socket, EntenduWeb.ModalComponent, modal_opts) + live_component(EntenduWeb.ModalComponent, modal_opts) end end diff --git a/lib/entendu_web/templates/link/just.html.eex b/lib/entendu_web/templates/link/just.html.eex index ed05a0f..68c9ad9 100644 --- a/lib/entendu_web/templates/link/just.html.eex +++ b/lib/entendu_web/templates/link/just.html.eex @@ -1 +1 @@ -<%= react_component("Components.JustPage") %> +<%= react_component("Components.JustPage", %{ csrf: Plug.CSRFProtection.get_csrf_token() }) %> diff --git a/lib/entendu_web/views/link_view.ex b/lib/entendu_web/views/link_view.ex index 013599e..1c19a1a 100644 --- a/lib/entendu_web/views/link_view.ex +++ b/lib/entendu_web/views/link_view.ex @@ -1,6 +1,5 @@ defmodule EntenduWeb.LinkView do use EntenduWeb, :view - alias EntenduWeb.LinkView def render("show_authorized.json", %{link: link}) do %{ diff --git a/priv/repo/migrations/20211204212315_add_service_and_recipient_to_links.exs b/priv/repo/migrations/20211204212315_add_service_and_recipient_to_links.exs new file mode 100644 index 0000000..d02b3b8 --- /dev/null +++ b/priv/repo/migrations/20211204212315_add_service_and_recipient_to_links.exs @@ -0,0 +1,10 @@ +defmodule Entendu.Repo.Migrations.AddServiceAndRecipientToLinks do + use Ecto.Migration + + def change do + alter table(:links) do + add :recipient, :string + add :service, :string + end + end +end