Include origin header in url generation request
This commit is contained in:
parent
25a8623193
commit
fe6ff7f915
@ -16,7 +16,7 @@ export async function GenerateUploadURL(
|
|||||||
env: Env,
|
env: Env,
|
||||||
path: string,
|
path: string,
|
||||||
size: number,
|
size: number,
|
||||||
fileExt: string
|
fileExt: string,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const accessToken = await GetAccessToken(env);
|
const accessToken = await GetAccessToken(env);
|
||||||
const contentTypes: { [k: string]: string } = {
|
const contentTypes: { [k: string]: string } = {
|
||||||
@ -38,21 +38,25 @@ export async function GenerateUploadURL(
|
|||||||
|
|
||||||
const resumableUploadReq = await fetch(
|
const resumableUploadReq = await fetch(
|
||||||
`https://storage.googleapis.com/upload/storage/v1/b/portal-carcrushers-cc/o?uploadType=resumable&name=${encodeURIComponent(
|
`https://storage.googleapis.com/upload/storage/v1/b/portal-carcrushers-cc/o?uploadType=resumable&name=${encodeURIComponent(
|
||||||
path
|
path,
|
||||||
)}`,
|
)}`,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
authorization: `Bearer ${accessToken}`,
|
authorization: `Bearer ${accessToken}`,
|
||||||
|
origin:
|
||||||
|
typeof env.LOCAL === "undefined"
|
||||||
|
? "https://carcrushers.cc"
|
||||||
|
: "http://localhost:8788",
|
||||||
"x-upload-content-type": contentTypes[fileExt],
|
"x-upload-content-type": contentTypes[fileExt],
|
||||||
"x-upload-content-length": size.toString(),
|
"x-upload-content-length": size.toString(),
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!resumableUploadReq.ok)
|
if (!resumableUploadReq.ok)
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Failed to create resumable upload: ${await resumableUploadReq.text()}`
|
`Failed to create resumable upload: ${await resumableUploadReq.text()}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
const url = resumableUploadReq.headers.get("location");
|
const url = resumableUploadReq.headers.get("location");
|
||||||
@ -71,7 +75,7 @@ async function GetAccessToken(env: Env): Promise<string> {
|
|||||||
iss: env.WORKER_GSERVICEACCOUNT,
|
iss: env.WORKER_GSERVICEACCOUNT,
|
||||||
scope:
|
scope:
|
||||||
"https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/devstorage.read_write",
|
"https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/devstorage.read_write",
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
.replaceAll("+", "-")
|
.replaceAll("+", "-")
|
||||||
.replaceAll("/", "_")
|
.replaceAll("/", "_")
|
||||||
@ -82,15 +86,15 @@ async function GetAccessToken(env: Env): Promise<string> {
|
|||||||
stringToBuffer(atob(env.STORAGE_ACCOUNT_KEY.replace(/(\r\n|\n|\r)/gm, ""))),
|
stringToBuffer(atob(env.STORAGE_ACCOUNT_KEY.replace(/(\r\n|\n|\r)/gm, ""))),
|
||||||
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
||||||
false,
|
false,
|
||||||
["sign"]
|
["sign"],
|
||||||
);
|
);
|
||||||
const signature = await crypto.subtle.sign(
|
const signature = await crypto.subtle.sign(
|
||||||
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
||||||
signingKey,
|
signingKey,
|
||||||
stringToBuffer(`eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.${claimSet}`)
|
stringToBuffer(`eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.${claimSet}`),
|
||||||
);
|
);
|
||||||
const assertion = `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.${claimSet}.${arrBufToB64Url(
|
const assertion = `eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.${claimSet}.${arrBufToB64Url(
|
||||||
signature
|
signature,
|
||||||
)}`;
|
)}`;
|
||||||
const tokenRequest = await fetch("https://oauth2.googleapis.com/token", {
|
const tokenRequest = await fetch("https://oauth2.googleapis.com/token", {
|
||||||
body: `grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${assertion}`,
|
body: `grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${assertion}`,
|
||||||
@ -109,7 +113,7 @@ async function GetAccessToken(env: Env): Promise<string> {
|
|||||||
async function getKeyIDs(
|
async function getKeyIDs(
|
||||||
access_token: string,
|
access_token: string,
|
||||||
projectId: string,
|
projectId: string,
|
||||||
keys: { partitionId: { projectId: string }; path: { kind: string }[] }[]
|
keys: { partitionId: { projectId: string }; path: { kind: string }[] }[],
|
||||||
) {
|
) {
|
||||||
const keyRequest = await fetch(
|
const keyRequest = await fetch(
|
||||||
`https://datastore.googleapis.com/v1/projects/${projectId}:allocateIds`,
|
`https://datastore.googleapis.com/v1/projects/${projectId}:allocateIds`,
|
||||||
@ -120,7 +124,7 @@ async function getKeyIDs(
|
|||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!keyRequest.ok) {
|
if (!keyRequest.ok) {
|
||||||
@ -134,7 +138,7 @@ async function getKeyIDs(
|
|||||||
export async function insertLogs(
|
export async function insertLogs(
|
||||||
userActionMap: { [k: string]: number },
|
userActionMap: { [k: string]: number },
|
||||||
reportId: string,
|
reportId: string,
|
||||||
context: RequestContext
|
context: RequestContext,
|
||||||
) {
|
) {
|
||||||
const accessToken = await GetAccessToken(context.env);
|
const accessToken = await GetAccessToken(context.env);
|
||||||
const actionBaseURLs: { [k: number]: string } = {
|
const actionBaseURLs: { [k: number]: string } = {
|
||||||
@ -171,7 +175,7 @@ export async function insertLogs(
|
|||||||
const keys = await getKeyIDs(
|
const keys = await getKeyIDs(
|
||||||
accessToken,
|
accessToken,
|
||||||
context.env.DATASTORE_PROJECT,
|
context.env.DATASTORE_PROJECT,
|
||||||
preAllocatedLogKeys
|
preAllocatedLogKeys,
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const [user, action] of Object.entries(userActionMap)) {
|
for (const [user, action] of Object.entries(userActionMap)) {
|
||||||
@ -208,7 +212,7 @@ export async function insertLogs(
|
|||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!mutationRequest.ok) {
|
if (!mutationRequest.ok) {
|
||||||
@ -253,7 +257,7 @@ export async function queryLogs(user: number, context: RequestContext) {
|
|||||||
"content-type": "application/json",
|
"content-type": "application/json",
|
||||||
},
|
},
|
||||||
method: "POST",
|
method: "POST",
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!queryRequest.ok) {
|
if (!queryRequest.ok) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user