-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: authenticate users via API key
- Loading branch information
1 parent
c42a5e9
commit bc4473b
Showing
11 changed files
with
130 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
defmodule ArrowWeb.TryApiTokenAuth.Cognito do | ||
@moduledoc """ | ||
Signs in an API client via Cognito. | ||
""" | ||
|
||
require Logger | ||
|
||
@aws_cognito_target "AWSCognitoIdentityProviderService" | ||
@cognito_groups Application.compile_env!(:arrow, :cognito_groups) | ||
|
||
def sign_in(conn, auth_token) do | ||
user_pool_id = | ||
:ueberauth | ||
|> Application.get_env(Ueberauth.Strategy.Cognito) | ||
|> Keyword.get(:user_pool_id) | ||
|> config_value | ||
|
||
data = %{ | ||
"Username" => auth_token.username, | ||
"UserPoolId" => user_pool_id | ||
} | ||
|
||
headers = [ | ||
{"x-amz-target", "#{@aws_cognito_target}.AdminListGroupsForUser"}, | ||
{"content-type", "application/x-amz-json-1.1"} | ||
] | ||
|
||
operation = ExAws.Operation.JSON.new(:"cognito-idp", data: data, headers: headers) | ||
|
||
{module, function} = Application.get_env(:arrow, :ex_aws_requester) | ||
|
||
roles = | ||
case apply(module, function, [operation]) do | ||
{:ok, %{"Groups" => groups}} -> | ||
Enum.flat_map(groups, fn %{"GroupName" => group} -> | ||
case @cognito_groups[group] do | ||
role when is_binary(role) -> [role] | ||
_ -> [] | ||
end | ||
end) | ||
|
||
response -> | ||
:ok = Logger.warn("unexpected_aws_api_response: #{inspect(response)}") | ||
[] | ||
end | ||
|
||
conn | ||
|> Guardian.Plug.sign_in( | ||
ArrowWeb.AuthManager, | ||
auth_token.username, | ||
%{roles: roles} | ||
) | ||
end | ||
|
||
@spec config_value(binary() | {module(), atom(), [any()]}) :: any() | ||
defp config_value(value) when is_binary(value), do: value | ||
defp config_value({m, f, a}), do: apply(m, f, a) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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} | ||
) | ||
else | ||
other -> | ||
Logger.warn("unexpected response when logging in via Keycloak API: #{inspect(other)}") | ||
conn | ||
end | ||
end | ||
|
||
defp lookup_user_id(email) do | ||
with {:ok, [%{"id" => user_id}]} <- | ||
keycloak_api("users", %{ | ||
max: 1, | ||
email: email, | ||
exact: true, | ||
briefRepresentation: true | ||
}) do | ||
{:ok, user_id} | ||
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" | ||
|
||
with {:ok, response} <- keycloak_api(url) do | ||
roles = for r <- response, do: r["name"] | ||
{:ok, roles} | ||
end | ||
end | ||
|
||
defp keycloak_api(url, params \\ %{}) do | ||
base_url = Application.get_env(:arrow, :keycloak_api_base) | ||
opts = Map.new(Application.get_env(:ueberauth, Ueberauth.Strategy.OIDC)[:keycloak]) | ||
|
||
with {:ok, tokens} <- | ||
OpenIDConnect.fetch_tokens(opts, %{grant_type: "client_credentials", scope: "openid"}), | ||
headers = [{"authorization", "#{tokens["token_type"]} #{tokens["access_token"]}"}], | ||
{:ok, %{status_code: 200} = response} <- | ||
HTTPoison.get("#{base_url}#{url}", headers, params: params) do | ||
Jason.decode(response.body) | ||
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
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