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: Rewrite getPodBase to always get correct podBase #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
32 changes: 25 additions & 7 deletions addon/services/solid-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,32 @@ export default class AuthService extends Service {
let webIdDoc = webId;
if (webId) {
await this.store.load(sym(webId).doc());
podBase = this.store.any(sym(webId), SP('storage'), undefined, sym(webIdDoc).doc())?.value || this.store.any(undefined, RDF('type'), SP('Storage'), sym(webIdDoc).doc())?.value;
// Check if podBase is not undefined and webIdDoc is not the root domain

// Start with WebID and traverse upwards until we find a pim:Storage resource or a pim:Storage link response header.
let previousWebIdDoc = webIdDoc;
while (!podBase && !this.store.any(webIdDoc, RDF('type'), LDP('BasicContainer'), sym(webIdDoc).doc()) && !webIdDoc.endsWith('://')) {
// Substring of webIdDoc leaving off the last slash and last directory.
previousWebIdDoc = webIdDoc;
webIdDoc = webIdDoc.substring(0, webIdDoc.lastIndexOf('/', webIdDoc.length - 2)) + '/';
podBase = this.store.any(sym(webIdDoc), SP('storage'), undefined, sym(webIdDoc).doc())?.value || this.store.any(undefined, RDF('type'), SP('Storage'), sym(webIdDoc).doc())?.value;
while (!podBase && !this.store.any(webIdDoc, RDF("type"), LDP("BasicContainer"), sym(webIdDoc).doc()) && !webIdDoc.endsWith("://")) {
// Check if the current resource is a pim:Storage resource
podBase = this.store.any(sym(webIdDoc), SP("storage"), undefined, sym(webIdDoc).doc())?.value || this.store.any(undefined, RDF("type"), SP("Storage"), sym(webIdDoc).doc())?.value;

// Otherwise, check if the current resource has a pim:Storage link response header.
if (!podBase) {
// Get response headers from the webIdDoc
const response = await fetch(webIdDoc, { method: 'HEAD' });
const linkHeader = response.headers.get('Link');
if (
linkHeader &&
linkHeader.includes('http://www.w3.org/ns/pim/space#Storage')
) {
podBase = webIdDoc;
}
}

// Otherwise, traverse upwards
if (!podBase) {
// Prepare next iteration
previousWebIdDoc = webIdDoc;
webIdDoc = webIdDoc.substring(0, webIdDoc.lastIndexOf("/", webIdDoc.length - 2)) + "/";
}
}

if (!podBase) {
Expand Down