tino415

tino415

Hello all,

I’m trying to implement oauth server and I’m not sure how to implement inserting token into database. For simplicity I have this schema:

token
  client_id
  ...

scope
  name (string)
  ...

token_scope
  token_id (uuid)
  scope_id (uuid)

Create token should accept this kind of data:

token
  scopes: "scope1 scope2"

And my question, how to model this kind of insert using changeset? I have came up with some solutions:

  • preprocess params, select all required scopes and pass selected scopes to changeset
  • create everithing using Ecto.Multi (I have little bit of issue with multi mostly because of error reporting)
  • resolve it in changeset using prepare_changes

What do you think is the best solution?

Showing Posts 1 to 3

tino415

tino415 OP

For now I’m triing with prepare_changes method, it looks like this:

  def changeset(struct_or_changeset, client, body_params) do
    struct_or_changeset
    |> cast(body_params, [:scope])
    |> change(expires_in: 3600)
    |> put_now_moved_if_empty(:expires_at, 3600, :second)
    |> put_assoc(:client, client)
    |> create_scopes_list()
    |> validate_required([:expires_in, :expires_at])
    |> prepare_changes(fn changeset ->
      if changed?(changeset, :scope_list) do
        provided_scopes = get_change(changeset, :scope_list)

        existing_scopes =
          changeset.repo.all(
            Schemas.Scope.for_client_query(
              client.id,
              provided_scopes
            )
          )

        existing_scopes_names = Enum.map(existing_scopes, & &1.name)

        {changeset, error?} =
          Enum.reduce(provided_scopes, {changeset, false}, fn {changeset, error?}, name ->
            if name in existing_scopes_names do
              {changeset, error?}
            else
              {
                add_error(changeset, :scope, "invalid scope %{scope}", scope: name),
                true
              }
            end
          end)

        if error? do
          changeset
        else
          token_scopes =
            Enum.map(existing_scopes, fn scope ->
              Schemas.TokenScope.changeset(%Schemas.TokenScope{}, scope)
            end)

          put_assoc(changeset, :token_scopes, token_scopes)
        end
      else
        changeset
      end
    end)
  end

Do you think it is ok to use that prepare_changes this way?

fuelen

fuelen

Sorry for not responding to what you’re asking, but consider boruta | Hex instead of a custom implementation of OAuth server

tino415

tino415 OP

Actually I already checked your boruta, but scope between this and that project is little bit different, I’m exploring options of api only oauth2 servers, second half of motivation is test some libraries and architectures I’m curious about and does not have space to use them in the work. On the other side, I’m planing to use boruta on serious project in near future.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews