Skip to content

Commit f1d5729

Browse files
authored
feat: add possibility to authenticate requests (#65)
1 parent 15384fe commit f1d5729

File tree

6 files changed

+67
-3
lines changed

6 files changed

+67
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44
1. [#61](https://github.com/influxdata/influxdb-client-csharp/issues/61): Set User-Agent to influxdb-client-csharp/VERSION for all requests
5+
1. [#64](https://github.com/influxdata/influxdb-client-csharp/issues/64): Add authentication with Username and Password for Client.Legacy
56

67
### Bugs
78
1. [#63](https://github.com/influxdata/influxdb-client-csharp/pull/63): Correctly parse CSV where multiple results include multiple tables

Client.Legacy.Test/FluxClientPingTest.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System.Threading.Tasks;
2+
using InfluxDB.Client.Flux;
23
using NUnit.Framework;
4+
using WireMock.Matchers;
35
using WireMock.RequestBuilders;
46
using WireMock.ResponseBuilders;
57

@@ -32,5 +34,20 @@ public async Task NotRunningServer()
3234

3335
Assert.IsFalse(await FluxClient.PingAsync());
3436
}
37+
38+
[Test]
39+
public async Task WithAuthentication()
40+
{
41+
FluxClient = FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user", "my-password".ToCharArray()));
42+
43+
MockServer.Given(Request.Create()
44+
.WithPath("/ping")
45+
.WithParam("u", new ExactMatcher("my-user"))
46+
.WithParam("p", new ExactMatcher("my-password"))
47+
.UsingGet())
48+
.RespondWith(Response.Create().WithStatusCode(204));
49+
50+
Assert.IsTrue(await FluxClient.PingAsync());
51+
}
3552
}
3653
}

Client.Legacy.Test/FluxClientQueryTest.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
using InfluxDB.Client.Core.Exceptions;
77
using InfluxDB.Client.Core.Flux.Domain;
88
using InfluxDB.Client.Core.Flux.Exceptions;
9+
using InfluxDB.Client.Flux;
910
using NUnit.Framework;
11+
using WireMock.Matchers;
1012
using WireMock.RequestBuilders;
1113

1214
namespace Client.Legacy.Test
@@ -195,6 +197,23 @@ public async Task UserAgentHeader()
195197
StringAssert.EndsWith(".0.0", request.RequestMessage.Headers["User-Agent"].First());
196198
}
197199

200+
[Test]
201+
public async Task WithAuthentication()
202+
{
203+
FluxClient = FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user", "my-password".ToCharArray()));
204+
205+
MockServer.Given(Request.Create()
206+
.WithPath("/api/v2/query")
207+
.WithParam("u", new ExactMatcher("my-user"))
208+
.WithParam("p", new ExactMatcher("my-password"))
209+
.UsingPost())
210+
.RespondWith(CreateResponse());
211+
212+
var result = await FluxClient.QueryAsync("from(bucket:\"telegraf\")");
213+
214+
AssertSuccessResult(result);
215+
}
216+
198217
private void AssertSuccessResult(List<FluxTable> tables)
199218
{
200219
Assert.IsNotNull(tables);

Client.Legacy/FluxClient.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public FluxClient(FluxConnectionOptions options) : base(new RestClient())
2525
RestClient.BaseUrl = new Uri(options.Url);
2626
RestClient.Timeout = options.Timeout.Milliseconds;
2727
RestClient.AddDefaultHeader("Accept", "application/json");
28+
if (!string.IsNullOrEmpty(options.Username))
29+
{
30+
RestClient.AddDefaultQueryParameter("u", options.Username);
31+
RestClient.AddDefaultQueryParameter("p", new string(options.Password));
32+
}
2833
RestClient.UserAgent = $"influxdb-client-csharp/{version}";
2934
}
3035

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
using System;
32

43
namespace InfluxDB.Client.Flux
@@ -8,15 +7,25 @@ public class FluxConnectionOptions
87
public string Url { get; private set; }
98

109
public TimeSpan Timeout { get; private set; }
11-
10+
11+
public string Username { get; }
12+
public char[] Password { get; }
13+
1214
public FluxConnectionOptions(string url) : this(url, TimeSpan.FromSeconds(60))
1315
{
1416
}
1517

16-
public FluxConnectionOptions(string url, TimeSpan timeout)
18+
public FluxConnectionOptions(string url, string username = "", char[] password = null) : this(url,
19+
TimeSpan.FromSeconds(60), username, password)
20+
{
21+
}
22+
23+
public FluxConnectionOptions(string url, TimeSpan timeout, string username = "", char[] password = null)
1724
{
1825
Url = url;
1926
Timeout = timeout;
27+
Username = username;
28+
Password = password;
2029
}
2130
}
2231
}

Client.Legacy/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@ The `FluxClientFactory` creates an instance of a `FluxClient` client that can be
1414

1515
- `url` - the url to connect to InfluxDB
1616
- `okHttpClient` - custom HTTP client to use for communications with InfluxDB (optional)
17+
- `username` - name of your InfluxDB user (optional)
18+
- `password` - password of your InfluxDB user (optional)
1719

1820
```c#
1921
// client creation
2022
var options = new FluxConnectionOptions("http://127.0.0.1:8086");
2123

2224
var fluxClient = FluxClientFactory.Create(options);
2325

26+
fluxClient.QueryAsync(...)
27+
...
28+
```
29+
#### Authenticate requests
30+
31+
```c#
32+
// client creation
33+
var options = new FluxConnectionOptions("http://127.0.0.1:8086", "my-user", "my-password".ToCharArray());
34+
35+
var fluxClient = FluxClientFactory.Create(options);
36+
2437
fluxClient.QueryAsync(...)
2538
...
2639
```

0 commit comments

Comments
 (0)