-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3079af6
commit de64aeb
Showing
12 changed files
with
478 additions
and
6 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
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
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
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
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
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
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,104 @@ | ||
defmodule ArrowWeb.TryApiTokenAuth.Keycloak do | ||
@moduledoc """ | ||
Signs in an API client via Keycloak. | ||
""" | ||
|
||
require Logger | ||
|
||
def sign_in(conn, auth_token) do | ||
with {:ok, user_id} <- lookup_user_id(auth_token.username), | ||
{:ok, roles} <- lookup_user_roles(user_id) do | ||
conn | ||
|> Guardian.Plug.sign_in( | ||
ArrowWeb.AuthManager, | ||
auth_token.username, | ||
%{roles: roles}, | ||
ttl: {0, :second} | ||
) | ||
else | ||
other -> | ||
Logger.warn( | ||
"unexpected response when logging #{auth_token.username} in via Keycloak API: #{inspect(other)}" | ||
) | ||
|
||
conn | ||
end | ||
end | ||
|
||
defp lookup_user_id(email) do | ||
case keycloak_api("/users", %{ | ||
max: 1, | ||
email: String.downcase(email), | ||
exact: true, | ||
briefRepresentation: true | ||
}) do | ||
{:ok, [%{"id" => user_id}]} -> | ||
{:ok, user_id} | ||
|
||
{:ok, _} -> | ||
{:error, :missing_user} | ||
|
||
e -> | ||
e | ||
end | ||
end | ||
|
||
defp lookup_user_roles(user_id) do | ||
client_uuid = Application.get_env(:arrow, :keycloak_client_uuid) | ||
url = "/users/#{user_id}/role-mappings/clients/#{client_uuid}/composite" | ||
|
||
case keycloak_api(url) do | ||
{:ok, response} -> | ||
roles = for r <- response, do: r["name"] | ||
{:ok, roles} | ||
|
||
e -> | ||
e | ||
end | ||
end | ||
|
||
defp keycloak_api(url, params \\ %{}) do | ||
base_url = | ||
String.replace_suffix( | ||
Application.get_env(:arrow, :keycloak_api_base), | ||
"/", | ||
"" | ||
) | ||
|
||
{_, base_opts} = Application.get_env(:ueberauth, Ueberauth)[:providers][:keycloak] | ||
runtime_opts = Application.get_env(:ueberauth_oidcc, :providers)[:keycloak] | ||
|
||
opts = | ||
base_opts | ||
|> Keyword.merge(runtime_opts) | ||
|> Map.new() | ||
|
||
http_module = Application.get_env(:arrow, :http_client) | ||
oidcc_module = Map.get(opts, :module, Oidcc) | ||
|
||
with {:ok, token} <- | ||
oidcc_module.client_credentials_token( | ||
opts.issuer, | ||
opts.client_id, | ||
opts.client_secret, | ||
%{} | ||
), | ||
headers = [{"authorization", "Bearer #{token.access.token}"}], | ||
{:ok, %{status_code: 200} = response} <- | ||
http_module.get("#{base_url}#{url}", headers, | ||
params: params, | ||
hackney: [ | ||
ssl_options: | ||
Keyword.merge( | ||
:httpc.ssl_verify_host_options(false), | ||
versions: [:"tlsv1.3", :"tlsv1.2"] | ||
) | ||
] | ||
) do | ||
Jason.decode(response.body) | ||
else | ||
{:ok, %{status_code: _} = response} -> {:error, response} | ||
e -> e | ||
end | ||
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
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
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 |
---|---|---|
|
@@ -22,10 +22,71 @@ defmodule ArrowWeb.Controllers.AuthControllerTest do | |
response = html_response(conn, 302) | ||
|
||
assert response =~ Routes.disruption_path(conn, :index) | ||
|
||
assert Enum.sort(Guardian.Plug.current_claims(conn)["roles"]) == ["admin", "read-only"] | ||
assert Guardian.Plug.current_resource(conn) == "[email protected]" | ||
end | ||
|
||
test "redirects on success (keycloak)", %{conn: conn} do | ||
current_time = System.system_time(:second) | ||
|
||
auth = %Ueberauth.Auth{ | ||
uid: "[email protected]", | ||
provider: :keycloak, | ||
credentials: %Ueberauth.Auth.Credentials{ | ||
expires_at: current_time + 1_000, | ||
other: %{id_token: "id_token"} | ||
}, | ||
extra: %{ | ||
raw_info: %{ | ||
userinfo: %{ | ||
"roles" => ["admin"] | ||
} | ||
} | ||
} | ||
} | ||
|
||
conn = | ||
conn | ||
|> assign(:ueberauth_auth, auth) | ||
|> get(Routes.auth_path(conn, :callback, "keycloak")) | ||
|
||
response = html_response(conn, 302) | ||
|
||
assert response =~ Routes.disruption_path(conn, :index) | ||
assert Guardian.Plug.current_claims(conn)["roles"] == ["admin"] | ||
assert Guardian.Plug.current_resource(conn) == "[email protected]" | ||
end | ||
|
||
test "handles missing roles (keycloak)", %{conn: conn} do | ||
current_time = System.system_time(:second) | ||
|
||
auth = %Ueberauth.Auth{ | ||
uid: "[email protected]", | ||
provider: :keycloak, | ||
credentials: %Ueberauth.Auth.Credentials{ | ||
expires_at: current_time + 1_000, | ||
other: %{id_token: "id_token"} | ||
}, | ||
extra: %{ | ||
raw_info: %{ | ||
userinfo: %{} | ||
} | ||
} | ||
} | ||
|
||
conn = | ||
conn | ||
|> assign(:ueberauth_auth, auth) | ||
|> get(Routes.auth_path(conn, :callback, "keycloak")) | ||
|
||
response = html_response(conn, 302) | ||
|
||
assert response =~ Routes.disruption_path(conn, :index) | ||
assert Guardian.Plug.current_claims(conn)["roles"] == [] | ||
assert Guardian.Plug.current_resource(conn) == "[email protected]" | ||
end | ||
|
||
test "handles generic failure", %{conn: conn} do | ||
conn = | ||
conn | ||
|
Oops, something went wrong.