From 1c2c94e4350d6d30a42ef204136a2defd9fc1e1c Mon Sep 17 00:00:00 2001 From: tusmester Date: Thu, 23 Feb 2023 14:48:30 +0100 Subject: [PATCH] Add api key as a new authentication mode. --- src/SenseNet.Client.Tests/Initializer.cs | 6 +++++- .../Authentication/AuthenticationOptions.cs | 1 + src/SenseNet.Client/RESTCaller.cs | 20 +++++++++++++++---- src/SenseNet.Client/ServerContext.cs | 14 ++++++++++++- src/SenseNet.Client/ServerContextFactory.cs | 3 +++ 5 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/SenseNet.Client.Tests/Initializer.cs b/src/SenseNet.Client.Tests/Initializer.cs index f6c1e86..dd93df4 100644 --- a/src/SenseNet.Client.Tests/Initializer.cs +++ b/src/SenseNet.Client.Tests/Initializer.cs @@ -18,7 +18,11 @@ public static void InitializeServer() { new ServerContext { - Url = "https://localhost:44362" + Url = "https://localhost:44362", + Authentication = + { + ApiKey = "" + } } }); diff --git a/src/SenseNet.Client/Authentication/AuthenticationOptions.cs b/src/SenseNet.Client/Authentication/AuthenticationOptions.cs index 893ef9e..6937809 100644 --- a/src/SenseNet.Client/Authentication/AuthenticationOptions.cs +++ b/src/SenseNet.Client/Authentication/AuthenticationOptions.cs @@ -4,5 +4,6 @@ public class AuthenticationOptions { public string ClientId { get; set; } public string ClientSecret { get; set; } + public string ApiKey { get; set; } } } diff --git a/src/SenseNet.Client/RESTCaller.cs b/src/SenseNet.Client/RESTCaller.cs index 1dba536..cff2091 100644 --- a/src/SenseNet.Client/RESTCaller.cs +++ b/src/SenseNet.Client/RESTCaller.cs @@ -690,8 +690,7 @@ private static async Task ReadResponseStringAsync(WebResponse response) private static void SetAuthenticationForRequest(WebRequest myReq, ServerContext server) { - if (server == null) - server = ClientContext.Current.Server; + server ??= ClientContext.Current.Server; // use token authentication if (!string.IsNullOrEmpty(server.Authentication.AccessToken)) @@ -700,6 +699,13 @@ private static void SetAuthenticationForRequest(WebRequest myReq, ServerContext return; } + // api key authentication + if (!string.IsNullOrEmpty(server.Authentication.ApiKey)) + { + myReq.Headers.Add("apikey", server.Authentication.ApiKey); + return; + } + if (string.IsNullOrEmpty(server.Username)) { // use NTLM authentication @@ -799,8 +805,7 @@ public static async Task ProcessWebRequestResponseAsync(string url, HttpMethod m private static void SetAuthenticationForRequest(HttpClientHandler handler, HttpRequestMessage request, ServerContext server) { - if (server == null) - server = ClientContext.Current.Server; + server ??= ClientContext.Current.Server; // use token authentication if (!string.IsNullOrEmpty(server.Authentication.AccessToken)) @@ -809,6 +814,13 @@ private static void SetAuthenticationForRequest(HttpClientHandler handler, HttpR return; } + // api key authentication + if (!string.IsNullOrEmpty(server.Authentication.ApiKey)) + { + request.Headers.Add("apikey", server.Authentication.ApiKey); + return; + } + if (string.IsNullOrEmpty(server.Username)) { // use NTLM authentication diff --git a/src/SenseNet.Client/ServerContext.cs b/src/SenseNet.Client/ServerContext.cs index c4d42b1..1fe8fa5 100644 --- a/src/SenseNet.Client/ServerContext.cs +++ b/src/SenseNet.Client/ServerContext.cs @@ -17,6 +17,7 @@ public class AuthenticationInfo { public string AccessToken { get; set; } public string RefreshToken { get; set; } + public string ApiKey { get; set; } } /// @@ -78,6 +79,16 @@ public Task GetCurrentUserAsync(string[] select = null, string[] expand Logger?.LogTrace(ex, "Error during JWT access token conversion."); } } + else if (!string.IsNullOrEmpty(Authentication?.ApiKey)) + { + request = new ODataRequest(this) + { + Path = "/Root", + ActionName = "GetCurrentUser", + Select = select, + Expand = expand + }; + } // no token or invalid: load Visitor request ??= new ODataRequest(this) @@ -106,7 +117,8 @@ internal ServerContext Clone() Authentication = { AccessToken = this.Authentication.AccessToken, - RefreshToken = this.Authentication.RefreshToken + RefreshToken = this.Authentication.RefreshToken, + ApiKey = this.Authentication.ApiKey } }; diff --git a/src/SenseNet.Client/ServerContextFactory.cs b/src/SenseNet.Client/ServerContextFactory.cs index 24a3664..bc771e6 100644 --- a/src/SenseNet.Client/ServerContextFactory.cs +++ b/src/SenseNet.Client/ServerContextFactory.cs @@ -108,6 +108,9 @@ private async Task GetAuthenticatedServerAsync(string name) Url = options.Url.AppendSchema() }; + if (!string.IsNullOrEmpty(options.Authentication.ApiKey)) + server.Authentication.ApiKey = options.Authentication.ApiKey; + // do not try to authenticate if the values are not provided if (string.IsNullOrEmpty(options.Authentication.ClientId) || string.IsNullOrEmpty(options.Authentication.ClientSecret))