这是indexloc提供的服务,不要输入任何密码
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions lib/authentication/authentication.ex
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
defmodule Authority.Authentication do
@type id :: any
@type credential :: any
@type credential :: {id, any} | any
@type opts :: Keyword.t()

@callback authenticate(credential) :: {:ok, any} | {:error, term}
@callback authenticate(id, credential) :: {:ok, any} | {:error, term}
@callback authenticate(credential, opts) :: {:ok, any} | {:error, term}

@doc false
def authenticate(%{store: store}, credential) do
with {:ok, identity} <- store.identify(credential),
:ok <- store.validate(credential, identity) do
def authenticate(%{store: store}, {identifier, credential}, opts) do
with {:ok, identity} <- store.identify(identifier, opts),
:ok <- store.validate(credential, identity, opts) do
{:ok, identity}
end
end

@doc false
def authenticate(%{store: store}, identifier, credential) do
with {:ok, identity} <- store.identify(identifier),
:ok <- store.validate(credential, identity) do
def authenticate(%{store: store}, credential, opts) do
with {:ok, identity} <- store.identify(credential, opts),
:ok <- store.validate(credential, identity, opts) do
{:ok, identity}
end
end

defmacro __using__(config) do
quote do
@behaviour Authority.Authentication
@config Enum.into(unquote(config), %{})
@__authentication__ Enum.into(unquote(config), %{})

def authenticate(credential) do
Authority.Authentication.authenticate(@config, credential)
def authenticate(credential, opts \\ []) do
Authority.Authentication.authenticate(@__authentication__, credential, opts)
end

def authenticate(identifier, credential) do
Authority.Authentication.authenticate(@config, identifier, credential)
def __authentication__ do
@__authentication__
end

defoverridable Authority.Authentication
Expand Down
6 changes: 4 additions & 2 deletions lib/authentication/store.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
defmodule Authority.Authentication.Store do
@type opts :: Keyword.t()
@type id :: any
@type identity :: any
@type credential :: any

@callback identify(any) :: {:ok, identity} | {:error, term}
@callback validate(credential, identity) :: :ok | {:error, term}
@callback identify(id, opts) :: {:ok, identity} | {:error, term}
@callback validate(credential, identity, opts) :: :ok | {:error, term}
end
83 changes: 0 additions & 83 deletions lib/authentication/stores/ecto_store.ex

This file was deleted.

51 changes: 51 additions & 0 deletions lib/exchange/exchange.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Authority.Exchange do
# MyModule.exchange("user@email.com", for: MyApp.Token, context: :recovery)
# MyModule.Exchange.exchange(OAuthCode{provider: :facebook, code: "..."}, for: MyApp.Token, context: :identity)
# MyModule.Exchange.exchange({"user@email.com", "password"}, for: MyApp.Token, context: :identity)

alias Authority.Authentication

@type opts :: Keyword.t()

@callback exchange(Authentication.credential()) ::
{:ok, Authentication.credential()}
| {:error, term}

@callback exchange(Authentication.credential(), opts) ::
{:ok, Authentication.credential()}
| {:error, term}

@doc false
def exchange(%{store: store, authentication: authentication}, credential, opts) do
with {:ok, identity} <- authentication.authenticate(credential, opts) do
store.exchange(identity, opts)
end
end

@doc false
def exchange(
%{store: store, authentication: authentication},
identifier,
credential,
opts
) do
with {:ok, identity} <- authentication.authenticate(identifier, credential, opts) do
store.exchange(identity, opts)
end
end

defmacro __using__(config) do
quote do
@behaviour Authority.Exchange
@__exchange__ Enum.into(unquote(config), %{})

def exchange(credential, opts \\ []) do
Authority.Exchange.exchange(@__exchange__, credential, opts)
end

def __exchange__ do
@__exchange__
end
end
end
end
9 changes: 9 additions & 0 deletions lib/exchange/store.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
defmodule Authority.Exchange.Store do
alias Authority.Authentication

@type opts :: Keyword.t()

@callback exchange(Authentication.identity(), opts) ::
{:ok, Authentication.credential()}
| {:error, term}
end
Loading