-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dddb1e8
commit b6fd6d0
Showing
10 changed files
with
235 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
defmodule TeslaMate.Auth do | ||
@moduledoc """ | ||
The Auth context. | ||
""" | ||
|
||
import Ecto.Query, warn: false | ||
alias TeslaMate.Repo | ||
|
||
### Credentials | ||
|
||
alias TeslaMate.Auth.Credentials | ||
|
||
def get_credentials do | ||
opts = Application.fetch_env!(:teslamate, :tesla_auth) | ||
|
||
with username when not is_nil(username) <- Keyword.get(opts, :username), | ||
password when not is_nil(password) <- Keyword.get(opts, :password) do | ||
%Credentials{email: username, password: password} | ||
else | ||
_ -> nil | ||
end | ||
end | ||
|
||
def change_credentials(attrs \\ %{}) do | ||
%Credentials{} |> Credentials.changeset(attrs) | ||
end | ||
|
||
### Tokens | ||
|
||
alias TeslaMate.Auth.Tokens | ||
|
||
def get_tokens do | ||
case Repo.all(Tokens) do | ||
[tokens] -> tokens | ||
[] -> nil | ||
end | ||
end | ||
|
||
def save(%{token: access, refresh_token: refresh}) do | ||
attrs = %{access: access, refresh: refresh} | ||
|
||
maybe_created_or_updated = | ||
case get_tokens() do | ||
nil -> create_tokens(attrs) | ||
tokens -> update_tokens(tokens, attrs) | ||
end | ||
|
||
with {:ok, _tokens} <- maybe_created_or_updated do | ||
:ok | ||
end | ||
end | ||
|
||
defp create_tokens(attrs) do | ||
%Tokens{} | ||
|> Tokens.changeset(attrs) | ||
|> Repo.insert() | ||
end | ||
|
||
defp update_tokens(%Tokens{} = tokens, attrs) do | ||
tokens | ||
|> Tokens.changeset(attrs) | ||
|> Repo.update() | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
defmodule TeslaMate.Auth.Credentials do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "" do | ||
field :email, :string | ||
field :password, :string | ||
end | ||
|
||
@doc false | ||
def changeset(credentials, attrs) do | ||
credentials | ||
|> cast(attrs, [:email, :password]) | ||
|> validate_required([:email, :password]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
defmodule TeslaMate.Auth.Tokens do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
schema "tokens" do | ||
field :refresh, :string | ||
field :access, :string | ||
|
||
timestamps() | ||
end | ||
|
||
@doc false | ||
def changeset(tokens, attrs) do | ||
tokens | ||
|> cast(attrs, [:access, :refresh]) | ||
|> validate_required([:access, :refresh]) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
defmodule TeslaMate.Repo.Migrations.CreateTokens do | ||
use Ecto.Migration | ||
|
||
def change do | ||
create table(:tokens) do | ||
add(:access, :string, null: false) | ||
add(:refresh, :string, null: false) | ||
|
||
timestamps() | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
defmodule TeslaMate.AuthTest do | ||
use TeslaMate.DataCase | ||
|
||
alias TeslaMate.Auth | ||
|
||
describe "tokens" do | ||
@valid_attrs %{refresh_token: "some refresh token", token: "some access token"} | ||
@update_attrs %{ | ||
refresh_token: "some updated refresh token", | ||
token: "some updated access token" | ||
} | ||
@invalid_attrs %{refresh_token: nil, token: nil} | ||
|
||
test "save/1 with valid data creates or updats the tokens" do | ||
assert Auth.get_tokens() == nil | ||
|
||
assert :ok = Auth.save(@valid_attrs) | ||
assert tokens = Auth.get_tokens() | ||
assert tokens.refresh == "some refresh token" | ||
assert tokens.access == "some access token" | ||
|
||
assert :ok = Auth.save(@update_attrs) | ||
assert tokens = Auth.get_tokens() | ||
assert tokens.refresh == "some updated refresh token" | ||
assert tokens.access == "some updated access token" | ||
end | ||
|
||
test "save/1 with invalid data returns error changeset" do | ||
assert {:error, %Ecto.Changeset{} = changeset} = Auth.save(@invalid_attrs) | ||
|
||
assert %{refresh: ["can't be blank"], access: ["can't be blank"]} == | ||
errors_on(changeset) | ||
end | ||
end | ||
end |