-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(auth/api): add implicit oauth flow and generate client for swagger
- Loading branch information
Showing
5 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
apps/ex_fleet_yards/priv/repo/migrations/20230730111440_no_user_for_oauth_client.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
defmodule ExFleetYards.Repo.Migrations.NoUserForOauthClient do | ||
use Ecto.Migration | ||
|
||
def up do | ||
execute "ALTER TABLE oauth_user_clients DROP CONSTRAINT oauth_user_clients_user_id_fkey" | ||
|
||
alter table(:oauth_user_clients) do | ||
modify :user_id, references(:users, type: :uuid, on_delete: :delete_all), null: true | ||
end | ||
end | ||
|
||
def down do | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
apps/ex_fleet_yards_auth/lib/ex_fleet_yards_auth/release/oauth_client.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
defmodule ExFleetYardsAuth.Release.OauthClient do | ||
@moduledoc """ | ||
Helper to create static Oauth Clients | ||
""" | ||
|
||
alias ExFleetYards.Repo | ||
alias ExFleetYardsAuth.Endpoint | ||
use ExFleetYardsAuth, :verified_routes | ||
|
||
# Swagger ui | ||
def swagger_ui_uuid, do: "52d96be7-8e29-412b-a9f2-05a532ff9708" | ||
|
||
def swagger_ui_callback_uri do | ||
Endpoint.url() <> ~p"/api/oauth2-redirect.html" | ||
end | ||
|
||
def get_or_create_swagger_client_id() do | ||
id = swagger_ui_uuid() | ||
import Ecto.Query | ||
|
||
Repo.exists?(from(c in Boruta.Ecto.Client, where: c.id == ^id)) | ||
|> case do | ||
true -> | ||
id | ||
|
||
false -> | ||
create_client(id, "Fleetyards Auth API", [swagger_ui_callback_uri()]) | ||
end | ||
end | ||
|
||
def create_client(id \\ nil, name, redirect_uris) do | ||
id = if(is_nil(id), do: SecureRandom.uuid(), else: id) | ||
|
||
args = | ||
Map.merge(Repo.Account.OauthClient.default_args(), %{ | ||
id: id, | ||
name: name, | ||
authorize_scopes: true, | ||
redirect_uris: redirect_uris | ||
}) | ||
|
||
client_changeset = Boruta.Ecto.Client.create_changeset(%Boruta.Ecto.Client{}, args) | ||
|
||
{:ok, client} = Repo.Account.OauthClient.create(nil, client_changeset) | ||
client.client.id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters