Skip to content
Open
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
84 changes: 10 additions & 74 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
"webpack-cli": "^7.0.2"
},
"dependencies": {
"@inrupt/solid-client-authn-browser": "^4.0.0",
"@uvdsl/solid-oidc-client-browser": "^0.2.2",
"solid-namespace": "^0.5.4"
},
"peerDependencies": {
Expand Down
60 changes: 55 additions & 5 deletions src/authSession/authSession.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,57 @@
import {
Session,
} from '@inrupt/solid-client-authn-browser'
import { Session as UvdslSession, SessionIDB } from '@uvdsl/solid-oidc-client-browser'

export const authSession = new Session()
/** Inrupt-shape `Session` API consumed by solid-logic + downstream panes,
* backed by uvdsl's default `Session` (WebWorkerSession). */

export const EVENTS = { LOGIN: 'login', LOGOUT: 'logout', SESSION_RESTORED: 'sessionRestore' } as const
type Cb = (...args: any[]) => void

export class Session {
private inner: UvdslSession | null = null
private subs: Record<string, Set<Cb>> = {}
readonly events = {
on: (n: string, cb: Cb) => { (this.subs[n] ||= new Set()).add(cb) },
off: (n: string, cb: Cb) => this.subs[n]?.delete(cb),
removeListener: (n: string, cb: Cb) => this.subs[n]?.delete(cb),
}
private emit(n: string, ...a: any[]) {
this.subs[n]?.forEach(cb => { try { cb(...a) } catch (_) {} })
Comment on lines +17 to +18
}
private ensure(redirect?: string) {
return this.inner ||= new UvdslSession(
{ client_name: 'SolidOS', redirect_uris: [redirect || location.origin + location.pathname] } as any,
{ database: new SessionIDB() } as any,
)
Comment on lines +20 to +24
}

get info() {
const s = this.inner
return { webId: s?.isActive ? s.webId : undefined, isLoggedIn: !!s?.isActive }
}

fetch = (input: RequestInfo | URL, init?: RequestInit) =>
this.ensure().authFetch(input as any, init)


async login(opts: { oidcIssuer: string; redirectUrl?: string }) {
const r = opts.redirectUrl || location.href
await this.ensure(r).login(opts.oidcIssuer, r)
}

async logout() { await this.ensure().logout(); this.emit('logout') }

async handleIncomingRedirect(opts?: { restorePreviousSession?: boolean; url?: string }) {
const url = new URL(opts?.url || location.href)
const s = this.ensure()
if (url.searchParams.has('code') && url.searchParams.has('state')) {
await s.handleRedirectFromLogin()
for (const k of ['code', 'state', 'iss']) url.searchParams.delete(k)
try { history.replaceState(null, '', url.toString()) } catch (_) {}
if (s.isActive) this.emit('login')
} else if (opts?.restorePreviousSession !== false) {
await s.restore()
if (s.isActive) this.emit('sessionRestore', url.toString())
}
Comment on lines +42 to +53
}
}

export const authSession = new Session()
2 changes: 1 addition & 1 deletion src/authn/SolidAuthnLogic.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { namedNode, NamedNode, sym } from 'rdflib'
import { appContext, offlineTestID } from './authUtil'
import * as debug from '../util/debug'
import { EVENTS, Session } from '@inrupt/solid-client-authn-browser'
import { EVENTS, Session } from '../authSession/authSession'
import { AuthenticationContext, AuthnLogic } from '../types'

export class SolidAuthnLogic implements AuthnLogic {
Expand Down
2 changes: 1 addition & 1 deletion src/logic/solidLogic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Session } from '@inrupt/solid-client-authn-browser'
import { Session } from '../authSession/authSession'
import * as rdf from 'rdflib'
import { LiveStore, NamedNode, Statement } from 'rdflib'
import { createAclLogic } from '../acl/aclLogic'
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Session } from '@inrupt/solid-client-authn-browser'
import { Session } from './authSession/authSession'
import { LiveStore, NamedNode, Statement } from 'rdflib'

export type AppDetails = {
Expand Down
19 changes: 18 additions & 1 deletion test/helpers/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,21 @@ import { TextEncoder, TextDecoder } from 'util'
global.TextEncoder = TextEncoder as any
global.TextDecoder = TextDecoder as any

fetchMock.enableMocks()
fetchMock.enableMocks()

// @uvdsl/solid-oidc-client-browser uses `import.meta.url` (for its
// SharedWorker URL), which jest/jsdom cannot evaluate. Stub the module
// surface used by the adapter so SolidAuthnLogic can be unit-tested.
jest.mock('@uvdsl/solid-oidc-client-browser', () => {
class Session {
isActive = false
webId = undefined
async login() {}
async logout() {}
async handleRedirectFromLogin() {}
async restore() {}
authFetch(input: any, init?: any) { return fetch(input, init) }
}
class SessionIDB {}
return { Session, SessionIDB }
}, { virtual: true })
8 changes: 7 additions & 1 deletion webpack.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ const externalsBase = {
'@trust/webcrypto': 'crypto',
'@xmldom/xmldom': 'window',
'whatwg-url': 'URL',
'rdflib': '$rdf'
'rdflib': '$rdf',
// Must externalize: uvdsl's SharedWorker URL is constructed via
// `new URL('./RefreshWorker.js', import.meta.url)`. If bundled inline here,
// webpack bakes a `file://…/solid-logic/dist/…` path that browsers refuse
// to load over http:// origin. Leaving it external lets the consumer bundle
// (mashlib) resolve the worker URL relative to its own publicPath.
'@uvdsl/solid-oidc-client-browser': '@uvdsl/solid-oidc-client-browser'
}

const externalsESM = {
Expand Down
Loading