Skip to content

Commit

Permalink
Merge pull request #422 from smpallen99/elixir.1.14
Browse files Browse the repository at this point in the history
Elixir.1.14
  • Loading branch information
Sinc63 authored Sep 13, 2023
2 parents e398b54 + f9c0748 commit a0ace99
Show file tree
Hide file tree
Showing 36 changed files with 123 additions and 278 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
erlang 25.3.2.5
elixir 1.14.5-otp-25
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is responsible for configuring your application
# and its dependencies with the aid of the Mix.Config module.
use Mix.Config
import Config

config :coherence, Coherence.Mailer,
adapter: Swoosh.Adapters.Sendgrid,
Expand Down
2 changes: 1 addition & 1 deletion config/dev.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use Mix.Config
import Config
2 changes: 1 addition & 1 deletion config/prod.exs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
use Mix.Config
import Config
2 changes: 1 addition & 1 deletion config/test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use Mix.Config
import Config

# config :coherence, ecto_repos: [TestCoherence.Repo]

Expand Down
6 changes: 3 additions & 3 deletions lib/coherence.ex
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ defmodule Coherence do
Other sessions belonging to the same user won't be updated.
Requires access to the `conn`, which means it can't be called outside of the context of a conn.
To update all session belonging to the user see `t:update_user_login/1`.
To update all session belonging to the user see `update_user_login/1`.
"""
def update_user_login(conn, user) do
apply(
Expand All @@ -250,11 +250,11 @@ defmodule Coherence do
end

@doc """
Updates the user login data in the all sessions belonging to the user.
Updates the user login data in all the sessions belonging to the user.
All sessions belonging to the same user will be updated.
Doesn't need access to the `conn`, which means it can be called anywhere.
To update only the current session see `t:update_user_login/2`
To update only the current session see `update_user_login/2`
"""
def update_user_logins(user) do
# Handle a user's DBStore
Expand Down
6 changes: 1 addition & 5 deletions lib/coherence/controllers/registration_controller_base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ defmodule Coherence.RegistrationControllerBase do
@type conn :: Plug.Conn.t()
@type params :: map()

@dialyzer [
{:nowarn_function, update: 2}
]

@schemas unquote(opts)[:schemas] || raise("Schemas option required")

def schema(which), do: Coherence.Schemas.schema(which)
Expand Down Expand Up @@ -163,7 +159,7 @@ defmodule Coherence.RegistrationControllerBase do
@doc """
Delete a registration.
"""
@spec update(conn, params) :: conn
@spec delete(conn, params) :: conn
def delete(conn, params) do
user = Coherence.current_user(conn)
conn = Controller.logout_user(conn)
Expand Down
4 changes: 2 additions & 2 deletions lib/coherence/controllers/session_controller_base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ defmodule Coherence.SessionControllerBase do
@spec delete(conn, params) :: conn
def delete(conn, params) do
conn
|> logout_user
|> logout_user()
|> respond_with(:session_delete_success, %{params: params})
end

Expand Down Expand Up @@ -351,7 +351,7 @@ defmodule Coherence.SessionControllerBase do

{:error, :invalid_token} ->
# this is a case of potential fraud
Logger.warn("Invalid token. Potential Fraud.")
Logger.warning("Invalid token #{token} for id: #{id}. Potential Fraud.")

conn
|> delete_req_header(opts[:login_key])
Expand Down
13 changes: 2 additions & 11 deletions lib/coherence/plugs/authentication/basic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,6 @@ defmodule Coherence.Authentication.Basic do
This module is derived from https://github.com/bitgamma/plug_auth which is derived from https://github.com/lexmag/blaguth
"""
@dialyzer [
{:nowarn_function, call: 2},
{:nowarn_function, get_auth_header: 1},
{:nowarn_function, verify_creds: 2},
{:nowarn_function, assert_creds: 4},
{:nowarn_function, init: 1},
{:nowarn_function, halt_with_login: 3}
]

@type t :: Ecto.Schema.t() | map()
@type conn :: Plug.Conn.t()

Expand Down Expand Up @@ -48,7 +39,7 @@ defmodule Coherence.Authentication.Basic do
create_login(email, password, user_data, opts)
end

# @spec init(Keyword.t) :: map
@spec init(Keyword.t) :: map
def init(opts) do
%{
realm: Keyword.get(opts, :realm, Messages.backend().restricted_area()),
Expand All @@ -58,7 +49,7 @@ defmodule Coherence.Authentication.Basic do
}
end

# @spec call(conn, Keyword.t) :: none
@spec call(conn, Keyword.t) :: conn
def call(conn, opts) do
conn
|> get_auth_header
Expand Down
12 changes: 6 additions & 6 deletions lib/coherence/plugs/authentication/credential_store/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ defmodule Coherence.CredentialStore.Server do
GenServer.start_link(__MODULE__, args, name: @name)
end

@spec update_user_logins(T.user_data()) :: no_return
@spec update_user_logins(T.user_data()) :: :ok
def update_user_logins(%{id: _} = user_data) do
GenServer.cast(@name, {:update_user_logins, user_data})
end
Expand All @@ -38,27 +38,27 @@ defmodule Coherence.CredentialStore.Server do
# There is no need to update anything and we just return an empty list
def update_user_logins(_), do: []

@spec delete_user_logins(T.user_data()) :: no_return
@spec delete_user_logins(T.user_data()) :: :ok
def delete_user_logins(%{id: _} = user_data) do
GenServer.cast(@name, {:delete_user_logins, user_data})
end

@spec get_user_data(T.credentials()) :: T.user_data()
@spec get_user_data(T.credentials()) :: T.user_data() | nil
def get_user_data(credentials) do
GenServer.call(@name, {:get_user_data, credentials})
end

@spec put_credentials(T.credentials(), T.user_data()) :: no_return
@spec put_credentials(T.credentials(), T.user_data()) :: :ok
def put_credentials(credentials, user_data) do
GenServer.cast(@name, {:put_credentials, credentials, user_data})
end

@spec delete_credentials(T.credentials()) :: no_return
@spec delete_credentials(T.credentials()) :: :ok
def delete_credentials(credentials) do
GenServer.cast(@name, {:delete_credentials, credentials})
end

@spec stop() :: no_return
@spec stop() :: :ok
def stop do
GenServer.cast(@name, :stop)
end
Expand Down
12 changes: 6 additions & 6 deletions lib/coherence/plugs/authentication/credential_store/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule Coherence.CredentialStore.Session do
@moduledoc """
Stores current credential information.
Uses an Server to save logged in credentials.
Uses a Server to save logged in credentials.
Note: If you restart the phoenix server, this information
is lost, requiring the user to log in again.
Expand Down Expand Up @@ -43,8 +43,8 @@ defmodule Coherence.CredentialStore.Session do
@doc """
Gets the user data for the given credentials
"""

@spec get_user_data({T.credentials(), nil | struct, integer | nil}) :: any
@dialyzer [{:no_match, get_user_data: 1}]
@spec get_user_data({T.credentials(), nil | struct, integer | nil}) :: T.user_data() | nil
def get_user_data({credentials, nil, _}) do
get_data(credentials)
end
Expand Down Expand Up @@ -72,7 +72,7 @@ defmodule Coherence.CredentialStore.Session do
@doc """
Puts the `user_data` for the given `credentials`.
"""
@spec put_credentials({T.credentials(), any, atom}) :: any
@spec put_credentials({T.credentials(), any, atom}) :: :ok
def put_credentials({credentials, user_data, id_key}) do
Server.put_credentials(credentials, user_data)
DbStore.put_credentials(user_data, credentials, id_key)
Expand All @@ -83,7 +83,7 @@ defmodule Coherence.CredentialStore.Session do
Returns the current value of `credentials`, if `credentials` exists.
"""
@spec delete_credentials(T.credentials()) :: any
@spec delete_credentials(T.credentials()) :: :ok
def delete_credentials(credentials) do
case get_data(credentials) do
nil ->
Expand All @@ -98,7 +98,7 @@ defmodule Coherence.CredentialStore.Session do
@doc """
Deletes the sessions for all logged in users.
"""
@spec delete_user_logins(any) :: no_return
@spec delete_user_logins(any) :: :ok
def delete_user_logins(user_data) do
Server.delete_user_logins(user_data)
DbStore.delete_user_logins(user_data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Coherence.CredentialStore.Types do
@type credentials :: String.t()
@type user_data :: Ecto.Schema.t() | map()
@type user_id :: any
@type user_id :: any()
end
16 changes: 8 additions & 8 deletions lib/coherence/plugs/authentication/db_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,37 @@ defprotocol Coherence.DbStore do
"""
@fallback_to_any true

@type schema :: Ecto.Schema.t() | map()
@type schema :: Ecto.Schema.t() | map() | nil

@doc """
Get authenticated user data.
"""
@spec get_user_data(schema, HashDict.t(), atom) :: schema
@spec get_user_data(schema, String.t(), atom) :: schema
def get_user_data(resource, credentials, id_key)

@doc """
Save authenticated user data in the database.
"""
@spec put_credentials(schema, HashDict.t(), atom) :: schema
@spec put_credentials(schema, String.t(), atom) :: :ok
def put_credentials(resource, credentials, id_key)

@doc """
Delete current user credentials.
"""
@spec delete_credentials(schema, HashDict.t()) :: schema
@spec delete_credentials(schema, String.t()) :: :ok
def delete_credentials(resource, credentials)

@doc """
Delete all logged in users.
"""
@spec delete_user_logins(schema) :: no_return
@spec delete_user_logins(schema) :: :ok
def delete_user_logins(resource)
end

defimpl Coherence.DbStore, for: Any do
require Logger
def get_user_data(_, _, _), do: nil
def put_credentials(_, _, _), do: nil
def delete_credentials(_, _), do: nil
def delete_user_logins(_), do: nil
def put_credentials(_, _, _), do: :ok
def delete_credentials(_, _), do: :ok
def delete_user_logins(_), do: :ok
end
14 changes: 3 additions & 11 deletions lib/coherence/plugs/authentication/ip_address.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ defmodule Coherence.Authentication.IpAddress do
"""

@behaviour Plug
use Bitwise

import Bitwise
import Plug.Conn
import Coherence.Authentication.Utils

Expand All @@ -49,14 +48,7 @@ defmodule Coherence.Authentication.IpAddress do

require Logger

@dialyzer [
{:nowarn_function, call: 2},
# {:nowarn_function, get_auth_header: 1},
# {:nowarn_function, verify_creds: 2},
# {:nowarn_function, assert_creds: 4},
{:nowarn_function, init: 1}
# {:nowarn_function, halt_with_login: 3},
]
@dialyzer [ {:nowarn_function, call: 2} ]

@type t :: Ecto.Schema.t() | map()
@type conn :: Plug.Conn.t()
Expand All @@ -77,7 +69,7 @@ defmodule Coherence.Authentication.IpAddress do
store.delete_credentials(ip)
end

@spec init(keyword()) :: [tuple]
@spec init(keyword()) :: map()
def init(opts) do
%{
allow: Keyword.get(opts, :allow, []),
Expand Down
13 changes: 2 additions & 11 deletions lib/coherence/plugs/authentication/session.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,7 @@ defmodule Coherence.Authentication.Session do
@type t :: Ecto.Schema.t() | map()
@type conn :: Plug.Conn.t()

@session_key Application.get_env(:coherence, :session_key, "session_auth")

@dialyzer [
{:nowarn_function, call: 2},
{:nowarn_function, get_session_data: 1},
{:nowarn_function, verify_rememberable: 2},
{:nowarn_function, verify_auth_key: 3},
{:nowarn_function, assert_login: 3},
{:nowarn_function, init: 1}
]
@session_key Application.compile_env(:coherence, :session_key, "session_auth")

@doc """
Create a login for a user. `user_data` can be any term but must not be `nil`.
Expand Down Expand Up @@ -133,7 +124,7 @@ defmodule Coherence.Authentication.Session do
end

@doc false
@spec init(keyword()) :: [tuple]
@spec init(keyword()) :: map()
def init(opts) do
login =
case opts[:login] do
Expand Down
28 changes: 9 additions & 19 deletions lib/coherence/plugs/authentication/token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,18 @@ defmodule Coherence.Authentication.Token do

@behaviour Plug

@dialyzer [
{:nowarn_function, call: 2},
{:nowarn_function, get_token_from_header: 2},
{:nowarn_function, verify_creds: 2},
{:nowarn_function, assert_creds: 2},
# {:nowarn_function, assert_login: 3},
{:nowarn_function, init: 1}
]

@doc """
Add the credentials for a `token`. `user_data` can be any term but must not be `nil`.
"""
@spec add_credentials(String.t(), t, module) :: t
@spec add_credentials(String.t(), t, module) :: :ok
def add_credentials(token, user_data, store \\ Coherence.CredentialStore.Server) do
store.put_credentials(token, user_data)
end

@doc """
Remove the credentials for a `token`.
"""
@spec remove_credentials(String.t(), module) :: t
@spec remove_credentials(String.t(), module) :: :ok
def remove_credentials(token, store \\ Coherence.CredentialStore.Server) do
store.delete_credentials(token)
end
Expand All @@ -75,7 +66,7 @@ defmodule Coherence.Authentication.Token do
|> Base.url_encode64()
end

@spec init(keyword()) :: [tuple]
@spec init(keyword()) :: map()
def init(opts) do
param = Keyword.get(opts, :param)

Expand Down Expand Up @@ -103,32 +94,31 @@ defmodule Coherence.Authentication.Token do
when is_atom(module) and is_atom(fun) and is_list(args),
do: source

@spec get_token_from_params(conn, map()) :: {conn, map()}
@spec get_token_from_params(conn, atom() | binary()) :: {conn, any()}
def get_token_from_params(conn, param),
do: {conn, conn.params[param]}

@spec get_token_from_header(conn, map()) :: {conn, map() | String.t}
@spec get_token_from_header(conn, binary()) :: {conn, String.t}
def get_token_from_header(conn, param),
do: {conn, get_first_req_header(conn, param)}

@spec get_token_from_session(conn, map()) :: {conn, String.t()}
@spec get_token_from_session(conn, atom() | binary()) :: {conn, nil | String.t()}
def get_token_from_session(conn, param),
do: {conn, get_session(conn, param)}

@spec get_token_from_params_session(conn, map()) :: {conn, nil | String.t()}
@spec get_token_from_params_session(conn, atom() | binary()) :: {conn, nil | String.t()}
def get_token_from_params_session(conn, param) do
conn
|> get_token_from_params(param)
|> check_token_from_session(param)
|> save_token_in_session(param)
end

@spec get_token_from_session({conn, nil | String.t()}, map()) ::
String.t() | {conn, String.t()}
@spec check_token_from_session({conn, nil | String.t()}, atom() | binary()) :: {conn, nil | String.t()}
def check_token_from_session({conn, nil}, param), do: get_token_from_session(conn, param)
def check_token_from_session({conn, creds}, _param), do: {conn, creds}

@spec save_token_in_session({conn, nil | String.t()}, map()) :: {conn, nil | String.t()}
@spec save_token_in_session({conn, nil | String.t()}, atom() | binary()) :: {conn, nil | String.t()}
def save_token_in_session({conn, nil}, _), do: {conn, nil}

def save_token_in_session({conn, creds}, param) do
Expand Down
Loading

0 comments on commit a0ace99

Please sign in to comment.