Skip to content

Commit 215d101

Browse files
committed
Pass mu-session-id and mu-authorization-groups headers on queries
1 parent bb8c25b commit 215d101

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

helpers/mu/server.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import httpContext from 'express-http-context';
12
import express from 'express';
23
import bodyParser from 'body-parser';
34

@@ -13,6 +14,14 @@ app.use('/', function(req, res, next) {
1314
next();
1415
});
1516

17+
app.use(httpContext.middleware);
18+
19+
app.use(function(req, res, next) {
20+
httpContext.set('request', req);
21+
httpContext.set('response', res);
22+
next();
23+
});
24+
1625
const errorHandler = function(err, req, res, next) {
1726
res.status(err.status || 400);
1827
res.json({

helpers/mu/sparql.js

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,56 @@
1+
import httpContext from 'express-http-context';
12
import SC2 from 'sparql-client-2';
23
const { SparqlClient, SPARQL } = SC2;
34

45
//==-- logic --==//
56

67
// builds a new sparqlClient
78
function newSparqlClient() {
8-
return new SparqlClient( process.env.MU_SPARQL_ENDPOINT ).register({
9+
let options = {
10+
requestDefaults: {
11+
headers: {
12+
'mu-session-id': httpContext.get('request').get('mu-session-id')
13+
}
14+
}
15+
};
16+
17+
const authorizationGroups = httpContext.get('response').get('mu-authorization-groups');
18+
if (authorizationGroups)
19+
options.requestDefaults.headers['mu-authorization-groups'] = authorizationGroups;
20+
21+
console.log(`Add options on SparqlClient: ${JSON.stringify(options)}`);
22+
23+
return new SparqlClient(process.env.MU_SPARQL_ENDPOINT, options).register({
924
mu: 'http://mu.semte.ch/vocabularies/',
1025
muCore: 'http://mu.semte.ch/vocabularies/core/',
1126
muExt: 'http://mu.semte.ch/vocabularies/ext/'
1227
});
1328
}
1429

1530
// executes a query (you can use the template syntax)
16-
function query( queryString ){
31+
function query( queryString ) {
1732
console.log(queryString);
18-
return newSparqlClient().query( queryString ).execute();
33+
return newSparqlClient().queryRaw(queryString).execute().then(response => {
34+
const authorizationGroups = response.headers['mu-authorization-groups'];
35+
if (authorizationGroups) {
36+
httpContext.get('response').setHeader('mu-authorization-groups', authorizationGroups);
37+
console.log(`Set mu-authorization-groups header to ${authorizationGroups}`);
38+
} else {
39+
httpContext.get('response').removeHeader('mu-authorization-groups');
40+
console.log('Remove mu-authorization-groups header');
41+
}
42+
43+
function maybeParseJSON(body) {
44+
// Catch invalid JSON
45+
try {
46+
return JSON.parse(body);
47+
} catch (ex) {
48+
return null;
49+
}
50+
}
51+
52+
return maybeParseJSON(response.body);
53+
});
1954
};
2055

2156
// executes an update query

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"babel-preset-es2017": "^6.16.0",
2121
"body-parser": "~1.15.1",
2222
"express": "^4.14.0",
23+
"express-http-context": "~1.0.2",
2324
"sparql-client-2": "^0.6.0",
2425
"supervisor": "^0.12.0"
2526
},

0 commit comments

Comments
 (0)