Skip to content

Commit be0edb5

Browse files
update to terminus client libs
1 parent 7e17981 commit be0edb5

16 files changed

+8673
-1168
lines changed

lib/connectionCapabilities.js

Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
const UTILS = require('./utils.js');
2-
const CONST = require('./const.js');
3-
const ErrorMessage = require('./errorMessage');
4-
/*
1+
/**
2+
* @file Connection Capabilities
3+
* @license Apache Version 2
4+
* @summary Object which helps manage the capabilities available for a given TerminusDB connection
55
* Creates an entry in the connection registry for the server
66
* and all the databases that the client has access to
77
* maps the input authorties to a per-db array for internal storage and easy
88
* access control checks
9-
* {doc:dbid => {terminus:authority =>
10-
* [terminus:woql_select, terminus:create_document, auth3, ...]}}
11-
*
129
*/
10+
const CONST = require('./const.js');
11+
const ErrorMessage = require('./errorMessage');
1312

13+
/**
14+
* @constructor
15+
* @param {connectionConfig} connectionConfig connectionConfig object containing the connection parameters
16+
* @param {String} key API key
17+
*/
1418
function ConnectionCapabilities(connectionConfig, key) {
1519
this.connection = {};
1620
this.connectionConfig = connectionConfig;
@@ -19,6 +23,10 @@ function ConnectionCapabilities(connectionConfig, key) {
1923
}
2024
}
2125

26+
/**
27+
* @summary Retrieves the API key for the given TerminusDB Server
28+
* @param {String} serverURL optional - URL of TerminusDB - if ommited the current server URL is used
29+
*/
2230
ConnectionCapabilities.prototype.getClientKey = function (serverURL) {
2331
if (!serverURL) serverURL = this.connectionConfig.serverURL();
2432
if (serverURL && this.connection[serverURL]) return this.connection[serverURL].key;
@@ -29,6 +37,12 @@ ConnectionCapabilities.prototype.getClientKey = function (serverURL) {
2937
/*
3038
* Utility functions for changing the state of connections with Terminus servers
3139
*/
40+
41+
/**
42+
* @summary sets the api key for the given url
43+
* @param {String} curl a valid terminusDB server URL
44+
* @param {String} key an optional API key
45+
*/
3246
ConnectionCapabilities.prototype.setClientKey = function (curl, key) {
3347
if (typeof key === 'string' && key.trim()) {
3448
if (typeof this.connection[curl] === 'undefined') {
@@ -40,8 +54,8 @@ ConnectionCapabilities.prototype.setClientKey = function (curl, key) {
4054

4155

4256
/**
43-
* @params {string} curl a valid terminusDB server URL
44-
* @params {object} capabilities it is the connect call response
57+
* @param {string} curl a valid terminusDB server URL
58+
* @param {object} capabilities the JSON object returned by the connect API call
4559
*/
4660
ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
4761
if (typeof this.connection[curl] === 'undefined') {
@@ -59,9 +73,8 @@ ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
5973
let scope = auths[i]['terminus:authority_scope'];
6074
const actions = auths[i]['terminus:action'];
6175

62-
if (Array.isArray(scope) === false) scope = [scope];
63-
const actionsArr = UTILS.authorityActionsToArr(actions);
64-
76+
if (Array.isArray(scope) === false) scope = [scope];
77+
const actionsArr = ((Array.isArray(actions)) ? actions.map(item => item['@id']) : [] );
6578
for (let j = 0; j < scope.length; j += 1) {
6679
const nrec = scope[j];
6780
if (typeof this.connection[curl][nrec['@id']] === 'undefined') {
@@ -76,34 +89,20 @@ ConnectionCapabilities.prototype.addConnection = function (curl, capabilities) {
7689
}
7790
};
7891

92+
/**
93+
* @summary returns true if the client is currently connected to a server
94+
* @returns {Boolean}
95+
*/
7996
ConnectionCapabilities.prototype.serverConnected = function () {
8097
return typeof this.connection[this.connectionConfig.server] !== 'undefined';
8198
};
8299

83-
ConnectionCapabilities.prototype.capabilitiesPermit = function (action, dbid, server) {
84-
if (!this.connectionConfig.connectionMode()
85-
|| this.connectionConfig.client_checks_capabilities !== true
86-
|| action === CONST.CONNECT) { return true; }
87-
88-
server = server || this.connectionConfig.server;
89-
dbid = dbid || this.connectionConfig.dbid;
90-
91-
let rec;
92-
93-
if (action === CONST.CREATE_DATABASE) {
94-
rec = this.getServerRecord(server);
95-
} else if (dbid) rec = this.getDBRecord(dbid);
96-
else console.log('no dbid', server, dbid);
97-
if (rec) {
98-
const auths = rec['terminus:authority'];
99-
if (auths && auths.indexOf(`terminus:${action}`) !== -1) return true;
100-
} else {
101-
console.log('problem with ', this.connection, action, server, dbid);
102-
}
103-
this.error = ErrorMessage.accessDenied(action, dbid, server);
104-
return false;
105-
};
106100

101+
/**
102+
* @summary retrieves the meta-data record returned by connect for a particular server
103+
* @param {String} [srvr] optional server URL - if omitted current connection config will be used
104+
* @returns {terminus:Server} JSON server record as returned by WOQLClient.connect
105+
*/
107106
ConnectionCapabilities.prototype.getServerRecord = function (srvr) {
108107
const url = (srvr || this.connectionConfig.server);
109108
const connectionObj = this.connection[url] || {};
@@ -117,6 +116,12 @@ ConnectionCapabilities.prototype.getServerRecord = function (srvr) {
117116
return false;
118117
};
119118

119+
/**
120+
* @summary retrieves the meta-data record returned by connect for a particular database
121+
* @param {String} [dbid] optional database id - if omitted current connection config db will be used
122+
* @param {String} [srvr] optional server URL - if omitted current connection config server will be used
123+
* @returns {terminus:Database} terminus:Database JSON document as returned by WOQLClient.connect
124+
*/
120125
ConnectionCapabilities.prototype.getDBRecord = function (dbid, srvr) {
121126
const url = (srvr || this.connectionConfig.server);
122127
dbid = (dbid || this.connectionConfig.dbid);
@@ -132,6 +137,11 @@ ConnectionCapabilities.prototype.getDBRecord = function (dbid, srvr) {
132137
return undefined;
133138
};
134139

140+
/**
141+
* @summary retrieves all the db meta-data records returned by connect for a particular server
142+
* @param {String} [srvr] optional server URL - if omitted current connection config server will be used
143+
* @returns {[terminus:Database]} array of terminus:Database JSON documents as returned by WOQLClient.connect
144+
*/
135145
ConnectionCapabilities.prototype.getServerDBRecords = function (srvr) {
136146
const url = (srvr || this.connectionConfig.server);
137147
const dbrecs = {};
@@ -145,8 +155,11 @@ ConnectionCapabilities.prototype.getServerDBRecords = function (srvr) {
145155
return dbrecs;
146156
};
147157

148-
/*
149-
* removes a database record from the connection registry (after deletion, for example)
158+
/**
159+
* @summary removes a database record from the connection registry (after deletion, for example)
160+
* @param {String} [dbid] optional DB ID - if omitted current connection config db will be used
161+
* @param {String} [srvr] optional server URL - if omitted current connection config server will be used
162+
* @returns {[terminus:Database]} array of terminus:Database JSON documents as returned by WOQLClient.connect
150163
*/
151164
ConnectionCapabilities.prototype.removeDB = function (dbid, srvr) {
152165
dbid = dbid || this.connectionConfig.dbid;
@@ -162,24 +175,41 @@ ConnectionCapabilities.prototype.removeDB = function (dbid, srvr) {
162175
}
163176
};
164177

165-
178+
/**
179+
* @param {String} dbid local id of database
180+
* @returns {String} the id of the terminus:Document describing the DB
181+
* @summary Generates the ID of the terminus:Database document from the database ID
182+
*/
166183
ConnectionCapabilities.prototype.dbCapabilityID = function (dbid) {
167184
return `doc:${dbid}`;
168185
};
169186

170-
ConnectionCapabilities.prototype.getDBRecord = function (dbid, srvr) {
171-
const url = srvr || this.connectionConfig.server;
187+
/**
188+
* @param {String} action - the action that will be carried out
189+
* @param {String} [dbid] optional DB ID - if omitted current connection config db will be used
190+
* @param {String} [srvr] optional server URL - if omitted current connection config server will be used
191+
* @returns {Boolean} true if the client's capabilities allow the action on the given server / db
192+
* @summary supports client side access control checks (in addition to server side)
193+
*/
194+
ConnectionCapabilities.prototype.capabilitiesPermit = function (action, dbid, server) {
195+
server = server || this.connectionConfig.server;
172196
dbid = dbid || this.connectionConfig.dbid;
173-
if (typeof this.connection[url] !== 'object') {
174-
return false;
175-
}
176-
if (typeof this.connection[url][dbid] !== 'undefined') { return this.connection[url][dbid]; }
177-
178-
dbid = this.dbCapabilityID(dbid);
179197

180-
if (typeof this.connection[url][dbid] !== 'undefined') { return this.connection[url][dbid]; }
198+
let rec;
181199

182-
return undefined;
200+
if (action === CONST.CREATE_DATABASE) {
201+
rec = this.getServerRecord(server);
202+
} else if (dbid) rec = this.getDBRecord(dbid);
203+
else console.log('no dbid provided in capabilities check ', server, dbid);
204+
if (rec) {
205+
const auths = rec['terminus:authority'];
206+
if (auths && auths.indexOf(`terminus:${action}`) !== -1) return true;
207+
} else {
208+
console.log('No record found for connection: ', this.connection, action, server, dbid);
209+
}
210+
this.error = ErrorMessage.accessDenied(action, dbid, server);
211+
return false;
183212
};
184213

214+
185215
module.exports = ConnectionCapabilities;

lib/connectionConfig.js

Lines changed: 71 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
1+
/**
2+
* @file Terminus DB connection configuration
3+
* @license Apache Version 2
4+
* @summary Object representing the state of a connection to a terminus db - server url, current dbid, document id
5+
* along with some configuration information (key, connected mode, client_side_access_control)
6+
* provides methods for getting and setting connection parameters
7+
*/
28
const IDParser = require('./terminusIDParser');
39

410
function ConnectionConfig(params) {
@@ -14,63 +20,12 @@ function ConnectionConfig(params) {
1420
* tells the client to first connect to the server before invoking other services
1521
*/
1622

17-
this.connected_mode = newParams.connected_mode || 'connected';
1823
// include a terminus:user_key API key in the calls
1924
this.include_key = newParams.include_key || true;
20-
// client side checking of access control (in addition to server-side access control)
21-
this.client_checks_capabilities = newParams.client_checks_capabilities || true;
2225
}
2326

24-
ConnectionConfig.prototype.serverURL = function () {
25-
return this.server;
26-
};
27-
28-
29-
ConnectionConfig.prototype.connectionMode = function () {
30-
return this.connected_mode === 'connected';
31-
};
32-
33-
ConnectionConfig.prototype.includeKey = function () {
34-
return this.include_key === true;
35-
};
36-
37-
ConnectionConfig.prototype.schemaURL = function () {
38-
return `${this.dbURL()}/schema`;
39-
};
40-
ConnectionConfig.prototype.queryURL = function () {
41-
return `${this.dbURL()}/woql`;
42-
};
43-
ConnectionConfig.prototype.frameURL = function () {
44-
return `${this.dbURL()}/frame`;
45-
};
46-
ConnectionConfig.prototype.docURL = function () {
47-
return `${this.dbURL()}/document/${this.docid ? this.docid : ''}`;
48-
};
49-
50-
ConnectionConfig.prototype.dbURL = function (call) {
51-
// url swizzling to talk to platform using server/dbid/platform/ pattern..
52-
if (this.platformEndpoint()) {
53-
const baseURL = this.server.substring(0, this.server.lastIndexOf('/platform/'));
54-
55-
if (!call || call !== 'create') {
56-
return `${baseURL}/${this.dbid}/platform`;
57-
} if (call === 'platform') {
58-
return `${baseURL}/${this.dbid}`;
59-
}
60-
}
61-
return this.server + this.dbid;
62-
};
63-
64-
ConnectionConfig.prototype.platformEndpoint = function () {
65-
if (this.server && this.server.lastIndexOf('/platform/') === this.server.length - 10) {
66-
return true;
67-
}
68-
return false;
69-
};
70-
7127
/**
72-
* Utility functions for setting and parsing urls and determining
73-
* the current server, database and document
28+
* Server URL will always end in a / character - automatically appended if missing
7429
*/
7530
ConnectionConfig.prototype.setServer = function (inputStr, context) {
7631
const parser = new IDParser(context);
@@ -83,6 +38,15 @@ ConnectionConfig.prototype.setServer = function (inputStr, context) {
8338
return false;
8439
};
8540

41+
42+
ConnectionConfig.prototype.serverURL = function () {
43+
return this.server;
44+
};
45+
46+
/**
47+
* Database URL does not have a trailing slash "/" character and it will be chopped if it is passed in at the end of the input string
48+
* @param {String} inputStr - URL of DB or local id of DB
49+
*/
8650
ConnectionConfig.prototype.setDB = function (inputStr, context) {
8751
const parser = new IDParser(context);
8852
if (parser.parseDBID(inputStr)) {
@@ -94,8 +58,14 @@ ConnectionConfig.prototype.setDB = function (inputStr, context) {
9458
return false;
9559
};
9660

97-
// @param {string} inputStr TerminusDB server URL or a valid TerminusDB Id or omitted
61+
ConnectionConfig.prototype.dbURL = function () {
62+
return this.server + this.dbid;
63+
};
64+
9865

66+
/**
67+
* @summary sets the connection to point at the schema api URL
68+
*/
9969
ConnectionConfig.prototype.setSchemaURL = function (inputStr, context) {
10070
const parser = new IDParser(context);
10171
if (parser.parseSchemaURL(inputStr)) {
@@ -107,17 +77,15 @@ ConnectionConfig.prototype.setSchemaURL = function (inputStr, context) {
10777
return false;
10878
};
10979

110-
ConnectionConfig.prototype.setDocument = function (inputStr, context) {
111-
const parser = new IDParser(context);
112-
if (parser.parseDocumentURL(inputStr)) {
113-
if (parser.server()) this.server = parser.server();
114-
if (parser.dbid()) this.dbid = parser.dbid();
115-
if (parser.docid()) this.docid = parser.docid();
116-
return true;
117-
}
118-
return false;
80+
ConnectionConfig.prototype.schemaURL = function () {
81+
return `${this.dbURL()}/schema`;
11982
};
12083

84+
/**
85+
* @param {String} inputStr - the URL of a Terminus DB base install or schema endpoint
86+
* @param {Object} [context] optional json-ld context
87+
* @summary sets the connection to point at the query api URL
88+
*/
12189
ConnectionConfig.prototype.setQueryURL = function (inputStr, context) {
12290
const parser = new IDParser(context);
12391
if (parser.parseQueryURL(inputStr)) {
@@ -129,6 +97,15 @@ ConnectionConfig.prototype.setQueryURL = function (inputStr, context) {
12997
return false;
13098
};
13199

100+
ConnectionConfig.prototype.queryURL = function () {
101+
return `${this.dbURL()}/woql`;
102+
};
103+
104+
/**
105+
* @param {String} inputStr - the URL of a Terminus DB base install or frame endpoint
106+
* @param {Object} [context] optional json-ld context
107+
* @summary sets the connection to point at the class frame api URL
108+
*/
132109
ConnectionConfig.prototype.setClassFrameURL = function (inputStr, context) {
133110
const parser = new IDParser(context);
134111
if (parser.parseClassFrameURL(inputStr)) {
@@ -140,4 +117,35 @@ ConnectionConfig.prototype.setClassFrameURL = function (inputStr, context) {
140117
return false;
141118
};
142119

120+
ConnectionConfig.prototype.frameURL = function () {
121+
return `${this.dbURL()}/frame`;
122+
};
123+
124+
/**
125+
* @param {String} inputStr - the URL of a Terminus DB base install or document endpoint
126+
* @param {Object} [context] optional json-ld context
127+
* @summary sets the connection to point at the document api URL
128+
*/
129+
ConnectionConfig.prototype.setDocument = function (inputStr, context) {
130+
const parser = new IDParser(context);
131+
if (parser.parseDocumentURL(inputStr)) {
132+
if (parser.server()) this.server = parser.server();
133+
if (parser.dbid()) this.dbid = parser.dbid();
134+
if (parser.docid()) this.docid = parser.docid();
135+
return true;
136+
}
137+
return false;
138+
};
139+
140+
ConnectionConfig.prototype.docURL = function () {
141+
return `${this.dbURL()}/document/${this.docid ? this.docid : ''}`;
142+
};
143+
144+
/**
145+
* will a key be used in this connnection?
146+
*/
147+
ConnectionConfig.prototype.includeKey = function () {
148+
return this.include_key === true;
149+
};
150+
143151
module.exports = ConnectionConfig;

0 commit comments

Comments
 (0)