-
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
e5c50d7
commit ed9a745
Showing
12 changed files
with
164 additions
and
60 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,56 @@ | ||
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}} -> | ||
for %{"GroupName" => group} <- groups, | ||
{:ok, role} <- [Map.fetch(@cognito_groups, group)] do | ||
role | ||
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,77 @@ | ||
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 #{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, :no_users} | ||
|
||
{:ok, [_, _ | _]} -> | ||
{:error, :multiple_users} | ||
|
||
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 = 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) | ||
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
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