Skip to content

Commit

Permalink
run mix format, #74
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed May 13, 2020
1 parent fa512c5 commit 326b524
Show file tree
Hide file tree
Showing 12 changed files with 428 additions and 285 deletions.
15 changes: 8 additions & 7 deletions lib/auth/apikey.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ defmodule Auth.Apikey do
@doc false
def changeset(apikey, attrs) do
apikey
|> cast(attrs, [:client_id, :client_secret,
:name, :description, :url, :person_id])
|> cast(attrs, [:client_id, :client_secret, :name, :description, :url, :person_id])
|> validate_required([:client_secret])
end

Expand All @@ -38,11 +37,13 @@ defmodule Auth.Apikey do

def list_apikeys_for_person(person_id) do
# IO.inspect(person_id, label: "person_id")
query = from(
a in __MODULE__,
where: a.person_id == ^person_id,
select: a
)
query =
from(
a in __MODULE__,
where: a.person_id == ^person_id,
select: a
)

Repo.all(query)
end

Expand Down
10 changes: 8 additions & 2 deletions lib/auth/email.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ defmodule Auth.Email do
secret = System.get_env("SECRET_KEY_BASE")
jwt = AuthPlug.Token.generate_jwt!(params, secret)
headers = [Authorization: "#{jwt}"]
options = [ssl: [{:versions, [:"tlsv1.2"]}],
timeout: 50_000, recv_timeout: 50_000] # github.com/dwyl/auth/issues/48

options = [
ssl: [{:versions, [:"tlsv1.2"]}],
# github.com/dwyl/auth/issues/48
timeout: 50_000,
recv_timeout: 50_000
]

{:ok, response} = HTTPoison.post(url, "_nobody", headers, options)
Jason.decode!(response.body)
end
Expand Down
23 changes: 15 additions & 8 deletions lib/auth/person.ex
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ defmodule Auth.Person do
%Person{}
|> changeset(person)
|> put_email_status_verified()
# |> IO.inspect(label: "after put_email_status_verified")

# |> IO.inspect(label: "after put_email_status_verified")

case get_person_by_email(person.changes.email) do
nil ->
Expand Down Expand Up @@ -151,12 +152,14 @@ defmodule Auth.Person do
givenName: profile.given_name,
auth_provider: "google"
})

# |> IO.inspect(label: "merged")
end

def create_google_person(profile) do
transform_google_profile_data_to_person(profile)
|> upsert_person()

# |> IO.inspect(label: "create_person:")
end

Expand All @@ -182,6 +185,7 @@ defmodule Auth.Person do

def put_email_status_verified(changeset) do
provider = changeset.changes.auth_provider

if provider == "google" or provider == "github" do
put_change(changeset, :status, get_status_verified())
else
Expand Down Expand Up @@ -225,24 +229,27 @@ defmodule Auth.Person do
nil ->
create_person(person)

ep -> # existing person
# existing person
ep ->
merged = Map.merge(AuthPlug.Helpers.strip_struct_metadata(ep), person)
{:ok, person} = changeset(%Person{id: ep.id}, merged)
# |> IO.inspect(label: "changeset transformed:234")
|> Repo.update()

person # |> IO.inspect(label: "updated person:230")
{:ok, person} =
changeset(%Person{id: ep.id}, merged)
# |> IO.inspect(label: "changeset transformed:234")
|> Repo.update()

# |> IO.inspect(label: "updated person:230")
person
end
end


@doc """
`decrypt_email/1` accepts a `cyphertext` and attempts to Base58.decode
followed by AES.decrypt it. If decode or decrypt fails, return 0 (zero).
"""
def decrypt_email(cyphertext) do
try do
cyphertext |> Base58.decode |> Fields.AES.decrypt()
cyphertext |> Base58.decode() |> Fields.AES.decrypt()
rescue
ArgumentError ->
# IO.puts("AES.decrypt() unable to decrypt client_id")
Expand Down
9 changes: 5 additions & 4 deletions lib/auth_web/controllers/apikey_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ defmodule AuthWeb.ApikeyController do
end

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

def create_api_key(person_id) do
Expand All @@ -27,7 +27,7 @@ defmodule AuthWeb.ApikeyController do
"""
def decode_decrypt(key) do
try do
key |> Base58.decode |> Fields.AES.decrypt() |> String.to_integer()
key |> Base58.decode() |> Fields.AES.decrypt() |> String.to_integer()
rescue
ArgumentError ->
# IO.puts("AES.decrypt() unable to decrypt client_id")
Expand Down Expand Up @@ -71,7 +71,7 @@ defmodule AuthWeb.ApikeyController do
changeset = Auth.Apikey.change_apikey(apikey)
render(conn, "edit.html", apikey: apikey, changeset: changeset)
else
AuthWeb.AuthController.not_found(conn, "API KEY " <> id <> " not found." )
AuthWeb.AuthController.not_found(conn, "API KEY " <> id <> " not found.")
end
end

Expand All @@ -93,7 +93,7 @@ defmodule AuthWeb.ApikeyController do
render(conn, "edit.html", apikey: apikey, changeset: changeset)
end
else
AuthWeb.AuthController.not_found(conn, "API KEY " <> id <> " not found." )
AuthWeb.AuthController.not_found(conn, "API KEY " <> id <> " not found.")
end
end

Expand All @@ -102,6 +102,7 @@ defmodule AuthWeb.ApikeyController do
# check that the person attempting to delete the key owns it!
if apikey.person_id == conn.assigns.person.id do
{:ok, _apikey} = Apikey.delete_apikey(apikey)

conn
|> put_flash(:info, "Apikey deleted successfully.")
|> redirect(to: Routes.apikey_path(conn, :index))
Expand Down
Loading

0 comments on commit 326b524

Please sign in to comment.