Skip to content

Commit

Permalink
feat: add Edge authentication support
Browse files Browse the repository at this point in the history
  • Loading branch information
alespour committed Jul 31, 2024
1 parent e3ea3a4 commit 63994db
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Features

1. [#101](https://github.com/InfluxCommunity/influxdb3-csharp/pull/101): Add standard `user-agent` header to all calls.
1. [#111](https://github.com/InfluxCommunity/influxdb3-csharp/pull/111): Add InfluxDB Edge (OSS) authentication support.

## 0.6.0 [2024-04-16]

Expand Down
16 changes: 16 additions & 0 deletions Client.Test/Internal/RestClientTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public async Task Authorization()
Assert.That(requests[0].RequestMessage.Headers?["Authorization"][0], Is.EqualTo("Token my-token"));
}

[Test]
public async Task AuthorizationCustomScheme()
{
CreateAndConfigureRestClient(new ClientConfig
{
Host = MockServerUrl,
Token = "my-token",
AuthScheme = "my-scheme"
});
await DoRequest();

var requests = MockServer.LogEntries.ToList();

Assert.That(requests[0].RequestMessage.Headers?["Authorization"][0], Is.EqualTo("my-scheme my-token"));
}

[Test]
public async Task UserAgent()
{
Expand Down
9 changes: 9 additions & 0 deletions Client/Config/ClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace InfluxDB3.Client.Config;
/// You can configure following options:
/// - Host: The URL of the InfluxDB server.
/// - Token: The authentication token for accessing the InfluxDB server.
/// - AuthScheme: Token authenication scheme. Default is 'null' for Cloud access. Set to 'Bearer' for Edge access.
/// - Organization: The organization to be used for operations.
/// - Database: The database to be used for InfluxDB operations.
/// - Headers: The set of HTTP headers to be included in requests.
Expand Down Expand Up @@ -44,6 +45,7 @@ public class ClientConfig
{
internal const string EnvInfluxHost = "INFLUX_HOST";
internal const string EnvInfluxToken = "INFLUX_TOKEN";
internal const string EnvInfluxAuthScheme = "INFLUX_AUTH_SCHEME";
internal const string EnvInfluxOrg = "INFLUX_ORG";
internal const string EnvInfluxDatabase = "INFLUX_DATABASE";
internal const string EnvInfluxPrecision = "INFLUX_PRECISION";
Expand All @@ -67,6 +69,7 @@ internal ClientConfig(string connectionString)
Host = uri.GetLeftPart(UriPartial.Path);
var values = HttpUtility.ParseQueryString(uri.Query);
Token = values.Get("token");
AuthScheme = values.Get("authScheme");
Organization = values.Get("org");
Database = values.Get("database");
ParsePrecision(values.Get("precision"));
Expand All @@ -80,6 +83,7 @@ internal ClientConfig(IDictionary env)
{
Host = (string)env[EnvInfluxHost];
Token = env[EnvInfluxToken] as string;
AuthScheme = env[EnvInfluxAuthScheme] as string;
Organization = env[EnvInfluxOrg] as string;
Database = env[EnvInfluxDatabase] as string;
ParsePrecision(env[EnvInfluxPrecision] as string);
Expand All @@ -100,6 +104,11 @@ public string Host
/// </summary>
public string? Token { get; set; }

/// <summary>
/// Token authentication scheme.
/// </summary>
public string? AuthScheme { get; set; }

/// <summary>
/// The organization to be used for operations.
/// </summary>
Expand Down
15 changes: 12 additions & 3 deletions Client/InfluxDBClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,19 +285,21 @@ public class InfluxDBClient : IInfluxDBClient
/// </summary>
/// <param name="host">The URL of the InfluxDB server.</param>
/// <param name="token">The authentication token for accessing the InfluxDB server.</param>
/// <param name="authScheme">Token authentications scheme.</param>
/// <param name="organization">The organization name to be used for operations.</param>
/// <param name="database">The database to be used for InfluxDB operations.</param>
/// <example>
/// using var client = new InfluxDBClient("https://us-east-1-1.aws.cloud2.influxdata.com", "my-token", "my-org", "my-database");
/// </example>
public InfluxDBClient(string host, string token, string? organization = null,
public InfluxDBClient(string host, string token, string? authScheme = null, string? organization = null,
string? database = null) : this(
new ClientConfig
{
Host = host,
Organization = organization,
Database = database,
Token = token,
AuthScheme = authScheme,
WriteOptions = WriteOptions.DefaultOptions
})
{
Expand Down Expand Up @@ -343,6 +345,9 @@ public InfluxDBClient(ClientConfig config)
/// <description>token - authentication token (required)</description>
/// </item>
/// <item>
/// <description>authScheme - token authentication scheme (default <c>null</c> for Cloud access)</description>
/// </item>
/// <item>
/// <description>org - organization name</description>
/// </item>
/// <item>
Expand Down Expand Up @@ -371,12 +376,15 @@ public InfluxDBClient(string connectionString) : this(new ClientConfig(connectio
/// Supported parameters are:
/// <list type="bullet">
/// <item>
/// <description>INFLUX_HOST - authentication token (required)</description>
/// <description>INFLUX_HOST - InfluxDB host URL (required)</description>
/// </item>
/// <item>
/// <description>INFLUX_TOKEN - authentication token (required)</description>
/// </item>
/// <item>
/// <description>INFLUX_AUTH_SCHEME - token authentication scheme (default is <c>null</c> for Cloud access)</description>
/// </item>
/// <item>
/// <description>INFLUX_ORG - organization name</description>
/// </item>
/// <item>
Expand Down Expand Up @@ -866,7 +874,8 @@ internal static HttpClient CreateAndConfigureHttpClient(ClientConfig config)
client.DefaultRequestHeaders.UserAgent.ParseAdd(AssemblyHelper.GetUserAgent());
if (!string.IsNullOrEmpty(config.Token))
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", config.Token);
string authScheme = string.IsNullOrEmpty(config.AuthScheme) ? "Token" : config.AuthScheme;

Check warning on line 877 in Client/InfluxDBClient.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

Converting null literal or possible null value to non-nullable type.

Check warning on line 877 in Client/InfluxDBClient.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

Converting null literal or possible null value to non-nullable type.
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(authScheme, config.Token);
}

return client;
Expand Down

0 comments on commit 63994db

Please sign in to comment.