Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add api key as a new authentication mode. #86

Merged
merged 2 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/SenseNet.Client.Tests/Initializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public static void InitializeServer()
{
new ServerContext
{
Url = "https://localhost:44362"
Url = "https://localhost:44362",
Authentication =
{
ApiKey = ""
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ public class AuthenticationOptions
{
public string ClientId { get; set; }
public string ClientSecret { get; set; }
public string ApiKey { get; set; }
}
}
20 changes: 16 additions & 4 deletions src/SenseNet.Client/RESTCaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -690,8 +690,7 @@ private static async Task<string> 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))
Expand All @@ -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
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion src/SenseNet.Client/ServerContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class AuthenticationInfo
{
public string AccessToken { get; set; }
public string RefreshToken { get; set; }
public string ApiKey { get; set; }
}

/// <summary>
Expand Down Expand Up @@ -79,6 +80,16 @@ public Task<Content> 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)
Expand Down Expand Up @@ -107,7 +118,8 @@ internal ServerContext Clone()
Authentication =
{
AccessToken = this.Authentication.AccessToken,
RefreshToken = this.Authentication.RefreshToken
RefreshToken = this.Authentication.RefreshToken,
ApiKey = this.Authentication.ApiKey
}
};

Expand Down
3 changes: 3 additions & 0 deletions src/SenseNet.Client/ServerContextFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ private async Task<ServerContext> 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))
Expand Down