Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix containerd unauthorized response header #63

Merged
merged 1 commit into from
Oct 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 26 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,9 @@ async function handleRequest(request) {
redirect: "follow",
});
if (resp.status === 401) {
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,
});
} else {
return resp;
return responseUnauthorized(url);
}
return resp;
}
// get token
if (url.pathname == "/v2/auth") {
Expand Down Expand Up @@ -122,7 +107,11 @@ async function handleRequest(request) {
headers: request.headers,
redirect: "follow",
});
return await fetch(newReq);
const resp = await fetch(newReq);
if (resp.status == 401) {
return responseUnauthorized(url);
}
return resp;
}

function parseAuthenticate(authenticateStr) {
Expand Down Expand Up @@ -153,3 +142,22 @@ async function fetchToken(wwwAuthenticate, scope, authorization) {
}
return await fetch(url, { method: "GET", headers: headers });
}

function responseUnauthorized(url) {
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,
});
}
Loading