Skip to content

Commit

Permalink
feat: Authentication for Client.Legacy (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolincova committed Apr 21, 2020
1 parent aff3843 commit 91aa988
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
1. [#81](https://github.com/influxdata/influxdb-client-csharp/pull/81): Fixed potentially hangs on of WriteApi.Dispose()
1. [#83](https://github.com/influxdata/influxdb-client-csharp/pull/83): Fixed parsing error response for 1.x

### Features
1. [#84](https://github.com/influxdata/influxdb-client-csharp/issues/84): Add possibility to authenticate by Basic Authentication or the URL query parameters

## 1.7.0 [2020-04-17]

### Features
Expand Down
19 changes: 19 additions & 0 deletions Client.Legacy.Test/FluxClientPingTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Threading.Tasks;
using InfluxDB.Client.Flux;
using NUnit.Framework;
Expand Down Expand Up @@ -49,5 +50,23 @@ public async Task WithAuthentication()

Assert.IsTrue(await FluxClient.PingAsync());
}

[Test]
public async Task WithBasicAuthentication()
{
FluxClient = FluxClientFactory.Create(new FluxConnectionOptions(MockServerUrl, "my-user",
"my-password".ToCharArray(), FluxConnectionOptions.AuthenticationType.BasicAuthentication));

var auth = System.Text.Encoding.UTF8.GetBytes("my-user:my-password");

MockServer.Given(Request.Create()
.WithPath("/ping")
.WithHeader("Authorization",
new ExactMatcher("Basic " + Convert.ToBase64String(auth)))
.UsingGet())
.RespondWith(Response.Create().WithStatusCode(204));

Assert.IsTrue(await FluxClient.PingAsync());
}
}
}
8 changes: 8 additions & 0 deletions Client.Legacy.Test/ItFluxClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ public async Task ErrorWithStatusOk()
Assert.That(e.Message.Contains("try bounding 'from' with a call to 'range'"));
}
}

[Test]
public async Task WithAuthentication()
{
FluxClient = FluxClientFactory.Create(new FluxConnectionOptions(GetInfluxDbUrl(), "my-user", "my-password".ToCharArray()));

Assert.IsTrue(await FluxClient.PingAsync());
}

[Test]
public async Task Callback()
Expand Down
13 changes: 11 additions & 2 deletions Client.Legacy/FluxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mime;
using System.Threading.Tasks;
using InfluxDB.Client.Core;
using InfluxDB.Client.Core.Exceptions;
Expand All @@ -27,8 +28,16 @@ public FluxClient(FluxConnectionOptions options) : base(new RestClient())
RestClient.AddDefaultHeader("Accept", "application/json");
if (!string.IsNullOrEmpty(options.Username))
{
RestClient.AddDefaultQueryParameter("u", options.Username);
RestClient.AddDefaultQueryParameter("p", new string(options.Password));
if (options.Authentication.Equals(FluxConnectionOptions.AuthenticationType.BasicAuthentication))
{
var auth = System.Text.Encoding.UTF8.GetBytes(options.Username + ":" + new string(options.Password));
RestClient.AddDefaultHeader("Authorization", "Basic " + Convert.ToBase64String(auth));
}
else
{
RestClient.AddDefaultQueryParameter("u", options.Username);
RestClient.AddDefaultQueryParameter("p", new string(options.Password));
}
}
RestClient.UserAgent = $"influxdb-client-csharp/{version}";
}
Expand Down
19 changes: 15 additions & 4 deletions Client.Legacy/FluxConnectionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,40 @@
namespace InfluxDB.Client.Flux
{
public class FluxConnectionOptions
{
{
public enum AuthenticationType
{
UrlQueryParameters,
BasicAuthentication
}

public string Url { get; private set; }

public TimeSpan Timeout { get; private set; }

public string Username { get; }
public char[] Password { get; }

public AuthenticationType Authentication { get; }

public FluxConnectionOptions(string url) : this(url, TimeSpan.FromSeconds(60))
{
}

public FluxConnectionOptions(string url, string username = "", char[] password = null) : this(url,
TimeSpan.FromSeconds(60), username, password)
public FluxConnectionOptions(string url, string username = "", char[] password = null,
AuthenticationType authentication = AuthenticationType.UrlQueryParameters) : this(url,
TimeSpan.FromSeconds(60), username, password, authentication)
{
}

public FluxConnectionOptions(string url, TimeSpan timeout, string username = "", char[] password = null)
public FluxConnectionOptions(string url, TimeSpan timeout, string username = "", char[] password = null,
AuthenticationType authentication = AuthenticationType.UrlQueryParameters)
{
Url = url;
Timeout = timeout;
Username = username;
Password = password;
Authentication = authentication;
}
}
}
14 changes: 14 additions & 0 deletions Client.Legacy/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The `FluxClientFactory` creates an instance of a `FluxClient` client that can be
- `okHttpClient` - custom HTTP client to use for communications with InfluxDB (optional)
- `username` - name of your InfluxDB user (optional)
- `password` - password of your InfluxDB user (optional)
- `authentication` - type of authentication (optional). There are two options for authenticating: Basic Authentication and the URL query parameters (default).

```c#
// client creation
Expand All @@ -28,6 +29,7 @@ fluxClient.QueryAsync(...)
```
#### Authenticate requests

##### URL query parameters
```c#
// client creation
var options = new FluxConnectionOptions("http://127.0.0.1:8086", "my-user", "my-password".ToCharArray());
Expand All @@ -38,6 +40,18 @@ fluxClient.QueryAsync(...)
...
```

##### Basic authentication
```c#
// client creation
var options = new FluxConnectionOptions("http://127.0.0.1:8086", "my-user", "my-password".ToCharArray(),
FluxConnectionOptions.AuthenticationType.BasicAuthentication);

var fluxClient = FluxClientFactory.Create(options);

fluxClient.QueryAsync(...)
...
```

### Query using the Flux language

The library supports an asynchronous queries.
Expand Down

0 comments on commit 91aa988

Please sign in to comment.