Skip to content

Commit aab2b85

Browse files
authored
Merge pull request #336 from SolidOS/feat/access-input
Support for the Input feature on the Manage access dialog
2 parents 1e5242b + 918c145 commit aab2b85

16 files changed

Lines changed: 1122 additions & 6 deletions

‎package-lock.json‎

Lines changed: 2 additions & 2 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 & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "solid-logic",
3-
"version": "6.0.0",
3+
"version": "6.0.0-1",
44
"description": "Core business logic of SolidOS",
55
"main": "dist/index.cjs.js",
66
"module": "dist/index.esm.js",

‎src/acl/accessControlSubjects.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import { LiveStore, sym } from 'rdflib'
2+
import { ns } from '../util/ns'
3+
4+
export type AccessControlSubjectKind = 'agent' | 'agentGroup' | 'agentClass' | 'origin'
5+
6+
export type AccessControlSubject = {
7+
kind: AccessControlSubjectKind
8+
subjectValue: string
9+
}
10+
11+
const ACCESS_CONTROL_SUBJECT_GROUPS = {
12+
agentClass: [ns.foaf('Agent'), ns.acl('AuthenticatedAgent'), ns.rdf('Resource'), ns.owl('Thing')],
13+
agent: [ns.vcard('WebID'), ns.vcard('Individual'), ns.foaf('Person'), ns.foaf('Agent')],
14+
agentGroup: [ns.vcard('Group')],
15+
origin: [ns.solid('AppProvider'), ns.solid('AppProviderClass')]
16+
} as const
17+
18+
function hasKnownTypes (types: Record<string, unknown>): boolean {
19+
return Object.keys(types).length > 0
20+
}
21+
22+
function isHttpUrl (value: string): boolean {
23+
return value.startsWith('http://') || value.startsWith('https://')
24+
}
25+
26+
function isBareOriginUrl (value: string): boolean {
27+
try {
28+
const parsed = new URL(value)
29+
return (parsed.protocol === 'http:' || parsed.protocol === 'https:') &&
30+
parsed.pathname === '/' &&
31+
!parsed.search &&
32+
!parsed.hash
33+
} catch (_error) {
34+
return false
35+
}
36+
}
37+
38+
function normalizeOriginUrl (value: string): string {
39+
try {
40+
const parsed = new URL(value)
41+
if (
42+
(parsed.protocol === 'http:' || parsed.protocol === 'https:') &&
43+
parsed.pathname === '/' &&
44+
!parsed.search &&
45+
!parsed.hash
46+
) {
47+
return `${parsed.protocol}//${parsed.host}`
48+
}
49+
} catch (_error) {
50+
// Return the original value below when parsing fails.
51+
}
52+
53+
return value
54+
}
55+
56+
export async function classifyAccessControlSubject (store: LiveStore, principle: string): Promise<AccessControlSubject | undefined> {
57+
const subject = sym(principle)
58+
let types = store.findTypeURIs(subject)
59+
60+
if (isBareOriginUrl(principle)) {
61+
return { kind: 'origin', subjectValue: normalizeOriginUrl(principle) }
62+
}
63+
64+
if (!hasKnownTypes(types) && isHttpUrl(principle)) {
65+
try {
66+
await store.fetcher.load(subject.doc())
67+
} catch (error) {
68+
console.error(`Failed to load access target ${principle}`, error)
69+
}
70+
71+
types = store.findTypeURIs(subject)
72+
}
73+
74+
if (ACCESS_CONTROL_SUBJECT_GROUPS.origin.some(term => term.uri in types)) {
75+
return { kind: 'origin', subjectValue: principle }
76+
}
77+
78+
if (ACCESS_CONTROL_SUBJECT_GROUPS.agent.some(term => term.uri in types)) {
79+
const preferredURI = store.any(subject, ns.foaf('preferredURI'))
80+
return {
81+
kind: 'agent',
82+
subjectValue: preferredURI?.value ?? principle
83+
}
84+
}
85+
86+
if (ACCESS_CONTROL_SUBJECT_GROUPS.agentGroup.some(term => term.uri in types)) {
87+
return { kind: 'agentGroup', subjectValue: principle }
88+
}
89+
90+
if (ACCESS_CONTROL_SUBJECT_GROUPS.agentClass.some(term => subject.sameTerm(term))) {
91+
return { kind: 'agentClass', subjectValue: principle }
92+
}
93+
94+
return undefined
95+
}

‎src/acl/aclLogic.ts‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import { applyPlan as applyAuthorizationPlan, findEffectiveACL, planGrant as pla
22
import { graph, NamedNode, Namespace, serialize, sym } from 'rdflib'
33
import type { AclLogic } from '../types'
44
import { ns as namespace } from '../util/ns'
5+
import * as accessControlSubjects from './accessControlSubjects'
56

7+
// Will remove the below comment when i'm done with the manage access work
68
// Helpers available from @dokieli/web-access-control:
79
// - Discovery: findEffectiveACL, parentContainer
810
// - ACL context and parsing: buildACLContext, authorizationsFromDataset, parseTurtle
@@ -12,6 +14,9 @@ import { ns as namespace } from '../util/ns'
1214
// - Link and allow parsing: allows, parseWacAllow, linkTargets, parseLinkHeader
1315
// - Terms and constants: ACCESS_MODES, ACL, Authenticated, FOAF, modeFromIRI, modeIRI, namedNode, Public, quad, RDF_TYPE, variable
1416

17+
export const ACCESS_ROLES = ['Owner', 'Editor', 'No Access', 'Viewer', 'Poster', 'Submitter'] as const
18+
19+
export type AccessRole = typeof ACCESS_ROLES[number]
1520

1621
export const ACL_LINK = sym(
1722
'http://www.iana.org/assignments/link-relations/acl'
@@ -20,6 +25,25 @@ export const ACL_LINK = sym(
2025
export function createAclLogic(store): AclLogic {
2126

2227
const ns = namespace
28+
const ACCESS_ROLE_RULES: Array<{ label: AccessRole, modes: AccessMode[] }> = [
29+
{ label: 'Owner', modes: ['Read', 'Write', 'Control'] },
30+
{ label: 'Editor', modes: ['Read', 'Write'] },
31+
{ label: 'Poster', modes: ['Append', 'Read'] },
32+
{ label: 'Submitter', modes: ['Append'] },
33+
{ label: 'Viewer', modes: ['Read'] },
34+
{ label: 'No Access', modes: [] }
35+
]
36+
37+
function roleFromModes (modes: Iterable<AccessMode>): AccessRole {
38+
const modeSet = new Set(modes)
39+
const matchingRule = ACCESS_ROLE_RULES.find(rule => rule.label !== 'No Access' && rule.modes.every(mode => modeSet.has(mode)))
40+
41+
return matchingRule?.label ?? 'No Access'
42+
}
43+
44+
function modesFromRole (role: AccessRole): AccessMode[] {
45+
return [...(ACCESS_ROLE_RULES.find(rule => rule.label === role)?.modes ?? [])]
46+
}
2347

2448
function getFetch() {
2549
const fetcher = store.fetcher as {
@@ -70,6 +94,10 @@ export function createAclLogic(store): AclLogic {
7094
async function applyPlan(plan: PatchPlan): Promise<Response> {
7195
return applyAuthorizationPlan(plan, { fetch: getFetch() })
7296
}
97+
98+
async function classifyAccessControlSubject(principle: string) {
99+
return accessControlSubjects.classifyAccessControlSubject(store, principle)
100+
}
73101
/**
74102
* Simple Access Control
75103
*
@@ -202,10 +230,13 @@ export function createAclLogic(store): AclLogic {
202230
findAclDocUrl,
203231
findEffectiveAcl,
204232
findAccessGrants,
233+
roleFromModes,
234+
modesFromRole,
205235
planGrant,
206236
planRevoke,
207237
planPublicRead,
208238
applyPlan,
239+
classifyAccessControlSubject,
209240
setACLUserPublic,
210241
genACLText
211242
}

‎src/directory/catalogDirectory.ts‎

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { type DirectoryEntry } from '../types'
2+
import { graph, type LiveStore, NamedNode, parse } from 'rdflib'
3+
import { ns } from '../util/ns'
4+
import * as debug from '../util/debug'
5+
import { isHttpUri } from './directoryHelpers'
6+
7+
const CATALOG_VOCAB = 'http://example.org#'
8+
9+
export function createCatalogDirectoryDiscovery () {
10+
const catalogEntriesCache = new Map<string, Promise<DirectoryEntry[]>>()
11+
12+
async function discoverCatalogEntries (
13+
catalogUrl: string,
14+
onEntry: (entry: DirectoryEntry) => void | Promise<void>
15+
): Promise<void> {
16+
const entries = await fetchCatalogEntries(catalogUrl)
17+
for (const entry of entries) {
18+
await onEntry(entry)
19+
}
20+
}
21+
22+
async function fetchCatalogEntries (catalogUrl: string): Promise<DirectoryEntry[]> {
23+
const cached = catalogEntriesCache.get(catalogUrl)
24+
if (cached) {
25+
return cached
26+
}
27+
28+
const promise = loadCatalogEntries(catalogUrl).catch(error => {
29+
catalogEntriesCache.delete(catalogUrl)
30+
throw error
31+
})
32+
33+
catalogEntriesCache.set(catalogUrl, promise)
34+
try {
35+
return await promise
36+
} catch (error) {
37+
debug.warn('[Directory] Error fetching directory catalog:', error)
38+
return []
39+
}
40+
}
41+
42+
async function loadCatalogEntries (catalogUrl: string): Promise<DirectoryEntry[]> {
43+
if (typeof fetch !== 'function') {
44+
throw new Error('Fetch API is unavailable')
45+
}
46+
47+
const response = await fetch(catalogUrl, {
48+
headers: { accept: 'text/turtle' }
49+
})
50+
51+
if (!response.ok) {
52+
throw new Error(`Failed to fetch ${catalogUrl}: ${response.status}`)
53+
}
54+
55+
const turtle = await response.text()
56+
const catalogStore = graph() as LiveStore
57+
parse(turtle, catalogStore, catalogUrl, 'text/turtle')
58+
59+
const personType = new NamedNode(`${CATALOG_VOCAB}Person`)
60+
const webIdPredicate = new NamedNode(`${CATALOG_VOCAB}webid`)
61+
const namePredicate = new NamedNode(`${CATALOG_VOCAB}name`)
62+
const catalogPeople = new Map<string, DirectoryEntry>()
63+
64+
const statements = catalogStore.statementsMatching(undefined, ns.rdf('type'), personType)
65+
for (const statement of statements) {
66+
const subject = statement.subject
67+
const webIdNode = catalogStore.any(subject, webIdPredicate)
68+
if (webIdNode && webIdNode.termType === 'NamedNode') {
69+
const webId = webIdNode.value
70+
const personName = catalogStore.anyValue(subject, namePredicate)
71+
if (isHttpUri(webId) && personName) {
72+
catalogPeople.set(webId, {
73+
kind: 'person',
74+
uri: webId,
75+
label: personName,
76+
subjectType: 'agent',
77+
relationshipLabel: 'People',
78+
sources: ['catalog']
79+
})
80+
}
81+
}
82+
}
83+
84+
return Array.from(catalogPeople.values())
85+
}
86+
87+
return {
88+
discoverCatalogEntries
89+
}
90+
}

0 commit comments

Comments
 (0)