Skip to content

Commit 1bc5639

Browse files
authored
Fix containerd unauthorized response header (#63)
1 parent aa61ad5 commit 1bc5639

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/index.js

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,24 +58,9 @@ async function handleRequest(request) {
5858
redirect: "follow",
5959
});
6060
if (resp.status === 401) {
61-
if (MODE == "debug") {
62-
headers.set(
63-
"Www-Authenticate",
64-
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
65-
);
66-
} else {
67-
headers.set(
68-
"Www-Authenticate",
69-
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
70-
);
71-
}
72-
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
73-
status: 401,
74-
headers: headers,
75-
});
76-
} else {
77-
return resp;
61+
return responseUnauthorized(url);
7862
}
63+
return resp;
7964
}
8065
// get token
8166
if (url.pathname == "/v2/auth") {
@@ -122,7 +107,11 @@ async function handleRequest(request) {
122107
headers: request.headers,
123108
redirect: "follow",
124109
});
125-
return await fetch(newReq);
110+
const resp = await fetch(newReq);
111+
if (resp.status == 401) {
112+
return responseUnauthorized(url);
113+
}
114+
return resp;
126115
}
127116

128117
function parseAuthenticate(authenticateStr) {
@@ -153,3 +142,22 @@ async function fetchToken(wwwAuthenticate, scope, authorization) {
153142
}
154143
return await fetch(url, { method: "GET", headers: headers });
155144
}
145+
146+
function responseUnauthorized(url) {
147+
const headers = new(Headers);
148+
if (MODE == "debug") {
149+
headers.set(
150+
"Www-Authenticate",
151+
`Bearer realm="http://${url.host}/v2/auth",service="cloudflare-docker-proxy"`
152+
);
153+
} else {
154+
headers.set(
155+
"Www-Authenticate",
156+
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
157+
);
158+
}
159+
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
160+
status: 401,
161+
headers: headers,
162+
});
163+
}

0 commit comments

Comments
 (0)