Skip to content

Commit 1e5242b

Browse files
authored
Merge pull request #331 from SolidOS/feat/access
support for manage access dialog
2 parents b0a3882 + 0431915 commit 1e5242b

7 files changed

Lines changed: 809 additions & 1554 deletions

File tree

‎package-lock.json‎

Lines changed: 574 additions & 1527 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"solidos-toolkit": "dev"
6464
},
6565
"dependencies": {
66+
"@dokieli/web-access-control": "^1.0.0",
6667
"@uvdsl/solid-oidc-client-browser": "^0.2.3",
6768
"solid-namespace": "^0.5.4",
6869
"vite": "^8.0.16",

‎src/acl/aclLogic.ts‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1+
import { applyPlan as applyAuthorizationPlan, findEffectiveACL, planGrant as planAuthorizationGrant, planPublicRead as planAuthorizationPublicRead, planRevoke as planAuthorizationRevoke, type AccessMode, type AccessSubject, type ACLContext, type Authorization, type PatchPlan } from '@dokieli/web-access-control'
12
import { graph, NamedNode, Namespace, serialize, sym } from 'rdflib'
2-
import { AclLogic } from '../types'
3+
import type { AclLogic } from '../types'
34
import { ns as namespace } from '../util/ns'
45

6+
// Helpers available from @dokieli/web-access-control:
7+
// - Discovery: findEffectiveACL, parentContainer
8+
// - ACL context and parsing: buildACLContext, authorizationsFromDataset, parseTurtle
9+
// - Query helpers: agentsWithMode, hasControl, isPublic, modesFor, subjectsWithMode
10+
// - Plan helpers: planContainerACL, planGrant, planOwnerControl, planPublicRead, planRevoke
11+
// - Serialization and application: serializeTerm, toN3Patch, toSparqlUpdate, toTurtle, applyPlan, negotiatePatchContentType
12+
// - Link and allow parsing: allows, parseWacAllow, linkTargets, parseLinkHeader
13+
// - Terms and constants: ACCESS_MODES, ACL, Authenticated, FOAF, modeFromIRI, modeIRI, namedNode, Public, quad, RDF_TYPE, variable
14+
515

616
export const ACL_LINK = sym(
717
'http://www.iana.org/assignments/link-relations/acl'
@@ -10,6 +20,18 @@ export const ACL_LINK = sym(
1020
export function createAclLogic(store): AclLogic {
1121

1222
const ns = namespace
23+
24+
function getFetch() {
25+
const fetcher = store.fetcher as {
26+
_fetch?: (url: string, init?: RequestInit) => Promise<Response>
27+
fetch?: (url: string, init?: RequestInit) => Promise<Response>
28+
} | undefined
29+
const fetch = fetcher?._fetch ?? fetcher?.fetch
30+
if (!fetch) {
31+
throw new Error('Cannot find effective ACL, store has no fetcher')
32+
}
33+
return fetch.bind(fetcher)
34+
}
1335

1436
async function findAclDocUrl(url: NamedNode) {
1537
await store.fetcher.load(url)
@@ -19,6 +41,35 @@ export function createAclLogic(store): AclLogic {
1941
}
2042
return docNode.value
2143
}
44+
45+
async function findEffectiveAcl(resourceURL: string | NamedNode): Promise<ACLContext> {
46+
const url = typeof resourceURL === 'string' ? resourceURL : resourceURL.value
47+
return findEffectiveACL(url, { fetch: getFetch() })
48+
}
49+
50+
async function findAccessGrants(resourceURL: string | NamedNode): Promise<Authorization[]> {
51+
const context = await findEffectiveAcl(resourceURL)
52+
return context.authorizations
53+
}
54+
55+
async function planGrant(resourceURL: string | NamedNode, subject: AccessSubject, modes: AccessMode[]): Promise<PatchPlan> {
56+
const context = await findEffectiveAcl(resourceURL)
57+
return planAuthorizationGrant(context, subject, modes)
58+
}
59+
60+
async function planRevoke(resourceURL: string | NamedNode, subject: AccessSubject): Promise<PatchPlan> {
61+
const context = await findEffectiveAcl(resourceURL)
62+
return planAuthorizationRevoke(context, subject)
63+
}
64+
65+
async function planPublicRead(resourceURL: string | NamedNode, enabled: boolean): Promise<PatchPlan> {
66+
const context = await findEffectiveAcl(resourceURL)
67+
return planAuthorizationPublicRead(context, enabled)
68+
}
69+
70+
async function applyPlan(plan: PatchPlan): Promise<Response> {
71+
return applyAuthorizationPlan(plan, { fetch: getFetch() })
72+
}
2273
/**
2374
* Simple Access Control
2475
*
@@ -149,6 +200,12 @@ export function createAclLogic(store): AclLogic {
149200
}
150201
return {
151202
findAclDocUrl,
203+
findEffectiveAcl,
204+
findAccessGrants,
205+
planGrant,
206+
planRevoke,
207+
planPublicRead,
208+
applyPlan,
152209
setACLUserPublic,
153210
genACLText
154211
}

‎src/resource/resourceLogic.ts‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { ACL_LINK } from '../acl/aclLogic'
33
import { ns } from '../util/ns'
44
import { assertSuccessfulHttpResponse, isMissingError } from './resourceHttp'
55
import { readWacAccessInfo } from './resourceMetadata'
6-
import { type AclLogic, type ResourceAccess, type ResourceAccessWithDelete, type ResourceDeleteOptions, type ResourceLogic, type ResourceMetadata, type ResourceMetadataWithDelete, type TypeIndexLogic } from '../types'
6+
import { type ResourceAccess, type ResourceAccessWithDelete, type ResourceDeleteOptions, type ResourceLogic, type ResourceMetadata, type ResourceMetadataWithDelete, type TypeIndexLogic } from '../types'
7+
import type { AclLogic } from '../types'
78

89
export function createResourceLogic(store, aclLogic: AclLogic, containerLogic, typeIndexLogic: TypeIndexLogic): ResourceLogic {
910
function createContainer(url: string) {
@@ -18,6 +19,17 @@ export function createResourceLogic(store, aclLogic: AclLogic, containerLogic, t
1819
return containerLogic.getContainerMemberCount(resourceNode)
1920
}
2021

22+
function isWebId(resource: string | NamedNode) {
23+
const uri = typeof resource === 'string' ? resource : resource.value
24+
25+
try {
26+
const parsed = new URL(uri)
27+
return parsed.hash === '#me' && /\/profile\/card\.ttl$/.test(parsed.pathname)
28+
} catch (_error) {
29+
return false
30+
}
31+
}
32+
2133
function readMetadata(subject: NamedNode, response: Response): ResourceMetadata {
2234
let contentType: string | undefined
2335
let canEdit = false
@@ -175,6 +187,7 @@ export function createResourceLogic(store, aclLogic: AclLogic, containerLogic, t
175187
fetchMetadataWithDelete,
176188
createContainer,
177189
isContainer,
178-
getContainerMemberCount
190+
getContainerMemberCount,
191+
isWebId
179192
}
180193
}

‎src/types.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { SessionWithLegacyEvents } from './authSession/authSession'
2+
import type { ACLContext, AccessMode, AccessSubject, Authorization, PatchPlan } from '@dokieli/web-access-control'
23
import { LiveStore, NamedNode, Statement } from 'rdflib'
34

45
export type AppDetails = {
@@ -79,6 +80,12 @@ export interface ProfileLogic {
7980

8081
export interface AclLogic {
8182
findAclDocUrl: (url: NamedNode) => Promise<string | undefined>,
83+
findEffectiveAcl: (resourceURL: string | NamedNode) => Promise<ACLContext>,
84+
findAccessGrants: (resourceURL: string | NamedNode) => Promise<Authorization[]>,
85+
planGrant: (resourceURL: string | NamedNode, subject: AccessSubject, modes: AccessMode[]) => Promise<PatchPlan>,
86+
planRevoke: (resourceURL: string | NamedNode, subject: AccessSubject) => Promise<PatchPlan>,
87+
planPublicRead: (resourceURL: string | NamedNode, enabled: boolean) => Promise<PatchPlan>,
88+
applyPlan: (plan: PatchPlan) => Promise<Response>,
8289
setACLUserPublic: (docURI: string, me: NamedNode,
8390
options: {
8491
defaultForNew?: boolean,
@@ -93,6 +100,8 @@ export interface AclLogic {
93100
) => string | undefined
94101
}
95102

103+
export type { ACLContext, AccessMode, AccessSubject, Authorization, PatchPlan }
104+
96105
export interface InboxLogic {
97106
createInboxFor: (peerWebId: string, nick: string) => Promise<string>,
98107
getNewMessages: (user?: NamedNode) => Promise<NamedNode[]>,
@@ -132,7 +141,8 @@ export interface ResourceLogic {
132141
fetchMetadataWithDelete: (subject: NamedNode) => Promise<ResourceMetadataWithDelete>,
133142
createContainer: (url: string) => Promise<void>,
134143
isContainer: (resource: NamedNode) => boolean,
135-
getContainerMemberCount: (resource: NamedNode) => number
144+
getContainerMemberCount: (resource: NamedNode) => number,
145+
isWebId: (resource: string | NamedNode) => boolean
136146
}
137147

138148
export interface TypeIndexLogic {

‎test/aclLogic.test.ts‎

Lines changed: 136 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,138 @@
1-
import { beforeAll, describe, expect, it } from 'vitest'
1+
import { beforeEach, describe, expect, it, vi } from 'vitest'
22
import { Fetcher, Store, sym, UpdateManager } from 'rdflib'
3-
import { createAclLogic } from '../src/acl/aclLogic'
4-
5-
describe('setACLUserPublic', () => {
6-
let aclLogic: any
7-
let store: Store
8-
beforeAll(() => {
9-
const options = { fetch: fetch }
10-
store = new Store()
11-
store.fetcher = new Fetcher(store, options)
12-
store.updater = new UpdateManager(store)
13-
aclLogic = createAclLogic(store)
14-
})
15-
it('exists', () => {
16-
expect(aclLogic.setACLUserPublic).toBeInstanceOf(Function)
17-
})
18-
it.skip('runs', async () => {
19-
expect(await aclLogic.setACLUserPublic(
20-
'https://test.test#',
21-
sym('https://test.test#'),
22-
{}
23-
)).toEqual({})
24-
})
3+
import { applyPlan, findEffectiveACL, planGrant, planPublicRead, planRevoke } from '@dokieli/web-access-control'
4+
import { ACL_LINK, createAclLogic } from '../src/acl/aclLogic'
5+
6+
vi.mock('@dokieli/web-access-control', () => ({
7+
findEffectiveACL: vi.fn(),
8+
planGrant: vi.fn(),
9+
planPublicRead: vi.fn(),
10+
planRevoke: vi.fn(),
11+
applyPlan: vi.fn(),
12+
Authenticated: Symbol('Authenticated'),
13+
Public: Symbol('Public'),
14+
planOwnerControl: vi.fn()
15+
}))
16+
17+
describe('createAclLogic', () => {
18+
let store: Store & {
19+
fetcher: Fetcher & {
20+
_fetch: ReturnType<typeof vi.fn>
21+
load: ReturnType<typeof vi.fn>
22+
webOperation: ReturnType<typeof vi.fn>
23+
}
24+
any: ReturnType<typeof vi.fn>
25+
}
26+
let aclLogic: ReturnType<typeof createAclLogic>
27+
28+
beforeEach(() => {
29+
const fetcher = {
30+
_fetch: vi.fn(),
31+
load: vi.fn(),
32+
webOperation: vi.fn(),
33+
unload: vi.fn()
34+
} as unknown as Fetcher & {
35+
_fetch: ReturnType<typeof vi.fn>
36+
load: ReturnType<typeof vi.fn>
37+
webOperation: ReturnType<typeof vi.fn>
38+
unload: ReturnType<typeof vi.fn>
39+
}
40+
41+
store = {
42+
fetcher,
43+
updater: new UpdateManager(new Store()),
44+
any: vi.fn()
45+
} as unknown as Store & {
46+
fetcher: typeof fetcher
47+
any: ReturnType<typeof vi.fn>
48+
}
49+
50+
aclLogic = createAclLogic(store)
51+
})
52+
53+
it('exports the ACL link relation constant', () => {
54+
expect(ACL_LINK.value).toBe('http://www.iana.org/assignments/link-relations/acl')
55+
})
56+
57+
it('finds the ACL document URL from the discovered link', async () => {
58+
const resource = sym('https://example.com/resource.ttl')
59+
const aclDoc = sym('https://example.com/resource.ttl.acl')
60+
const loadResult = { ok: true }
61+
62+
store.fetcher.load.mockResolvedValue(loadResult)
63+
store.any.mockReturnValue(aclDoc)
64+
65+
await expect(aclLogic.findAclDocUrl(resource)).resolves.toBe(aclDoc.value)
66+
expect(store.fetcher.load).toHaveBeenCalledWith(resource)
67+
expect(store.any).toHaveBeenCalledWith(resource, ACL_LINK)
68+
})
69+
70+
it('returns the authorizations from the effective ACL context', async () => {
71+
const resource = sym('https://example.com/resource.ttl')
72+
const context = { authorizations: [{ id: 'a1' }] }
73+
74+
vi.mocked(findEffectiveACL).mockResolvedValue(context as any)
75+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
76+
77+
await expect(aclLogic.findAccessGrants(resource)).resolves.toEqual(context.authorizations)
78+
expect(findEffectiveACL).toHaveBeenCalledWith(resource.value, expect.objectContaining({ fetch: expect.any(Function) }))
79+
})
80+
81+
it('delegates planGrant through the effective ACL context', async () => {
82+
const resource = sym('https://example.com/resource.ttl')
83+
const subject = { type: 'agent', value: 'https://example.com/profile/card#me' }
84+
const plan = { updates: [] }
85+
86+
vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any)
87+
vi.mocked(planGrant).mockReturnValue(plan as any)
88+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
89+
90+
await expect(aclLogic.planGrant(resource, subject as any, ['Read'])).resolves.toBe(plan)
91+
expect(planGrant).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), subject, ['Read'])
92+
})
93+
94+
it('delegates planPublicRead through the effective ACL context', async () => {
95+
const resource = sym('https://example.com/resource.ttl')
96+
const plan = { updates: [] }
97+
98+
vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any)
99+
vi.mocked(planPublicRead).mockReturnValue(plan as any)
100+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
101+
102+
await expect(aclLogic.planPublicRead(resource, true)).resolves.toBe(plan)
103+
expect(planPublicRead).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), true)
104+
})
105+
106+
it('delegates planRevoke through the effective ACL context', async () => {
107+
const resource = sym('https://example.com/resource.ttl')
108+
const subject = { type: 'agent', iri: 'https://example.com/profile/card#me' }
109+
const plan = { target: 'https://example.com/resource.ttl.acl', deletes: [], inserts: [] }
110+
111+
vi.mocked(findEffectiveACL).mockResolvedValue({ authorizations: [] } as any)
112+
vi.mocked(planRevoke).mockReturnValue(plan as any)
113+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
114+
115+
await expect(aclLogic.planRevoke(resource, subject as any)).resolves.toBe(plan)
116+
expect(planRevoke).toHaveBeenCalledWith(expect.objectContaining({ authorizations: [] }), subject)
117+
})
118+
119+
it('applies a patch plan with the store fetcher', async () => {
120+
const plan = { target: 'https://example.com/resource.ttl.acl', deletes: [], inserts: [] }
121+
const response = new Response('ok', { status: 200 })
122+
123+
store.fetcher._fetch.mockResolvedValue(new Response('', { status: 200 }))
124+
vi.mocked(applyPlan).mockResolvedValue(response)
125+
126+
await expect(aclLogic.applyPlan(plan as any)).resolves.toBe(response)
127+
expect(applyPlan).toHaveBeenCalledWith(plan, expect.objectContaining({ fetch: expect.any(Function) }))
128+
})
129+
130+
it('throws when the store cannot supply a fetch function', async () => {
131+
const resource = sym('https://example.com/resource.ttl')
132+
store.fetcher = { _fetch: vi.fn(), load: vi.fn(), webOperation: vi.fn(), unload: vi.fn() } as any
133+
delete (store.fetcher as any).fetch
134+
delete (store.fetcher as any)._fetch
135+
136+
await expect(aclLogic.findEffectiveAcl(resource)).rejects.toThrow('Cannot find effective ACL, store has no fetcher')
137+
})
25138
})

‎test/resourceLogic.test.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ describe('resourceLogic', () => {
3535
store = makeStore()
3636
aclLogic = {
3737
findAclDocUrl: vi.fn().mockResolvedValue(undefined),
38+
findEffectiveAcl: vi.fn().mockResolvedValue({ authorizations: [] }),
39+
findAccessGrants: vi.fn().mockResolvedValue([]),
40+
planGrant: vi.fn(),
41+
planRevoke: vi.fn(),
42+
planPublicRead: vi.fn(),
43+
applyPlan: vi.fn(),
3844
setACLUserPublic: vi.fn(),
3945
genACLText: vi.fn()
4046
}
@@ -103,4 +109,12 @@ describe('resourceLogic', () => {
103109
await expect(resourceLogic.recursiveDelete(resource)).resolves.toBeUndefined()
104110
expect(store.removeDocument).toHaveBeenCalledWith(resource)
105111
})
112+
113+
it('recognizes only profile-card.ttl WebIDs', () => {
114+
const resourceLogic = createResourceLogic(store, aclLogic, containerLogic, typeIndexLogic)
115+
116+
expect(resourceLogic.isWebId(sym('https://alice.example.com/profile/card.ttl#me'))).toBe(true)
117+
expect(resourceLogic.isWebId(sym('https://alice.example.com/profile/card#me'))).toBe(false)
118+
expect(resourceLogic.isWebId(sym('https://alice.example.com/profile/card.jsonld#me'))).toBe(false)
119+
})
106120
})

0 commit comments

Comments
 (0)