Skip to content

Commit

Permalink
Fix containerd unauthorized response header
Browse files Browse the repository at this point in the history
Copy Code: ciiiii#63
  • Loading branch information
DeyiXu committed Sep 22, 2024
1 parent dde4d01 commit e168469
Showing 1 changed file with 26 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,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 @@ -121,7 +106,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 @@ -152,3 +141,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,
});
}

0 comments on commit e168469

Please sign in to comment.