Packages
boruta
1.0.0-rc.3
3.0.0-beta.4
3.0.0-beta.3
3.0.0-beta.2
3.0.0-beta.1
2.3.6
2.3.5
2.3.4
2.3.3
2.3.2
2.3.1
2.3.0
2.2.2
2.2.1
2.2.0
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-rc.1
2.0.0-rc.0
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.2.1
0.2.0
0.1.1
0.1.0
0.1.0-rc.5
0.1.0-rc.4
0.1.0-rc.3
0.1.0-rc.2
0.1.0-rc.1
Core of an OAuth/OpenID Connect provider enabling authorization in your applications.
Current section
Files
Jump to
Current section
Files
lib/boruta/oauth/authorization/client.ex
defmodule Boruta.Oauth.Authorization.Client do
@moduledoc """
Client authorization
"""
import Boruta.Config, only: [clients: 0]
alias Boruta.Oauth.Client
alias Boruta.Oauth.Error
@doc """
Authorize the client corresponding to the given params.
## Examples
iex> authorize(id: "id", secret: "secret")
{:ok, %Boruta.Oauth.Client{...}}
"""
@spec authorize(
[id: String.t(), secret: String.t()] |
[id: String.t(), secret: String.t(), grant_type: String.t()] |
[id: String.t(), redirect_uri: String.t(), grant_type: String.t()]
) ::
{:ok, %Client{}}
| {:error,
%Error{
:error => :invalid_client,
:error_description => String.t(),
:format => nil,
:redirect_uri => nil,
:status => :unauthorized
}}
def authorize(id: id, secret: secret) do
case clients().get_by(id: id, secret: secret) do
%Client{} = client ->
{:ok, client}
nil ->
{:error,
%Error{
status: :unauthorized,
error: :invalid_client,
error_description: "Invalid client_id or client_secret."
}
}
end
end
def authorize(id: id, secret: secret, grant_type: grant_type) do
with %Client{supported_grant_types: supported_grant_types} = client <- clients().get_by(id: id, secret: secret),
true <- Enum.member?(supported_grant_types, grant_type) do
{:ok, client}
else
nil ->
{:error,
%Error{
status: :unauthorized,
error: :invalid_client,
error_description: "Invalid client_id or client_secret."
}
}
false ->
{:error,
%Error{
status: :bad_request,
error: :unsupported_grant_type,
error_description: "Client do not support given grant type."
}
}
end
end
def authorize(id: id, redirect_uri: redirect_uri, grant_type: grant_type) do
with %Client{
supported_grant_types: supported_grant_types
} = client <- clients().get_by(id: id, redirect_uri: redirect_uri),
true <- Enum.member?(supported_grant_types, grant_type) do
{:ok, client}
else
nil ->
{:error,
%Error{
status: :unauthorized,
error: :invalid_client,
error_description: "Invalid client_id or redirect_uri."
}
}
false ->
{:error,
%Error{
status: :bad_request,
error: :unsupported_grant_type,
error_description: "Client do not support given grant type."
}
}
end
end
end