Skip to content

Commit 62dfcb5

Browse files
committed
feat: Support basic authentication, #29
1 parent 2ad57a5 commit 62dfcb5

File tree

5 files changed

+90
-25
lines changed

5 files changed

+90
-25
lines changed

README.md

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,27 @@ View the [./examples](examples) directory for working examples.
2323
[API](https://vicanso.github.io/influxdb-nodejs/)
2424

2525

26-
Write point
26+
### New Client
27+
28+
```js
29+
// no auth
30+
const Influx = require('influxdb-nodejs');
31+
const client = new Influx('http://127.0.0.1:8086/mydb');
32+
```
33+
34+
```js
35+
// normal auth (user and password will be added to URL query parameters)
36+
const Influx = require('influxdb-nodejs');
37+
const client = new Influx('http://user:pwd@127.0.0.1:8086/mydb');
38+
```
39+
40+
```js
41+
// basic auth (will be used http basic auth)
42+
const Influx = require('influxdb-nodejs');
43+
const client = new Influx('http://user:pwd@127.0.0.1:8086/mydb?auth=basic');
44+
```
45+
46+
### Write point
2747

2848
```js
2949
const Influx = require('influxdb-nodejs');
@@ -63,7 +83,7 @@ client.write('http')
6383
```
6484

6585

66-
Query influxdb with multi where condition
86+
### Query influxdb with multi where condition
6787

6888
```js
6989
const Influx = require('influxdb-nodejs');
@@ -97,7 +117,7 @@ client.query('http')
97117
// select bottom("use",5) from "http" where "spdy" = '1'
98118
```
99119

100-
Write points to influxdb in queue
120+
### Write points to influxdb in queue
101121

102122
```js
103123
const Influx = require('influxdb-nodejs');
@@ -124,7 +144,7 @@ setInterval(() => {
124144
}, 5000);
125145
```
126146

127-
Sub query
147+
### Sub query
128148

129149
```js
130150
const Influx = require('influxdb-nodejs');
@@ -140,7 +160,7 @@ client.query('http')
140160
}).catch(console.error);
141161
```
142162

143-
Multi query
163+
### Multi query
144164

145165
```js
146166
const Influx = require('influxdb-nodejs');
@@ -162,7 +182,7 @@ reader.then(data => {
162182
}).catch(console.error);
163183
```
164184

165-
Use influxdb for express
185+
### Use influxdb for express
166186

167187
```js
168188
const express = require('express');

lib/client.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const EventEmitter = require('events');
44
const InfluxQL = require('influx-ql');
55
const request = require('superagent');
66
const _ = require('lodash');
7+
const querystring = require('querystring');
78

89
const Influx = require('./influx');
910
const debug = require('./debug');
@@ -84,13 +85,28 @@ class Client extends EventEmitter {
8485
port: parseInt(arr[1], 10),
8586
};
8687
});
87-
opts.database = result[4] || '';
88+
const db = result[4] || '';
89+
const queryIndex = db.indexOf('?');
90+
let query = '';
91+
if (queryIndex === -1) {
92+
opts.database = db;
93+
} else {
94+
opts.database = db.substring(0, queryIndex);
95+
query = db.substr(queryIndex + 1);
96+
}
8897
/* istanbul ignore else */
8998
if (result[2]) {
9099
const authInfos = result[2].substring(0, result[2].length - 1).split(
91100
':');
92101
opts.username = authInfos[0];
93102
opts.password = authInfos[1];
103+
// 根据query配置,判断使用哪种认证方式
104+
if (query) {
105+
const queryInfo = querystring.parse(query);
106+
if (queryInfo.auth) {
107+
opts.authType = queryInfo.auth;
108+
}
109+
}
94110
}
95111
debug('init options:%j', opts);
96112
const internalData = internal(this);
@@ -115,8 +131,8 @@ class Client extends EventEmitter {
115131
*/
116132
startHealthCheck(ping, interval) {
117133
const defaultPing = (backend) => {
118-
const url = `${backend.protocol || 'http'}://${backend.host}:${backend.port || 80}/ping`;
119-
return request.get(url).timeout(1000);
134+
const pingUrl = `${backend.protocol || 'http'}://${backend.host}:${backend.port || 80}/ping`;
135+
return request.get(pingUrl).timeout(1000);
120136
};
121137
const pingFn = _.isNumber(ping) ? null : ping;
122138
const ms = _.isNumber(ping) ? ping : interval;

lib/http.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,16 @@ class HTTP {
3030
debug('http backends:%j', servers);
3131
const balancer = new Balancer(servers, type);
3232
data.balancer = balancer;
33-
data.plugin = balancer.plugin();
33+
data.plugins = [
34+
balancer.plugin(),
35+
];
36+
}
37+
/**
38+
* [addPlugin add plugin for http request]
39+
* @param {Function} fn plugin function
40+
*/
41+
addPlugin(fn) {
42+
internal(this).plugins.push(fn);
3443
}
3544
/**
3645
* [timeout set request time out]
@@ -60,7 +69,7 @@ class HTTP {
6069
debug('GET %s, query:%j', url, query);
6170
const internalData = internal(this);
6271
const req = getRequest('get', url, query, internalData.timeout);
63-
req.use(internalData.plugin);
72+
_.forEach(internalData.plugins, plugin => req.use(plugin));
6473
return req;
6574
}
6675
/**
@@ -76,7 +85,7 @@ class HTTP {
7685
const req = getRequest('post', url, query, internalData.timeout);
7786
req.type('form')
7887
.send(data);
79-
req.use(internalData.plugin);
88+
_.forEach(internalData.plugins, plugin => req.use(plugin));
8089
return req;
8190
}
8291
startHealthCheck(ping, interval) {

lib/influx.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ class Influx {
7979
}
8080
debug('Influx init options:%j', options);
8181
const client = new HTTP(options.servers, options.loadBalancingAlgorithm);
82+
// 如果有配置了用户与密码
83+
if (options.username && options.password) {
84+
// basic auth认证
85+
if (options.authType === 'basic') {
86+
const str = `${options.username}:${options.password}`;
87+
const authValue = `Basic ${Buffer.from(str).toString('base64')}`;
88+
client.addPlugin((req) => {
89+
req.set('Authorization', authValue);
90+
return req;
91+
});
92+
} else {
93+
client.addPlugin((req) => {
94+
req.query({
95+
u: options.username,
96+
p: options.password,
97+
});
98+
return req;
99+
});
100+
}
101+
}
102+
82103
const internalData = internal(this);
83104
internalData.opts = _.extend({}, options);
84105
internalData.client = client;
@@ -113,10 +134,6 @@ class Influx {
113134
q,
114135
db: db || opts.database,
115136
};
116-
if (opts.username && opts.password) {
117-
queryData.u = opts.username;
118-
queryData.p = opts.password;
119-
}
120137
if (epoch) {
121138
queryData.epoch = epoch;
122139
}
@@ -138,10 +155,6 @@ class Influx {
138155
const queryData = {
139156
db: opts.database,
140157
};
141-
if (opts.username && opts.password) {
142-
queryData.u = opts.username;
143-
queryData.p = opts.password;
144-
}
145158
if (points[0].precision) {
146159
queryData.precision = points[0].precision;
147160
}
@@ -165,17 +178,12 @@ class Influx {
165178

166179
queryPost(q, db) {
167180
const internalData = internal(this);
168-
const opts = internalData.opts;
169181
debug('q:%s', q);
170182
const queryData = {};
171183
/* istanbul ignore if */
172184
if (db) {
173185
queryData.db = db;
174186
}
175-
if (opts.username && opts.password) {
176-
queryData.u = opts.username;
177-
queryData.p = opts.password;
178-
}
179187
return internalData.client.post('/query', {
180188
q,
181189
}, queryData).then(res => res.body, (err) => {

test/client.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,24 @@ describe('Client:Auth', () => {
563563
});
564564
});
565565

566+
566567
it('create database', done => {
567568
client.createDatabase().then(() => {
568569
done();
569570
}).catch(done);
570571
});
571572

573+
it('basic auth client', done => {
574+
const tmp = new Client(`http://vicanso:mypwd@localhost:8085/${db}?auth=basic`);
575+
tmp.showDatabases().then((data) => {
576+
if (data.length === 0) {
577+
done(new Error('show data bases fail'));
578+
return;
579+
}
580+
done();
581+
}).catch(done);
582+
});
583+
572584
it('write point', done => {
573585
client.write('http')
574586
.tag({

0 commit comments

Comments
 (0)