Skip to content

Commit

Permalink
Fix registry auth (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ciiiii authored Jun 12, 2024
1 parent 3e8acaf commit 109f2d4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
35 changes: 9 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,6 @@ function routeByHosts(host) {

async function handleRequest(request) {
const url = new URL(request.url);
const authorization = request.headers.get("Authorization");
if (url.pathname == "/v2/") {
if (authorization === null || authorization === "") {
const headers = new Headers();
if (MODE == "debug") {
headers.set(
"Www-Authenticate",
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
);
} else {
headers.set(
"Www-Authenticate",
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
);
}
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
status: 401,
headers: headers,
});
}
}
const upstream = routeByHosts(url.hostname);
if (upstream === "") {
return new Response(
Expand All @@ -57,20 +36,24 @@ async function handleRequest(request) {
}
);
}
// check if need to authenticate
const authorization = request.headers.get("Authorization");
if (url.pathname == "/v2/") {
const newUrl = new URL(upstream + "/v2/");
const headers = new Headers();
if (authorization) {
headers.set("Authorization", authorization);
}
// check if need to authenticate
const resp = await fetch(newUrl.toString(), {
method: "GET",
headers: headers,
redirect: "follow",
});
if (resp.status === 200) {
} else if (resp.status === 401) {
const headers = new Headers();
if (resp.status === 401) {
if (MODE == "debug") {
headers.set(
"Www-Authenticate",
`Bearer realm="${LOCAL_ADDRESS}/v2/auth",service="cloudflare-docker-proxy"`
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
);
} else {
headers.set(
Expand Down
1 change: 0 additions & 1 deletion wrangler.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ upstream_protocol="https"

[vars]
MODE="production"
LOCAL_ADDRESS=""
TARGET_UPSTREAM=""

[env.dev.vars]
Expand Down

0 comments on commit 109f2d4

Please sign in to comment.