@@ -29,6 +29,22 @@ function getResultWithDataVersion(response) {
2929 : '' ,
3030 } ;
3131}
32+ /**
33+ * Create the authorization header string
34+ * @param {object } auth_obj
35+ * @returns {object } Object with the Authorization header
36+ */
37+
38+ function formatAuthHeader ( auth_obj ) {
39+ if ( ! auth_obj ) return '' ;
40+ const authType = { jwt : 'Bearer' , basic : 'Basic' , apikey : 'Token' } ;
41+ let auth_key = auth_obj . key ;
42+
43+ if ( auth_obj . type === 'basic' ) {
44+ auth_key = btoaImplementation ( `${ auth_obj . user } :${ auth_obj . key } ` ) ;
45+ }
46+ return `${ authType [ auth_obj . type ] } ${ auth_key } ` ;
47+ }
3248
3349/**
3450 * @file Dispatch Request
@@ -41,6 +57,7 @@ function getResultWithDataVersion(response) {
4157 * @param {object } customHeaders the unique reqID
4258 * @param {boolean } compress If true, compress the data with gzip if its size is bigger than 1024
4359 */
60+
4461// eslint-disable-next-line max-len
4562function DispatchRequest ( url , action , payload , local_auth , remote_auth = null , customHeaders = null , getDataVersion = false , compress = false ) {
4663 /*
@@ -72,24 +89,30 @@ function DispatchRequest(url, action, payload, local_auth, remote_auth = null, c
7289 * I can call the local database using the local authorization key or
7390 * a jwt token
7491 */
75- if ( local_auth && local_auth . type === 'basic' ) {
92+ /* if (local_auth && local_auth.type === 'basic') {
7693 const encoded = btoaImplementation(`${local_auth.user}:${local_auth.key}`);
7794 options.headers = { Authorization: `Basic ${encoded}` };
7895 } else if (local_auth && local_auth.type === 'jwt') {
7996 options.headers = { Authorization: `Bearer ${local_auth.key}` };
8097 } else if (local_auth && local_auth.type === 'apikey') {
8198 options.headers = { Authorization: `Token ${local_auth.key}` };
99+ } */
100+ /*
101+ * I can call the local database or a custom installation using the local authorization key or
102+ * I Can call TerminusX using the jwt token or an apiToken
103+ */
104+ if ( local_auth && typeof local_auth === 'object' ) {
105+ options . headers . Authorization = formatAuthHeader ( local_auth ) ;
82106 }
83107
84108 /*
85- * pass the Authorization information of another
86- * terminusdb server to the local terminusdb
87- */
88- if ( remote_auth && remote_auth . type === 'jwt' ) {
89- options . headers [ 'Authorization-Remote' ] = `Bearer ${ remote_auth . key } ` ;
90- } else if ( remote_auth && remote_auth . type === 'basic' ) {
91- const rencoded = btoaImplementation ( `${ remote_auth . user } :${ remote_auth . key } ` ) ;
92- options . headers [ 'Authorization-Remote' ] = `Basic ${ rencoded } ` ;
109+ * pass the Authorization information of another
110+ * terminusdb server to the local terminusdb
111+ * for authentication you can use jwt or the apiKey token in TerminusX or
112+ * the Basic autentication if is allowed in the custom server
113+ */
114+ if ( remote_auth && typeof remote_auth === 'object' ) {
115+ options . headers [ 'Authorization-Remote' ] = formatAuthHeader ( remote_auth ) ;
93116 }
94117
95118 if ( customHeaders && typeof customHeaders === 'object' ) {
0 commit comments