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

chore: run unauthenticated fetch before authenticated #127

Closed
wants to merge 4 commits into from
Closed
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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
- run: rm .gitignore
- uses: JamesIves/[email protected]
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
BRANCH: gh-pages
FOLDER: build
CLEAN: true
33 changes: 31 additions & 2 deletions src/ldf-client-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,37 @@ function initEngine(config) {
config.context.httpProxyHandler = new ProxyHandlerStatic(config.context.httpProxy);

// Set up authenticated fetch
if (config.context.workerSolidAuth)
config.context.fetch = workerToWindowHandler.buildAuthenticatedFetch();
if (config.context.workerSolidAuth) {
function getNamespace(input) {
const url = new URL(new Request(input).url);
return url.origin + url.pathname;
}

const authenticatedFetch = workerToWindowHandler.buildAuthenticatedFetch();
const authUrls = {};

async function comunicaFetch(...args) {
if (authUrls[getNamespace(args[0])]) {
// In future we should check if this is a 403? (double check code) response
// and if so try the *unathenticated* fetch. This handles cases like a qpf endpoint
// changing an endpoint from being auth-required to public halfway through a session
return await authenticatedFetch(...args);
}

const response = await global.fetch(...args);

if (response.status === 401) {
// In the future we should probably also cache any URLs that we
// are redirected to as well as the URL we started with
authUrls[getNamespace(args[0])] = true;
return await authenticatedFetch(...args);
}

return response;
}

config.context.fetch = comunicaFetch;
}

// Transform query format to expected structure
if (config.context.queryFormat)
Expand Down
Loading