Skip to content

Commit

Permalink
Add oauth metadata endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
geekingfrog committed Jun 16, 2024
1 parent a37f006 commit 42afe49
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
8 changes: 5 additions & 3 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ if Teiserver.ConfigHelpers.get_env("PHX_SERVER", nil) do
config :teiserver, TeiserverWeb.Endpoint, server: true
end

# used for mailing, checking origins, finding tls certs…
domain_name = Teiserver.ConfigHelpers.get_env("TEI_DOMAIN_NAME", "beyondallreason.info")

# Only do some runtime configuration in production since in dev and tests the
# files are automatically recompiled on the fly and thus, config/{dev,test}.exs
# are just fine
if config_env() == :prod do
# used for mailing, checking origins, finding tls certs…
domain_name = Teiserver.ConfigHelpers.get_env("TEI_DOMAIN_NAME", "beyondallreason.info")

certificates = [
keyfile: Teiserver.ConfigHelpers.get_env("TEI_TLS_PRIVATE_KEY_PATH"),
certfile: Teiserver.ConfigHelpers.get_env("TEI_TLS_CERT_PATH"),
Expand Down Expand Up @@ -190,3 +190,5 @@ if config_env() == :prod do
bot_name: Teiserver.ConfigHelpers.get_env("TEI_DISCORD_BOT_NAME")
end
end

config :teiserver, Teiserver.OAuth, issuer: "https://#{domain_name}"
4 changes: 4 additions & 0 deletions lib/teiserver_web/controllers/o_auth/code_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ defmodule TeiserverWeb.OAuth.CodeController do
_ -> conn |> put_status(400) |> render(:error, error_description: "invalid request")
end
end

def metadata(conn, _params) do
conn |> put_status(200) |> render(:metadata)
end
end
6 changes: 6 additions & 0 deletions lib/teiserver_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,12 @@ defmodule TeiserverWeb.Router do
post("/token", CodeController, :token)
end

scope "/", TeiserverWeb.OAuth do
pipe_through(:api)
# https://datatracker.ietf.org/doc/html/rfc8414
get("/.well-known/oauth-authorization-server", CodeController, :metadata)
end

scope "/admin", TeiserverWeb.Admin do
pipe_through([:live_browser, :protected])

Expand Down
19 changes: 19 additions & 0 deletions lib/teiserver_web/views/api/o_auth/code_view.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule TeiserverWeb.OAuth.CodeView do
use TeiserverWeb, :view

def token(%{token: token}) do
expires_in = DateTime.diff(token.expires_at, DateTime.utc_now(), :second)

Expand All @@ -13,4 +15,21 @@ defmodule TeiserverWeb.OAuth.CodeView do
def error(conn) do
Map.take(conn, [:error_description]) |> Map.put("error", "invalid_request")
end

def metadata(_) do
base = Application.fetch_env!(:teiserver, Teiserver.OAuth)[:issuer]

%{
issuer: base,
authorization_endpoint: base <> ~p"/oauth/authorize",
token_endpoint: base <> ~p"/oauth/token",
token_endpoint_auth_methods_supported: ["none", "client_secret_post"],
grant_types_supported: [
"authorization_code",
"refresh_token",
"client_credentials"
],
code_challenge_methods_supported: ["S256"]
}
end
end
22 changes: 22 additions & 0 deletions test/teiserver_web/controllers/o_auth/code_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ defmodule TeiserverWeb.OAuth.CodeControllerTest do
client_id: credential.client_id,
client_secret: "definitely-not-the-correct-secret"
}

resp = post(conn, ~p"/oauth/token", data)
json_resp = json_response(resp, 400)

Check warning on line 155 in test/teiserver_web/controllers/o_auth/code_controller_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

variable "json_resp" is unused (if the variable is not meant to be used, prefix it with an underscore)
end
Expand Down Expand Up @@ -201,4 +202,25 @@ defmodule TeiserverWeb.OAuth.CodeControllerTest do
assert %{"error" => "invalid_request"} = json_response(resp, 400)
end
end

describe "medatata endpoint" do
setup :setup_conn

test "can query oauth metadata", %{conn: conn} do
resp = json_response(get(conn, ~p"/.well-known/oauth-authorization-server"), 200)

assert resp == %{
"issuer" => "https://beyondallreason.info",
"authorization_endpoint" => "https://beyondallreason.info/oauth/authorize",
"token_endpoint" => "https://beyondallreason.info/oauth/token",
"token_endpoint_auth_methods_supported" => ["none", "client_secret_post"],
"grant_types_supported" => [
"authorization_code",
"refresh_token",
"client_credentials"
],
"code_challenge_methods_supported" => ["S256"]
}
end
end
end

0 comments on commit 42afe49

Please sign in to comment.