Skip to content

Commit 2c3b520

Browse files
committed
review the access control documentation
1 parent f7ab71b commit 2c3b520

File tree

3 files changed

+67
-34
lines changed

3 files changed

+67
-34
lines changed

docs/_sidebar.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@
162162
- [first_commit](api/woqlLibrary.js?id=first_commit)
163163
- [AccessControl](api/accessControl.js?id=AccessControl)
164164
- [getDefaultOrganization](api/accessControl.js?id=getDefaultOrganization)
165-
- [getJwtToken](api/accessControl.js?id=getJwtToken)
166165
- [setJwtToken](api/accessControl.js?id=setJwtToken)
166+
- [setApiToken](api/accessControl.js?id=setApiToken)
167167
- [getAPIUrl](api/accessControl.js?id=getAPIUrl)
168168
- [getAccessRoles](api/accessControl.js?id=getAccessRoles)
169169
- [createOrganization](api/accessControl.js?id=createOrganization)

docs/api/accessControl.js.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,30 @@
55

66
#### new AccessControl()
77
The AccessControl object has various methods to control the access for users.
8+
for the credential you can use the JWT token or the API token
89

910
**Example**
1011
```js
12+
//connect with the API token
13+
//(to request a token create an account in https://terminusdb.com/)
14+
const accessContol = new AccessControl("https://servername.com",
15+
{organization:"my_team_name",
16+
token:"dGVybWludXNkYjovLy9kYXRhL2tleXNfYXB........"})
17+
accessControl.getOrgUsers().then(result=>{
18+
console.log(result)
19+
})
20+
21+
//connect with the jwt token this type of connection is only for the dashboard
22+
//or for application integrate with our login workflow
1123
const accessContol = new AccessControl("https://servername.com",
1224
{organization:"my_team_name",
1325
jwt:"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd........"})
1426
accessControl.getOrgUsers().then(result=>{
1527
console.log(result)
1628
})
29+
1730
//if the jwt is expired you can change it with
18-
accessControl.setJwtToken("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd.......")
31+
accessControl.setJwtToken("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd.......")
1932
```
2033

2134
### getDefaultOrganization
@@ -29,25 +42,24 @@ Get a organization from parameters.
2942
| params | <code>object</code> | The parameters |
3043

3144

32-
### getJwtToken
33-
#### accessControl.getJwtToken(params) ⇒ <code>string</code>
34-
Get a API token from parameters.
45+
### setJwtToken
46+
#### accessControl.setJwtToken(jwt)
47+
Sets the Jwt token for the object
3548

36-
**Returns**: <code>string</code> - jwt api token
3749

3850
| Param | Type | Description |
3951
| --- | --- | --- |
40-
| params | <code>object</code> | The parameters |
52+
| jwt | <code>string</code> | The jwt api token to use |
4153

4254

43-
### setJwtToken
44-
#### accessControl.setJwtToken(jwt)
45-
Sets the API token for the object
55+
### setApiToken
56+
#### accessControl.setApiToken(atokenpi)
57+
Sets the API token for the object, to request a token create an account in https://terminusdb.com/
4658

4759

4860
| Param | Type | Description |
4961
| --- | --- | --- |
50-
| jwt | <code>string</code> | The jwt api token to use |
62+
| atokenpi | <code>string</code> | The API token to use to connect with TerminusX |
5163

5264

5365
### getAPIUrl

lib/accessControl.js

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,43 @@ const UTILS = require('./utils')
99
* @module AccessControl
1010
* @constructor AccessControl
1111
* @description The AccessControl object has various methods to control the access for users.
12+
* for the credential you can use the JWT token or the API token
1213
* @example
14+
* //connect with the API token
15+
* //(to request a token create an account in https://terminusdb.com/)
16+
* const accessContol = new AccessControl("https://servername.com",
17+
* {organization:"my_team_name",
18+
* token:"dGVybWludXNkYjovLy9kYXRhL2tleXNfYXB........"})
19+
* accessControl.getOrgUsers().then(result=>{
20+
* console.log(result)
21+
* })
22+
*
23+
* //connect with the jwt token this type of connection is only for the dashboard
24+
* //or for application integrate with our login workflow
1325
* const accessContol = new AccessControl("https://servername.com",
1426
* {organization:"my_team_name",
1527
* jwt:"eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd........"})
1628
* accessControl.getOrgUsers().then(result=>{
1729
* console.log(result)
1830
* })
31+
*
1932
* //if the jwt is expired you can change it with
20-
* accessControl.setJwtToken("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd.......")
33+
* accessControl.setJwtToken("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkpXUjBIOXYyeTFORUd.......")
34+
*
35+
*
36+
*
2137
*/
2238

2339
function AccessControl(cloudAPIUrl, params) {
2440

2541
this.baseURL = this.getAPIUrl(cloudAPIUrl);
26-
this.apiJwtToken = this.getJwtToken(params);
42+
if(params.jwt){
43+
this.setJwtToken(params.jwt)
44+
45+
}else if (params.token){
46+
this.setApiToken(params.token)
47+
}
48+
2749
this.defaultOrganization = this.getDefaultOrganization(params);
2850

2951
}
@@ -35,40 +57,37 @@ function AccessControl(cloudAPIUrl, params) {
3557
*/
3658
AccessControl.prototype.getDefaultOrganization = function(params) {
3759
if(params && params.organization && typeof params.organization === 'string') {
38-
return params.organization;
60+
return params.organization
3961
}
4062
return undefined;
4163
}
42-
4364
/**
44-
* Get a API token from parameters.
45-
* @param {object} params - The parameters
46-
* @return {string} jwt api token
65+
* Sets the Jwt token for the object
66+
* @param {string} jwt - The jwt api token to use
4767
*/
48-
AccessControl.prototype.getJwtToken = function(params) {
49-
if(params) {
50-
if(!params.jwt) throw new Error('TerminusX Access token required');
51-
52-
return params.jwt;
53-
} else {
54-
if(!process.env.TERMINUSDB_ACCESS_TOKEN) throw new Error('TerminusX Access token required');
55-
56-
return process.env.TERMINUSDB_ACCESS_TOKEN;
68+
AccessControl.prototype.setJwtToken = function(jwt) {
69+
if(!jwt) {
70+
throw new Error('TerminusX Access token required');
5771
}
72+
73+
this.apiJwtToken = jwt;
74+
this.apiType = 'jwt'
5875
}
5976

6077
/**
61-
* Sets the API token for the object
62-
* @param {string} jwt - The jwt api token to use
78+
* Sets the API token for the object, to request a token create an account in https://terminusdb.com/
79+
* @param {string} atokenpi - The API token to use to connect with TerminusX
6380
*/
64-
AccessControl.prototype.setJwtToken = function(jwt) {
65-
if(!jwt) {
81+
AccessControl.prototype.setApiToken = function(token) {
82+
if(!token) {
6683
throw new Error('TerminusX Access token required');
6784
}
6885

69-
this.apiJwtToken = jwt;
86+
this.apiToken = token;
87+
this.apiType = "apikey"
7088
}
7189

90+
7291
/**
7392
* Get a API url from cloudAPIUrl
7493
* @param {string} cloudAPIUrl - The base url for cloud
@@ -97,11 +116,13 @@ AccessControl.prototype.dispatch = function(requestUrl, action, payload) {
97116
);
98117
}
99118

119+
const apiToken = this.apiJwtToken || this.apiToken
120+
100121
return DispatchRequest(
101122
requestUrl,
102123
action,
103124
payload,
104-
{ type: 'jwt', key: this.apiJwtToken }
125+
{ type:this.apiType, key:apiToken }
105126
);
106127
}
107128

@@ -555,4 +576,4 @@ AccessControl.prototype.updateUserRole = function (userId, capabilityId, scope,
555576
});
556577
};
557578

558-
module.exports = AccessControl;
579+
module.exports = AccessControl

0 commit comments

Comments
 (0)