@@ -5,6 +5,10 @@ const debug = require('./../debug').authentication
55const AuthRequest = require ( './auth-request' )
66
77const url = require ( 'url' )
8+ const intoStream = require ( 'into-stream' )
9+
10+ const $rdf = require ( 'rdflib' )
11+ const ACL = $rdf . Namespace ( 'http://www.w3.org/ns/auth/acl#' )
812
913/**
1014 * Models a local Login request
@@ -65,7 +69,7 @@ class ConsentRequest extends AuthRequest {
6569 // Check if is already registered or is data browser
6670 if (
6771 appOrigin === req . app . locals . ldp . serverUri ||
68- await request . isAppRegistered ( appOrigin , request . authQueryParams . web_id )
72+ await request . isAppRegistered ( req . app . locals . ldp , appOrigin , request . authQueryParams . web_id )
6973 ) {
7074 request . redirectPostConsent ( )
7175 } else {
@@ -95,9 +99,10 @@ class ConsentRequest extends AuthRequest {
9599 debug ( 'Providing consent for app sharing' )
96100
97101 if ( consented ) {
98- await request . registerApp ( appOrigin , accessModes , request . authQueryParams . web_id )
102+ await request . registerApp ( req . app . locals . ldp , appOrigin , accessModes , request . authQueryParams . web_id )
99103 }
100104
105+ console . log ( 'oh no didnt update' )
101106 // Redirect once that's all done
102107 return request . authenticator . findValidUser ( )
103108 . then ( validUser => {
@@ -113,12 +118,51 @@ class ConsentRequest extends AuthRequest {
113118 return `${ parsed . protocol } //${ parsed . host } `
114119 }
115120
116- async isAppRegistered ( appOrigin , webId ) {
117- return false
121+ async getProfileGraph ( ldp , webId ) {
122+ return await new Promise ( async ( resolve , reject ) => {
123+ const store = $rdf . graph ( )
124+ const profileText = await ldp . readResource ( webId )
125+ $rdf . parse ( profileText . toString ( ) , store , 'https://localhost:8443/profile/card' , 'text/turtle' , ( error , kb ) => {
126+ if ( error ) {
127+ reject ( error )
128+ } else {
129+ resolve ( kb )
130+ }
131+ } )
132+ } )
133+ }
134+
135+ async saveProfileGraph ( ldp , store , webId ) {
136+ const text = $rdf . serialize ( undefined , store , webId , 'text/turtle' )
137+ console . log ( text )
138+ await ldp . put ( webId , intoStream ( text ) , 'text/turtle' )
139+ }
140+
141+ async isAppRegistered ( ldp , appOrigin , webId ) {
142+ const store = await this . getProfileGraph ( ldp , webId )
143+ return store . each ( $rdf . sym ( webId ) , ACL ( 'trustedApp' ) ) . find ( ( app ) => {
144+ return store . each ( app , ACL ( 'origin' ) ) . find ( rdfAppOrigin => rdfAppOrigin . value === appOrigin )
145+ } )
118146 }
119147
120- async registerApp ( appOrigin , accessModes , webId ) {
121- return
148+ async registerApp ( ldp , appOrigin , accessModes , webId ) {
149+ const store = await this . getProfileGraph ( ldp , webId )
150+ const origin = $rdf . sym ( appOrigin )
151+ // remove existing statements on same origin - if it exists
152+ store . statementsMatching ( null , ACL ( 'origin' ) , origin ) . forEach ( st => {
153+ store . removeStatements ( [ ...store . statementsMatching ( null , ACL ( 'trustedApp' ) , st . subject ) ] )
154+ store . removeStatements ( [ ...store . statementsMatching ( st . subject ) ] )
155+ } )
156+
157+ // add new triples
158+ const application = new $rdf . BlankNode ( )
159+ store . add ( $rdf . sym ( webId ) , ACL ( 'trustedApp' ) , application , webId )
160+ store . add ( application , ACL ( 'origin' ) , origin , webId )
161+ accessModes . forEach ( mode => {
162+ store . add ( application , ACL ( 'mode' ) , ACL ( mode ) )
163+ } )
164+ console . log ( store )
165+ await this . saveProfileGraph ( ldp , store , webId )
122166 }
123167
124168 /**
0 commit comments