|
| 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 | + group: [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.pathname === '/' && !parsed.hash |
| 30 | + } catch (_error) { |
| 31 | + return false |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +function normalizeOriginUrl (value: string): string { |
| 36 | + try { |
| 37 | + const parsed = new URL(value) |
| 38 | + if (parsed.pathname === '/' && !parsed.hash) { |
| 39 | + return `${parsed.protocol}//${parsed.host}` |
| 40 | + } |
| 41 | + } catch (_error) { |
| 42 | + // Return the original value below when parsing fails. |
| 43 | + } |
| 44 | + |
| 45 | + return value |
| 46 | +} |
| 47 | + |
| 48 | +export async function classifyAccessControlSubject (store: LiveStore, principle: string): Promise<AccessControlSubject | undefined> { |
| 49 | + const subject = sym(principle) |
| 50 | + let types = store.findTypeURIs(subject) |
| 51 | + |
| 52 | + if (isBareOriginUrl(principle)) { |
| 53 | + return { kind: 'origin', subjectValue: normalizeOriginUrl(principle) } |
| 54 | + } |
| 55 | + |
| 56 | + if (!hasKnownTypes(types) && isHttpUrl(principle)) { |
| 57 | + try { |
| 58 | + await store.fetcher.load(subject.doc()) |
| 59 | + } catch (error) { |
| 60 | + console.error(`Failed to load access target ${principle}`, error) |
| 61 | + } |
| 62 | + |
| 63 | + types = store.findTypeURIs(subject) |
| 64 | + } |
| 65 | + |
| 66 | + if (ACCESS_CONTROL_SUBJECT_GROUPS.origin.some(term => term.uri in types)) { |
| 67 | + return { kind: 'origin', subjectValue: principle } |
| 68 | + } |
| 69 | + |
| 70 | + if (ACCESS_CONTROL_SUBJECT_GROUPS.agent.some(term => term.uri in types)) { |
| 71 | + const preferredURI = store.any(subject, ns.foaf('preferredURI')) |
| 72 | + return { |
| 73 | + kind: 'agent', |
| 74 | + subjectValue: preferredURI?.value ?? principle |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (ACCESS_CONTROL_SUBJECT_GROUPS.group.some(term => term.uri in types)) { |
| 79 | + return { kind: 'agentGroup', subjectValue: principle } |
| 80 | + } |
| 81 | + |
| 82 | + if (ACCESS_CONTROL_SUBJECT_GROUPS.agentClass.some(term => subject.sameTerm(term))) { |
| 83 | + return { kind: 'agentClass', subjectValue: principle } |
| 84 | + } |
| 85 | + |
| 86 | + return undefined |
| 87 | +} |
0 commit comments