Skip to content

Commit

Permalink
style: correctly format all files
Browse files Browse the repository at this point in the history
Signed-off-by: Filip Vavera <[email protected]>
  • Loading branch information
Sgiath committed Dec 19, 2023
1 parent 7cc660b commit bb0ec90
Show file tree
Hide file tree
Showing 69 changed files with 1,673 additions and 692 deletions.
2 changes: 1 addition & 1 deletion .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[
inputs: ["mix.exs", "config/*.exs", "lib/*/**.ex", "test/*/**.exs"]
inputs: ["mix.exs", ".*.exs", "{lib,test,config}/**/*.{ex,exs}"]
]
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ Config now has the form `config :my_app, ExOauth2Provider`. You can still use th

If your configuration has `:resource_owner` setting with a UUID, you should remove it and only use the module name for your user schema. UUID is now handled in the schema modules directly.

The schemas can be generated with `mix ex_oauth2_provider.install --no-migrations --binary-id`.
The schemas can be generated with `mix ex_oauth2_provider.install --no-migrations --binary-id`.
28 changes: 26 additions & 2 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import Config

if Mix.env() == :test do
import_config "test.exs"
# configuration is only used for tests
if config_env() == :test do
# Print only warnings and errors during test
config :logger, level: :warning

config :ex_oauth2_provider, namespace: Dummy

config :ex_oauth2_provider, ExOauth2Provider,
repo: Dummy.Repo,
resource_owner: Dummy.Users.User,
default_scopes: ~w(public),
optional_scopes: ~w(read write),
password_auth: {Dummy.Auth, :auth},
use_refresh_token: true,
revoke_refresh_token_on_use: true,
grant_flows: ~w(authorization_code client_credentials)

config :ex_oauth2_provider, Dummy.Repo,
database: System.get_env("POSTGRES_DATABASE") || "ex_oauth2_provider_test",
username: System.get_env("POSTGRES_USERNAME") || "postgres",
password: System.get_env("POSTGRES_PASSWORD") || "postgres",
hostname: System.get_env("POSTGRES_HOSTNAME") || "localhost",
port: (System.get_env("POSTGRES_PORT") || "5432") |> String.to_integer(),
pool: Ecto.Adapters.SQL.Sandbox,
priv: "test/support/priv",
url: System.get_env("POSTGRES_URL")
end
23 changes: 0 additions & 23 deletions config/test.exs

This file was deleted.

14 changes: 9 additions & 5 deletions lib/ex_oauth2_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ defmodule ExOauth2Provider do
@spec authenticate_token(binary(), keyword()) :: {:ok, map()} | {:error, any()}
def authenticate_token(token, config \\ [])
def authenticate_token(nil, _config), do: {:error, :token_inaccessible}

def authenticate_token(token, config) do
token
|> load_access_token(config)
Expand All @@ -61,37 +62,40 @@ defmodule ExOauth2Provider do

defp load_access_token(token, config) do
case AccessTokens.get_by_token(token, config) do
nil -> {:error, :token_not_found}
nil -> {:error, :token_not_found}
access_token -> {:ok, access_token}
end
end

defp maybe_revoke_previous_refresh_token({:error, error}, _config), do: {:error, error}

defp maybe_revoke_previous_refresh_token({:ok, access_token}, config) do
case Config.refresh_token_revoked_on_use?(config) do
true -> revoke_previous_refresh_token(access_token, config)
true -> revoke_previous_refresh_token(access_token, config)
false -> {:ok, access_token}
end
end

defp revoke_previous_refresh_token(access_token, config) do
case AccessTokens.revoke_previous_refresh_token(access_token, config) do
{:error, _any} -> {:error, :no_association_found}
{:error, _any} -> {:error, :no_association_found}
{:ok, _access_token} -> {:ok, access_token}
end
end

defp validate_access_token({:error, error}), do: {:error, error}

defp validate_access_token({:ok, access_token}) do
case AccessTokens.is_accessible?(access_token) do
true -> {:ok, access_token}
true -> {:ok, access_token}
false -> {:error, :token_inaccessible}
end
end

defp load_resource_owner({:error, error}, _config), do: {:error, error}

defp load_resource_owner({:ok, access_token}, config) do
repo = Config.repo(config)
repo = Config.repo(config)
access_token = repo.preload(access_token, :resource_owner)

{:ok, access_token}
Expand Down
10 changes: 8 additions & 2 deletions lib/ex_oauth2_provider/access_grants/access_grant.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule ExOauth2Provider.AccessGrants.AccessGrant do
@doc false
def indexes() do
[
{:token, true},
{:token, true}
]
end

Expand Down Expand Up @@ -72,7 +72,13 @@ defmodule ExOauth2Provider.AccessGrants.AccessGrant do
|> put_token()
|> Scopes.put_scopes(grant.application.scopes, config)
|> Scopes.validate_scopes(grant.application.scopes, config)
|> Changeset.validate_required([:redirect_uri, :expires_in, :token, :resource_owner, :application])
|> Changeset.validate_required([
:redirect_uri,
:expires_in,
:token,
:resource_owner,
:application
])
|> Changeset.unique_constraint(:token)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/ex_oauth2_provider/access_grants/access_grants.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ defmodule ExOauth2Provider.AccessGrants do
{:error, %Ecto.Changeset{}}
"""
@spec create_grant(Ecto.Schema.t(), Application.t(), map(), keyword()) :: {:ok, AccessGrant.t()} | {:error, term()}
@spec create_grant(Ecto.Schema.t(), Application.t(), map(), keyword()) ::
{:ok, AccessGrant.t()} | {:error, term()}
def create_grant(resource_owner, application, attrs, config \\ []) do
config
|> Config.access_grant()
Expand Down
4 changes: 3 additions & 1 deletion lib/ex_oauth2_provider/access_tokens/access_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ defmodule ExOauth2Provider.AccessTokens.AccessToken do
opts = Keyword.put(opts, :resource_owner_id, resource_owner_id(opts[:resource_owner]))

case Config.access_token_generator(config) do
nil -> Utils.generate_token(opts)
nil -> Utils.generate_token(opts)
{module, method} -> apply(module, method, [opts])
end
end
Expand All @@ -139,6 +139,7 @@ defmodule ExOauth2Provider.AccessTokens.AccessToken do
defp resource_owner_id(_), do: nil

defp put_previous_refresh_token(changeset, nil), do: changeset

defp put_previous_refresh_token(changeset, refresh_token),
do: Changeset.change(changeset, %{previous_refresh_token: refresh_token.refresh_token})

Expand All @@ -147,5 +148,6 @@ defmodule ExOauth2Provider.AccessTokens.AccessToken do
|> Changeset.change(%{refresh_token: Utils.generate_token()})
|> Changeset.validate_required([:refresh_token])
end

defp put_refresh_token(changeset, _), do: changeset
end
54 changes: 41 additions & 13 deletions lib/ex_oauth2_provider/access_tokens/access_tokens.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,37 @@ defmodule ExOauth2Provider.AccessTokens do

queryable
|> where([a], is_nil(a.revoked_at))
|> where([a], is_nil(a.expires_in) or datetime_add(a.inserted_at, a.expires_in, "second") > ^now)
|> where(
[a],
is_nil(a.expires_in) or datetime_add(a.inserted_at, a.expires_in, "second") > ^now
)
|> order_by([a], desc: a.inserted_at, desc: :id)
|> Config.repo(config).all()
|> Enum.filter(&is_accessible?/1)
|> check_matching_scopes(scopes)
end

defp maybe_build_scopes(_application, scopes, _config) when is_binary(scopes), do: scopes
defp maybe_build_scopes(%{scopes: server_scopes}, nil, config), do: Scopes.parse_default_scope_string(server_scopes, config)
defp maybe_build_scopes(_application, nil, config), do: Scopes.parse_default_scope_string(nil, config)

defp maybe_build_scopes(%{scopes: server_scopes}, nil, config),
do: Scopes.parse_default_scope_string(server_scopes, config)

defp maybe_build_scopes(_application, nil, config),
do: Scopes.parse_default_scope_string(nil, config)

defp check_matching_scopes(tokens, scopes) when is_list(tokens) do
Enum.find(tokens, nil, &check_matching_scopes(&1, scopes))
end

defp check_matching_scopes(nil, _), do: nil

defp check_matching_scopes(token, scopes) do
token_scopes = ScopesUtils.to_list(token.scopes)
token_scopes = ScopesUtils.to_list(token.scopes)
request_scopes = ScopesUtils.to_list(scopes)

case ScopesUtils.equal?(token_scopes, request_scopes) do
true -> token
_ -> nil
_ -> nil
end
end

Expand All @@ -159,7 +168,7 @@ defmodule ExOauth2Provider.AccessTokens do
|> Config.access_token()
|> where([a], a.resource_owner_id == ^resource_owner.id)
|> where([a], is_nil(a.revoked_at))
|> Config.repo(config).all()
|> Config.repo(config).all()
end

@doc """
Expand All @@ -176,7 +185,8 @@ defmodule ExOauth2Provider.AccessTokens do
iex> create_token(resource_owner, %{expires_in: "invalid"}, otp_app: :my_app)
{:error, %Ecto.Changeset{}}
"""
@spec create_token(Schema.t(), map(), keyword()) :: {:ok, AccessToken.t()} | {:error, Changeset.t()}
@spec create_token(Schema.t(), map(), keyword()) ::
{:ok, AccessToken.t()} | {:error, Changeset.t()}
def create_token(resource_owner, attrs \\ %{}, config \\ []) do
config
|> Config.access_token()
Expand All @@ -187,7 +197,7 @@ defmodule ExOauth2Provider.AccessTokens do

defp put_application(access_token, attrs) do
case Map.get(attrs, :application) do
nil -> access_token
nil -> access_token
application -> %{access_token | application: application}
end
end
Expand All @@ -208,7 +218,8 @@ defmodule ExOauth2Provider.AccessTokens do
iex> create_application_token(application, %{scopes: "read write"}, otp_app: :my_app)
{:ok, %OauthAccessToken{}}
"""
@spec create_application_token(Schema.t() | nil, map(), keyword()) :: {:ok, AccessToken.t()} | {:error, Changeset.t()}
@spec create_application_token(Schema.t() | nil, map(), keyword()) ::
{:ok, AccessToken.t()} | {:error, Changeset.t()}
def create_application_token(application, attrs \\ %{}, config \\ []) do
config
|> Config.access_token()
Expand All @@ -230,6 +241,7 @@ defmodule ExOauth2Provider.AccessTokens do
"""
@spec is_accessible?(AccessToken.t() | nil) :: boolean()
def is_accessible?(nil), do: false

def is_accessible?(token) do
!is_expired?(token) and !is_revoked?(token)
end
Expand All @@ -248,7 +260,15 @@ defmodule ExOauth2Provider.AccessTokens do
@spec get_by_previous_refresh_token_for(AccessToken.t(), keyword()) :: AccessToken.t() | nil
def get_by_previous_refresh_token_for(%{previous_refresh_token: nil}, _config), do: nil
def get_by_previous_refresh_token_for(%{previous_refresh_token: ""}, _config), do: nil
def get_by_previous_refresh_token_for(%{previous_refresh_token: previous_refresh_token, resource_owner_id: resource_owner_id, application_id: application_id}, config) do

def get_by_previous_refresh_token_for(
%{
previous_refresh_token: previous_refresh_token,
resource_owner_id: resource_owner_id,
application_id: application_id
},
config
) do
config
|> Config.access_token()
|> scope_belongs_to(:application_id, application_id)
Expand All @@ -261,9 +281,11 @@ defmodule ExOauth2Provider.AccessTokens do
defp scope_belongs_to(queryable, belongs_to_column, nil) do
where(queryable, [x], is_nil(field(x, ^belongs_to_column)))
end

defp scope_belongs_to(queryable, belongs_to_column, %{id: id}) do
scope_belongs_to(queryable, belongs_to_column, id)
end

defp scope_belongs_to(queryable, belongs_to_column, id) do
where(queryable, [x], field(x, ^belongs_to_column) == ^id)
end
Expand All @@ -281,10 +303,16 @@ defmodule ExOauth2Provider.AccessTokens do
iex> revoke_previous_refresh_token(invalid_data, otp_app: :my_app)
{:error, %Ecto.Changeset{}}
"""
@spec revoke_previous_refresh_token(AccessToken.t()) :: {:ok, AccessToken.t()} | {:error, Changeset.t()}
@spec revoke_previous_refresh_token(AccessToken.t()) ::
{:ok, AccessToken.t()} | {:error, Changeset.t()}
def revoke_previous_refresh_token(access_token, config \\ [])
def revoke_previous_refresh_token(%{previous_refresh_token: ""} = access_token, _config), do: {:ok, access_token}
def revoke_previous_refresh_token(%{previous_refresh_token: nil} = access_token, _config), do: {:ok, access_token}

def revoke_previous_refresh_token(%{previous_refresh_token: ""} = access_token, _config),
do: {:ok, access_token}

def revoke_previous_refresh_token(%{previous_refresh_token: nil} = access_token, _config),
do: {:ok, access_token}

def revoke_previous_refresh_token(access_token, config) do
access_token
|> get_by_previous_refresh_token_for(config)
Expand Down
24 changes: 13 additions & 11 deletions lib/ex_oauth2_provider/applications/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ defmodule ExOauth2Provider.Applications.Application do
@doc false
def attrs() do
[
{:name, :string, [], null: false},
{:uid, :string, [], null: false},
{:secret, :string, [default: ""], null: false},
{:redirect_uri, :string, [], null: false},
{:scopes, :string, [default: ""], null: false},
{:name, :string, [], null: false},
{:uid, :string, [], null: false},
{:secret, :string, [default: ""], null: false},
{:redirect_uri, :string, [], null: false},
{:scopes, :string, [default: ""], null: false}
]
end

Expand All @@ -67,7 +67,7 @@ defmodule ExOauth2Provider.Applications.Application do
use ExOauth2Provider.Schema, unquote(config)

# For Phoenix integrations
if Code.ensure_loaded?(Phoenix.Param), do: @derive {Phoenix.Param, key: :uid}
if Code.ensure_loaded?(Phoenix.Param), do: @derive({Phoenix.Param, key: :uid})

import unquote(__MODULE__), only: [application_fields: 0]
end
Expand Down Expand Up @@ -98,13 +98,13 @@ defmodule ExOauth2Provider.Applications.Application do
defp validate_secret_not_nil(changeset) do
case Changeset.get_field(changeset, :secret) do
nil -> Changeset.add_error(changeset, :secret, "can't be blank")
_ -> changeset
_ -> changeset
end
end

defp maybe_new_application_changeset(application, params, config) do
case Ecto.get_meta(application, :state) do
:built -> new_application_changeset(application, params, config)
:built -> new_application_changeset(application, params, config)
:loaded -> application
end
end
Expand All @@ -127,18 +127,20 @@ defmodule ExOauth2Provider.Applications.Application do
url
|> RedirectURI.validate(config)
|> case do
{:error, error} -> Changeset.add_error(changeset, :redirect_uri, error)
{:ok, _} -> changeset
end
{:error, error} -> Changeset.add_error(changeset, :redirect_uri, error)
{:ok, _} -> changeset
end
end)
end

defp put_uid(%{changes: %{uid: _}} = changeset), do: changeset

defp put_uid(%{} = changeset) do
Changeset.change(changeset, %{uid: Utils.generate_token()})
end

defp put_secret(%{changes: %{secret: _}} = changeset), do: changeset

defp put_secret(%{} = changeset) do
Changeset.change(changeset, %{secret: Utils.generate_token()})
end
Expand Down
Loading

0 comments on commit bb0ec90

Please sign in to comment.