fix(gravatar): 🐛 fix nonce

This commit is contained in:
May 2024-11-22 22:38:06 -08:00
parent 577a856eed
commit 7b7364fcb9
Signed by: split
GPG key ID: C325C61F0BF517C0

View file

@ -22,7 +22,33 @@ export default translator({
if (!avatar) throw new Error("gravatar: failed to get avatar") if (!avatar) throw new Error("gravatar: failed to get avatar")
const Cookie = `gravatar=${cookie}; is-logged-in=1` let headers: Record<string, string> = {
Cookie: `gravatar=${encodeURIComponent(cookie)}; is-logged-in=1`,
"Alt-Used": "api.gravatar.com",
Origin: "https://gravatar.com",
Referer: "https://gravatar.com/",
}
// get X-GR-Nonce
const nonceRequest = await fetch(
"https://gravatar.com/profile/avatars",
{ headers }
)
if (!nonceRequest.ok)
throw new Error(
`gravatar: failed to get site to extract nonce with ${nonceRequest.status}`
)
// this is dumb, but idrgaf
const nonce = (await nonceRequest.text()).match(
/data-rest-api-nonce="(.*)"/
)?.[1]
if (!nonce) throw new Error(`gravatar: failed to scrape nonce`)
headers["X-GR-Nonce"] = nonce
// construct fd // construct fd
const fd = new FormData() const fd = new FormData()
@ -37,11 +63,7 @@ export default translator({
{ {
body: fd, body: fd,
method: "POST", method: "POST",
headers: { headers,
Cookie,
"Alt-Used": "api.gravatar.com",
"X-GR-Nonce": crypto.randomUUID(),
},
} }
) )
@ -68,11 +90,7 @@ export default translator({
{ {
method: "POST", method: "POST",
body: JSON.stringify({ altText: payload.altText }), body: JSON.stringify({ altText: payload.altText }),
headers: { headers,
Cookie,
"Alt-Used": "api.gravatar.com",
"X-GR-Nonce": crypto.randomUUID(),
},
} }
) )
if (!req.ok) if (!req.ok)
@ -86,11 +104,7 @@ export default translator({
{ {
method: "POST", method: "POST",
body: JSON.stringify({ image_id }), body: JSON.stringify({ image_id }),
headers: { headers,
Cookie,
"Alt-Used": "api.gravatar.com",
"X-GR-Nonce": crypto.randomUUID(),
},
} }
) )