@@ -20,19 +20,43 @@ function publisherTrustedApp (kb, doc, aclDoc, modesRequired, origin, docAuths)
20
20
// modesRequired.every(mode => appAuths.some(auth => kb.holds(auth, ACL('mode'), mode, aclDoc)))
21
21
}
22
22
23
+ /* Function checkAccess
24
+ ** @param kb A quadstore
25
+ ** @param doc the resource (A named node) or directory for which ACL applies
26
+ */
23
27
function checkAccess ( kb , doc , directory , aclDoc , agent , modesRequired , origin , trustedOrigins ) {
24
- var auths = kb . each ( null , ACL ( 'accessTo' ) , doc , aclDoc )
25
- console . log ( `checkAccess: checking access to ${ doc } by ${ agent } ` )
26
- if ( auths . length ) console . log ( ` ${ auths . length } authentications apply directly to doc` )
27
- if ( directory ) {
28
- auths = auths . concat ( null , ( ACL ( 'defaultForNew' ) , directory ) ) // Deprecated but keep for ages
29
- auths = auths . concat ( null , ( ACL ( 'default' ) , directory ) )
30
- if ( auths . length ) console . log ( ` ${ auths . length } total relevant authentications` )
28
+ let modeURIs = modesAllowed ( kb , doc , directory , aclDoc , agent , origin , trustedOrigins )
29
+ let ok = true
30
+ console . log ( `CheckAccess: modeURIs: ${ modeURIs . size } ` )
31
+ modesRequired . forEach ( mode => {
32
+ console . log ( ` checking ` + mode )
33
+ if ( modeURIs . has ( mode . uri ) ) {
34
+ console . log ( ' Mode required and allowed:' + mode )
35
+ } else if ( mode . sameTerm ( ACL ( 'Append' ) ) && modeURIs . has ( ACL ( 'Write' ) . uri ) ) {
36
+ console . log ( ' Append required and Write allowed. OK' )
37
+ } else {
38
+ console . log ( ' MODE REQUIRED NOT ALLOWED:' + mode )
39
+ ok = false
40
+ }
41
+ } )
42
+ return ok
43
+ }
44
+
45
+ function modesAllowed ( kb , doc , directory , aclDoc , agent , modesRequired , origin , trustedOrigins ) {
46
+ console . log ( `modesAllowed: checking access to ${ doc } by ${ agent } ` )
47
+ var auths
48
+ if ( ! directory ) { // Normal case, ACL for a file
49
+ auths = kb . each ( null , ACL ( 'accessTo' ) , doc , aclDoc )
50
+ console . log ( ` ${ auths . length } direct authentications about ${ doc } ` )
51
+ } else {
52
+ auths = kb . each ( null , ACL ( 'default' ) , directory , null )
53
+ auths = auths . concat ( kb . each ( null , ACL ( 'defaultForNew' ) , directory , null ) ) // Deprecated but keep for ages
54
+ console . log ( ` ${ auths . length } default authentications about ${ directory } in ${ aclDoc } ` )
31
55
}
32
56
if ( origin && trustedOrigins && trustedOrigins . includes ( origin ) ) {
33
57
console . log ( 'Origin ' + origin + ' is trusted' )
34
58
origin = null // stop worrying about origin
35
- console . log ( ` checkAccess : Origin ${ origin } is trusted.` )
59
+ console . log ( ` modesAllowed : Origin ${ origin } is trusted.` )
36
60
}
37
61
function agentOrGroupOK ( auth , agent ) {
38
62
console . log ( ` Checking auth ${ auth } with agent ${ agent } ` )
@@ -64,37 +88,37 @@ function checkAccess (kb, doc, directory, aclDoc, agent, modesRequired, origin,
64
88
function originOK ( auth , origin ) {
65
89
return kb . holds ( auth , ACL ( 'origin' ) , origin , aclDoc )
66
90
}
67
- let allowed = modesRequired . every ( mode => {
68
- console . log ( ' Checking needed mode ' + mode )
69
- let modeAuths = auths . filter ( auth => kb . holds ( auth , ACL ( 'mode' ) , mode , aclDoc ) )
70
- if ( mode . sameTerm ( ACL ( 'Append' ) ) ) { // If you want append, Write will work too.
71
- let writeAuths = auths . filter ( auth => kb . holds ( auth , ACL ( 'mode' ) , ACL ( 'Write' ) , aclDoc ) )
72
- console . log ( ` Authorizations that work with Write: ${ writeAuths . length } ` )
73
- modeAuths = modeAuths . concat ( writeAuths )
91
+
92
+ function agentAndAppOK ( auth ) {
93
+ if ( ! agentOrGroupOK ( auth , agent ) ) {
94
+ console . log ( ' The agent/group/public check fails' )
95
+ return false
74
96
}
75
- console . log ( ` Authorizations that work with mode: ${ modeAuths . length } ` )
76
- let modeResult = modeAuths . some ( auth => {
77
- if ( ! agentOrGroupOK ( auth , agent ) ) {
78
- console . log ( ' The agent/group/public check fails' )
79
- return false
80
- }
81
- if ( ! origin ) {
82
- console . log ( ' Origin check not needed: no origin.' )
83
- return true
84
- }
85
- if ( originOK ( auth , origin ) ) {
86
- console . log ( ' Origin check succeeded.' )
87
- return true
88
- }
89
- console . log ( ' Origin check FAILED. Origin not tested.' )
97
+ if ( ! origin ) {
98
+ console . log ( ' Origin check not needed: no origin.' )
90
99
return true
100
+ }
101
+ if ( originOK ( auth , origin ) ) {
102
+ console . log ( ' Origin check succeeded.' )
103
+ return true
104
+ }
105
+ console . log ( ' Origin check FAILED. Origin not trusted.' )
106
+ return false // @@ look for other trusted apps
107
+ }
108
+
109
+ auths = auths . filter ( agentAndAppOK )
110
+ console . log ( ' auths with good who and what: ' + auths . length )
111
+ var modeURIs = new Set ( )
112
+ auths . forEach ( auth => {
113
+ let modes = kb . each ( auth , ACL ( 'mode' ) , null , aclDoc )
114
+ modes . forEach ( mode => {
115
+ console . log ( ' Mode allowed: ' + mode )
116
+ modeURIs . add ( mode . uri )
91
117
} )
92
- console . log ( ' Mode result ' + modeResult )
93
- return modeResult
94
118
} )
95
- console . log ( 'Overall result:' + allowed )
96
- return allowed
119
+ return modeURIs
97
120
}
98
121
99
122
module . exports . checkAccess = checkAccess
123
+ module . exports . modesAllowed = modesAllowed
100
124
module . exports . publisherTrustedApp = publisherTrustedApp
0 commit comments