View Source MyApp.CharonOauth2.Authorizations (CharonOauth2 v0.0.5)

Context to manage authorizations

Link to this section Summary

Functions

Get a list of all oauth2 authorizations.

Delete an authorization.

Get a single authorization by one or more clauses, optionally with preloads. Returns nil if Authorization cannot be found.

Insert a new authorization

Update an authorization.

Link to this section Functions

Link to this function

all(filters \\ %{}, preloads \\ [])

View Source

Get a list of all oauth2 authorizations.

doctests

Doctests

iex> insert_test_authorization()
iex> [%Authorization{}] = Authorizations.all()

# can be filtered
iex> authorization = insert_test_authorization()
iex> [%Authorization{}] = Authorizations.all(%{resource_owner_id: authorization.resource_owner_id})
iex> [%Authorization{}] = Authorizations.all(%{scope: authorization.scope |> List.first()})
iex> [] = Authorizations.all(%{resource_owner_id: authorization.resource_owner_id + 1})
@spec delete(MyApp.CharonOauth2.Authorization.t() | keyword() | map()) ::
  {:ok, MyApp.CharonOauth2.Authorization.t()} | {:error, :not_found}

Delete an authorization.

examples-doctests

Examples / doctests

# authorization must exist
iex> {:error, :not_found} = Authorizations.delete(id: -1)

# succesfully deletes an authorization
iex> authorization = insert_test_authorization()
iex> {:ok, _} = Authorizations.delete([id: authorization.id])
iex> {:error, :not_found} = Authorizations.delete([id: authorization.id])
Link to this function

get_by(clauses, preloads \\ [])

View Source

Get a single authorization by one or more clauses, optionally with preloads. Returns nil if Authorization cannot be found.

doctests

Doctests

iex> authorization = insert_test_authorization()
iex> %Authorization{} = Authorizations.get_by(id: authorization.id)
iex> nil = Authorizations.get_by(id: authorization.id + 1)

# preloads things
iex> authorization = insert_test_authorization()
iex> %{resource_owner: %{id: _}, client: %{id: _}} = Authorizations.get_by([id: authorization.id], Authorization.supported_preloads)
@spec insert(map()) ::
  {:ok, MyApp.CharonOauth2.Authorization.t()} | {:error, Changeset.t()}

Insert a new authorization

examples-doctests

Examples / doctests

# succesfully creates an authorization
iex> {:ok, _} = authorization_params() |> Authorizations.insert()

# a user can authorize a client only once
iex> {:ok, authorization} = authorization_params() |> Authorizations.insert()
iex> authorization_params(client_id: authorization.client_id, resource_owner_id: authorization.resource_owner_id) |> Authorizations.insert() |> errors_on()
%{client_id: ["user already authorized this client"]}

# owner and client must exist
iex> authorization_params(resource_owner_id: -1) |> Authorizations.insert() |> errors_on()
%{resource_owner: ["does not exist"]}
iex> authorization_params(client_id: Ecto.UUID.generate()) |> Authorizations.insert() |> errors_on()
%{client: ["does not exist"]}

iex> Authorizations.insert(%{}) |> errors_on()
%{scope: ["can't be blank"], client_id: ["can't be blank"], resource_owner_id: ["can't be blank"]}
Link to this function

update(authorization, params)

View Source
@spec update(MyApp.CharonOauth2.Authorization.t() | keyword() | map(), map()) ::
  {:ok, MyApp.CharonOauth2.Authorization.t()}
  | {:error, Changeset.t()}
  | {:error, :not_found}

Update an authorization.

examples-doctests

Examples / doctests

# scopes must be subset of configured scopes and of client scopes
iex> insert_test_authorization() |> Authorizations.update(%{scope: ~w(cry)}) |> errors_on()
%{scope: ["must be subset of party, read, write"]}
iex> insert_test_authorization() |> Authorizations.update(%{scope: ~w(write write)}) |> errors_on()
%{scope: ["client not allowed to access scope(s): write"]}

# client and resource owner can't be updated
iex> %{id: id, client_id: client_id, resource_owner_id: owner_id} = insert_test_authorization()
iex> {:ok, %{client_id: ^client_id, resource_owner_id: ^owner_id}} = Authorizations.update([id: id], %{client_id: -1, owner_id: -1})