Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/common/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { logServerError } from "./errors"

const back2BaseUrl = process.env.BACK_2_BASE_URL || "http://localhost:5000"
const back2BasePath = "/internal/storage-service"
const back2BasePath = "/internal/storage-service/v2"
const back2ApiKey = process.env.BACK_2_API_KEY || "local"

export async function fetchStorage(path: string, init?: RequestInit): Promise<Response | undefined> {
Expand Down
65 changes: 21 additions & 44 deletions src/hooks/access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,54 +38,31 @@ export async function verifyYDocAccess(
throw new HocusPocusError("Invalid document name")
}

if (token == documentName) {
const response = await fetchStorage(
`/ydocs/${documentName}/access-level/`,
{ headers: proxyAuthHeaders }
)

if (response?.ok) {
const data = await response.json()
if (data === "read-write") {
return proxyAuthHeaders
} else if (data === "read-only") {
connection.readOnly = true
return proxyAuthHeaders
} else {
throw new HocusPocusError("Access Denied")
const response = await fetchStorage(
`/ydocs/${documentName}/access-level/`,
{
headers: {
"X-Storage-Token": token,
...proxyAuthHeaders,
}
} else if (response?.status === 404) {
throw new HocusPocusError("YDoc not found")
} else {
throw new HocusPocusError()
}
} else {
const response = await fetchStorage(
`/v2/ydocs/${documentName}/access-level/`,
{
headers: {
"X-Storage-Token": token,
...proxyAuthHeaders,
}
}
)
)

if (response?.ok) {
const data = await response.json()
if (data === "read-write") {
return proxyAuthHeaders
} else if (data === "read-only") {
connection.readOnly = true
return proxyAuthHeaders
} else {
throw new HocusPocusError("Access Denied")
}
} else if (response?.status === 403) {
throw new HocusPocusError("Invalid storage token")
} else if (response?.status === 404) {
throw new HocusPocusError("YDoc not found")
if (response?.ok) {
const data = await response.json()
if (data === "read-write") {
return proxyAuthHeaders
} else if (data === "read-only") {
connection.readOnly = true
return proxyAuthHeaders
} else {
throw new HocusPocusError()
throw new HocusPocusError("Access Denied")
}
} else if (response?.status === 403) {
throw new HocusPocusError("Invalid storage token")
} else if (response?.status === 404) {
throw new HocusPocusError("YDoc not found")
} else {
throw new HocusPocusError()
}
}
Loading