Skip to content

Commit cf81444

Browse files
committed
feat: add gen keypair
1 parent 193a12e commit cf81444

File tree

2 files changed

+15
-24
lines changed

2 files changed

+15
-24
lines changed

lib/web_push_elixir.ex

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
defmodule WebPushElixir do
2-
@moduledoc """
3-
Documentation for `WebPushElixir`.
4-
"""
2+
require Logger
53

6-
@doc """
7-
Hello world.
4+
def gen_keypair do
5+
{public, private} = :crypto.generate_key(:ecdh, :prime256v1)
86

9-
## Examples
10-
11-
iex> WebPushElixir.hello()
12-
:world
13-
14-
"""
15-
def hello do
16-
:world
7+
fn ->
8+
Logger.info(%{:public_key => Base.url_encode64(public, padding: false)})
9+
Logger.info(%{:private_key => Base.url_encode64(private, padding: false)})
10+
Logger.info(%{:subject => "mailto:[email protected]"})
11+
end
1712
end
1813
end

test/web_push_elixir_test.exs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
defmodule WebPushElixirTest do
22
use ExUnit.Case
33

4-
test "it should decode" do
5-
assert Jason.decode!(
6-
'{"endpoint":"https://some.pushservice.com/something-unique","keys":{"p256dh":"BIPUL12DLfytvTajnryr2PRdAgXS3HGKiLqndGcJGabyhHheJYlNGCeXl1dn18gSJ1WAkAPIxr4gK0_dQds4yiI=","auth":"FPssNDTKnInHVndSTdbKFw=="}}'
7-
) == %{
8-
"endpoint" => "https://some.pushservice.com/something-unique",
9-
"keys" => %{
10-
"auth" => "FPssNDTKnInHVndSTdbKFw==",
11-
"p256dh" =>
12-
"BIPUL12DLfytvTajnryr2PRdAgXS3HGKiLqndGcJGabyhHheJYlNGCeXl1dn18gSJ1WAkAPIxr4gK0_dQds4yiI="
13-
}
14-
}
4+
import ExUnit.CaptureLog
5+
6+
test "it should gen keypair" do
7+
assert capture_log(WebPushElixir.gen_keypair()) =~ "public_key:"
8+
assert capture_log(WebPushElixir.gen_keypair()) =~ "private_key:"
9+
assert capture_log(WebPushElixir.gen_keypair()) =~ "subject:"
10+
assert capture_log(WebPushElixir.gen_keypair()) =~ "mailto:[email protected]"
1511
end
1612
end

0 commit comments

Comments
 (0)