Skip to content

Commit a1955c9

Browse files
committed
Consent screen data properly saves
1 parent ba11592 commit a1955c9

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

default-views/auth/consent.hbs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@
1919

2020
<form method="post" action="/consent">
2121

22-
<input id="read" type="checkbox" name="access_mode" value="read" checked>
22+
<input id="read" type="checkbox" name="access_mode" value="Read" checked>
2323
<label for="read">Read your data</label>
2424
<br>
2525

26-
<input id="write" type="checkbox" name="access_mode" value="write" checked>
26+
<input id="write" type="checkbox" name="access_mode" value="Write" checked>
2727
<label for="write">Write new data</label>
2828
<br>
2929

30-
<input id="append" type="checkbox" name="access_mode" value="append" checked>
30+
<input id="append" type="checkbox" name="access_mode" value="Append" checked>
3131
<label for="append">Add to existing data</label>
3232
<br>
3333

34-
<input id="control" type="checkbox" name="access_mode" value="control">
34+
<input id="control" type="checkbox" name="access_mode" value="Control">
3535
<label for="control">Control who can access your data</label>
3636
<br>
3737
<br>

lib/requests/consent-request.js

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ const debug = require('./../debug').authentication
55
const AuthRequest = require('./auth-request')
66

77
const 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

Comments
 (0)