Skip to content

Commit

Permalink
Revert "Use ServiceWorker for reactive authentication"
Browse files Browse the repository at this point in the history
This reverts commit 3cd2818
  • Loading branch information
langsamu committed Apr 28, 2024
1 parent 999f5c8 commit b3b79ed
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 108 deletions.
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<meta charset="UTF-8">
<title>Solid Explorer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script type="module" src="./scripts/reactiveAuthenticationServiceWorker.js"></script>
<script src="./scripts/index.js" type="module" defer></script>
<style>
body {
Expand Down
4 changes: 3 additions & 1 deletion scripts/ReactiveAuthenticationClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {DPopBoundAccessToken} from "./DPopBoundAccessToken.js"

const DEFAULT_AUTHENTICATION_MECHANISM = "Bearer"

export class ReactiveAuthenticationClient {
export class ReactiveAuthenticationClient extends EventTarget {
/** @type {TokenProvider[]} */
#tokenProviders

Expand All @@ -19,6 +19,8 @@ export class ReactiveAuthenticationClient {
* @param {function(input: RequestInfo | URL, init?: RequestInit): Promise<Response>} fetch
*/
constructor(cache, tokenProviders, fetch) {
super()

this.#challengeCache = cache
this.#tokenProviders = tokenProviders
this.#underlyingFetch = fetch
Expand Down
34 changes: 23 additions & 11 deletions scripts/SolidClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@ import {ResourceUri} from "./ResourceUri.js"
import {GraphNode} from "../packages/wrap/GraphNode.js"

export class SolidClient extends EventTarget {
/** @type {ReactiveAuthenticationClient} */
#authenticationClient

constructor(authenticationClient) {
super()

this.#authenticationClient = authenticationClient

this.#authenticationClient.addEventListener("fetching", e => this.dispatchEvent(new CustomEvent("fetching", {bubbles: true})))
this.#authenticationClient.addEventListener("fetched", e => this.dispatchEvent(new CustomEvent("fetched", {bubbles: true})))
}

async getAcrUri(resourceUri) {
const response = await fetch(resourceUri, {
const response = await this.#authenticationClient.fetch(resourceUri, {
cache: "no-store",
method: HttpMethod.Head
})
Expand All @@ -21,7 +33,7 @@ export class SolidClient extends EventTarget {
method: HttpMethod.Get
}

return await fetch(resourceUri, init)
return await this.#authenticationClient.fetch(resourceUri, init)
}

async deleteResource(resourceUri) {
Expand All @@ -30,7 +42,7 @@ export class SolidClient extends EventTarget {
method: HttpMethod.Delete
}

return await fetch(resourceUri, init)
return await this.#authenticationClient.fetch(resourceUri, init)
}

async getChildren(resourceUri) {
Expand All @@ -50,7 +62,7 @@ export class SolidClient extends EventTarget {
}
}

const resourceResponse = await fetch(resourceUri, init)
const resourceResponse = await this.#authenticationClient.fetch(resourceUri, init)

const resourceText = await resourceResponse.text()
const dataset = new N3.Store(new N3.Parser({baseIRI: resourceUri.toString()}).parse(resourceText))
Expand Down Expand Up @@ -82,7 +94,7 @@ export class SolidClient extends EventTarget {
method: HttpMethod.Head
}

const resourceResponse = await fetch(resourceUri, init)
const resourceResponse = await this.#authenticationClient.fetch(resourceUri, init)
const links = resourceResponse.headers.get(HttpHeader.Link)

if (links.includes('<http://www.w3.org/ns/pim/space#Storage>; rel="type"')) {
Expand All @@ -102,14 +114,14 @@ export class SolidClient extends EventTarget {
method: HttpMethod.Head
}

const resourceResponse = await fetch(resourceUri, init)
const resourceResponse = await this.#authenticationClient.fetch(resourceUri, init)
const links = resourceResponse.headers.get(HttpHeader.Link)

return links.includes('<http://www.w3.org/ns/pim/space#Storage>; rel="type"')
}

async getAcr(acrUri, accessToken) {
const acrResponse = await fetch(acrUri, {
const acrResponse = await this.#authenticationClient.fetch(acrUri, {
cache: "no-store",
headers: {
[HttpHeader.Accept]: Mime.Turtle,
Expand All @@ -133,7 +145,7 @@ export class SolidClient extends EventTarget {
}
}))

await fetch(acrUri, {
await this.#authenticationClient.fetch(acrUri, {
cache: "no-store",
method: HttpMethod.Put,
headers: {
Expand All @@ -155,7 +167,7 @@ export class SolidClient extends EventTarget {
body
}

return await fetch(resourceUri, init)
return await this.#authenticationClient.fetch(resourceUri, init)
}

async postResource(resourceUri, sourceType, contentType, slug) {
Expand All @@ -169,7 +181,7 @@ export class SolidClient extends EventTarget {
}
}

return await fetch(resourceUri, init)
return await this.#authenticationClient.fetch(resourceUri, init)
}

async grantAccess(resourceUri, isProvidedTo, mode) {
Expand Down Expand Up @@ -205,7 +217,7 @@ export class SolidClient extends EventTarget {
}
}
}
const accessGrantResponse = await fetch(issueEndpoint, {
const accessGrantResponse = await this.#authenticationClient.fetch(issueEndpoint, {
method: HttpMethod.Post,
headers: {
[HttpHeader.ContentType]: Mime.Json
Expand Down
16 changes: 15 additions & 1 deletion scripts/elements/AppElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import {SolidClient} from "../SolidClient.js"
import {HttpHeader, Ldp, Mime} from "../../packages/common/Vocabulary.js"
import {ResourceUri} from "../ResourceUri.js"
import {OidcCredentialManager} from "../../packages/oidc/OidcCredentialManager.js"
import {ReactiveAuthenticationClient} from "../ReactiveAuthenticationClient.js"
import {UmaTokenProvider} from "../UmaTokenProvider.js"
import {DPoPTokenProvider} from "../../packages/oidc/DPoPTokenProvider.js"
import {OidcTokenProvider} from "../../packages/oidc/OidcTokenProvider.js"
import {ResourceSelectedEvent} from "../ResourceSelectedEvent.js"
import {ResourceUriString} from "../ResourceUriStringEvent.js"
import {ContentType} from "../../packages/common/ContentType.js"
Expand All @@ -26,7 +30,17 @@ class AppElement extends HTMLBodyElement {
super()

this.#oidc = new OidcCredentialManager
this.#solid = new SolidClient()
const authCache = new Map

const umaTokenProvider = new UmaTokenProvider(authCache)
const dpopTokenProvider = new DPoPTokenProvider
const oidcTokenProvider = new OidcTokenProvider

umaTokenProvider.addEventListener("needCredentials", async e => e.detail.resolve(await this.#oidc.getCredentials()))
dpopTokenProvider.addEventListener("needCredentials", async e => e.detail.resolve(await this.#oidc.getCredentials()))
oidcTokenProvider.addEventListener("needCredentials", async e => e.detail.resolve(await this.#oidc.getCredentials()))

this.#solid = new SolidClient(new ReactiveAuthenticationClient(authCache, [umaTokenProvider, dpopTokenProvider, oidcTokenProvider], window.fetch))
}

connectedCallback() {
Expand Down
32 changes: 0 additions & 32 deletions scripts/reactiveAuthenticationServiceWorker.js

This file was deleted.

62 changes: 0 additions & 62 deletions worker.js
Original file line number Diff line number Diff line change
@@ -1,62 +0,0 @@
import {ReactiveAuthenticationClient} from "./scripts/ReactiveAuthenticationClient.js"
import {UmaTokenProvider} from "./scripts/UmaTokenProvider.js"
import {OidcTokenProvider} from "./packages/oidc/OidcTokenProvider.js"
import {DPoPTokenProvider} from "./packages/oidc/DPoPTokenProvider.js"

self.addEventListener("install", onInstall)
self.addEventListener("activate", onActivate)
self.addEventListener("fetch", onFetch)

const session = new Map

async function onInstall(e) {
console.log("sw.onInstall", e)

e.waitUntil(self.skipWaiting())
}

async function onActivate(e) {
console.log("sw.onActivate", e)
await self.clients.claim()
}

function onFetch(e) {
console.debug("sw.onFetch", e)

if (!e.clientId) {
console.debug("Ignore navigation")
return
}

e.respondWith(fetchInternal(e))
}

async function fetchInternal(e) {
const client = await self.clients.get(e.clientId)

const umaTokenProvider = new UmaTokenProvider(session)
const dpopTokenProvider = new DPoPTokenProvider
const oidcTokenProvider = new OidcTokenProvider

umaTokenProvider.addEventListener("needCredentials", async e => e.detail.resolve(await postAndWaitForResponse(client, "needCredentials")))
dpopTokenProvider.addEventListener("needCredentials", async e => e.detail.resolve(await postAndWaitForResponse(client, "needCredentials")))
oidcTokenProvider.addEventListener("needCredentials", async e => e.detail.resolve(await postAndWaitForResponse(client, "needCredentials")))

const rac = new ReactiveAuthenticationClient(session, [umaTokenProvider, dpopTokenProvider, oidcTokenProvider], self.fetch)
return await rac.fetch(e.request)
}

async function postAndWaitForResponse(target, message) {
const channel = new MessageChannel()

try {
return await new Promise(resolve => {
channel.port1.onmessage = e => resolve(e.data)

target.postMessage(message, [channel.port2])
})

} finally {
channel.port1.onmessage = undefined
}
}

0 comments on commit b3b79ed

Please sign in to comment.