Skip to content

Commit 56e185e

Browse files
committed
Add support for sudo and mu-auth-scope for queries
This is an extension to how the query and update functions work. It brings these functions a bit more in line with current use and could ease the path to using scopes instead of sudo where it's possible.
1 parent c2abd73 commit 56e185e

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ mu.app.get('/', function( req, res ) {
202202
```
203203
204204
The following helper functions are provided by the template
205-
- `query(query) => Promise`: Function for sending queries to the triplestore
206-
- `update(query) => Promise`: Function for sending updates to the triplestore
205+
- `query(query, options) => Promise`: Function for sending queries to the triplestore. Options is an object which may include `sudo` and `scope` keys.
206+
- `update(query, options) => Promise`: Function for sending updates to the triplestore. Options is an object which may include `sudo` and `scope` keys.
207207
- `uuid() => string`: Generates a random UUID (e.g. to construct new resource URIs)
208208
209209
The following SPARQL escape helpers are provided to construct safe SPARQL query strings
@@ -254,6 +254,8 @@ The following environment variables can be configured:
254254
- `MAX_BODY_SIZE` (default: `100kb`): max size of the request body. See [ExpressJS documentation](https://expressjs.com/en/resources/middleware/body-parser.html#limit).
255255
- `HOST` (default: `0.0.0.0`): The hostname you want the service to bind to.
256256
- `PORT` (default: `80`): The port you want the service to bind to.
257+
- `ALLOW_MU_AUTH_SUDO`: Allow sudo queries when the service requests it.
258+
- `DEFAULT_MU_AUTH_SCOPE`: Default mu-auth-scope to use for calls.
257259
258260
259261
#### Mounting `/config`

helpers/mu/sparql.js

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,23 @@ const DEBUG_AUTH_HEADERS = env.get('DEBUG_AUTH_HEADERS').asBool();
1111
//==-- logic --==//
1212

1313
// builds a new sparqlClient
14-
function newSparqlClient() {
14+
function newSparqlClient(userOptions) {
1515
let options = { requestDefaults: { headers: { } } };
1616

17+
if (userOptions.sudo === true) {
18+
if (env.get("ALLOW_MU_AUTH_SUDO").asBool()) {
19+
options.requestDefaults.headers['mu-auth-sudo'] = "true";
20+
} else {
21+
throw "Error, sudo request but service lacks ALLOW_MU_AUTH_SUDO header";
22+
}
23+
}
24+
25+
if (userOptions.scope) {
26+
options.requestDefaults.headers['mu-auth-scope'] = userOptions.scope;
27+
} else if (process.env.DEFAULT_MU_AUTH_SCOPE) {
28+
options.requestDefaults.headers['mu-auth-scope'] = process.env.DEFAULT_MU_AUTH_SCOPE;
29+
}
30+
1731
if (httpContext.get('request')) {
1832
options.requestDefaults.headers['mu-session-id'] = httpContext.get('request').get('mu-session-id');
1933
options.requestDefaults.headers['mu-call-id'] = httpContext.get('request').get('mu-call-id');
@@ -38,24 +52,25 @@ function newSparqlClient() {
3852
}
3953

4054
// executes a query (you can use the template syntax)
41-
function query( queryString ) {
55+
function query( queryString, options ) {
4256
if (LOG_SPARQL_QUERIES) {
4357
console.log(queryString);
4458
}
45-
return executeQuery(queryString);
59+
return executeQuery(queryString, options);
4660
};
4761

4862
// executes an update query
49-
function update( queryString ) {
63+
function update( queryString, options ) {
5064
if (LOG_SPARQL_UPDATES) {
5165
console.log(queryString);
5266
}
53-
return executeQuery(queryString);
67+
return executeQuery(queryString, options);
5468
};
5569

56-
function executeQuery( queryString ) {
57-
return newSparqlClient().query(queryString).executeRaw().then(response => {
70+
function executeQuery( queryString, options ) {
71+
return newSparqlClient(options || {}).query(queryString).executeRaw().then(response => {
5872
const temp = httpContext;
73+
5974
if (httpContext.get('response') && !httpContext.get('response').headersSent) {
6075
// set mu-auth-allowed-groups on outgoing response
6176
const allowedGroups = response.headers['mu-auth-allowed-groups'];

0 commit comments

Comments
 (0)