@@ -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
2339function 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