Skip to content

Commit 9fba8a2

Browse files
alespourbednar
andauthored
feat: add Edge authentication support (#111)
Co-authored-by: Jakub Bednář <jakub.bednar@gmail.com>
1 parent c0e42c8 commit 9fba8a2

File tree

5 files changed

+108
-25
lines changed

5 files changed

+108
-25
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
1. [#101](https://github.com/InfluxCommunity/influxdb3-csharp/pull/101): Add standard `user-agent` header to all calls.
66
1. [#110](https://github.com/InfluxCommunity/influxdb3-csharp/pull/110): InfluxDB Edge (OSS) error handling.
7+
1. [#111](https://github.com/InfluxCommunity/influxdb3-csharp/pull/111): Add InfluxDB Edge (OSS) authentication support.
8+
9+
### Migration Notice
10+
11+
- `InfluxDBClient` constructor with connection options has new option `authScheme` with `null` default value:
12+
13+
```diff
14+
- public InfluxDBClient(string host, string token, string? organization = null, string? database = null);
15+
+ public InfluxDBClient(string host, string token, string? organization = null, string? database = null, string? authScheme = null)
16+
```
17+
18+
This new option is used for Edge (OSS) authentication.
719

820
## 0.6.0 [2024-04-16]
921

Client.Test/Config/ClientConfigTest.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,21 @@ public void CreateFromConnectionStringBasic()
4747
});
4848
}
4949

50+
[Test]
51+
public void CreateFromConnectionStringWithAuthScheme()
52+
{
53+
var cfg = new ClientConfig("http://localhost:8086?token=my-token&org=my-org&authScheme=my-scheme");
54+
Assert.That(cfg, Is.Not.Null);
55+
cfg.Validate();
56+
Assert.Multiple(() =>
57+
{
58+
Assert.That(cfg.Host, Is.EqualTo("http://localhost:8086/"));
59+
Assert.That(cfg.Token, Is.EqualTo("my-token"));
60+
Assert.That(cfg.AuthScheme, Is.EqualTo("my-scheme"));
61+
Assert.That(cfg.WriteOptions, Is.EqualTo(null));
62+
});
63+
}
64+
5065
[Test]
5166
public void CreateFromConnectionStringWithWriteOptions()
5267
{
@@ -143,6 +158,28 @@ public void CreateFromEnvBasic()
143158
});
144159
}
145160

161+
[Test]
162+
public void CreateFromEnvWithAuthScheme()
163+
{
164+
var env = new Dictionary<String, String>
165+
{
166+
{"INFLUX_HOST", "http://localhost:8086"},
167+
{"INFLUX_TOKEN", "my-token"},
168+
{"INFLUX_AUTH_SCHEME", "my-scheme"},
169+
};
170+
SetEnv(env);
171+
var cfg = new ClientConfig(env);
172+
Assert.That(cfg, Is.Not.Null);
173+
cfg.Validate();
174+
Assert.Multiple(() =>
175+
{
176+
Assert.That(cfg.Host, Is.EqualTo("http://localhost:8086/"));
177+
Assert.That(cfg.Token, Is.EqualTo("my-token"));
178+
Assert.That(cfg.AuthScheme, Is.EqualTo("my-scheme"));
179+
Assert.That(cfg.WriteOptions, Is.EqualTo(null));
180+
});
181+
}
182+
146183
[Test]
147184
public void CreateFromEnvWithWriteOptions()
148185
{

Client.Test/Internal/RestClientTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ public async Task Authorization()
3636
Assert.That(requests[0].RequestMessage.Headers?["Authorization"][0], Is.EqualTo("Token my-token"));
3737
}
3838

39+
[Test]
40+
public async Task AuthorizationCustomScheme()
41+
{
42+
CreateAndConfigureRestClient(new ClientConfig
43+
{
44+
Host = MockServerUrl,
45+
Token = "my-token",
46+
AuthScheme = "my-scheme"
47+
});
48+
await DoRequest();
49+
50+
var requests = MockServer.LogEntries.ToList();
51+
52+
Assert.That(requests[0].RequestMessage.Headers?["Authorization"][0], Is.EqualTo("my-scheme my-token"));
53+
}
54+
3955
[Test]
4056
public async Task UserAgent()
4157
{

Client/Config/ClientConfig.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace InfluxDB3.Client.Config;
1313
/// You can configure following options:
1414
/// - Host: The URL of the InfluxDB server.
1515
/// - Token: The authentication token for accessing the InfluxDB server.
16+
/// - AuthScheme: Token authentication scheme. Default is 'null' for Cloud access. Set to 'Bearer' for Edge access.
1617
/// - Organization: The organization to be used for operations.
1718
/// - Database: The database to be used for InfluxDB operations.
1819
/// - Headers: The set of HTTP headers to be included in requests.
@@ -44,6 +45,7 @@ public class ClientConfig
4445
{
4546
internal const string EnvInfluxHost = "INFLUX_HOST";
4647
internal const string EnvInfluxToken = "INFLUX_TOKEN";
48+
internal const string EnvInfluxAuthScheme = "INFLUX_AUTH_SCHEME";
4749
internal const string EnvInfluxOrg = "INFLUX_ORG";
4850
internal const string EnvInfluxDatabase = "INFLUX_DATABASE";
4951
internal const string EnvInfluxPrecision = "INFLUX_PRECISION";
@@ -67,6 +69,7 @@ internal ClientConfig(string connectionString)
6769
Host = uri.GetLeftPart(UriPartial.Path);
6870
var values = HttpUtility.ParseQueryString(uri.Query);
6971
Token = values.Get("token");
72+
AuthScheme = values.Get("authScheme");
7073
Organization = values.Get("org");
7174
Database = values.Get("database");
7275
ParsePrecision(values.Get("precision"));
@@ -80,6 +83,7 @@ internal ClientConfig(IDictionary env)
8083
{
8184
Host = (string)env[EnvInfluxHost];
8285
Token = env[EnvInfluxToken] as string;
86+
AuthScheme = env[EnvInfluxAuthScheme] as string;
8387
Organization = env[EnvInfluxOrg] as string;
8488
Database = env[EnvInfluxDatabase] as string;
8589
ParsePrecision(env[EnvInfluxPrecision] as string);
@@ -100,6 +104,11 @@ public string Host
100104
/// </summary>
101105
public string? Token { get; set; }
102106

107+
/// <summary>
108+
/// Token authentication scheme.
109+
/// </summary>
110+
public string? AuthScheme { get; set; }
111+
103112
/// <summary>
104113
/// The organization to be used for operations.
105114
/// </summary>

0 commit comments

Comments
 (0)