-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[federation] implement resolve endpoint
- Loading branch information
Showing
8 changed files
with
164 additions
and
5 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
43 changes: 43 additions & 0 deletions
43
apps/boruta_federation/lib/boruta_federation/openid_federation.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,43 @@ | ||
defmodule BorutaFederation.OpenidFederationApplication do | ||
@callback resolve_success(context :: any, federation_entity_statement :: String.t()) :: any() | ||
@callback resolve_failure(context :: any, error :: Boruta.Oauth.Error.t()) :: any() | ||
end | ||
|
||
defmodule BorutaFederation.OpenidFederation do | ||
alias Boruta.Oauth.Error | ||
alias BorutaFederation.FederationEntities | ||
alias BorutaFederation.TrustChains | ||
|
||
@type resolve_params :: %{ | ||
sub: String.t(), | ||
anchor: String.t() | ||
} | ||
|
||
@spec resolve(context :: any(), resolve_params :: resolve_params(), module :: atom()) :: any() | ||
def resolve(context, resolve_params, module) do | ||
case FederationEntities.get_entity(resolve_params[:sub]) do | ||
nil -> | ||
error = %Error{ | ||
status: :bad_request, | ||
error: :invalid_request, | ||
error_description: "Federation entity could not be found." | ||
} | ||
module.resolve_failure(context, error) | ||
|
||
entity -> | ||
case TrustChains.generate_statement(entity) do | ||
{:ok, statement} -> | ||
module.resolve_success(context, statement) | ||
|
||
{:error, error} -> | ||
error = %Error{ | ||
status: :bad_request, | ||
error: :invalid_request, | ||
error_description: "Could not generate federation entity statement #{error}." | ||
} | ||
|
||
module.resolve_failure(context, error) | ||
end | ||
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
32 changes: 32 additions & 0 deletions
32
apps/boruta_federation/lib/boruta_federation_web/controllers/resolve_controller.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,32 @@ | ||
defmodule BorutaFederationWeb.ResolveController do | ||
@behaviour BorutaFederation.OpenidFederationApplication | ||
|
||
alias BorutaFederationWeb.ErrorView | ||
use BorutaFederationWeb, :controller | ||
|
||
alias BorutaFederation.OpenidFederation | ||
|
||
def resolve(conn, params) do | ||
resolve_params = %{ | ||
sub: params["sub"], | ||
anchor: params["anchor"] | ||
} | ||
|
||
OpenidFederation.resolve(conn, resolve_params, __MODULE__) | ||
end | ||
|
||
@impl BorutaFederation.OpenidFederationApplication | ||
def resolve_success(conn, federation_entity_statement) do | ||
conn | ||
|> put_resp_header("content-type", "application/resolve-response+jwt") | ||
|> send_resp(200, federation_entity_statement) | ||
end | ||
|
||
@impl BorutaFederation.OpenidFederationApplication | ||
def resolve_failure(conn, error) do | ||
conn | ||
|> put_status(error.status) | ||
|> put_view(ErrorView) | ||
|> render("error.json", error: error) | ||
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
47 changes: 47 additions & 0 deletions
47
apps/boruta_federation/test/boruta_federation_web/controllers/resolve_controller_test.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,47 @@ | ||
defmodule BorutaFederationWeb.ResolveControllerTest do | ||
use BorutaFederationWeb.ConnCase | ||
|
||
import BorutaFederation.Factory | ||
|
||
alias BorutaFederation.FederationEntities.LeafEntity.Token | ||
|
||
describe "GET /resolve" do | ||
test "retruns not found", %{conn: conn} do | ||
conn = get(conn, Routes.resolve_path(conn, :resolve, %{sub: "sub", anchor: "anchor"})) | ||
assert json_response(conn, 400) == %{ | ||
"error" => "invalid_request", | ||
"error_description" => "Federation entity could not be found." | ||
} | ||
end | ||
|
||
test "retruns a statement", %{conn: conn} do | ||
entity = insert(:entity) | ||
|
||
conn = get(conn, Routes.resolve_path(conn, :resolve, %{sub: entity.id, anchor: "anchor"})) | ||
assert statement = response(conn, 200) | ||
|
||
entity_id = entity.id | ||
|
||
assert {:ok, | ||
%{ | ||
"exp" => exp, | ||
"iat" => iat, | ||
"iss" => "http://localhost:4000", | ||
"jwks" => [jwk], | ||
"metadata" => %{"openid_provider" => %{"issuer" => "http://localhost:4000"}}, | ||
"sub" => ^entity_id, | ||
"trust_marks" => [trust_mark] | ||
}} = Joken.peek_claims(statement) | ||
|
||
signer = | ||
Joken.Signer.create(entity.trust_chain_statement_alg, %{ | ||
"pem" => JOSE.JWK.from_map(jwk) |> JOSE.JWK.to_pem() |> elem(1) | ||
}) | ||
|
||
assert {:ok, _} = Token.verify_and_validate(statement, signer) | ||
assert {:ok, _} = Token.verify_and_validate(trust_mark, signer) | ||
assert iat | ||
assert exp | ||
end | ||
end | ||
end |