Skip to content

Commit

Permalink
Add api key as a new authentication mode. (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
tusmester authored Feb 28, 2023
1 parent 574b0f5 commit a3f9c93
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
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

0 comments on commit a3f9c93

Please sign in to comment.