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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews