-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAuthorization.ts
More file actions
222 lines (174 loc) · 5.56 KB
/
Authorization.ts
File metadata and controls
222 lines (174 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import {
NamedNodeAs,
NamedNodeFrom,
OptionalAs,
OptionalFrom,
SetFrom,
TermAs,
TermFrom,
TermWrapper
} from "@rdfjs/wrapper"
import { ACL, FOAF, RDF } from "../vocabulary/mod.js"
import { Group } from "./Group.js"
/**
* Represents an Authorization according to the Web Access Control specification.
*
* Authorization is the most fundamental unit of access control describing access permissions granting to agents the ability to perform operations on resources.
*
* @see https://solidproject.org/TR/wac#authorization-rule
*/
export class Authorization extends TermWrapper {
//#region Data
//#region Access Objects
/**
* Denotes the resource to which access is being granted.
*
* @see https://solidproject.org/TR/wac#acl-accessto
*/
get accessTo(): string | undefined {
return OptionalFrom.subjectPredicate(this, ACL.accessTo, NamedNodeAs.string)
}
set accessTo(value: string | undefined) {
OptionalAs.object(this, ACL.accessTo, value, NamedNodeFrom.string)
}
/**
* Denotes the container resource whose Authorization can be applied to a resource lower in the collection hierarchy.
*
* @see https://solidproject.org/TR/wac#acl-default
*/
get default(): string | undefined {
return OptionalFrom.subjectPredicate(this, ACL.default, NamedNodeAs.string)
}
set default(value: string | undefined) {
OptionalAs.object(this, ACL.default, value, NamedNodeFrom.string)
}
//#endregion
//#region Access Modes
/**
* Denotes a class of operations that the agents can perform on a resource.
*
* @see https://solidproject.org/TR/wac#acl-mode
*/
get mode(): Set<string> {
return SetFrom.subjectPredicate(this, ACL.mode, NamedNodeAs.string, NamedNodeFrom.string)
}
//#endregion
//#region Access Subjects
/**
* Denotes an agent being given the access permission.
*
* @see https://solidproject.org/TR/wac#acl-agent
*/
get agent(): Set<string> {
return SetFrom.subjectPredicate(this, ACL.agent, NamedNodeAs.string, NamedNodeFrom.string)
}
/**
* Denotes a class of agents being given the access permission.
*
* @see https://solidproject.org/TR/wac#acl-agentclass
*/
get agentClass(): Set<string> {
return SetFrom.subjectPredicate(this, ACL.agentClass, NamedNodeAs.string, NamedNodeFrom.string)
}
/**
* Denotes a group of agents being given the access permission.
*
* @see https://solidproject.org/TR/wac#acl-agentgroup
*/
get agentGroup(): Group | undefined {
return OptionalFrom.subjectPredicate(this, ACL.agentGroup, TermAs.instance(Group))
}
set agentGroup(value: Group | undefined) {
OptionalAs.object(this, ACL.agentGroup, value, TermFrom.instance)
}
/**
* Denotes the origin of an HTTP request that is being given the access permission.
*
* @see https://solidproject.org/TR/wac#acl-origin
*/
get origin(): Set<string> {
return SetFrom.subjectPredicate(this, ACL.origin, NamedNodeAs.string, NamedNodeFrom.string)
}
//#endregion
get type(): Set<string> {
return SetFrom.subjectPredicate(this, RDF.type, NamedNodeAs.string, NamedNodeFrom.string)
}
//#endregion
//#region Computed
/**
* @see https://solidproject.org/TR/wac#authorization-conformance
*/
get conforms(): boolean {
if (!this.type.has(ACL.Authorization)) {
return false
}
if (this.accessTo === undefined || this.default === undefined) {
return false
}
if (this.mode.size === 0) {
return false
}
if (this.agent.size === 0 && (this.agentGroup == undefined || this.agentGroup.hasMember.size === 0) && this.agentClass.size === 0 && this.origin.size === 0) {
return false
}
return true
}
get accessibleToAny(): boolean {
return this.agentClass.has(FOAF.Agent)
}
set accessibleToAny(value: boolean) {
this.overwrite(this.agentClass, FOAF.Agent, value)
}
get accessibleToAuthenticated(): boolean {
return this.agentClass.has(ACL.AuthenticatedAgent)
}
set accessibleToAuthenticated(value: boolean) {
this.overwrite(this.agentClass, ACL.AuthenticatedAgent, value)
}
get canRead(): boolean {
return this.mode.has(ACL.Read)
}
set canRead(value: boolean) {
this.overwrite(this.mode, ACL.Read, value)
}
get canWrite(): boolean {
return this.mode.has(ACL.Write)
}
set canWrite(value: boolean) {
this.overwrite(this.mode, ACL.Write, value)
}
get canAppend(): boolean {
return this.mode.has(ACL.Append)
}
set canAppend(value: boolean) {
this.overwrite(this.mode, ACL.Append, value)
}
get canCreate(): boolean {
return this.canWrite || this.canAppend
}
get canUpdate(): boolean {
return this.canCreate
}
get canDelete(): boolean {
return this.canWrite
}
set canDelete(value: boolean) {
this.canWrite = value
}
get canReadWriteAcl(): boolean {
return this.mode.has(ACL.Control)
}
set canReadWriteAcl(value: boolean) {
this.overwrite(this.mode, ACL.Control, value)
}
//#endregion
//#region Utilities
private overwrite(set: Set<string>, object: string, value: boolean) {
set.delete(object)
if (!value) {
return
}
set.add(object)
}
//#endregion
}