@@ -20,19 +20,43 @@ function publisherTrustedApp (kb, doc, aclDoc, modesRequired, origin, docAuths)
2020 // modesRequired.every(mode => appAuths.some(auth => kb.holds(auth, ACL('mode'), mode, aclDoc)))
2121}
2222
23+ /* Function checkAccess
24+ ** @param kb A quadstore
25+ ** @param doc the resource (A named node) or directory for which ACL applies
26+ */
2327function 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 } ` )
3155 }
3256 if ( origin && trustedOrigins && trustedOrigins . includes ( origin ) ) {
3357 console . log ( 'Origin ' + origin + ' is trusted' )
3458 origin = null // stop worrying about origin
35- console . log ( ` checkAccess : Origin ${ origin } is trusted.` )
59+ console . log ( ` modesAllowed : Origin ${ origin } is trusted.` )
3660 }
3761 function agentOrGroupOK ( auth , agent ) {
3862 console . log ( ` Checking auth ${ auth } with agent ${ agent } ` )
@@ -64,37 +88,37 @@ function checkAccess (kb, doc, directory, aclDoc, agent, modesRequired, origin,
6488 function originOK ( auth , origin ) {
6589 return kb . holds ( auth , ACL ( 'origin' ) , origin , aclDoc )
6690 }
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
7496 }
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.' )
9099 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 )
91117 } )
92- console . log ( ' Mode result ' + modeResult )
93- return modeResult
94118 } )
95- console . log ( 'Overall result:' + allowed )
96- return allowed
119+ return modeURIs
97120}
98121
99122module . exports . checkAccess = checkAccess
123+ module . exports . modesAllowed = modesAllowed
100124module . exports . publisherTrustedApp = publisherTrustedApp
0 commit comments