Skip to content

Commit

Permalink
Fix formatter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasbernet committed Sep 2, 2021
1 parent 9622781 commit 3b8127d
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 52 deletions.
3 changes: 1 addition & 2 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[
import_deps: [:ecto, :phoenix],
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
subdirectories: ["priv/*/migrations"]
inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
8 changes: 4 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ config :phoenix, :json_library, Jason
# OpenID provider config
config :schlusseli, :openid_connect_providers,
keycloak: [
discovery_document_uri: "http://127.0.0.1:8085/auth/realms/Schlusseli/.well-known/openid-configuration",
introspect_uri: "http://127.0.0.1:8085/auth/realms/Schlusseli/protocol/openid-connect/token/introspect",
discovery_document_uri:
"http://127.0.0.1:8085/auth/realms/Schlusseli/.well-known/openid-configuration",
introspect_uri:
"http://127.0.0.1:8085/auth/realms/Schlusseli/protocol/openid-connect/token/introspect",
client_id: "schlusseli-api",
client_secret: "9b81d2f0-1f5d-4a12-8d3a-3032be945c5a",
redirect_uri: "",
Expand All @@ -38,8 +40,6 @@ config :schlusseli, :openid_connect_providers,
verify_token_audience: true
]



# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{Mix.env()}.exs"
4 changes: 2 additions & 2 deletions lib/schlusseli/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ defmodule Schlusseli.Factory do

def key_factory() do
%{
id: sequence(:id, &(&1), start_at: 1),
id: sequence(:id, & &1, start_at: 1),
serial: :os.system_time(:millisecond),
type: sequence(:type, @key_types)
}
end

def customer_factory do
%{
id: sequence(:id, &(&1), start_at: 1),
id: sequence(:id, & &1, start_at: 1),
name: "Hans Lock Smith",
email: sequence(:email, &"email-#{&1}@example.com")
}
Expand Down
16 changes: 8 additions & 8 deletions lib/schlusseli_web/plug/keycloak_introspect.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Schlusseli.Plug.KeycloakIntorspect do
@moduledoc """
@moduledoc """
Plug to verify token via keycloak's introspection endpoint.
Expand All @@ -19,9 +19,9 @@ defmodule Schlusseli.Plug.KeycloakIntorspect do
@spec call(Plug.Conn.t(), keyword()) :: Plug.Conn.t()
def call(conn, _) do
conn
|> get_req_header("authorization")
|> fetch_token()
|> verify_token(conn)
|> get_req_header("authorization")
|> fetch_token()
|> verify_token(conn)
end

def fetch_token([token]) when is_binary(token) do
Expand All @@ -37,7 +37,7 @@ defmodule Schlusseli.Plug.KeycloakIntorspect do
"""
def verify_token(token, conn) do
with {:ok, claims} <- token_introspect(token),
true <- verify_audience(claims, get_provider_conf(:verify_token_audience)) do
true <- verify_audience(claims, get_provider_conf(:verify_token_audience)) do
conn
|> assign(:claims, claims)
else
Expand Down Expand Up @@ -88,9 +88,9 @@ defmodule Schlusseli.Plug.KeycloakIntorspect do

defp auth_error(conn) do
conn
|> put_resp_content_type("application/vnd.api+json")
|> send_resp(401, Poison.encode!(%{error: :not_authorized}))
|> halt()
|> put_resp_content_type("application/vnd.api+json")
|> send_resp(401, Poison.encode!(%{error: :not_authorized}))
|> halt()
end

defp assert_json(%{"error" => reason}), do: {:error, reason}
Expand Down
16 changes: 8 additions & 8 deletions lib/schlusseli_web/plug/openid_connector.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Schlusseli.Plug.OpenidConnector do
@moduledoc """
@moduledoc """
Plug for verifying authorization on a per request basis, verifies that a token is set in the
`Authorization` header.
Expand All @@ -19,9 +19,9 @@ defmodule Schlusseli.Plug.OpenidConnector do
@spec call(Plug.Conn.t(), keyword()) :: Plug.Conn.t()
def call(conn, _) do
conn
|> get_req_header("authorization")
|> fetch_token()
|> verify_token(conn)
|> get_req_header("authorization")
|> fetch_token()
|> verify_token(conn)
end

def fetch_token([token]) when is_binary(token) do
Expand All @@ -37,7 +37,7 @@ defmodule Schlusseli.Plug.OpenidConnector do
"""
def verify_token(token, conn, auth_provider \\ :keycloak) do
with {:ok, claims} <- OpenIDConnect.verify(auth_provider, token),
true <- verify_audience(claims, get_provider_conf(:verify_token_audience)) do
true <- verify_audience(claims, get_provider_conf(:verify_token_audience)) do
conn
|> Absinthe.Plug.put_options(context: %{claims: normalize_claims(claims)})
else
Expand All @@ -63,9 +63,9 @@ defmodule Schlusseli.Plug.OpenidConnector do

defp auth_error(conn) do
conn
|> put_resp_content_type("application/vnd.api+json")
|> send_resp(401, Poison.encode!(%{error: :not_authorized}))
|> halt()
|> put_resp_content_type("application/vnd.api+json")
|> send_resp(401, Poison.encode!(%{error: :not_authorized}))
|> halt()
end

defp normalize_claims(claims) do
Expand Down
2 changes: 0 additions & 2 deletions lib/schlusseli_web/resolver/customer.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
defmodule SchlusseliWeb.Resolvers.Customer do

alias Schlusseli.Factory

def list_customers(_parent, _args, %{context: %{claims: _context}}) do
{:ok, Factory.build_list(10, :customer)}
end

end
2 changes: 0 additions & 2 deletions lib/schlusseli_web/resolver/key.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
defmodule SchlusseliWeb.Resolvers.Key do

alias Schlusseli.Factory

def list_keys(_parent, _args, _resolution) do
{:ok, Factory.build_list(10, :key)}
end

end
13 changes: 5 additions & 8 deletions lib/schlusseli_web/schema.ex
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
defmodule SchlusseliWeb.Schema do
use Absinthe.Schema

import_types SchlusseliWeb.Schema.KeyTypes
import_types SchlusseliWeb.Schema.CustomerTypes
import_types(SchlusseliWeb.Schema.KeyTypes)
import_types(SchlusseliWeb.Schema.CustomerTypes)

alias SchlusseliWeb.Resolvers

query do

@desc "Get all keys"
field :keys, list_of(:key) do
resolve &Resolvers.Key.list_keys/3
resolve(&Resolvers.Key.list_keys/3)
end

@desc "Get all customers"
field :customers, list_of(:customer) do
middleware SchlusseliWeb.Schema.Middleware.Authorize, "view_customers"
resolve &Resolvers.Customer.list_customers/3
middleware(SchlusseliWeb.Schema.Middleware.Authorize, "view_customers")
resolve(&Resolvers.Customer.list_customers/3)
end

end

end
2 changes: 1 addition & 1 deletion lib/schlusseli_web/schema/middleware/authorize.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule SchlusseliWeb.Schema.Middleware.Authorize do

def call(resolution, scope) do
with %{claims: %{scope: current_scopes}} <- resolution.context,
true <- correct_scope?(current_scopes, scope) do
true <- correct_scope?(current_scopes, scope) do
resolution
else
_ ->
Expand Down
4 changes: 0 additions & 4 deletions priv/repo/migrations/.formatter.exs

This file was deleted.

11 changes: 0 additions & 11 deletions priv/repo/seeds.exs

This file was deleted.

0 comments on commit 3b8127d

Please sign in to comment.