Skip to content

Commit

Permalink
create_api_key/1 decrypt_api_key/1 for #42
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 26, 2020
1 parent 81c790f commit 61f812b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/auth_web/controllers/apikey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ defmodule AuthWeb.ApikeyController do
render(conn, "new.html", changeset: changeset)
end

def encrypt_encode(person_id) do
Fields.AES.encrypt(person_id) |> Base58.encode
end

def create_api_key(person_id) do
encrypt_encode(person_id) <> "/" <> encrypt_encode(person_id)
end

def decode_decrypt(key) do
key |> Base58.decode |> Fields.AES.decrypt() |> String.to_integer()
end

def decrypt_api_key(key) do
key |> String.split("/") |> List.first() |> decode_decrypt()
end

def create(conn, %{"apikey" => apikey_params}) do
IO.inspect(apikey_params, label: "apikey_params")
person_id = conn.assigns.decoded.id
Expand Down
40 changes: 40 additions & 0 deletions test/auth_web/controllers/apikey_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@ defmodule AuthWeb.ApikeyControllerTest do
use AuthWeb.ConnCase

# alias Auth.Apikey
import AuthWeb.ApikeyController

describe "Create a DWYL_API_KEY for a given person_id" do
test "encrypt_encode/1 returns a base58 we can decrypt" do
person_id = 1
key = encrypt_encode(person_id)
# |> IO.inspect(label: "key")

decrypted = key
|> Base58.decode
# |> IO.inspect(label: "decoded")
|> Fields.AES.decrypt()
# |> IO.inspect(label: "decrypted")
|> String.to_integer()
# |> IO.inspect(label: "int")

assert decrypted == person_id
end

test "decode_decrypt/1 reverses the operation of encrypt_encode/1" do
person_id = 4869234521
key = encrypt_encode(person_id)
id = decode_decrypt(key)
assert person_id == id
end

test "create_api_key/1 creates a DWYL_API_KEY" do
person_id = 123456789
key = create_api_key(person_id)
assert key =~ "/"
end

test "decrypt_api_key/1 decrypts a DWYL_API_KEY" do
person_id = 123456789
key = create_api_key(person_id)
decrypted = decrypt_api_key(key) # |> IO.inspect()
assert decrypted == person_id
end

end



Expand Down

0 comments on commit 61f812b

Please sign in to comment.