Skip to content

Commit 109f2d4

Browse files
authored
Fix registry auth (ciiiii#15)
1 parent 3e8acaf commit 109f2d4

File tree

2 files changed

+9
-27
lines changed

2 files changed

+9
-27
lines changed

src/index.js

+9-26
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,6 @@ function routeByHosts(host) {
2525

2626
async function handleRequest(request) {
2727
const url = new URL(request.url);
28-
const authorization = request.headers.get("Authorization");
29-
if (url.pathname == "/v2/") {
30-
if (authorization === null || authorization === "") {
31-
const headers = new Headers();
32-
if (MODE == "debug") {
33-
headers.set(
34-
"Www-Authenticate",
35-
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
36-
);
37-
} else {
38-
headers.set(
39-
"Www-Authenticate",
40-
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
41-
);
42-
}
43-
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
44-
status: 401,
45-
headers: headers,
46-
});
47-
}
48-
}
4928
const upstream = routeByHosts(url.hostname);
5029
if (upstream === "") {
5130
return new Response(
@@ -57,20 +36,24 @@ async function handleRequest(request) {
5736
}
5837
);
5938
}
60-
// check if need to authenticate
39+
const authorization = request.headers.get("Authorization");
6140
if (url.pathname == "/v2/") {
6241
const newUrl = new URL(upstream + "/v2/");
42+
const headers = new Headers();
43+
if (authorization) {
44+
headers.set("Authorization", authorization);
45+
}
46+
// check if need to authenticate
6347
const resp = await fetch(newUrl.toString(), {
6448
method: "GET",
49+
headers: headers,
6550
redirect: "follow",
6651
});
67-
if (resp.status === 200) {
68-
} else if (resp.status === 401) {
69-
const headers = new Headers();
52+
if (resp.status === 401) {
7053
if (MODE == "debug") {
7154
headers.set(
7255
"Www-Authenticate",
73-
`Bearer realm="${LOCAL_ADDRESS}/v2/auth",service="cloudflare-docker-proxy"`
56+
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
7457
);
7558
} else {
7659
headers.set(

wrangler.toml

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ upstream_protocol="https"
1111

1212
[vars]
1313
MODE="production"
14-
LOCAL_ADDRESS=""
1514
TARGET_UPSTREAM=""
1615

1716
[env.dev.vars]

0 commit comments

Comments
 (0)