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

Context to manage clients

Link to this section Summary

Functions

Get a list of all oauth2 clients.

Delete a client.

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

Insert a new client.

Update a client.

Link to this section Functions

Link to this function

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

View Source
@spec all(%{required(atom()) => any()}, [MyApp.CharonOauth2.Client.resolvable()]) :: [
  MyApp.CharonOauth2.Client.t()
]

Get a list of all oauth2 clients.

doctests

Doctests

iex> client = insert_test_client()
iex> [^client] = Clients.all()

# can be filtered
iex> client = insert_test_client()
iex> [^client] = Clients.all(%{owner_id: client.owner_id})
iex> [^client] = Clients.all(%{grant_types: "authorization_code"})
iex> [] = Clients.all(%{owner_id: client.owner_id + 1})
@spec delete(MyApp.CharonOauth2.Client.t() | keyword() | map()) ::
  {:ok, MyApp.CharonOauth2.Client.t()} | {:error, :not_found}

Delete a client.

examples-doctests

Examples / doctests

# client must exist
iex> {:error, :not_found} = Clients.delete(id: Ecto.UUID.generate())

# succesfully deletes a client
iex> client = insert_test_client()
iex> {:ok, _} = Clients.delete([id: client.id])
iex> {:error, :not_found} = Clients.delete([id: client.id])
Link to this function

get_by(clauses, preloads \\ [])

View Source

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

doctests

Doctests

iex> client = insert_test_client()
iex> %Client{} = Clients.get_by(id: client.id)
iex> nil = Clients.get_by(id: Ecto.UUID.generate())

# preloads things
iex> client = insert_test_client()
iex> auth = insert_test_authorization(client_id: client.id)
iex> insert_test_grant(authorization_id: auth.id)
iex> %{owner: %{id: _}, authorizations: [_]} = Clients.get_by([id: client.id], Client.supported_preloads)
@spec insert(map()) :: {:ok, MyApp.CharonOauth2.Client.t()} | {:error, Changeset.t()}

Insert a new client.

examples-doctests

Examples / doctests

# succesfully creates a client with a secret
iex> user = insert_test_user()
iex> {:ok, client} = client_params(owner_id: user.id) |> Clients.insert()
iex> %{secret: <<_::binary>>} = client

# owner must exist
iex> client_params(owner_id: -1) |> Clients.insert() |> errors_on()
%{owner: ["does not exist"]}

iex> Clients.insert(%{}) |> errors_on()
%{grant_types: ["can't be blank"], name: ["can't be blank"], owner_id: ["can't be blank"], redirect_uris: ["can't be blank"], scope: ["can't be blank"]}
@spec update(MyApp.CharonOauth2.Client.t() | keyword() | map(), map()) ::
  {:ok, MyApp.CharonOauth2.Client.t()}
  | {:error, Changeset.t()}
  | {:error, :not_found}

Update a client.

examples-doctests

Examples / doctests

iex> client = insert_test_client()
iex> {:ok, updated} = Clients.update(client, %{secret: "new!"})
iex> false = updated.secret == client.secret

# secret is randomly generated on update
iex> client = insert_test_client()
iex> {:ok, updated} = Clients.update([id: client.id], %{secret: "new!"})
iex> false = updated.secret == client.secret
iex> false = updated.secret == "new!"

# scopes must be subset of configured scopes
iex> client = insert_test_client()
iex> Clients.update([id: client.id], %{scope: ~w(cry)}) |> errors_on()
%{scope: ["must be subset of party, read, write"]}

# underlying authrorization scopes are reduced to client's reduced scopes
iex> client = insert_test_client(scope: ~w(read write))
iex> authorization = insert_test_authorization(client_id: client.id, scope: ~w(read write))
iex> {:ok, _} = Clients.update([id: client.id], %{scope: ~w(read)})
iex> %{scope: ~w(read)} = Authorizations.get_by(id: authorization.id)

# id and owner id can't be updated
iex> %{id: id, owner_id: owner_id} = insert_test_client()
iex> {:ok, %{id: ^id, owner_id: ^owner_id}} = Clients.update([id: id], %{id: Ecto.UUID.generate(), owner_id: -1})