From 91aa988647a340a0df1bd643ae55511cf1be6456 Mon Sep 17 00:00:00 2001 From: Pavlina Rolincova Date: Tue, 21 Apr 2020 14:04:33 +0200 Subject: [PATCH] feat: Authentication for Client.Legacy (#84) --- CHANGELOG.md | 3 +++ Client.Legacy.Test/FluxClientPingTest.cs | 19 +++++++++++++++++++ Client.Legacy.Test/ItFluxClientTest.cs | 8 ++++++++ Client.Legacy/FluxClient.cs | 13 +++++++++++-- Client.Legacy/FluxConnectionOptions.cs | 19 +++++++++++++++---- Client.Legacy/README.md | 14 ++++++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d321da5e..6e001a6cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Client.Legacy.Test/FluxClientPingTest.cs b/Client.Legacy.Test/FluxClientPingTest.cs index acb4313a8..994758c28 100644 --- a/Client.Legacy.Test/FluxClientPingTest.cs +++ b/Client.Legacy.Test/FluxClientPingTest.cs @@ -1,3 +1,4 @@ +using System; using System.Threading.Tasks; using InfluxDB.Client.Flux; using NUnit.Framework; @@ -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()); + } } } \ No newline at end of file diff --git a/Client.Legacy.Test/ItFluxClientTest.cs b/Client.Legacy.Test/ItFluxClientTest.cs index ab42bad58..7ec83491a 100644 --- a/Client.Legacy.Test/ItFluxClientTest.cs +++ b/Client.Legacy.Test/ItFluxClientTest.cs @@ -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() diff --git a/Client.Legacy/FluxClient.cs b/Client.Legacy/FluxClient.cs index db91e536e..4b856441b 100644 --- a/Client.Legacy/FluxClient.cs +++ b/Client.Legacy/FluxClient.cs @@ -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; @@ -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}"; } diff --git a/Client.Legacy/FluxConnectionOptions.cs b/Client.Legacy/FluxConnectionOptions.cs index aba1ba8af..472a6c326 100644 --- a/Client.Legacy/FluxConnectionOptions.cs +++ b/Client.Legacy/FluxConnectionOptions.cs @@ -3,7 +3,13 @@ namespace InfluxDB.Client.Flux { public class FluxConnectionOptions - { + { + public enum AuthenticationType + { + UrlQueryParameters, + BasicAuthentication + } + public string Url { get; private set; } public TimeSpan Timeout { get; private set; } @@ -11,21 +17,26 @@ public class FluxConnectionOptions 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; } } } \ No newline at end of file diff --git a/Client.Legacy/README.md b/Client.Legacy/README.md index c561604e2..526748cbd 100644 --- a/Client.Legacy/README.md +++ b/Client.Legacy/README.md @@ -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 @@ -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()); @@ -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.