Skip to content

Commit b0c6e2c

Browse files
committed
acl classify subject
1 parent 1e5242b commit b0c6e2c

2 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎src/acl/accessControlSubjects.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

‎src/acl/aclLogic.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ 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

67
// Helpers available from @dokieli/web-access-control:
78
// - Discovery: findEffectiveACL, parentContainer
@@ -70,6 +71,10 @@ export function createAclLogic(store): AclLogic {
7071
async function applyPlan(plan: PatchPlan): Promise<Response> {
7172
return applyAuthorizationPlan(plan, { fetch: getFetch() })
7273
}
74+
75+
async function classifyAccessControlSubject(principle: string) {
76+
return accessControlSubjects.classifyAccessControlSubject(store, principle)
77+
}
7378
/**
7479
* Simple Access Control
7580
*
@@ -206,6 +211,7 @@ export function createAclLogic(store): AclLogic {
206211
planRevoke,
207212
planPublicRead,
208213
applyPlan,
214+
classifyAccessControlSubject,
209215
setACLUserPublic,
210216
genACLText
211217
}

0 commit comments

Comments
 (0)