Skip to content

Commit 2a77608

Browse files
committed
feat: add send web push
1 parent 98ac0d2 commit 2a77608

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

lib/web_push_elixir.ex

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,24 @@ defmodule WebPushElixir do
107107
defp headers("aesgcm", jwt, pub) do
108108
%{"Authorization" => "WebPush " <> jwt, "Crypto-Key" => "p256ecdsa=" <> pub}
109109
end
110+
111+
def send_web_push(message, %{endpoint: endpoint} = subscription) do
112+
payload = encrypt(message, subscription)
113+
114+
headers =
115+
get_headers(make_audience(endpoint), "aesgcm")
116+
|> Map.merge(%{
117+
"TTL" => "0",
118+
"Content-Encoding" => "aesgcm",
119+
"Encryption" => "salt=#{Base.url_encode64(payload.salt, padding: false)}",
120+
"Crypto-Key" => "dh=#{Base.url_encode64(payload.server_public_key, padding: false)};"
121+
})
122+
123+
HTTPoison.post(endpoint, payload.ciphertext, headers)
124+
end
125+
126+
defp make_audience(endpoint) do
127+
parsed = URI.parse(endpoint)
128+
parsed.scheme <> "://" <> parsed.host
129+
end
110130
end

test/web_push_elixir_test.exs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ defmodule WebPushElixirTest do
33

44
import ExUnit.CaptureLog
55

6-
@subscription_from_client '{"endpoint":"https://some.pushservice.com/something-unique","keys":{"p256dh":"BIPUL12DLfytvTajnryr2PRdAgXS3HGKiLqndGcJGabyhHheJYlNGCeXl1dn18gSJ1WAkAPIxr4gK0_dQds4yiI=","auth":"FPssNDTKnInHVndSTdbKFw=="}}'
6+
@subscription_from_client '{"endpoint":"http://localhost:4040/some-endpoint","keys":{"p256dh":"BIPUL12DLfytvTajnryr2PRdAgXS3HGKiLqndGcJGabyhHheJYlNGCeXl1dn18gSJ1WAkAPIxr4gK0_dQds4yiI=","auth":"FPssNDTKnInHVndSTdbKFw=="}}'
77
@subscription_decoded %{
8-
endpoint: "https://some.pushservice.com/something-unique",
8+
endpoint: "http://localhost:4040/some-endpoint",
99
keys: %{
1010
auth: "FPssNDTKnInHVndSTdbKFw==",
1111
p256dh:
@@ -20,6 +20,7 @@ defmodule WebPushElixirTest do
2020
assert capture_log(WebPushElixir.gen_key_pair() |> WebPushElixir.output_key_pair()) =~ "public_key:"
2121
assert capture_log(WebPushElixir.gen_key_pair() |> WebPushElixir.output_key_pair()) =~ "private_key:"
2222
assert capture_log(WebPushElixir.gen_key_pair() |> WebPushElixir.output_key_pair()) =~ "subject:"
23+
2324
assert capture_log(WebPushElixir.gen_key_pair() |> WebPushElixir.output_key_pair()) =~ "mailto:[email protected]"
2425
end
2526

@@ -56,4 +57,26 @@ defmodule WebPushElixirTest do
5657

5758
assert {true, _, _} = JOSE.JWT.verify_strict(jwk, ["ES256"], jwt)
5859
end
60+
61+
test "it should send web push" do
62+
{public, private} = WebPushElixir.gen_key_pair()
63+
64+
System.put_env("PUBLIC_KEY", public)
65+
66+
System.put_env("PRIVATE_KEY", private)
67+
68+
System.put_env("SUBJECT", "mailto:[email protected]")
69+
70+
{:ok, response} = WebPushElixir.send_web_push("some message", @subscription_decoded)
71+
72+
assert [
73+
{"Authorization", "WebPush " <> <<_JWT::binary>>},
74+
{"Content-Encoding", "aesgcm"},
75+
{"Crypto-Key", <<_server_public_key::binary>>},
76+
{"Encryption", "salt=" <> <<_salt::binary>>},
77+
{"TTL", "0"}
78+
] = response.request.headers
79+
80+
assert <<_body::binary>> = response.request.body
81+
end
5982
end

0 commit comments

Comments
 (0)