Skip to content

Commit dd9ec1f

Browse files
committed
remove router, require moutning under v2
1 parent 3b4cabd commit dd9ec1f

4 files changed

Lines changed: 94 additions & 1143 deletions

File tree

lib/oci/phoenix_router.ex

Lines changed: 0 additions & 32 deletions
This file was deleted.

lib/oci/plug.ex

Lines changed: 56 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,8 @@ defmodule OCI.Plug do
3636
%{registry: registry}
3737
end
3838

39-
defp get_authorization_header(conn) do
40-
conn
41-
|> Plug.Conn.get_req_header("authorization")
42-
|> List.first()
43-
end
44-
45-
def authenticate(conn) do
46-
conn
47-
|> get_authorization_header()
48-
|> case do
49-
nil ->
50-
conn
51-
52-
authorization ->
53-
authorization
54-
|> OCI.Auth.Adapter.authenticate()
55-
|> case do
56-
{:ok, ctx} ->
57-
conn
58-
|> assign(:oci_ctx, ctx)
59-
60-
{:error, reason} ->
61-
error_resp(conn, reason)
62-
end
63-
end
64-
end
65-
6639
@impl true
67-
def call(conn, %{registry: registry}) do
40+
def call(%{script_name: ["v2"]} = conn, %{registry: registry}) do
6841
conn =
6942
conn
7043
|> ensure_request_id()
@@ -81,42 +54,28 @@ defmodule OCI.Plug do
8154
|> assign(:raw_body, body)
8255
|> fetch_query_params()
8356
|> OCI.Inspector.inspect("before:handle_request/1")
84-
|> handle_request()
57+
|> handle_v2()
8558

8659
{:error, :UNAUTHORIZED} ->
8760
challenge(conn)
8861
end
8962
end
9063

91-
defp authorize(%{assigns: %{oci_ctx: ctx}}) do
92-
# TODO: infer and pass authorization info
93-
OCI.Auth.Adapter.authorize(ctx, "TODO:ACTION", "TODO:RESOURCE")
64+
def call(conn, _opts) do
65+
error_resp(conn, :UNSUPPORTED, "OCI Registry must be mounted at /v2")
9466
end
9567

96-
defp authorize(_) do
97-
{:error, :UNAUTHORIZED}
98-
end
99-
100-
defp challenge(conn) do
101-
registry = conn.private[:oci_registry]
102-
{scheme, auth_param} = OCI.Auth.Adapter.challenge(registry)
103-
104-
conn
105-
|> put_resp_header("www-authenticate", "#{scheme} #{auth_param}")
106-
|> send_resp(401, "")
107-
|> halt
108-
end
109-
110-
defp handle_request(%{path_info: ["v2"]} = conn), do: ping(conn)
111-
defp handle_request(%{path_info: ["v2" | _]} = conn), do: handle_v2(conn)
112-
defp handle_request(conn), do: error_resp(conn, :UNSUPPORTED)
113-
11468
defp handle_v2(conn) do
115-
[_v2 | segments] = conn.path_info
69+
segments = conn.path_info
11670

11771
segments
11872
|> Enum.reverse()
11973
|> case do
74+
[] ->
75+
conn
76+
|> put_resp_content_type("application/json")
77+
|> send_resp(200, "{}")
78+
12079
["list", "tags" | repo] ->
12180
repo = repo |> Enum.reverse() |> Enum.join("/")
12281
list_tags(conn, repo)
@@ -463,12 +422,6 @@ defmodule OCI.Plug do
463422
end
464423
end
465424

466-
defp ping(conn) do
467-
conn
468-
|> put_resp_content_type("application/json")
469-
|> send_resp(200, "{}")
470-
end
471-
472425
defp error_resp(conn, code, details \\ nil) do
473426
error = OCI.Error.init(code, details)
474427
body = %{errors: [error]} |> Jason.encode!()
@@ -500,4 +453,50 @@ defmodule OCI.Plug do
500453
put_private(conn, :plug_request_id, existing_id)
501454
end
502455
end
456+
457+
defp get_authorization_header(conn) do
458+
conn
459+
|> Plug.Conn.get_req_header("authorization")
460+
|> List.first()
461+
end
462+
463+
def authenticate(conn) do
464+
conn
465+
|> get_authorization_header()
466+
|> case do
467+
nil ->
468+
conn
469+
470+
authorization ->
471+
authorization
472+
|> OCI.Auth.Adapter.authenticate()
473+
|> case do
474+
{:ok, ctx} ->
475+
conn
476+
|> assign(:oci_ctx, ctx)
477+
478+
{:error, reason} ->
479+
error_resp(conn, reason)
480+
end
481+
end
482+
end
483+
484+
defp authorize(%{assigns: %{oci_ctx: ctx}}) do
485+
# TODO: infer and pass authorization info
486+
OCI.Auth.Adapter.authorize(ctx, "TODO:ACTION", "TODO:RESOURCE")
487+
end
488+
489+
defp authorize(_) do
490+
{:error, :UNAUTHORIZED}
491+
end
492+
493+
defp challenge(conn) do
494+
registry = conn.private[:oci_registry]
495+
{scheme, auth_param} = OCI.Auth.Adapter.challenge(registry)
496+
497+
conn
498+
|> put_resp_header("www-authenticate", "#{scheme} #{auth_param}")
499+
|> send_resp(401, "")
500+
|> halt
501+
end
503502
end

0 commit comments

Comments
 (0)